If you are an Angular developer, you’ve probably noticed that since Angular 2+ they break with almost every release. After wasting many hours in futile attempts to fix them and going through numerous report on Angular GitHub page I finally got to a setup that works.
Fixing dependencies (vendor) sourcemaps
- Open
angular.jsonand findtargets->build->configurations - Add a new configuration after
production. Name itserve."configurations": { "production": { ... } "serve": { "extractCss": true } - Change
servetask to use the new config by changingtargets->serve->options"options": { "browserTarget": "PROJECT_NAME:build:serve" - Open
package.jsonand update start scripts"scripts: { ... "start": "ng serve --source-map --vendor-source-map --ssl", } - Run your project with
npm start. Inspect your markup. You’ll see dependencies SCSS using the source maps and showing correct paths.
This will solve vendor sourcemaps issues, however you’ll see that your components sourcemaps aren’t working like before.
Fixing components sourcemaps
Components css is inlined on compile. The solution is to force inline sourcemaps as well. In order to do that we’ll need to match @angular-devkit
- Install shelljs as a dev dependency
npm install shelljs --save-devThis will allow us to run shell scripts from nodejs.
-
Create 2 files under
tools/cli-patchesat project root.patch.js
enable-sourcemaps.patch
- Add a postinstall script in
package.jsonthat will patch@angular-devkitand force inline sourcemaps for components on serve. Your scripts will now look like this:"scripts": { "ng": "ng", "start": "ng serve --source-map --vendor-source-map --ssl", "build": "ng build", "test": "ng test", "lint": "ng lint", "e2e": "ng e2e", "postinstall": "node tools/cli-patches/patch.js" },- Do
npm installornpm run postinstallto force the patching. - Always use
npm startinstead of ~~ng serve~~ to start your project. Once you run the server you’ll see correct files and code paths on components.
- Do
WARNING:
The patch might break with any future version of @angular-devkit. If this happens you’ll need to generate another patch file using git diff.
Sources:
– Angular GitHub issues discussions
– Angular CLI sourcemaps issue
Leave a Reply