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

v1.7.3 still no change. 👎
If this issue is not soon resolved, I will be incorporating a separate parallel CSS build process managed via gulp.
@adamfoley Any good ideas how to integrate that? I was also thinking of maybe using CodeKit for SCSS build, but not sure how to get the css file into the bundle.
@smlombardi I ended up going with gulp for now because I am working on styling and needed something that would also work for deploy. My config may be oversimplified, but it works. I am using font-awesome 4.7, bootstrap 4 and a single entry point for global SCSS. I added the font-awesome fonts as assets in .angular-cli.json so they get copied, a style sheet reference to project's index.html and emptied the styles array. In my styles/styles.scss file I set $fa-css-prefix, $fa-font-path vars appropriately, then import font-awesome and bootstrap .SCSS from relative node_modules paths.
I have gulp calling ng cli commands defined in package.json. NOTE: May run into issues without --delete-output-path=false on ng build command
added to index.html head:
<link href="styles.css" rel="stylesheet">
relevant styles.scss:
$fa-css-prefix: fa;
$fa-font-path: "assets/fonts";
@import "../../node_modules/font-awesome/scss/font-awesome";
@import "../../node_modules/bootstrap/scss/bootstrap";
@import "../../node_modules/bootstrap/scss/bootstrap-grid";
// .. other imports and custom SCSS
relevant package.json scripts:
"ngbuild": "ng build --prod -oh=none --delete-output-path=false",
"ngbuilddevw": "ng build --dev --watch --delete-output-path=false",
gulp dependencies:
"cssnano": "^3.10.0",
"gulp": "3.9.1",
"gulp-postcss": "7.0.1",
"gulp-rename": "1.2.2",
"gulp-sass": "3.2.0",
"gulp-sourcemaps": "2.6.4"
gulpfile.js:
const fs = require('fs');
const nodeSpawn = require('child_process').spawn;
const gulp = require('gulp');
const plugins = {
sourcemaps: require('gulp-sourcemaps'),
sass: require('gulp-sass'),
postcss: require('gulp-postcss')
}
const cssnano = require('cssnano');
var STYLES_SRC = './src/styles/';
var STYLES_ENTRY = './src/styles/styles.scss';
var STYLES_DEST = './dist/';
var processors = [
cssnano({
discardComments: { removeAll: true },
discardUnused: false, // unsafe, see http://goo.gl/RtrzwF
zindex: false, // unsafe, see http://goo.gl/vZ4gbQ
reduceIdents: false // unsafe, see http://goo.gl/tNOPv0
})
];
var isProd = false;
// execute process
function spawn(command, args, cb) {
var proc = nodeSpawn(command, args, {
stdio: 'inherit',
detached: false,
env: process.env, shell: true
});
proc.on('error', function (err) { console.error(err) });
if (cb != null) {
proc.on('close', function (code, signal) { cb(code); });
}
return proc;
}
// watch styles task
gulp.task('watch::styles', ['build.styles'], function (done) {
gulp.watch(STYLES_SRC + '**/*.scss', ['build.styles']);
});
// Build styles task
gulp.task('build.styles', function () {
var src =
[STYLES_ENTRY];
return isProd ?
gulp.src(src)
.pipe(plugins.sass({ includePaths: ['./node_modules/'] }).on('error', plugins.sass.logError))
.pipe(plugins.postcss(processors))
.pipe(gulp.dest(STYLES_DEST))
: gulp.src(src)
.pipe(plugins.sourcemaps.init())
.pipe(plugins.sass({ includePaths: ['./node_modules/'] }).on('error', plugins.sass.logError))
.pipe(plugins.sourcemaps.write(''))
.pipe(gulp.dest(STYLES_DEST));
});
// task to build and watch dev
gulp.task('build.devw', function (done) {
spawn('npm', ['run ngbuilddevw'], done);
gulp.start('watch::styles');
});
// task to build prod
gulp.task('build.prod', function () {
isProd = true;
spawn('npm', ['run ngbuild'], function (code) {
gulp.start('build.styles');
});
});
Hope this is of some help.
My2cents. @michael-letcher I have tested using CLI 1.7.0 and can confirm the work around is no longer working. It was strange as the Webpack API related to the sourcemap fix did not change as far as I am concerned.
Did a little more digging & comparing the webpack config file, found that the new CLI is using Webpack plugin "raw-loader" which does not yet support sourcemap (somehow the ticket has been closed but I am not sure if the sourcemap has been implemented). See the link below: https://github.com/webpack-contrib/raw-loader/pull/8
As for reference, there is a difference block of code in "webpack-config/style.js" file, the commonLoaders variable:
- CLI 1.7.0 line 180: "raw-loader" is used
- CLI 1.6.6 line 170: "css-loader" (and "postcss-loader") is used
"raw-loader" could be the issue here.
This is a duplicate of https://github.com/angular/angular-cli/issues/8558
downgraded to 1.6.6 and applied fix by @CharltonC - source maps work as expected. my steps:
- update global angular:
[sudo] npm uninstall -g @angular/cli
npm cache clean
[sudo] npm install -g @angular/cli
npm cache clear --forcenpm cache verifynpm i @angular/[email protected]or use exact version in package.json"@angular/cli": "1.6.6",and donpm i- go to
node_modules@angular\cli\models\webpack-configs\common.js:162
...
catch (e) { }
return {
devtool: 'source-map', // add this line
resolve: {
extensions: ['.ts', '.js'],
...
- done
Had some spare time to test it out. As suspected, it was caused by "raw-loader" the plugin. I tried enabling the sourcemap with the option for "raw-loader" plugin like follows (approx. line 180) but unfortunately didn't work.
const commonLoaders = [
{
loader: 'raw-loader',
// option added here but didn't work
options: {
sourceMap: cssSourceMap,
import: false,
}
},
];
~~Perhaps the good news is that it is not a super verbose work around.~~ It has turned into a verbose work around. It seems that "raw-loader" is only used for Html beside the "styles.js" file. Not sure what it may break though. Please do check.
Solution
- requires the
devtool: 'source-map'in the "common.js" file as per previous discussion - requires "css-loader" & "exports-loader" plugin to be installed as dependency in CLI
a) in terminal, under your project folder, navigate to "node_modules/@angular/cli"
b) install the plugin as dependency:
npm install --save css-loader exports-loader - in the file "node_modules@angular\cli\models\webpack-configs\styles.js" file, make a few changes:
a) swap out the variable
commonLoaders(approx. line 180) withcss-loaderone, for example:
/* const commonLoaders = [
{ loader: 'raw-loader' },
]; */
// new code looks something like this
const commonLoaders = [
{
loader: 'css-loader',
options: {
sourceMap: cssSourceMap,
import: false,
}
}
];
...
b) Right underneath the commonLoaders, add the 'exports-loader?module.exports.toString()', above the ...commonLoaders. This is the same as CLI 1.6.6 and eliminates the error "compiler.js:2520 Uncaught Error: Expected 'styles' to be an array of strings." in the devtool console
...
// load component css as raw strings
const rules = baseRules.map(({ test, use }) => ({
exclude: globalStylePaths, test, use: [
'exports-loader?module.exports.toString()', // <-- add this line
...commonLoaders,
{
loader: 'postcss-loader',
...
Test Context:
- angular-cli version 1.7.0
- node v6.9.4
- Scss
Tested with the following Commands in terminal (also works with Sass @import):
ng serve // no sourcemap
ng serve -sm -ec // has sourcemap
ng serve --sourcemap --extract-css // has sourcemap
ng serve --sourcemap --extractCss // has sourcemap
@CharltonC Doesn't work for me. Throws a mess of errors, beginning with a missing font (which did not happen before the fix).
~~@smlombardi ~~ ~~Yes you are right. Sorry I missed the error in the console saying "compiler.js:2520 Uncaught Error: Expected 'styles' to be an array of strings.". which cause the page not displaying correctly even though the sourcemaps is working. Will need to take another look.~~ @smlombardi edited the solution, turned out I missed to specify another plugin when playing with the solution. I can confirm it is working now in my development environment without the console error and UI is showing correctly. Can you verify again to see if it works?
Nope. Same initial error, with a bunch of other errors. They are all font-related.
ERROR in ./src/sass/designsystem/designsystem.scss
Module build failed: ModuleNotFoundError: Module not found: Error: Can't resolve './PrecisionSans_W_Bd.61449ec099a8a01e77f9.woff' in '/Users/st016lo/github/ng-designsystem/client/src/sass/designsystem'
at factoryCallback (/Users/st016lo/github/ng-designsystem/client/node_modules/webpack/lib/Compilation.js:276:40)
at factory (/Users/st016lo/github/ng-designsystem/client/node_modules/webpack/lib/NormalModuleFactory.js:237:20)
at resolver (/Users/st016lo/github/ng-designsystem/client/node_modules/webpack/lib/NormalModuleFactory.js:60:20)
at asyncLib.parallel (/Users/st016lo/github/ng-designsystem/client/node_modules/webpack/lib/NormalModuleFactory.js:127:20)
at /Users/st016lo/github/ng-designsystem/client/node_modules/async/dist/async.js:3874:9
at /Users/st016lo/github/ng-designsystem/client/node_modules/async/dist/async.js:473:16
at iteratorCallback (/Users/st016lo/github/ng-designsystem/client/node_modules/async/dist/async.js:1048:13)
at /Users/st016lo/github/ng-designsystem/client/node_modules/async/dist/async.js:958:16
at /Users/st016lo/github/ng-designsystem/client/node_modules/async/dist/async.js:3871:13
at resolvers.normal.resolve (/Users/st016lo/github/ng-designsystem/client/node_modules/webpack/lib/NormalModuleFactory.js:119:22)
at onError (/Users/st016lo/github/ng-designsystem/client/node_modules/enhanced-resolve/lib/Resolver.js:65:10)
at loggingCallbackWrapper (/Users/st016lo/github/ng-designsystem/client/node_modules/enhanced-resolve/lib/createInnerCallback.js:31:19)
at runAfter (/Users/st016lo/github/ng-designsystem/client/node_modules/enhanced-resolve/lib/Resolver.js:158:4)
at innerCallback (/Users/st016lo/github/ng-designsystem/client/node_modules/enhanced-resolve/lib/Resolver.js:146:3)
at loggingCallbackWrapper (/Users/st016lo/github/ng-designsystem/client/node_modules/enhanced-resolve/lib/createInnerCallback.js:31:19)
at next (/Users/st016lo/github/ng-designsystem/client/node_modules/tapable/lib/Tapable.js:252:11)
ERROR in ./node_modules/font-awesome/css/font-awesome.min.css
Module build failed: ModuleNotFoundError: Module not found: Error: Can't resolve './fontawesome-webfont.674f50d287a8c48dc19b.eot' in '/Users/st016lo/github/ng-designsystem/client/node_modules/font-awesome/css'
at factoryCallback (/Users/st016lo/github/ng-designsystem/client/node_modules/webpack/lib/Compilation.js:276:40)
at factory (/Users/st016lo/github/ng-designsystem/client/node_modules/webpack/lib/NormalModuleFactory.js:237:20)
at resolver (/Users/st016lo/github/ng-designsystem/client/node_modules/webpack/lib/NormalModuleFactory.js:60:20)
at asyncLib.parallel (/Users/st016lo/github/ng-designsystem/client/node_modules/webpack/lib/NormalModuleFactory.js:127:20)
at /Users/st016lo/github/ng-designsystem/client/node_modules/async/dist/async.js:3874:9
at /Users/st016lo/github/ng-designsystem/client/node_modules/async/dist/async.js:473:16
at iteratorCallback (/Users/st016lo/github/ng-designsystem/client/node_modules/async/dist/async.js:1048:13)
at /Users/st016lo/github/ng-designsystem/client/node_modules/async/dist/async.js:958:16
at /Users/st016lo/github/ng-designsystem/client/node_modules/async/dist/async.js:3871:13
at resolvers.normal.resolve (/Users/st016lo/github/ng-designsystem/client/node_modules/webpack/lib/NormalModuleFactory.js:119:22)
at onError (/Users/st016lo/github/ng-designsystem/client/node_modules/enhanced-resolve/lib/Resolver.js:65:10)
at loggingCallbackWrapper (/Users/st016lo/github/ng-designsystem/client/node_modules/enhanced-resolve/lib/createInnerCallback.js:31:19)
at runAfter (/Users/st016lo/github/ng-designsystem/client/node_modules/enhanced-resolve/lib/Resolver.js:158:4)
at innerCallback (/Users/st016lo/github/ng-designsystem/client/node_modules/enhanced-resolve/lib/Resolver.js:146:3)
at loggingCallbackWrapper (/Users/st016lo/github/ng-designsystem/client/node_modules/enhanced-resolve/lib/createInnerCallback.js:31:19)
at next (/Users/st016lo/github/ng-designsystem/client/node_modules/tapable/lib/Tapable.js:252:11)
ERROR in ./src/sass/designsystem/designsystem.scss
and so one for every font.
This gets to a point where it just takes too much effort or work around to solve a fundamental issue. There are too many unknowns as in what we need to test against the solution so that other functionalities don't break because the work around involves changes in so many areas. I think it would best to leave this to original author as in why there was a major change in the implementation from v1.7.0 onwards.
I can share some observations for reference purpose only. Apart from the changes I mentioned, if you do either of the following, the font error mentioned by @smlombardi disappears.
- If you replace the
node_modules\@angular\cli\models\webpack-configs\style.jsfile from CLI v1.6.6 to CLI v1.70 it will work. (requires an extra npm dependency calledcssnanofor the CLI) - If you comment out the
PostcssCliResourcesin thepostcssPluginCreatorconstant in thenode_modules\@angular\cli\models\webpack-configs\style.jsfile, like following (It is using anode_modules\@angular\cli\plugins\postcss-cli-resources.jsfile which was not in v1.6.6 before):
...
{ url: 'rebase' },
]),
// PostcssCliResources({
// ...
// }) ,
autoprefixer({ grid: true }),
];
Hope this helps. For now I myself am just going to stick with CLI v1.6.6
One just wonders when the team is planning to actually fix this critical piece of tooling.
version 1.7.3 does't work, even patched with devtool: 'source-map', anyone know why
I don't think the sourcemap is working for the Command ng build -sm -ec in both CLI v1.6.6 and v1.7.0 although there is a test/spec file in the CLI source code that says so.
In the CLI source code, I only managed to find only one test that is relevant to scss style and sourcemap in tests/e2e/build/styles/import.ts file (around line 48). It looks like this:
...
.then(() => ng('build', '--extract-css', '--sourcemap')) // <---- simulate cli cmd
// verify global styles
.then(() => expectFileToMatch('dist/styles.bundle.css',
/body\s*{\s*background-color: #00f;\s*}/))
.then(() => expectFileToMatch('dist/styles.bundle.css',
/p\s*{\s*background-color: #f00;\s*}/))
// verify global styles sourcemap
.then(() => expectToFail(() =>
expectFileToMatch('dist/styles.bundle.css', '"mappings":""'))) // <---- this test
...
Now an external scss sourcemap file content example would look something like this:
{"version":3,"sources":["main.scss"],"names":[],"mappings":"AACA;EACI,mBAAmB,EACtB","file":"main.css","sourcesContent":["// @import '_common/_reset.2.0.min.scss';\r\nbody {\r\n font-family: Arial;\r\n}"]}
The mappings property in an external source map file should not be empty if there is the scss file has any content at all (compared to the spec/test in CLI source code above). In addition, it would also outline your source scss filename however the actual generated external source map file (when you run the command ng build -sm -ec in CLI v1.6.6 & v1.7.0) just contains ".css" filename (i.e. correspond to the output file main.css in the source map example above). Is the spec/test testing the source map correctly? Would appreciate if anyone else can share their findings.
version 1.7.4 still not work, even try patched with devtool: 'source-map', Plz show me how to fix this issue.
Has anyone tested if scss sourcemaps work in version 6.0 (to be released with angular 6.0)? For angular v5 I just stayed on v1.6.x and used CharltonC fix provided here. Note that fix does not work with 1.7.x versions of cli.
@hiepnguyen93 seems the only stable thing you can do is downgrading to 1.6.6, I provided my steps in the post above
Thanks for your help, I was downgrade angular cli and it run normaly. :D
any workaround? I am unable to use cli 1.6.6