Source map not working for SCSS files
Recently I updated cli version 1.3.2 to 1.6.3 and after that I can't see scss source mapped file in developer tools in Chrome of Firefox .
Versions : 1.6.3
Angular CLI: 1.6.3
Node: 8.9.3
OS: win32 x64
Angular: 5.0.0
... animations, common, compiler, compiler-cli, core, forms
... http, platform-browser, platform-browser-dynamic
... platform-server, router
@angular/cli: 1.6.3
@angular/language-service: 4.4.6
@angular-devkit/build-optimizer: 0.0.36
@angular-devkit/core: 0.0.22
@angular-devkit/schematics: 0.0.42
@ngtools/json-schema: 1.1.0
@ngtools/webpack: 1.9.3
@schematics/angular: 0.1.11
@schematics/schematics: 0.0.11
typescript: 2.4.2
webpack: 3.10.0
Repro steps
- Create new angular app using
ng new sample-app - Add scss file in angular-cli.json
"styles": ["styles/styles.scss"] - Launch app using
ng serve -sm -ec - Inspect styles using chrome dev-tools.
Observed behavior
No actual scss file, only style.bundle.css where all the styles compiled.

Desired behavior
Actual scss file can be seen in the developer tools with the line number

@smlombardi, it looks like your devtool definition is not at right level of configuration. Examine the structure of return {...} @CharltonC provided. The exact line number in the common.js file may vary. I hope this helps.
@CharltonC thanks for the workaround. Can we expect any kind of fix from the angular team?
@CharltonC It worked for me.
CSS Preprocessor: LESS
Version:
Angular CLI: 1.6.5
Node: 6.11.2
OS: darwin x64
Angular: 5.2.1
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router
@angular/cli: 1.6.5
@angular-devkit/build-optimizer: 0.0.41
@angular-devkit/core: 0.0.28
@angular-devkit/schematics: 0.0.51
@ngtools/json-schema: 1.1.0
@ngtools/webpack: 1.9.5
@schematics/angular: 0.1.16
typescript: 2.6.2
webpack: 3.10.0
@smlombardi You have entered the line in the wrong place.
This is where I imagine you have placed it:
return {
resolve: {
devtool: 'source-map',
...
}
}
in needs to be in the return object one level higher:
return {
devtool: 'source-map',
resolve: { ... },
...
}
@michael-letcher Thanks. missed that. I confirm it's working now for scss.
Glad it works for everyone. Cheers.
it works. but why angular cli not add it?
Issue still present in v1.7.0 The workaround DOES NOT WORK in v1.7.0!
Edit: Not sure why down voted, the work around does not work for me anymore when using CLI v1.7.0
Angular CLI: 1.7.0
Node: 6.11.2
OS: darwin x64
Angular: 5.2.5
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router
@angular/cli: 1.7.0
@angular-devkit/build-optimizer: 0.3.1
@angular-devkit/core: 0.3.1
@angular-devkit/schematics: 0.3.1
@ngtools/json-schema: 1.2.0
@ngtools/webpack: 1.10.0
@schematics/angular: 0.3.1
@schematics/package-update: 0.3.1
typescript: 2.6.2
webpack: 3.11.0
I create such file as postinstall can patch it. inspired from angular aio https://github.com/angular/angular/tree/master/aio/tools/cli-patches
in your project create following files
tools/cli-patches/patch.js
const fs = require('fs');
const sh = require('shelljs');
const PATCH_LOCK = 'node_modules/@angular/cli/models/webpack-configs/.patched-webpack-enable-sourcemap';
//can generate patch file by `git diff` or use `diff -Nur`
if (!fs.existsSync(PATCH_LOCK)) {
sh.exec('patch -p0 -i tools/cli-patches/webpack-enable-sourcemap.patch');
sh.touch(PATCH_LOCK);
}
tools/cli-patches/webpack-enable-sourcemap.patch
--- node_modules/@angular/cli/models/webpack-configs/common.js 2018-02-08 09:10:11.000000000 +0800
+++ node_modules/@angular/cli/models/webpack-configs/common.js 2018-02-20 20:36:43.000000000 +0800
@@ -158,6 +158,7 @@
}
catch (e) { }
return {
+ devtool: buildOptions.sourcemaps ? 'source-map' : false,
resolve: {
extensions: ['.ts', '.js'],
modules: ['node_modules', nodeModules],
package.json
...
"scripts": {
"postinstall": "node tools/cli-patches/patch.js"
}
...
So how do we use this? When is this going to be added to the actual CLI code?
patch doesn't work in 1.7.0. Trying to work on styling is a nightmare without sourcemaps, please fix!
I've never managed to make SCSS sourcemaps make work with any version of angular-cli! v1.3.2 no sourcemaps, v1.6.0 no sourcemaps, now a clean project generated with v1.7.2 with nothing changed but a color for the header in the example and launched the project with ng serve -sm -ec or ng serve --sourcemap --extractCss and still no sourcemaps.
Any good pointers are appreciated. Thanks!
@avril-verhaeghen Developers recently added sourcemaps for component styles, which I believe is what broke global sourcemaps. Are you using global or component SCSS? I don't remember last version of angular-cli I was using before the maps broke (1.6.something I believe) but my build command is ng build --dev -ec --watch. Have you had any luck with non-served builds? You can also try verifying that your global cli version matches your local project, as well as do a global install of latest node-sass package -- though really don't think that should matter.
I use ng serve -sm -ec and it does not work for global styles. I don't use component styles.
@smlombardi @anthonyartese and if you import you vendor styles via the cli that too will not be mapped.
@michael-letcher I don't care about the vendor styles, but my main scss file, designsystem.scss is not getting mapped:
"styles": [
"../node_modules/prismjs/themes/prism.css",
"../node_modules/@ngx-loading-bar/core/loading-bar.css",
"../node_modules/ng2-toastr/bundles/ng2-toastr.min.css",
"sass/designsystem/designsystem.scss",
"styles.scss"
],
FWIW, it's not working for scss files imported into the styles.scss file either. Our styles.scss has an import of
@import 'documentation/documentation';
Yet, all of the classes from that css file, when inspected in dev tools, are all showing styles_bundle.css instead of their respective scss files.
+1, sourcemaps are not working for me. I'm importing pretty much all my scss files into /assets/scss/styles.scss (all of them being in sub folders following sass best practices 7:1 style guides) and none of them is accessible from the dev tools, they all get bundled up
Doesn't seem like this bug is going to be fixed anytime soon. My work around at the moment is to build the bundle using global install of node-sass:
node-sass ./src/styles/styles.scss --include-path ./src/styles --source-map-root file://{$pwd} --source-map-embed true --source-comments --source-map-contents ./dist/styles.bundle.css
This can likely be modified with a watch.
+1, no more source maps with @angular/cli: 1.7.1