简体   繁体   English

Webpack 不发出 css sourcemap (with sourcemap: true)

[英]Webpack doesn't emit css sourcemap (with sourcemap: true)

I can't make my webpack to emit css sourcemap.我无法让我的 webpack 发出 css sourcemap。 I've put sourcemap: true everywhere where possible, and no effect, and all the solutions online suggest either this or some other plugin configuration, but I don't have any other plugin, it's super simple webpack.config.js我把 sourcemap: true 放在任何可能的地方,没有效果,网上所有的解决方案都建议这个或其他一些插件配置,但我没有任何其他插件,它是超级简单的 webpack.config.js

This is my webpack.config.js:这是我的 webpack.config.js:

 const MiniCssExtractPlugin = require("mini-css-extract-plugin"); var path = require("path"); module.exports = { entry: "./main.js", output: { path: path.resolve(__dirname, "dist"), filename: "bundle.js", publicPath: "/", sourceMapFilename: '[file].map' }, devtool: 'source-map', module: { rules: [{ test: /\\.js$/, use: { loader: "babel-loader", options: { presets: ["es2015"], sourceMap: true } } }, { test: /\\.scss$/, use: [{ loader: MiniCssExtractPlugin.loader, options: { publicPath: '/', sourceMap: true, hmr: process.env.NODE_ENV === 'development', }, }, { loader: "css-loader", options: { sourceMap: true } }, { loader: "sass-loader", options: { sourceMap: true }, } ] }, { test: /\\.(png|woff|woff2|eot|ttf|svg|jpg)$/, use: [{ loader: 'url-loader', options: { name: 'images/[hash]-[name].[ext]' } }] } ] }, plugins: [ new MiniCssExtractPlugin({ filename: '[name].css', chunkFilename: '[id].css', ignoreOrder: false, // Enable to remove warnings about conflicting order sourceMap: true }), ], };

I need this source map in dev mode, but only two files get emited main.css and bundle.js.我在开发模式下需要这个源映射,但只有两个文件被发出 main.css 和 bundle.js。

For those who also struggle with this, the below webpack.config.js is a proper configuration for having style's sourcemap in dev mode.对于那些也为此而苦恼的人,下面的 webpack.config.js 是在开发模式下使用样式源映射的正确配置。 Bundle.js needs to be included when in dev mode, and custom.min.css needs to be added to the HTML document in production mode.在开发模式下需要包含Bundle.js,在生产模式下需要将custom.min.css添加到HTML文档中。

Edit: Unfortunately this can be also wrong.编辑:不幸的是,这也可能是错误的。 Right now without any change to webpack or node, the sourcemap is not generating :/ sometimes it works, and sometimes it doesn't.现在没有对 webpack 或 node 进行任何更改,源映射没有生成:/有时它有效,有时它不起作用。 There is some bug in webpack, or in some plugins, either way it's a serious bug, but it's webpack so I am not surprised... webpack 或某些插件中存在一些错误,无论哪种方式,它都是一个严重的错误,但它是 webpack,所以我并不感到惊讶......

Edit 2: Now I see that the problem is only in Firefox, Chrome and Opera have the correct map.编辑 2:现在我发现问题仅在 Firefox、 Chrome 和 Opera 中具有正确的映射。

const MiniCssExtractPlugin = require("mini-css-extract-plugin");
var path = require("path");

module.exports = {
    entry: "./main.js",
    output: {
        path: path.resolve(__dirname, "dist"),
        filename: "bundle.js",
        publicPath: "/"
    },
    module: {
        rules: [{
                test: /\.js$/,
                use: {
                    loader: "babel-loader",
                    options: { presets: ["es2015"] }
                }
            },
            {
                test: /\.scss$/,
                use: [
                    {
                        loader: "style-loader" // creates style nodes from JS strings
                    },
                    {
                        loader: "css-loader" ,
                        options: {
                            sourceMap: true
                        }// translates CSS into CommonJS
                    },
                    {
                        loader: "sass-loader",
                        options: {
                            sourceMap: true
                        } // compiles Sass to CSS
                    }
                ]
            },
            { test: /\.(png|woff|woff2|eot|ttf|svg|jpg)$/,  
                use: [{
                    loader: 'url-loader',
                    options: { 
                         name: 'images/[hash]-[name].[ext]'
                    } 
                }] }
        ]
    },
    plugins: [
        new MiniCssExtractPlugin({
            filename: '../css/custom.min.css'
        })
    ]
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM