Sub topic 2
Profile a production-like build with source maps
This chapter assumes you can run an Angular app with the CLI and open Chrome DevTools. You should also know the difference between a development build and a production build.
Why `ng serve` misleads you
ng serve uses the development configuration. It skips minification and optimization, and Angular runs extra checks in development mode. One of them is a second change detection pass that looks for ExpressionChangedAfterItHasBeenChecked errors. Your trace therefore shows work that real users never trigger, and the timings are larger than they would be in production.
A production-like build has these properties:
- The code is minified and tree-shaken.
- Dev-mode checks are off.
- It is served as static files, as it would be in production.
Profile that build, so the numbers you read match what users experience.
Build with source maps
Minified code has short, meaningless function names. A source map links the minified output back to your TypeScript, so the flame chart and the Sources panel show readable names.
Production builds disable source maps by default. Turn them on for the build you profile:
ng build --configuration production --source-mapTo make this permanent, set it in angular.json under your project's build target:
{
"configurations": {
"production": {
"sourceMap": true
}
}
}Serve the output
Serve the build output with any static server. With the application builder, the files are in dist/<project-name>/browser:
npx http-server dist/my-app/browser -p 8080Open http://localhost:8080 in a new Incognito window. Extensions are disabled there by default, so they don't add scripts to your trace.
Check that source maps loaded
- Open DevTools and go to the Sources panel.
- Look for your
.tsfiles under the page's origin, or pressCtrl+P(Cmd+Pon macOS) and type a component file name. - Open the file and confirm you see your original TypeScript, not minified code.
If the files are missing, check that the .map files exist in the output folder. Also check that "JavaScript source maps" is enabled in DevTools settings.
Common mistakes
| Mistake | Effect |
|---|---|
| Profiling `ng serve` output | Inflated timings and extra dev-mode work in the trace |
| Building without `--source-map` | Minified names in the flame chart |
| Profiling with extensions enabled | Foreign scripts appear in your trace |
| Rebuilding between recordings without noting it | Before-and-after comparisons stop being valid |
Exercise
You profile an app served by ng serve and see a large block of work after every change detection pass. Give one reason this block might not appear in a production build, and say what you would do instead.
Show answer
In development mode Angular runs an extra checking pass to detect changed-after-checked expressions, and the code is unoptimized. Build with ng build --configuration production --source-map, serve the output folder with a static server, and record the trace again in an Incognito window.