gpt4 book ai didi

javascript - 如何使用 webpack 丑化 javascript

转载 作者:行者123 更新时间:2023-11-28 04:13:46 25 4
gpt4 key购买 nike

这是我的 webpack 配置:

var path = require('path');
var webpack = require('webpack');
var CompressionPlugin = require("compression-webpack-plugin");
const UglifyJSPlugin = require('uglifyjs-webpack-plugin');

module.exports = {
entry: './index.js',
output: {
path: __dirname,
filename: 'public_html/assets/js/bundle.js'
},
resolveLoader: {
modules: ["node_modules"]
},

module: {

rules: [
{
enforce: 'pre',
test: /\.tag$/,
exclude: /node_modules/,
loader: 'riotjs-loader',
},

{
test: /\.js$/,
loader: 'babel-loader',
exclude: /node_modules/,
options: {
presets: [
'es2015',
],
"babelrc": false
}
},

{
test: /\.css$/,
use: [
{
loader: "css-loader",
options: {
modules: false
}
}
]
}
]
},
plugins: [
new webpack.LoaderOptionsPlugin({debug: true}),
new UglifyJSPlugin(),
new webpack.ProvidePlugin({
riot: 'riot'
}),
new CompressionPlugin({
asset: "[path].gz[query]",
algorithm: "gzip",
test: /\.(js|html)$/,
threshold: 10240,
minRatio: 0.8
})
]
}

这完全丑化了bundle js,但问题是全局变量引用丢失了。我的意思是全局对象 DataMixin 的属性丢失了。

例如,在 index.html 中我有:

<script>
window.onload = function () {
DataMixin.get_data_page_load(); //DataMixin defined in other js file
};
</script>

丑化后,出现错误:

Cannot read property 'get_data_page_load' of undefined

我该如何解决这个问题?我正在使用 webpack 2。

最佳答案

对于 webpack 2,您不需要安装外部 uglify 插件。
在您的 webpack 配置中,将此 new UglifyJSPlugin(), 替换为以下内容:

new webpack.optimize.UglifyJsPlugin({
sourceMap: true,
minimize: true,
compressor: {
warnings: false,
},
}),

关于javascript - 如何使用 webpack 丑化 javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46050564/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com