Webpack中带有sass-loader和postcss-loader的sourceMap [英] sourceMap with sass-loader and postcss-loader in Webpack

查看:525
本文介绍了Webpack中带有sass-loader和postcss-loader的sourceMap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

同时启用sass-loader和postcss-loader,我的控制台显示无源":

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

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

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

推荐答案

您可以尝试以下方法.这就是我正在使用的并且有效.

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

extractTextPlugin 已被Webpack4弃用,而推荐使用 miniCssExtractPlugin

The extractTextPlugin has been deprecated for Webpack4 in favor of miniCssExtractPlugin

这篇关于Webpack中带有sass-loader和postcss-loader的sourceMap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆