Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

webpack -p fails to uglify es2015 code

The webpack -p cli command fails to uglify es2015 code

package.json

  "devDependencies": {
    "babel": "^6.5.2",
    "babel-core": "^6.13.2",
    "babel-loader": "^6.2.4",
    "babel-preset-es2015": "^6.13.2"
  }

webpack.config.js

var webpack = require("webpack");

var config = {
    entry: './src/app.js',
    devtool: "source-map",
    output: {
        path: '../Scripts',
        filename: 'bundle.js'
    },
    module: {
        loaders: [
            {
                test: /\.js$/,
                loader: 'babel-loader',
                query: {
                    presets: ['es2015']
                },
                include: ["./src"],
                exclude: /node_modules/
            }
        ]
    }
}

module.exports = config;

Error when running webpack -p

ERROR in bundle.js from UglifyJs
Unexpected character '`' [./src/LineEndRenderer.js:33,0]

(es2015 template string use)

Running webpack -d works fine.

like image 413
RoboKozo Avatar asked Aug 10 '16 21:08

RoboKozo


1 Answers

A user (fulls1z3) at github came up with a solution that does not require babel (see post from 11/26/2016):

Here is a copy:

webpack#2 users, I'm hereby trying to help by providing detailed instructions to use the Harmony branch of UglifyJs2 with webpack:

  • Fork webpack,
  • On that fork, delete all branches except master,
  • Clone master branch to a local folder,
  • On the local folder, delete all files,
  • Commit the empty local folder,
  • Download the latest release (ex: v2.1.0-beta.27 at the moment)
  • Extract contents of zip file to the local folder,
  • On the local folder, edit forked package.json, change "uglify-js": "git+https://github.com/mishoo/UglifyJS2.git#harmony" to point UglifyJs2 (harmony branch) dependency to that branch.

  • Commit changes,

  • Finally, point webpack in your package.json to your custom fork: "webpack": "[USERNAME]/webpack#master"

OR,

  • Point webpack in your package.json to fulls1z3/webpack (ES6/ES2015 friendly webpack fork): "webpack": "fulls1z3/webpack#v2.1.0-beta.27-harmony"

I was using webpack globally, so I had to run this too:

npm install yargs supports-color enhanced-resolve interpret tapable webpack-sources source-map uglify-js object-assign async loader-runner acorn watchpack mkdirp ajv ajv-keywords node-libs-browser -g

Then I replaced my global webpack npm folder with webpack-2.1.0-beta.27-harmony.zip from https://github.com/fulls1z3/webpack/releases

Lastly, I replaced my global uglify-js npm folder with UglifyJS2-harmony.zip from https://github.com/mishoo/UglifyJS2/tree/harmony

like image 167
KTCO Avatar answered Nov 13 '22 12:11

KTCO