简体   繁体   English

如何使webpack的uglifyjs插件不破坏sass的源映射?

[英]How to make webpack's uglifyjs plugin not break sass's source maps?

Here's the setup: 设置如下:

package.json : package.json

{
  "dependencies": {
    "css-loader": "^0.26.0",
    "extract-text-webpack-plugin": "^1.0.1",
    "html-webpack-plugin": "^2.24.1",
    "node-sass": "^3.13.0",
    "sass-loader": "^4.0.2",
    "style-loader": "^0.13.1",
    "webpack": "^1.13.3"
  }
}

webpack.config.js : webpack.config.js

var webpack = require('webpack');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var ExtractTextPlugin = require('extract-text-webpack-plugin');

module.exports = {
    entry: './1.js',
    output: {
        path: 'dist',
        filename: 'bundle.js',
    },
    module: {
        loaders: [
            {test: /\.sass$/, loader: ExtractTextPlugin.extract('style', 'css?sourceMap!sass?sourceMap')},
        ]
    },
    plugins: [
        new HtmlWebpackPlugin({
            template: 'template.ejs',
        }),
        new ExtractTextPlugin('[name].css'),
        new webpack.optimize.UglifyJsPlugin(),
    ],
    devtool: 'source-map',
};

template.ejs : template.ejs

<!doctype html>
<html>
<body>

<div></div>

</body>
</html>

1.js : 1.js

require('./1.sass');

1.sass : 1.sass

body
    background: #ddd
div
    width: 100px
    height: 100px
    margin: auto
    background: #333

Then 然后

$ rm -rf dist/* && ./node_modules/.bin/webpack

And open http://example.com/dist in Chrome. 然后在Chrome中打开http://example.com/dist Then go to Developer Tools > Elements tab . 然后转到Developer Tools > Elements tab Click on the div , and then on the link for div { width: 100px; ... } 点击div ,然后点击div的链接div { width: 100px; ... } div { width: 100px; ... } block. div { width: 100px; ... }块。 And you'll find yourself on line 2. But when you comment out the new webpack.optimize.UglifyJsPlugin() line, you'll be at line 3, as expected. 并且您会发现自己在第2行上。但是,当注释掉new webpack.optimize.UglifyJsPlugin()行时,您将排在第3行,这与预期的一样。 What am I doing wrong? 我究竟做错了什么?

First thing to mention is that UglifyJsPlugin switches all loaders into minimizing mode. 首先要提到的是UglifyJsPlugin 所有加载程序切换到最小化模式。 From the docs : 文档

Minimize all JavaScript output of chunks. 最小化所有JavaScript块的输出。 Loaders are switched into minimizing mode. 装载机切换到最小化模式。

But thankfully it was changed in the coming 2.x version . 但是幸运的是,它在即将发布的 2.x版本中进行了更改。

The other issue is that libsass generates wrong source map , when outputStyle is compressed . 另一个问题是,在compressed outputStyle时, libsass生成错误的源映射 And outputStyle is compressed, when you don't specify it explicitly and minification is turned on. outputStyle被压缩,当你不指定它明确地缩小时开启。

So, for now the workaround is to specify some outputStyle other than compressed : 因此,目前的解决方法是指定某些outputStyle而不是compressed

var webpack = require('webpack');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var ExtractTextPlugin = require('extract-text-webpack-plugin');

module.exports = {
    entry: './1.js',
    output: {
        path: 'dist',
        filename: 'bundle.js',
    },
    module: {
        loaders: [
            {test: /\.sass$/, loader: ExtractTextPlugin.extract('style', 'css?sourceMap!sass?sourceMap')},
        ]
    },
    sassLoader: {
        outputStyle: 'nested',
    },
    plugins: [
        new HtmlWebpackPlugin({
            template: 'template.ejs',
        }),
        new ExtractTextPlugin('[name].css'),
        new webpack.optimize.UglifyJsPlugin(),
    ],
    devtool: 'source-map',
};

UPD It seems there are also issues with most likely source-map package and css-loader package. UPD似乎最有可能的source-map软件包和css-loader软件包也存在问题。 So you might consider disabling minification, like in: css?sourceMap&-minimize . 因此,您可以考虑禁用缩小功能,例如: css?sourceMap&-minimize

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

相关问题 如何使Webpack,Sass和源地图在多页面应用程序中运行? - How to make webpack, sass and source maps go along in a multi page app? webpack + leaflet插件 - webpack + leaflet plugin(s) 如何在webpack中加载sass样式表中使用的图像? - How can I load image(s) used in sass stylesheet in webpack? webpack 4 sass-loader 不生成 scss 源映射 - webpack 4 sass-loader not generating scss source maps 使用UglifyJS插件进行Webpack优化会导致运行时错误 - Webpack optimize with UglifyJS plugin causes runtime error 将 TerserWebpackPlugin webpack 插件的源 map 选项设置为 true 会显着增加 webpack 构建时间 - Setting the TerserWebpackPlugin webpack plugin's source map option to true significantly increases webpack build time 如何使用 uglifyjs-webpack 将源文件 (Javascript) 缩小为缩小文件 - How to minify a source file (Javascript) into a minify file using uglifyjs-webpack 在Webpack配置的“插件”或“最小化器”部分中使用uglifyjs-webpack-plugin? - Use uglifyjs-webpack-plugin in `plugins` or in `minimizer` section of Webpack config? uglifyjs-webpack-plugin如何在不禁用Mangle属性的情况下禁用Mangle Builtns - uglifyjs-webpack-plugin how to disable mangle builtns without disable mangle properties Webpack 插件 API:在解析过程中获取模块的源映射 - Webpack plugin API: getting source maps for a module during parsing
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM