我尝试了上一个Webpack版本,但是当我使用UglifyJsPlugin时,我得到了错误:
无法将未定义或空值转换为对象。 UglifyJs app.min.js中的错误:无法将未定义或null转换为defineSetter () at defineSetter() at AST_Dot.eval eval at AST_Dot.eval as evaluate:13531:23( (.../node_modules/uglifyjs-webpack-plugin/node_modules/uglify-es/tools/node.js:21:1),:7660:24) AST_Dot.eval as optimize at Compressor.before:4687:43)在(.../node_modules/uglifyjs-webpack-plugin/node_modules/uglify-es/tools/node.js:21:1),的Compressor.before :7655:9)在AST_Call.eval as transform:4598:31)在(.../node_modules/uglifyjs-webpack-plugin/node_modules/uglify-es/tools/node.js:21:1),的Compressor.before :7655:9)在AST_SimpleStatement.eval as transform:4586:25)在地图上(在(.../node_modules/uglifyjs-webpack-plugin/node_modules/uglify-es/tools/node.js:21:1),:130:23) (在(.../node_modules/uglifyjs-webpack-plugin/node_modules/uglify-es/tools/node.js:21:1),):156:52)在(.../node_modules/uglifyjs-webpack-plugin/node_modules/uglify-es/tools/node.js:21:1),:4585:16在do_list:4682:25)在(.../node_modules/uglifyjs-webpack-plugin/node_modules/uglify-es/tools/node.js:21:1),的Compressor.before :7655:9)在AST_Function.eval as transform:4586:25)在地图上(在(.../node_modules/uglifyjs-webpack-plugin/node_modules/uglify-es/tools/node.js:21:1),:130:23) (在(.../node_modules/uglifyjs-webpack-plugin/node_modules/uglify-es/tools/node.js:21:1),):156:52)在(.../node_modules/uglifyjs-webpack-plugin/node_modules/uglify-es/tools/node.js:21:1),:4585:16在do_list:4688:21)在(.../node_modules/uglifyjs-webpack-plugin/node_modules/uglify-es/tools/node.js:21:1),的Compressor.before :7655:9)在AST_Call.eval as transform:4713:43)在(.../node_modules/uglifyjs-webpack-plugin/node_modules/uglify-es/tools/node.js:21:1),的Compressor.before :7655:9)在AST_UnaryPrefix.eval as transform:4718:33)在(.../node_modules/uglifyjs-webpack-plugin/node_modules/uglify-es/tools/node.js:21:1),的Compressor.before :7655:9)在AST_Binary.eval as transform:4718:33)在(.../node_modules/uglifyjs-webpack-plugin/node_modules/uglify-es/tools/node.js:21:1),( Compressor.before ):7655:9)在AST_Assign.eval as transform Child html-webpack-plugin for " index.html ":1资产入口点未定义=index.html index.html 651字节{0}已建(webpack)/buildin/global.js 489字节{0}已建(as transform)/buildin/module.js 497字节{0} built +1隐藏模块npm错误!代码ELIFECYCLE npm错误!错误的2 npm错误!霓虹灯@0.0.0 prod:
webpack -pnpm错误!退出状态2 npm错误!npm错误!neon@0.0.0 prod脚本失败。npm错误!这可能不是国家预防机制的问题。上面可能有额外的日志输出。 npm错误!这个运行的完整日志可以在: npm中找到! .../.npm/_logs/2018-06-12T10_12_21_278Z-debug.log
webpack.config.js
const webpack = require('webpack');
const webpackMerge = require('webpack-merge');
const path = require('path');
const WebpackNotifierPlugin = require('webpack-notifier');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CompressionPlugin = require('compression-webpack-plugin');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const TARGET = process.env.npm_lifecycle_event;
console.log(`target event is ${TARGET}`);
let outputFileName = 'app';
outputFileName += TARGET === 'prod' ? '.min.js' : '.js';
const common = {
entry: ['babel-polyfill', './index.jsx'],
output: {
publicPath: '/',
},
module: {
rules: [
{
test: /\.js[x]?$/,
exclude: /(node_modules)/,
use: {
loader: 'babel-loader',
},
},
],
},
plugins: [
new webpack.ProvidePlugin({
jQuery: 'jquery',
$: 'jquery',
jquery: 'jquery',
'window.jQuery': 'jquery',
}),
new WebpackNotifierPlugin(),
],
resolve: {
modules: [
path.resolve('.'),
path.resolve('script'),
path.resolve('script', 'views'),
'node_modules',
],
extensions: ['.js', '.jsx', '.json'],
},
};
if (TARGET === 'prod' || !TARGET) {
module.exports = webpackMerge(common, {
output: {
path: path.resolve(__dirname, 'dist'),
filename: outputFileName,
},
module: {
rules: [
{
test: /\.scss$/,
loaders: [
'style-loader',
'css-loader',
{
loader: 'postcss-loader',
options: {
config: {
path: 'postcss.config.js',
},
},
},
'sass-loader',
],
},
{
test: /\.less$/,
loaders: ['style-loader', 'css-loader', 'less-loader'],
},
{
test: /\.css$/,
use: ['style-loader', 'css-loader'],
},
{
test: /\.(eot|ttf|svg|gif|png|jpg|otf|woff|woff2)$/,
loader: 'file-loader',
},
],
},
optimization: {
minimizer: [
new UglifyJsPlugin({
uglifyOptions: {
parallel: true,
compress: {
warnings: false, // Suppress uglification warnings
pure_getters: true,
unsafe: true,
unsafe_comps: true,
conditionals: true,
unused: true,
comparisons: true,
sequences: true,
dead_code: true,
evaluate: true,
if_return: true,
join_vars: true,
},
output: {
comments: false,
},
exclude: [/\.min\.js$/gi], // skip pre-minified libs
},
}),
],
},
plugins: [
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify('production'),
}),
new HtmlWebpackPlugin({
title: 'test-project',
template: 'index-template.ejs',
}),
new webpack.optimize.AggressiveMergingPlugin(),
new webpack.optimize.OccurrenceOrderPlugin(),
new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
new CompressionPlugin({
asset: '[path].gz[query]',
algorithm: 'gzip',
test: /\.js$|\.css$|\.html$/,
threshold: 10240,
minRatio: 0,
}),
],
});
}package.json
"dependencies": {
"axios": "^0.18.0",
"bignumber.js": "^7.2.1",
"bootstrap": "^4.1.1",
"classnames": "^2.2.5",
"font-awesome": "^4.7.0",
"jquery": "^3.3.1",
"koa": "^2.5.1",
"koa-send": "^4.1.3",
"koa-static": "^4.0.3",
"lodash": "^4.17.10",
"moment": "^2.22.1",
"popper.js": "^1.14.3",
"react": "^16.3.2",
"react-dom": "^16.3.2",
"react-redux": "^5.0.7",
"react-slidedown": "^1.3.0",
"react-toastify": "^4.1.0",
"redux": "^4.0.0",
"redux-saga": "^0.16.0"
},
"devDependencies": {
"autoprefixer": "^8.4.1",
"babel": "^6.23.0",
"babel-core": "^6.26.3",
"babel-eslint": "^8.2.3",
"babel-loader": "^7.1.4",
"babel-plugin-syntax-dynamic-import": "^6.18.0",
"babel-plugin-transform-async-to-generator": "^6.24.1",
"babel-plugin-transform-class-properties": "^6.24.1",
"babel-plugin-transform-object-rest-spread": "^6.26.0",
"babel-polyfill": "^6.26.0",
"babel-preset-env": "^1.6.1",
"babel-preset-react": "^6.24.1",
"compression-webpack-plugin": "^1.1.11",
"css-loader": "^0.28.11",
"duplicate-package-checker-webpack-plugin": "^3.0.0",
"eslint": "^4.19.1",
"eslint-config-airbnb": "^16.1.0",
"eslint-plugin-import": "^2.11.0",
"eslint-plugin-jsx-a11y": "^6.0.3",
"eslint-plugin-react": "^7.7.0",
"file-loader": "^1.1.11",
"friendly-errors-webpack-plugin": "^1.7.0",
"html-webpack-plugin": "^3.2.0",
"http-server": "^0.11.1",
"less": "^3.0.2",
"less-loader": "^4.1.0",
"node-sass": "^4.9.0",
"nodemon": "^1.17.5",
"npm-install-webpack-plugin": "^4.0.5",
"postcss": "^6.0.22",
"postcss-loader": "^2.1.4",
"redux-devtools-extension": "^2.13.2",
"sass-loader": "^7.0.1",
"sass-resources-loader": "^1.3.3",
"style-loader": "^0.21.0",
"uglifyjs-webpack-plugin": "^1.2.5",
"url-loader": "^1.0.1",
"webpack": "^4.6.0",
"webpack-cli": "^2.1.2",
"webpack-dev-server": "^3.1.4",
"webpack-merge": "^4.1.2",
"webpack-notifier": "^1.6.0"
}项目结构:
project
├── script
│ ├── actions
│ ├── components
│ ├── constants
│ ├── sagas
│ ├── state
│ ├── utils
│ ├── views
│ └── server.js
├── postcss.config.js
├── index.html
├── index.jsx
├── postcss.config.js
├── webpack.config.js发布于 2018-06-15 14:46:29
我移动了exclude选项,它起作用了:
new UglifyJsPlugin({
uglifyOptions: {
compress: {
warnings: false, // Suppress uglification warnings
unsafe: true,
unsafe_comps: true,
conditionals: true,
unused: true,
comparisons: true,
sequences: true,
dead_code: true,
evaluate: true,
if_return: true,
join_vars: true,
},
output: {
comments: false,
},
},
exclude: [/\.min\.js$/gi], // skip pre-minified libs
}),发布于 2018-06-14 11:04:48
删除pure_getters或将pure_getters传递为“严格”
pure_getters (默认值:“严格”) --如果您对此传递为true,UglifyJS将假定对象属性访问(例如foo.bar或foo"bar")没有任何副作用。指定“严格”只在foo确定不抛出(即null或未定义)时将foo.bar视为无副作用的。
https://github.com/mishoo/UglifyJS2/tree/harmony#compress-options
https://stackoverflow.com/questions/50815159
复制相似问题