Webpack 4 和 Uglify 插件(类型错误:无法读取未定义的属性“长度") [英] Webpack 4 and Uglify Plugin (TypeError: Cannot read property 'length' of undefined)

查看:29
本文介绍了Webpack 4 和 Uglify 插件(类型错误:无法读取未定义的属性“长度")的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Linux 机器上使用 Webpack 4 时遇到问题.构建在开发模式下工作正常,但在生产中失败.它似乎也在 Windows 机器上工作.我确实尝试将 webpack 降级到旧版本,但什么也没做.

I'm having problems with Webpack 4 on a Linux machine. The build works fine in dev mode, but fail in production. It also seems to be working on a windows machine. I did try do downgrade webpack to an older version and nothing.

Nodejs:v10.2.1

Nodejs: v10.2.1

 *TypeError: Cannot read property 'length' of undefined* at node_modules/uglifyjs-webpack-plugin/dist/uglify/index.js:59
        this.workers = workers === true ? _os2.default.cpus().length - 1 : Math.min(Number(workers) || 0, _os2.default.cpus().length - 1);

packge.json

packge.json

{
  "name": "webpack-demo",
  "version": "1.0.0",
  "license": "MIT",
  "scripts": {
    "build": "webpack -p"
  },
  "devDependencies": {},
  "dependencies": {
    "@types/node": "^10.5.1",
    "css-loader": "^0.28.11",
    "global": "^4.3.2",
    "node-sass": "^4.9.1",
    "npm": "^6.1.0",
    "sass-loader": "^7.0.3",
    "style-loader": "^0.21.0",
    "ts-loader": "^4.4.2",
    "typescript": "^2.9.2",
    "uglifyjs-webpack-plugin": "1.0.0-beta.2",
    "webpack": "^4.15.1",
    "webpack-cli": "^3.0.8"
  }
}

webpack.config.js

webpack.config.js

const path = require('path');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
var webpack = require('webpack');
module.exports = {
    entry: './src/index.ts',
    devtool: 'source-map',
     mode: 'production',
     module: {
             rules: [{
                 test: /\.tsx?$/,
                 use: 'ts-loader',
                 exclude: /node_modules/
             },
             {
                 test: /\.scss$/,
                 use: ['style-loader', 'css-loader', 'sass-loader'],
                 exclude: /node_modules/
             }
            ],
         },
    resolve: {
             extensions: ['.tsx', '.ts', '.js','.css','.scss']
         },
    plugins: [
        new UglifyJsPlugin()
    ],
    output: {
        path: path.resolve(__dirname, 'dist'),
        filename: 'main.js'
    }
}

推荐答案

在 Webpack v4 中将 mode 设置为 production 应该做足够的优化,所以不需要特别要求丑化插件.尝试删除 uglifyjs-webpack-plugin 并且也不需要为 build 脚本传递 -p 标志.

Setting mode to production in Webpack v4 should do enough optimisations, so there's no need to specifically require the Uglify plugin. Try remove uglifyjs-webpack-plugin and there's also no need for passing the -p flag for the build script.

如果你想自定义 Uglify 插件,你也可以在 Webpack 的 optimization 配置中进行,参见 https://webpack.js.org/configuration/optimization/

If you want to customise the Uglify plugin, you can also do so in Webpack's optimization config, see https://webpack.js.org/configuration/optimization/

const UglifyJsPlugin = require('uglifyjs-webpack-plugin');

module.exports = {
  //...
  optimization: {
    minimizer: [
      new UglifyJsPlugin({ /* your config */ })
    ]
  }
};

最后,我有一个基本的 webpack v4 入门样板,其中包含 Github 上所有最新的生态系统,看看,看看它是否对你有帮助

Finally, I have a basic webpack v4 starter boilerplate with all the latest ecosystem on Github, take a look and see if it will help you or not

这篇关于Webpack 4 和 Uglify 插件(类型错误:无法读取未定义的属性“长度")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆