sourceMap with sass-loader and postcss-loader in Webpack

你离开我真会死。 提交于 2019-12-20 04:22:57

问题


I'm trying to enable sourceMaps in webpack but there seems to be a problem with sass-loader and postcss-loader combination.

With both sass-loader and postcss-loader enabled my console shows "no source":

But when I disable postcss-loader the sourceMap works fine and points to "typography" file:

webpack.config.js

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

module.exports = {
    mode: 'development',
    entry: './src/index.js',
    output: {
        path: path.resolve(__dirname, 'dist'),
        publicPath: '/dist',
        filename: 'js/bundle.js',
    },
    devtool: 'inline-source-map',
    module: {
        rules: [{
                test: /\.css$/i,
                use: ExtractTextPlugin.extract({
                    fallback: 'style-loader',
                    use: ['css-loader', 'postcss-loader']
                })
            },
            {
                test: /\.scss$/i,
                use: ExtractTextPlugin.extract({
                    fallback: 'style-loader',
                    use: [{
                            loader: 'css-loader',
                            options: {
                                sourceMap: true
                            }
                        },
                        {
                            loader: 'postcss-loader',
                            options: {
                                sourceMap: true
                            }
                        },
                        {
                            loader: 'sass-loader',
                            options: {
                                sourceMap: true
                            }
                        }
                    ]
                })
            },
            {
                test: /\.js$/i,
                exclude: /node_modules/,
                loader: 'babel-loader'
            }
        ]
    },
    plugins: [
        new ExtractTextPlugin('css/style.css')
    ]
};

main.scss

@import 'typography';

typography.scss

p {
    font-size: responsive 12px 18px;
}

回答1:


You can give the following a try. This is what I'm using and it's working.

{
    test: /\.(sa|sc|c)ss$/,
    exclude: ['/node_modules', './dist', '/src/js', '/docs'],
    use: [
        MiniCSSExtractPlugin.loader,
        {
            loader: 'css-loader',
            options: {
                sourceMap: true,
                minimize: process.env.NODE_ENV === 'production',
            }
        },
        {
            loader: 'postcss-loader',
            options: {
                sourceMap: true,
                syntax: postCssScss,
                plugins: () => [
                    autoprefixer,
                    postCssPresetEnv({
                        stage: 0,
                        features: {
                            'color-mod-function': true,
                            'alpha-hex-colors': true
                        }
                    }),
                ],
            },
        },
        {
            loader: 'sass-loader',
            options: {
                sourceMap: true
            }
        }
    ]
}

The extractTextPlugin has been deprecated for Webpack4 in favor of miniCssExtractPlugin



来源:https://stackoverflow.com/questions/52422220/sourcemap-with-sass-loader-and-postcss-loader-in-webpack

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!