Skip to content

nested styles refer to top-level parent in sourcemap line number #272

Closed

Description

@woohling

I encountered the same issue in sass/node-sass#1206.
But according to this post, this issue has been fixed in node-sass, but it still occurred with sass-loader.

This is my sass-loader config: 'style!css?sourceMap!sass?sourceMap=true&outputStyle=expanded&sourceMapContents=true'

Any help?

Activity

  1. ggedde commented on Oct 28, 2016

    @ggedde

    Mac OSX (El Capitan?)
    "sass-loader": "^4.0.2",
    "webpack": "^2.1.0-beta.25",
    "webpack-dev-server": "^2.1.0-beta.0"
    node: 6.7.0

    When running the SCSS SourceMaps only point to the Parent selector and not the correct selector.

    2016-10-22_0150

    But as you can see it is not pointing to the correct selector. It should be pointing to line 13 not line 5.

    2016-10-22_0156

    However,
    If I run 'node-sass' in the terminal then it works fine. So it seems to be an issue with sass-loader and webpack.

    Any solutions to resolve this or is there any settings in node-modules/sass-loader that I can change to get different results.

    Thanks

  2. folmert commented on Nov 24, 2016

    @folmert

    Same problem here. Did anyone found a workaround for this?

  3. jhnns commented on Nov 25, 2016

    @jhnns
    Contributor

    CSS source maps can only work if every loader in the loader chain passes them through. That's necessary because webpack, only supports JS source maps out of the box.

    @woohling Do you experience the same issue when you use this loader chain:
    style-loader?sourceMap!css-loader?sourceMap!sass?sourceMap&outputStyle=expanded&sourceMapContents=true'
    ?

  4. woohling commented on Nov 25, 2016

    @woohling
    Author

    Just tried, but still the same problem. 😥 @jhnns

  5. folmert commented on Nov 26, 2016

    @folmert

    I switched from sass-loader to postcss/precss and source maps are generated correctly now, so there must be something broken in sass-loader.

  6. maximusnikulin commented on Dec 28, 2016

    @maximusnikulin

    Did anyone solve this problem?

  7. pgayvallet commented on Jan 4, 2017

    @pgayvallet

    Same issue here. @import makes sourcemap point to the parent file.

  8. jhnns commented on Feb 13, 2017

    @jhnns
    Contributor

    I can confirm the problem. You can test that with ce198d1 and npm run test-source-map.

    Unfortunately, I don't know how to fix this since we don't actually mess with the source maps.

  9. ggedde commented on Feb 13, 2017

    @ggedde

    @jhnns Could you explain more on "we don't actually mess with the source maps". Are you saying that sass-loader doesn't create the source maps. If so then what does and how?
    As mentioned running "node-sass" in the terminal the source maps are fine, but running it though sass-loader and webpack they are not.

    Sorry I am not too familiar with sass-loader flow. Could someone explain the difference that running sass-loader through webpack has as compared to running node-sass through the terminal.

    It almost seems like the scss file is converted to (nested, expanded, or compact) prior to the source map is being applied then the css is compressed.

    Is there any way to test that sass-loader is getting the full un-compacted un-compressed scss files when creating the source mappings?

  10. jhnns commented on Feb 14, 2017

    @jhnns
    Contributor

    We receive the source maps directly from node-sass. But since the source maps are also processed by the css-loader and other loaders on the way, it's hard to say what's going on. Maybe they are incompatible?

    It would be good if you could investigate this. Build the smallest project possible that reproduces the error and inspect the source maps in the sass-loader and then in the css-loader. You can use the style-loader to apply the styles to the DOM. The style-loader uses a BLOB url to make the source maps available to the browser. It does not change the source maps anymore, so that should be fine.

  11. alexander-akait commented on May 3, 2017

    @alexander-akait
    Member

    @woohling @maximusnikulin @wayofspark @ggedde The problem still exists? If yes please create minimal reproducible test repo

  12. woohling commented on May 4, 2017

    @woohling
    Author

    No longer working on the project with this issue.
    But I will find time to create a small demo to see if the problem still exists.

  13. alexander-akait commented on May 4, 2017

    @alexander-akait
    Member

    @woohling just ping me when your do this 👍

  14. ggedde commented on May 9, 2017

    @ggedde

    Sorry @jhnns @evilebottnawi, I took a pause from this project.
    Nope, the issue still exists for me. I also just recently installed brand new ng-cli on a new laptop.
    Angular4.0 with CLI 1.0.2 which is using sass-loader 6.0.3

    I created a small project and just had a few lines of scss code.
    It still pointed to the top parent node, but running node-sass works fine.
    Not sure what I can do to help further.

  15. 23 remaining items

  16. alexander-akait commented on Aug 16, 2017

    @alexander-akait
    Member

    @woohling @ggedde @folmert @wayofspark @sandwich @aszmyd won't fix in sass-loader 😞

    Information about why this happens: sass/libsass#1747 (comment) and sass/libsass#1747 (comment).

    In short: browsers always show first selector in sequence nested selectors in source maps. I am closing this issue here because it is not related to sass-loader and we don't have time to fix this in other packages (libsass) which not related directly to webpack. Your can create new issue or write post in sass/libsass#1747 if your think it is can be fixed.

    Btw your can click in dev tools on property to move in right position in source maps.

  17. aszmyd commented on Aug 17, 2017

    @aszmyd

    @evilebottnawi im not sure this is the right decision. sass-loader is using node-sass. node-sass is:

    a library that provides binding for Node.js to LibSass

    And in one of my earlier posts i've created small repo that uses only node-sass and it works perfectly fine.

    So question is - why pure node-sass is working properly but sass-loader that is using it underneath, is not?

  18. alexander-akait commented on Aug 17, 2017

    @alexander-akait
    Member

    @aszmyd your can look loader code, we don't modify source map from node-sass.
    Just confirm:

    1. console.log(result.map.toString()) in https://github.com/webpack-contrib/sass-loader/blob/master/lib/loader.js#L60
    2. Output:
    {
            "version": 3,
            "file": "stdin.css",
            "sourceRoot": "/home/evilebottnawi/IdeaProjects/webpack-sass-loader-sourcemap-issue",
            "sources": [
                    "stdin"
            ],
            "sourcesContent": [
                    "html {\n    background: rgba(0, 0, 0, 0.7);\n}\n\nbody {\n\n    h1 {\n        color: white;\n    }\n}\n"
            ],
            "names": [],
            "mappings": "AAAA,AAAA,IAAI,CAAC;EACD,UAAU,EAAE,kBAAkB,GACjC;;AAED,AAEI,IAFA,CAEA,EAAE,CAAC;EACC,KAAK,EAAE,KAAK,GACf"
    }
    1. Open in chrome or mozilla http://127.0.0.1:8080/styles/index.css.map
    2. Output:
    {
    "version":3,
    "sources":["webpack:///./src/src/index.scss"],
    "names":[],
    "mappings":"AAAA;EACI,+BAA8B,EACjC;;AAED;EAGQ,aAAY,EACf",
    "file":"styles/index.css",
    "sourcesContent":["html {\n    background: rgba(0, 0, 0, 0.7);\n}\n\nbody {\n\n    h1 {\n        color: white;\n    }\n}\n\n\n\n// WEBPACK FOOTER //\n// ./src/src/index.scss"],"sourceRoot":""}

    Seems something invalid in mappings, but it is not sass-loader, let's investigate this, thanks!

  19. alexander-akait commented on Aug 17, 2017

    @alexander-akait
    Member

    @aszmyd something wrong in css-loader 😞

    1. Add style-loader to webpack.config.js
     loaders: [
                {
                    test: /\.scss$/,
                    /*use: ExtractTextPlugin.extract({*/
                        use: [{
                            loader: 'style-loader',
                            options: {
                                sourceMap: true
                            }
                        }, {
                            loader: 'css-loader',
                            options: {
                                sourceMap: true
                            }
                        }, {
                            loader: 'sass-loader',
                            options: {
                                sourceMap: true
                                // outputStyle: 'compressed'
                            }
                        }]
                   /* })*/
                }
            ]
    1. Open https://github.com/webpack-contrib/css-loader/blob/master/lib/loader.js#L114 (in node_modules/css-loader)
    2. Comment lines
    /*map = result.map;
    			if(map.sources) {
    				map.sources = map.sources.map(function(source) {
    					return source.split("!").pop().replace(/\\/g, '/');
    				}, this);
    				map.sourceRoot = "";
    			}
    			map.file = map.file.split("!").pop().replace(/\\/g, '/');*/
    1. Run npm run start and your can see that source map is valid
  20. alexander-akait commented on Aug 17, 2017

    @alexander-akait
    Member

    Problem in postcss, which using in css-loader, I'll try to update postcss to the latest version in css-loader and see what happens

  21. alexander-akait commented on Aug 17, 2017

    @alexander-akait
    Member

    Closing issue, because not related, sorry, but i can't do anything here 😞
    Based issue: postcss/postcss#926
    Why it is happens: postcss/postcss#926 (comment)
    Workaround: implement own loader which correct this behavior (difficult).

    Maybe we can fix this in https://github.com/mozilla/source-map, your can create issue there with examples (prefer where only node-sass and postcss without webpack - easy debug), thanks!

  22. ggedde commented on Sep 2, 2017

    @ggedde

    Hi @evilebottnawi ,
    Thanks for all the hard work on this. I know you wanted this sooner, but here is my repo:
    https://github.com/ggedde/ngtest

    It just uses AngularCLI (Requires Node and NPM)

    git clone https://github.com/ggedde/ngtest.git
    cd ngtest
    npm install
    npm run start
    

    After NG finishes then Navigate your browser to http://localhost:4200
    You will see that if you instead the <h1> that the color points to line 3 in the inspector, but is actually on line 9. This issue is on both Chorme and Firefox.

    However, if you add:

    outputStyle: 'compressed',
    

    To the sass-loader [options] in "node_modules/@angular/cli/models/webpack-configs/styles.js"
    Then it works fine in Chrome, but not Firefox.

    Hope this helps others.
    Thanks

  23. alexander-akait commented on Sep 2, 2017

    @alexander-akait
    Member

    @ggedde thanks!

  24. kevin074 commented on Dec 1, 2018

    @kevin074
    {
        test: /\.(css|scss)$/,
         use: [
             { loader:MiniCssExtractPlugin.loader },
             { loader: "css-loader",  options: { sourceMap: true } },
             { loader: "sass-loader", options: { sourceMap: true, outputStyle: 'compressed'} }
        ]
    },
    

    currently with this set up the source map would only be pretty darn close to the line in the scss file when I am inspecting through chrome console. Don't know if anyone else is seeing this as well.

        "webpack": "^4.20.2",
        "node-sass": "^4.10.0",
        "sass-loader": "^7.1.0",
        "css-loader": "^1.0.0",
        "mini-css-extract-plugin": "^0.4.3",
    
  25. SweetsXob commented on Jul 10, 2019

    @SweetsXob

    This problem does not seem to be solved now.
    It seems less doesn't have this problem

  26. fgblomqvist commented on Jan 23, 2020

    @fgblomqvist

    I'm experiencing this issue now as well. Source map always end up referring to the top-level parent instead of to the actual line. Our setup is quite simple:

    {
      test: /\.(s?css)$/,
      use: [{
        loader: MiniCssExtractPlugin.loader,
      }, {
        loader: 'css-loader',
        options: {
          sourceMap: true,
        },
      }, {
        loader: 'sass-loader',
        options: {
          sourceMap: true,
        },
      }]
    },

    Setting sassOptions: { outputStyle: 'compressed' } on sass-loader does not improve the situation. If I do that, the source map will even link to the wrong files (not just wrong lines).

    Running node-sass using the CLI works well, it outputs correct source-maps for nested classes no matter which outputStyle is selected.

    We're on pretty much the latest version of everything:

    "css-loader": "^3.4.1",
    "mini-css-extract-plugin": "^0.8.0",
    "node-sass": "^4.13.0",
    "sass-loader": "^8.0.0",
    "webpack": "^4.41.5",
    

    Any ideas of why this could be happening? I could try to put together a demo repo if needed.
    Looks like @evilebottnawi had some insight on this before?

  27. MichaelDWheeler commented on Oct 30, 2020

    @MichaelDWheeler

    This resolved it for me:

     {
                test: /\.scss$/,
                use: [
                    MiniCssExtractPlugin.loader,
                    {
                      loader: "css-loader",
                      options: {
                        sourceMap: true,
                        url: false
                      }
                    },
                    {
                      loader: 'sass-loader',
                      options: {
                        sourceMap: true,
                        sassOptions: {
                          outputStyle: argv.mode === "production" ? "compressed" : "expanded",
                        }
                      }
                    }
                ]
              },
              {
                test: /\.css$/,
                use: [
                  MiniCssExtractPlugin.loader,
                  "css-loader",
                  "style-loader"
                ]
              },
    

    I found it here:
    https://github.com/noreading/bootstrap4-webpack-boilerplate/blob/master/webpack.config.js

    you may have to pass argv into module.exports like it is on the page I linked.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions