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

works for me using version 6.0!!

solution:

  1. modify angular.json to add --extract-css, in 6.0 this argument is removed from ng serve
 "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
           ---
          },
          "configurations": {
            "production": {
              ---
            },
            "serve": {
              "extractCss": true,
               ---
            }
          }
        },

hen change serve->options->browserTarget->project-name:build to project-name:build:serve

"serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "options": {
            "open": true,
            "browserTarget": "<project-name>:build:serve"
          },
          "configurations": {
            "production": {
              ---
            }
          }
        }
  1. run ng serve --source-map image

metasong avatar May 14 '18 06:05 metasong

@metasong Thanks! That works for me. At least for "global" sass files. Component sass styles still don't get source mapped, but I don't use those anyways. Something to consider for others though.

ermcgrat avatar May 21 '18 15:05 ermcgrat

For others' reference, source maps for sass files were broken in the CLI way back in version 1.5.0. It's getting old now, but using cli version 1.4.x and older will work as expected when running:

ng serve -sm -ec

https://github.com/angular/angular-cli/issues/8314

ermcgrat avatar May 21 '18 16:05 ermcgrat

@CharltonC Thanks, your solution worked for me for angular cli 1.7.4. It is now showing the scss sourcemaps. #issuecomment-373430066

frensuren avatar May 27 '18 09:05 frensuren

@frensuren , can you share the exact steps you did? having a hard time following which of the solution will work for 1.7.4

icenold avatar May 27 '18 09:05 icenold

@rukshandangalla Could this ticket be closed now that v6 issue has been fixed?

michael-letcher avatar May 28 '18 00:05 michael-letcher

@rukshandangalla For me, component sass styles still don't get source mapped (v6).

imkremen avatar May 29 '18 15:05 imkremen

@icenold Installed css-loader and exports-loader dependencies for @angular/cli i) navigate to "node_modules/@angular/cli" ii) do: npm install --save css-loader exports-loader iii) Modify the file "node_modules@angular\cli\models\webpack-configs\styles.js" contents, mine was in line 180
Replace

     const commonLoaders = [
        { loader: 'raw-loader' },
    ]; 

with this

const commonLoaders = [
        {
            loader: 'css-loader',
            options: {
                sourceMap: cssSourceMap,
                import: false,
            }
        }
    ];

Also Replace

 // load component css as raw strings
    const rules = baseRules.map(({ test, use }) => ({
        exclude: globalStylePaths, test, use: [
            ...commonLoaders,
            {
                loader: 'postcss-loader',
                options: {
                    ident: 'embedded',
                    plugins: postcssPluginCreator,
                    sourceMap: cssSourceMap
                }
            },
            ...use
        ]
    }));

With

// load component css as raw strings
    const rules = baseRules.map(({ test, use }) => ({
        exclude: globalStylePaths, test, use: [
                'exports-loader?module.exports.toString()',
                ...commonLoaders,
            {
                loader: 'postcss-loader',
                options: {
                    ident: 'embedded',
                    plugins: postcssPluginCreator,
                    sourceMap: cssSourceMap
                }
            },
            ...use
        ]
    }));

This worked for me

frensuren avatar May 30 '18 12:05 frensuren

Sourcemapping for General (Non-component level) SCSS Compilation

@metasong's solution works for ANGULAR 6

  1. modify angular.json to add --extract-css, in 6.0 this argument is removed from ng serve
 "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
           ---
          },
          "configurations": {
            "production": {
              ---
            },
            "serve": {
              "extractCss": true,
               ---
            }
          }
        },
  1. Then change serve->options->browserTarget->project-name:build to project-name:build:serve
"serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "options": {
            "open": true,
            "browserTarget": "<project-name>:build:serve"
          },
          "configurations": {
            "production": {
              ---
            }
          }
        }
  1. run ng serve --source-map

image

cedriccjmusic avatar Jun 06 '18 15:06 cedriccjmusic

Sourcemapping for Component-level SCSS Files

Can you confirm, @metasong, @frensuren, @CharltonC, @rukshandangalla, @michael-letcher?

Suprisingly, the following solution works without any configuring of Angular 6's angular.json file.

Simply add /*# sourceURL=your-comp-style.component.scss */ to the bottom of your component-level .scss file.

Code

screen shot 2018-06-06 at 3 30 06 pm

Run

ng serve --source-map

Result in Chrome

screen shot 2018-06-06 at 3 37 02 pm

cedriccjmusic avatar Jun 06 '18 19:06 cedriccjmusic

@cedriccjmusic

Source mapping for Component-level

Can confirm that adding /*# sourceURL=navbar.component.less */ worked for me. Note this is using LESS

Source mapping for General (Non-component level)

I don't need to do this, LESS seems to work by just using --source-map

michael-letcher avatar Jun 20 '18 01:06 michael-letcher

This is causing an error for me:

Schema validation failed with the following errors:
  Data path "" should NOT have additional properties(extractsCss).

daddyschmack avatar Jul 05 '18 20:07 daddyschmack

The sourceURL= solution helps at least tell you the file, but still is far from ideal. It references generated css, so line numbers do not match up and any imported styles will show as belonging to the importing file.

@daddyschmack you have a typo, it should be extractCss and not extractsCss, although I was trying this method with a new angular project and its not working with the last couple cli versions.

ajspera avatar Jul 31 '18 20:07 ajspera

I found the source of the issue within the @angular-devkit/build-angular package.

You can temporarily fix this yourself editing changing the sourcemap option for both instances of postcss-loader in the following file ./node_modules/@angular-devkit/build-angular/src/angular-cli-files/models/webpack-configs/styles.js change

sourceMap: cssSourceMap

to

sourceMap: cssSourceMap ? 'inline' : false
    const rules = baseRules.map(({ test, use }) => ({
        exclude: globalStylePaths, test, use: [
            { loader: 'raw-loader' },
            {
                loader: 'postcss-loader',
                options: {
                    ident: 'embedded',
                    plugins: postcssPluginCreator,
                    sourceMap: cssSourceMap ? 'inline' : false
                }

I'll file an issue with the @angular-devkit/build-angular repository. edit: ah that package is in this repo, ha. I submitted a pull request with the fix.

ajspera avatar Jul 31 '18 21:07 ajspera

@cedriccjmusic thanks for the example!

Sadly isn't working for me with Angular cli 6.1

I get the following result: screen shot 2018-08-09 at 13 16 22

Versions

Angular CLI: 6.1.2
Node: 8.11.1
OS: darwin x64
Angular: 6.1.1
... animations, common, compiler, core, forms, http
... platform-browser, platform-browser-dynamic, platform-server
... router

Package                           Version
-----------------------------------------------------------
@angular-devkit/architect         0.7.2
@angular-devkit/build-angular     0.7.2
@angular-devkit/build-optimizer   0.7.2
@angular-devkit/build-webpack     0.7.2
@angular-devkit/core              0.7.2
@angular-devkit/schematics        0.7.2
@angular/cli                      6.1.2
@angular/compiler-cli             6.1.2
@angular/language-service         6.1.2
@ngtools/webpack                  6.1.2
@schematics/angular               0.7.2
@schematics/update                0.7.2
rxjs                              6.2.2
typescript                        2.9.2
webpack                           4.9.2

Config

{
    "build": {
      "builder": "@angular-devkit/build-angular:browser",
      "options": {
        ....
        "stylePreprocessorOptions": {
          "includePaths": [
            "styles/core-scss"
          ]
        },
        "assets": [
          ....
        ],
        "styles": [
          "apps/essent/src/core.scss"
        ],
        "scripts": []
      },
      "configurations": {
        "production": {
          ....
        },
        "css": {
          "sourceMap": true,
          "extractCss": true
        }
      }
    },
    "serve": {
      "builder": "@angular-devkit/build-angular:dev-server",
      "options": {
        "browserTarget": "essent:build",
        "port": 4200
      },
      "configurations": {
        "production": {
          "browserTarget": "essent:build:production"
        },
        "css": {
          "browserTarget": "essent:build:css"
        }
      }
    }
  }

Commands

Getting the same result with build & serve

yarn ng build essent --source-map --extract-css
yarn ng serve essent --proxy-config proxy.conf.js --configuration css --source-map

spike1292 avatar Aug 09 '18 11:08 spike1292

@ajspera this won't fix angular-cli 1.7.4, am I right? only new 6.x.x?

lazysergey avatar Aug 27 '18 17:08 lazysergey

@lazysergey Right. You might want to try #issuecomment-392316835 who had a hack working for 1.7.4. I would recommend getting up to 6, though. The next minor patch should have the fix in.

ajspera avatar Aug 27 '18 18:08 ajspera

@ajspera were you able to get smaps to work? Is there somewhere I could read up on how to get them to work properly? I've never actually seen them work in Angular.

BenRacicot avatar Sep 11 '18 02:09 BenRacicot

@BenRacicot Hmm, they said it would be in this past patch release but I don't see the commit in branch 6.2.x. It's on master, so it's in the maintainers' hands at this point. If you are desperate, you can make the changes from the commit mentioned above in your installation's source. It's only 2 lines.

ajspera avatar Sep 11 '18 14:09 ajspera

Awesome thanks for replying.

BenRacicot avatar Sep 11 '18 19:09 BenRacicot