与Webpack反应 - UglifyJSPlugin,意外的令牌:关键字(const)

时间:2017-08-06 07:42:44

标签: javascript reactjs webpack uglifyjs

我正在尝试为生产构建我的React项目。当我运行我的生产命令时,我得到一个关于UglifyJSPlugin的错误:Unexpected token: keyword (const)。下面我从package.json&添加了我的所有代码。 webpack.config.js文件。有几个问题与这个问题有关,但没有一个有效。任何建议都会很棒。

的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

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

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

1 个答案:

答案 0 :(得分:0)

UglifyJSPlugin说您的js文件具有关键字const。我认为这是因为输出文件是es6。但是UglifyJSPlugin需要es5