这里丑化JavaScript是我的WebPack配置:如何使用的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
})
]
}
这完全uglifies捆JS但问题是全局变量的引用都将丢失。我的意思是全局对象DataMixin
的属性丢失了。
例如,内部的index.html我:
<script>
window.onload = function() {
DataMixin.get_data_page_load(); //DataMixin defined in other js file
};
</script>
uglifying后,我得到错误:
Cannot read property 'get_data_page_load' of undefined
我该如何解决这个问题?我正在使用webpack 2.
AFIK,的WebPack封装了所有的JS代码所以,除非你指定'DataMixin'为'window'将不可用。 – evolutionxbox
@evolutionxbox我试着将它分配给窗口,但属性仍未定义 – Satyadev
可否更新问题以包含DataMixin的[mcve]? – evolutionxbox