reactjs - 使用 webpack 丑化 react bundle js
问题描述
我正在尝试使用以下方法对我的 bundle js 文件进行 Uglify
import webpack from 'webpack';
import path from 'path';
import ExtractTextPlugin from 'extract-text-webpack-plugin';
const GLOBALS = {
'process.env.NODE_ENV': JSON.stringify('production'), //This global makes sure React is built in prod mode. https://facebook.github.io/react/downloads.html
__DEV__: false // potentially useful for feature flags. More info: https://github.com/petehunt/webpack-howto#6-feature-flags
};
export default {
debug: true,
devtool: 'source-map', // more info:https://webpack.github.io/docs/build-performance.html#sourcemaps and https://webpack.github.io/docs/configuration.html#devtool
noInfo: true, // set to false to see a list of every file being bundled.
entry: './src/index',
target: 'web',
output: {
path: __dirname + '/dist', // Note: Physical files are only output by the production build task `npm run build`.
publicPath: '/',
filename: 'bundle.js'
},
devServer: {
contentBase: './dist'
},
plugins: [
new webpack.optimize.OccurenceOrderPlugin(),
new webpack.DefinePlugin(GLOBALS),
new ExtractTextPlugin('styles.css'),
new webpack.optimize.DedupePlugin(),
new webpack.optimize.UglifyJsPlugin({
uglifyOptions:{
output: {
comments: false, // remove comments
},
compress: {
unused: true,
dead_code: true, // big one--strip code that will never execute
warnings: false, // good for prod apps so users can't peek behind curtain
drop_debugger: true,
conditionals: true,
evaluate: true,
drop_console: true, // strips console statements
sequences: true,
booleans: true,
}
},
})
],
module: {
loaders: [
{test: /\.js$/, include: path.join(__dirname, 'src'), loaders: ['babel']},
{test: /(\.css)$/, loaders: ['style', 'css']},
{test: /\.eot(\?v=\d+\.\d+\.\d+)?$/, loader: 'file'},
{test: /\.(woff|woff2)$/, loader: 'url?prefix=font/&limit=5000'},
{test: /\.ttf(\?v=\d+\.\d+\.\d+)?$/, loader: 'url?limit=10000&mimetype=application/octet-stream'},
{test: /\.svg(\?v=\d+\.\d+\.\d+)?$/, loader: 'url?limit=10000&mimetype=image/svg+xml'},
{
test: /\.json$/,
loader: 'json-loader'
},
{
test: /\.(png|jpg|gif)$/,
loader: 'file-loader',
options: {
name: '[path][name].[ext]'
}
}
]
}
};
我在构建构建时遇到以下错误
ERROR in bundle.js from UglifyJs
SyntaxError: Unexpected token: operator (>) [./~/query-string/index.js:9,0]
解决方案
我假设您使用query-sting
的是最新版本 6 的 npm 包,它是使用 ES6 语法编写的,显然您的 webpack 使用的 UglifyJS 版本不支持这种语法(因为显然,您使用的是旧的 Webpack 版本?)。因此,您需要安装query-sting
第 5 版。您可以在他们的 README 页面的INSTALL 部分中查看此信息。
但是你使用哪个 webpack 版本?因为如果你使用的是最新的 Webpack 4,你应该使用optimization.minimize
支持 ES6+ 语法的 config。
推荐阅读
- java - 如何计算浮点值并将其拆分为两个单独的整数值?例如 18.5f 小时等于 int h=18 int m=30
- wix - 如何查看 MSI 是否启用了 InstanceTransforms?
- android - 如何测试依赖于匕首子组件的片段?
- highcharts - Highstocks 如何更改范围选择器按钮的字体
- java - 对 Maven Java 项目进行任何更改都必须执行 Maven“打包或安装”才能在 Eclipse 中运行
- javascript - React 对象初始状态被覆盖
- apache-nifi - 在 Windows 2012 Server 上运行 NiFi 的权限
- asynchronous - 正在运行的进程未显示在活动进程实例中以及 ASYNC 和 SYNC 任务之间的区别
- typescript - 在打字稿中重用函数重载
- python - 使用基本文本提取和映射相似的文本结果,使用 Pandas 转换为两列