简体   繁体   English

与Webpack交互-UglifyJSPlugin,意外令牌:关键字(const)

[英]React with Webpack - UglifyJSPlugin, Unexpected token: keyword (const)

I am trying to build my React project for production. 我正在尝试构建我的React项目进行生产。 When I run my production command I get an error about UglifyJSPlugin saying: Unexpected token: keyword (const) . 当我运行生产命令时,出现有关UglifyJSPlugin的错误,提示: Unexpected token: keyword (const) Below I have added all my code from my package.json & webpack.config.js file. 下面,我从package.json和webpack.config.js文件中添加了所有代码。 There are several questions related to this issue but none of them has worked. 有几个与此问题有关的问题,但没有一个起作用。 Any suggestions would be great. 任何建议都很好。

package.json 的package.json

{
    ...
    "scripts": {
        ...
        "prod": "NODE_ENV=production webpack -p"
    },
    "dependencies": {
        "babel-plugin-add-module-exports": "^0.2.1",
        "babel-plugin-react-html-attrs": "^2.0.0",
        "babel-plugin-transform-class-properties": "^6.24.1",
        "babel-plugin-transform-decorators-legacy": "^1.3.4",
        "babel-preset-stage-0": "^6.24.1",
        "babel-preset-stage-2": "^6.24.1",
        ...
        "webpack": "^3.4.1",
        "webpack-dev-server": "^2.6.1"
    },
    "devDependencies": {
        "babel-core": "^6.25.0",
        "babel-loader": "^7.1.1",
        "babel-preset-es2015": "^6.24.1",
        "babel-preset-react": "^6.24.1",
        "uglify-js": "git://github.com/mishoo/UglifyJS2#harmony-v2.8.22",
        "uglifyjs-webpack-plugin": "^0.4.6"
    }
}

webpack.config.js webpack.config.js

var path = require('path');
var webpack = require('webpack');

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

var debug = process.env.NODE_ENV !== 'production';

module.exports = {
    context: path.join(__dirname, "src"),
    devtool: debug ? "inline-sourcemap" : false,
    entry: './js/client.js',
    module: {
        loaders: [
            {
                test: /\.js?$/,
                exclude: /(node_modules|bower_components)/,
                loader: 'babel-loader',
                options: {
                    presets: ['stage-2', 'es2015', 'react'],
                    plugins: ['react-html-attrs', 'transform-class-properties', 'transform-decorators-legacy']
                }
            },
            {
                test: /\.css$/,
                loader: ['style-loader/url', 'file-loader']
            }
        ]
    },
    output: {
        path: __dirname + "/src/",
        filename: 'client.min.js'
    },
    plugins: debug ? [] : [
        new webpack.optimize.OccurrenceOrderPlugin(),
        new UglifyJSPlugin({
            mangle: false,
            sourceMap: false
        })
    ],
};

.babelrc .babelrc

{
    "presets": [
        ["stage-2"],
        ["es2015",  {"modules": false}],
        ["react"]
    ]
}

UglifyJSPlugin saying that yours js files have keyword const . UglifyJSPlugin说您的js文件具有关键字const。 i think its because of output file is es6. 我认为这是因为输出文件是es6。 but UglifyJSPlugin needs es5 但是UglifyJSPlugin需要es5

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM