Webpack 中带有 sass-loader 和 postcss-loader 的 sourceMap

sourceMap with sass-loader and postcss-loader in Webpack

我正在尝试在 webpack 中启用 sourceMaps,但 sass-loader 和 postcss-loader 组合似乎有问题。

同时启用 sass-loader 和 postcss-loader 我的控制台显示 "no source":

但是当我禁用 postcss-loader 时,sourceMap 工作正常并指向 "typography" 文件:

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;
}

您可以尝试以下方法。这就是我正在使用的并且它正在工作。

{
    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
            }
        }
    ]
}

extractTextPlugin has been deprecated for Webpack4 in favor of miniCssExtractPlugin