Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] source-maps with css or sass? #7

Closed
ericfong opened this issue Apr 28, 2014 · 33 comments
Closed

[Question] source-maps with css or sass? #7

ericfong opened this issue Apr 28, 2014 · 33 comments

Comments

@ericfong
Copy link

Hi,

I am using sass-loader to compile sass code. Can we have the source-map to trace back the source file with webpack for sass?

Currently, I setup Sass-loader by

 { test: /\.scss/, loader: "style-loader!sass-loader?outputStyle=expanded" },

with

    cache: true,
    debug: true,
    devtool: 'source-map',
    'output-pathinfo': true,

Is it possible to trace back the source sass file in chrome dev tools?

@glebm
Copy link

glebm commented Aug 9, 2014

I am using this together with ExtractTextPlugin to output .css files. I cannot figure out how to enable source maps either.

@mllocs
Copy link

mllocs commented Oct 8, 2014

I'm trying node-sass sourceComments and sourceMap options but without success
https://github.com/sass/node-sass

@jeffling
Copy link

I have it sort of working, with ExtractTextPlugin no less @glebm

  1. use node-sass sourceMap option sourceMap=map in sass-loader (probably doesn't do anything)
  2. use css-loader sourceMap option sourceMap (this might be what you were missing)
  3. choose your favourite source map option in the devtools option. I've used it with sourcemap and inline-source-map

@nickdima
Copy link

@jeffling are you sure sass source maps are supported?
From looking at the code it doesn't seem so. cc @jtangelder

@jeffling
Copy link

@nickdima you're right, it's basically just the compiled css. The original question asked for 'traced back to the source scss file', and so far I got the filename, not the actual sass code. It's helpful, but it would be nice to be able to use chrome workspaces to directly edit the file though.

@nickdima
Copy link

The reason source maps are not supported is that sass-loader passes the actual sass content to libsass and not a file, and it seems that libsass generates source maps only when a file is provided.
I couldn't find this documented in the libsass wiki but there's a comment by @svnieuw that talks about this.

libsass currently only supports source maps with sass_file_context. The source file (input path) can only be specified as path and not as data string.

Maybe we should specify this in the readme? cc @akiran @jhnns

@akiran
Copy link
Contributor

akiran commented Nov 15, 2014

gulp-sass had a similar problem dlmanning/gulp-sass#8
After digging in to gulp-sass code and docs,
I observed that file path is passed to node-sass instead of data if source maps is enabled.
gulp-sass either inlines source maps or uses gulp-sourcemaps to create a separate file.

Can we follow similar approach to sass-loader also ?

@nickdima
Copy link

I was also looking at this approach, I can try to make a PR.

@akiran
Copy link
Contributor

akiran commented Nov 16, 2014

@nickdima Thanks

nickdima referenced this issue in webpack-contrib/style-loader Nov 17, 2014
@nickdima
Copy link

OK, a quick update on this. The approach is doable the only barrier being that node-sass needs to support inlined file contents in the source maps. There's a discussion about it here.

@offminded
Copy link

Work in progress here

@cowwoc
Copy link

cowwoc commented Feb 2, 2015

@nickdima @offminded Any updates?

@jhnns
Copy link
Member

jhnns commented Feb 3, 2015

We should check if this is possible with the current beta version of node-sass.

The current master branch is already using node-sass 2.0, so we can start playing with it now.

@kyle-johnson
Copy link

This works for me:

{ test: /\.scss$/, loader: ExtractTextPlugin.extract('style-loader',
 'css-loader?sourceMap!sass-loader?outputStyle=expanded&sourceMap=true&sourceMapContents=true') }

@schlaup
Copy link

schlaup commented Feb 20, 2015

It almost works...

With aboves config Chrome displays "stdin" as the source.

File name and path is missing. You need to change "sass-loader/index.js":

...

    opt.success = function (result) {
        markDependencies();
        var sourceMap = {};
        if (typeof result.map === 'string' && result.map !== '{}') {
            var sourceFilename = utils.getRemainingRequest(this);
            var current = utils.getCurrentRequest(this);
            var sourceMap = JSON.parse(result.map);
            sourceMap.sources = [sourceFilename];
            sourceMap.file = current;
        }
        callback(null, result.css, sourceMap);
    }.bind(this);

...

After this change you can map the source in Chrome's workspace and edit / save the file in Chrome as well.

Could you add this to the official NPM release?

Thanks!

@adri
Copy link

adri commented Feb 27, 2015

I can confirm the solution from @kyle-johnson. Thanks!

@jhnns jhnns closed this as completed in ed1bc27 Mar 5, 2015
@jhnns
Copy link
Member

jhnns commented Mar 5, 2015

Has been published as v0.4.1. I've added instructions to the README.

@nickdima
Copy link

nickdima commented Apr 3, 2015

How are you guys live reloading the css when using ExtractTextPlugin for having source maps support? I'm asking this since HMR doesn't work with ExtractTextPlugin.

@adri
Copy link

adri commented Apr 3, 2015

I use two different configurations (dev and prod).

On 03.04.2015, at 17:01, Nick Dima notifications@github.com wrote:

How are you guys live reloading the css when using ExtractTextPlugin for having source maps support? I'm asking this since HMR doesn't work with ExtractTextPlugin.


Reply to this email directly or view it on GitHub.

@nickdima
Copy link

nickdima commented Apr 3, 2015

But on dev you would want both source maps and hot/live reloading. How do you do that?

@Todilo
Copy link

Todilo commented Jun 9, 2015

@nickdima did you ever find a solution to this? I would really like hot/live reloading if posible, it's such a nice feature.

@jeffling
Copy link

jeffling commented Jun 9, 2015

@nickdima @Todilo what I've been doing is to just use webpack in watch mode instead of dev-server, and using http://livejs.com/ (or fb's flo or any other css reload plugin) to get live reloading.

@Todilo
Copy link

Todilo commented Jun 9, 2015

@jeffling ok, I was thinking of look into if there was a tool for that instead since I do not use the dev-server atm (getting everything working isomorphic in mvc5 is no simple task). Thank you I will try livejs.com. Guess I only need the css changes updates as reloading entire page when working with SPA seems like a bad idea.

Only downside is that the scss sourcemap is not updated, any way to deal with that?

@jeffling
Copy link

jeffling commented Jun 9, 2015

@Todilo if you use inline source maps, it should be updated I think.

@Todilo
Copy link

Todilo commented Jun 9, 2015

@jeffling dont know how to set that up. However since I do most development in Chrome I setup a mapping from the the .scss from the server to the local location on the filesystem. Then I can both make changes in the browser(directly changin the scss files) as well as make changes in visual studio.

Still if I make changes in VS I dont get updated .scss file but well, whatever :) (if you dont have an easy guide on how to set tha tup).

Edit: Now I read the documentation again and saw the setting you were talking about:
// must be 'source-map' or 'inline-source-map'
devtool: 'source-map',

Now it works perfectly. I can change the sourcecode from .scss / browser and everything is updated live as well as correctly! Perfect!

@jhnns
Copy link
Member

jhnns commented Jun 10, 2015

inline-source-map is the best option for development, because it requires no setup to retrieve the original source. However, it's not suitable for production, that's why we leave it up to the user to configure.

@teameh
Copy link

teameh commented Dec 23, 2015

Thanks, this works indeed @kyle-johnson. But it doesn't if you would like to use React-Css-Modules. Any ideas? http://stackoverflow.com/questions/34434849/how-to-get-sourcemaps-working-for-react-css-modules

@JustMaier
Copy link

JustMaier commented Aug 7, 2016

I started with what @kyle-johnson provided and then made it so it still had HMR in dev mode.

var isDevelopment = process.env.ASPNETCORE_ENVIRONMENT === 'Development', //Replace with whatever you're using to determine if this is a dev env
    extractCSS = new ExtractTextPlugin('site.css', {disable: isDevelopment}),
    sassLoader = [
        'css'+(isDevelopment? '?sourceMap=true': ''),
        'postcss',
        'sass'+(isDevelopment? '?outputStyle=expanded&sourceMap=true&sourceMapContents=true': '')
    ];

if(isDevelopment) sassLoader.unshift('style');

var config = {
    module: {
        loaders: [
            { test: /\.scss/, loader: extractCSS.extract(sassLoader) },
        ]
    },
    plugins: [
        extractCSS
    ]
};

if(isDevelopment) config.devtool = 'inline-source-map';

@pdrittenhouse
Copy link

pdrittenhouse commented Jan 10, 2017

Here's what I did:

  1. Make sure to include devtool: "source-map", before the module property.
  2. Be sure to add the ?sourceMap option to both the css-loader and sass-loader or pass a config property to the sass-loader.

loader: ExtractTextPlugin.extract('style-loader', 'css-loader?sourceMap!sass-loader?sourceMap'),

or

`loader: ExtractTextPlugin.extract('style-loader', 'css-loader?sourceMap!sass-loader?config=sassLoader'),

sassLoader: {

    outFile:        './httpdocs/wp-content/themes/PDFK/build/styles.bundle.css',

    outputStyle:    'expanded',

    sourceMap:      'true'

},`

Here's what my webpack.config.js looks like:

`var path = require('path');

var ExtractTextPlugin = require("extract-text-webpack-plugin");

module.exports = {

entry: './httpdocs/wp-content/themes/PDFK/js/scripts.core.js',

output: {

    path: './httpdocs/wp-content/themes/PDFK/build/',
    filename: "scripts.bundle.js",

},

devtool: "source-map",

module: {

    preLoaders: [

        {

            test: /.*\.js$/,

            exclude: [
                /(node_modules|vendor|wp-admin|wp-includes|plugins|twentyfifteen|twentysixteen|twentyseventeen|libs)/
            ],

            loader: 'jshint-loader'

        }
    ],

    loaders: [
        {

            test: /.*\.js$/,

            exclude: [
                /(node_modules|vendor|wp-admin|wp-includes|plugins|twentyfifteen|twentysixteen|twentyseventeen|libs)/
            ],

            loader: 'babel-loader',

            query: {

                presets: ['es2015']

            }

        },

        {

            test: /.*\.js$/,

            exclude: [
                /(node_modules|vendor|wp-admin|wp-includes|plugins|twentyfifteen|twentysixteen|twentyseventeen|libs)/
            ],

            loader: 'uglify'

        },

        {

            test: /\.(png|woff|woff2|eot|ttf|svg)(\?[0-9].+)?$/,

            exclude: [
                /(node_modules|vendor|wp-admin|wp-includes|plugins|twentyfifteen|twentysixteen|twentyseventeen|libs)/
            ],

            loader: 'url-loader?limit=100000'

        },

        {

            test: /.*\.scss$/,

            loader: ExtractTextPlugin.extract('style-loader', 'css-loader?sourceMap!sass-loader?config=sassLoader'),

            fallbackLoader: 'style-loader!css-loader!sass-loader'

        }

    ]

},

plugins: [
    new ExtractTextPlugin('styles.bundle.css')
],

sassLoader: {

    outFile:        './httpdocs/wp-content/themes/PDFK/build/styles.bundle.css',

    outputStyle:    'expanded',

    sourceMap:      'true'

},

watch: false, 

watchOptions: {

    aggregateTimeout: 500,

},

keepalive: false,

};`

@goztrk
Copy link

goztrk commented Jan 27, 2017

  {
    test: /\.(scss|sass)$/,
    loader: 'style!css?sourceMap!postcss!sass?sourceMap'
  },

this is directly from my config. (ejected create-react-app) it works just fine for me. You need to add ?sourceMap to css loader.

@shannonsumner-zz
Copy link

shannonsumner-zz commented Mar 9, 2017

I was able to get it working by doing the following:

const path = require('path');
const webpack = require('webpack');
const ExtractTextPlugin = require('extract-text-webpack-plugin');

module.exports = {
    entry: [
        path.join(__dirname, 'src/index')
    ],
    output: {
        path: path.join(__dirname, 'dist'),
        publicPath: "/dist",
        filename: 'js/bundle.js'
    },
    devtool: 'source-map',
    module: {
        loaders: [
            {
                test: /\.js$/,
                exclude: /(node_modules|bower_components)/,
                loader: 'babel-loader',
                query: {
                    presets: [ 'es2015' ],
                    plugins: [ 'transform-runtime' ]
                }
            },
            {
                test: /(\.scss|\.css)$/,
                use: ExtractTextPlugin.extract({
                    fallback: 'style-loader',
                    use: [
                        {
                            loader: 'css-loader',
                            options: {
                                sourceMap: true
                            }
                        },
                        {
                            loader: 'sass-loader',
                            options: {
                                sourceMap: true,
                                outFile: 'css/bundle.css',
                                outputStyle: 'expanded',
                            }
                        }
                    ],
                })
            },
            {
                test: /\.(png)$/,
                use: {
                    loader: 'url-loader',
                    options: {
                        limit: 100000,
                        outputPath: '../images/',
                        publicPath: '/dist/images/'
                    }
                },
            }, {
                test: /\.jpg$/,
                use: {
                    loader: 'file-loader',
                    options: {
                        outputPath: '../images/',
                        publicPath: '/dist/images/'
                    }
                }
            }, {
                test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/,
                use: {
                    loader: 'url-loader',
                    options: {
                        limit: 10000,
                        outputPath: '../fonts/',
                        publicPath: '/dist/fonts/',
                        mimetype: 'application/font-woff'
                    }
                }
            }, {
                test: /\.(ttf|eot|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/,
                use: {
                    loader: "file-loader",
                    options: {
                        outputPath: '../fonts/',
                        publicPath: '/dist/fonts/'
                    }
                }
            }
        ],
    },
    resolve: {
        modules: [ 'node_modules', './src' ]
    },
    plugins: [
        new webpack.ProvidePlugin({
            $: 'jquery',
            jQuery: 'jquery'
        }),
        new ExtractTextPlugin({ filename: 'css/bundle.css', disable: false, allChunks: true }
        )
    ]
};

@xuyongli
Copy link

xuyongli commented Apr 6, 2017

@shannonsumner And do you find that your css was not compiled when you use disable option in new ExtractTextPlugin?

@shannonsumner-zz
Copy link

@xuyongli I don't think I tried with disable: true

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests