Nuxt.js: Use BabiliPlugin() instead of UglifyJs()

Created on 15 Mar 2017  路  17Comments  路  Source: nuxt/nuxt.js

version

  • nuxt 0.9.9
  • element-ui 1.2.3

Error Reason

ERROR in 0.console.js from UglifyJs
Unexpected token name 芦i禄, expected punc 芦;禄 [0.console.js:2128,11]

So I look up the source file 0.console.js, I found the source file is element-ui/src/utils/merge.js

export default function(target) {
  for (let i = 1, j = arguments.length; i < j; i++) {
    let source = arguments[i] || {};
    for (let prop in source) {
      if (source.hasOwnProperty(prop)) {
        let value = source[prop];
        if (value !== undefined) {
          target[prop] = value;
        }
      }
    }
  }

  return target;
};

I found the let keyword. But webpack.optimize.UglifyJsPlugin unsupport ES6 grammar default.

My solution
change UglifyJsPlugin plugin

const BabiliPlugin = require('babili-webpack-plugin')
module.exports = {
  build: {
    extend (config, {dev}) {
      if(!dev) {
        config.plugins = config.plugins.filter((plugin) => plugin.constructor.name !== 'UglifyJsPlugin')
        config.plugins.push(new BabiliPlugin())
      }
    }
  }
}

@Atinux Do you have any better ideas?

This feature request is available on Nuxt.js community (#c335)
enhancement

Most helpful comment

I ended up using this:

    extend (config, ctx) {     
      if (!ctx.dev) {
        const UglifyJSWebpackPlugin = require('uglifyjs-webpack-plugin')
        config.plugins = config.plugins.filter((plugin) => plugin.constructor.name !== 'UglifyJsPlugin')
        config.plugins.push(new UglifyJSWebpackPlugin())
      }

as I was having issues with BabiliPlugin.

All 17 comments

Hi @liyanlong

I will take a look at it, indeed, it's not the first time that UglifyJS create problems.
Even if I think that element-ui should be transpiled to ES2015, I don't want nuxt.js to have errors because of this.

Thanks @Atinux

In development, we can ignore BabiliPlugin()

Well isn't this rather related to element-ui and a missing transpiled build?
It's actually kind of weird, because in their package.json they specify

"main": "lib/element-ui.common.js",

as the entry. So it seems they have an transpiled version? But your error points to the src/ folder. 馃

But this would also mean, that element-ui would be unuseable in most vue cli scaffolded projects, as the default webpack production config uses uglifyjs.

@apertureless

you are right, it has beean compiled in the [email protected].

@liyanlong

Thanks for this solution.

@liyanlong thanks for the solution! It worked great for my build process in CircleCI.

If anyone else comes across this issue, "nuxt build" will fail for many components with an Unexpected token: name error. The code above fixed it. I'd recommend re-opening because the issue, as opened, is correct. UglifyJS will fail with ES6 code.

@matthew-dean thanks for your advice.

+1

I ended up using this:

    extend (config, ctx) {     
      if (!ctx.dev) {
        const UglifyJSWebpackPlugin = require('uglifyjs-webpack-plugin')
        config.plugins = config.plugins.filter((plugin) => plugin.constructor.name !== 'UglifyJsPlugin')
        config.plugins.push(new UglifyJSWebpackPlugin())
      }

as I was having issues with BabiliPlugin.

Would this be a solution for this problem? Could someone explain in more detail?
https://github.com/nuxt/nuxt.js/issues/2468

This could be really useful. Last version of uglifyjs 3.3.13 broke a lot of projects, it is less and less reliable. Alternative could be very useful.

@Atinux @clarkdo closable?

How about the so called fabiosantoscode/terser ?
It's a fork of uglify-es that is specifically only interested in ES6+ minification.
Babili is still weaker compression + slower processing than uglify.

Could you open a feature request on CMTY about terser? I am interested to see what the community think of it too

Currently, uglifyjs-webpack-plugin is the default plugin od webpack optimization.minimizer.

And it supports terser as well, so you can use build.optimization.minimizer in nuxt.config.js to config terser.

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

uptownhr picture uptownhr  路  3Comments

bimohxh picture bimohxh  路  3Comments

mattdharmon picture mattdharmon  路  3Comments

surmon-china picture surmon-china  路  3Comments

danieloprado picture danieloprado  路  3Comments