Monday, October 16, 2017

Webpack source-map does not resolve sass imports

Leave a Comment

I have webpack configured to transpile scss -> css, but sourcemap generated by webpack does not resolve scss @imports.

webpack.config.js:

const path = require('path'); const ExtractTextPlugin = require('extract-text-webpack-plugin');  const outputPath = path.join(__dirname, 'dist');  module.exports = {     devtool: 'source-map',     entry: ['./src/main.scss'],     target: 'web',     output: {         filename: 'js/[name].bundle.js',         path: outputPath     },     module: {         rules: [             { // sass / scss loader for webpack                 test: /\.(sass|scss)$/,                 loader: ExtractTextPlugin.extract([                     {                         loader: 'css-loader',                         options: {                             url: false,                             import: true,                             minimize: true,                             sourceMap: true,                         }                     },                     'sass-loader'                 ])             },         ]     },     plugins: [         new ExtractTextPlugin({ // define where to save the file             filename: 'css/[name].bundle.css',             allChunks: true,         })     ] }; 

main.scss:

@import 'foo'; 

_foo.scss:

h1 { color: red; } 

However, in Chrome dev tools, I see a reference to main.scss where I expect reference to _foo.scss - see the screenshot below:

_foo.scss not resolved

Compiled demo: http://store.amniverse.net/webpacktest/

2 Answers

Answers 1

You have sass-loader there, switch it with:

{    loader: 'sass-loader',    options: {      sourceMap: true    } } 

And I think that should work.

Answers 2

You should not use extractTextPlugin when you are in dev mode. Please make extra configs for dev and production mode. In production the use of extractTextPlugin is fine but in dev mode it is not necessary and can lead to other features not working. So instead use the style-loader.

Also - I am not shure if that fixes your problem - try to use importLoaders prop on the css loader. Look here for more info:

https://github.com/webpack-contrib/css-loader#importloaders

const path = require('path');  const outputPath = path.join(__dirname, 'dist');  module.exports = {     devtool: 'source-map',     entry: ['./src/main.scss'],     target: 'web',     output: {         filename: 'js/[name].bundle.js',         path: outputPath     },     module: {         rules: [             { // sass / scss loader for webpack                 test: /\.(sass|scss)$/,                 loader: [                     {                         loader: 'style-loader',                         options: {                           sourceMap: true                         }                     },                     {                         loader: 'css-loader',                         options: {                             url: false,                             import: true,                             minimize: true,                             sourceMap: true,                             importLoaders: 1,                         }                     },                     {                        loader: 'sass-loader',                        options: {                          sourceMap: true                        }                     }                 ]             },         ]     } }; 
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment