angular-cli icon indicating copy to clipboard operation
angular-cli copied to clipboard

Source map not working for SCSS files

Open rukshandangalla opened this issue 8 years ago • 87 comments

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. image

Desired behavior

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

rukshandangalla avatar Jan 05 '18 03:01 rukshandangalla

@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.

dmijatovic avatar Feb 07 '18 07:02 dmijatovic

@CharltonC thanks for the workaround. Can we expect any kind of fix from the angular team?

rukshandangalla avatar Feb 07 '18 07:02 rukshandangalla

@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

michael-letcher avatar Feb 07 '18 08:02 michael-letcher

@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 avatar Feb 07 '18 09:02 michael-letcher

@michael-letcher Thanks. missed that. I confirm it's working now for scss.

smlombardi avatar Feb 07 '18 13:02 smlombardi

Glad it works for everyone. Cheers.

CharltonC avatar Feb 08 '18 02:02 CharltonC

it works. but why angular cli not add it?

LinboLen avatar Feb 11 '18 08:02 LinboLen

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

michael-letcher avatar Feb 16 '18 12:02 michael-letcher

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"
 }
...

LinboLen avatar Feb 20 '18 12:02 LinboLen

So how do we use this? When is this going to be added to the actual CLI code?

smlombardi avatar Feb 20 '18 15:02 smlombardi

patch doesn't work in 1.7.0. Trying to work on styling is a nightmare without sourcemaps, please fix!

adamfoley avatar Feb 22 '18 21:02 adamfoley

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 avatar Mar 01 '18 13:03 avril-verhaeghen

@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.

adamfoley avatar Mar 01 '18 14:03 adamfoley

I use ng serve -sm -ec and it does not work for global styles. I don't use component styles.

smlombardi avatar Mar 01 '18 14:03 smlombardi

@smlombardi @anthonyartese and if you import you vendor styles via the cli that too will not be mapped.

michael-letcher avatar Mar 05 '18 16:03 michael-letcher

@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"
      ],

smlombardi avatar Mar 05 '18 18:03 smlombardi

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.

smlombardi avatar Mar 05 '18 18:03 smlombardi

+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

CatinhoCR avatar Mar 06 '18 03:03 CatinhoCR

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.

adamfoley avatar Mar 06 '18 12:03 adamfoley

+1, no more source maps with @angular/cli: 1.7.1

sdielmann avatar Mar 08 '18 11:03 sdielmann