Superagent: Error when Uglifying with Webpack 4

Created on 18 Sep 2018  路  5Comments  路  Source: visionmedia/superagent

Superagent Version: 4.0.0-beta.5
Webpack Version: 4.19.1
UglifyJS Webpack Plugin: 2.0.1

Appears to have an issue when running a production build in Webpack 4 with UglifyJS.

ERROR in bundle.js from UglifyJs Unexpected token: name (root) [./node_modules/superagent/lib/client.js:5,0]...

Possibly an issue with using the reserved keyword 'root'.

Most helpful comment

No, you are wrong it's not a problem of Uglify plugin.

It's because many people use webpack this way:

      {
        test: /\.tsx?$/,
        loader: "ts-loader",
        exclude: /node_modules/,
        options: {
          configFile: path.resolve(__dirname, "../../src/tsconfig.json"),
        },
      },

so the const/let etc... will not be transpiled to es5.

add a rule:

      {
        test: /\.jsx?$/,
        loader: "ts-loader",
        include: /(autobind-decorator)|(superagent)/,
        options: {
          configFile: path.resolve(__dirname, "../../src/tsconfig.json"),
          transpileOnly: true,
        },
      },

will fix this problem.

Situation1: People usually use webpack to bundle with loaders exclude file in node_modules
Situation2: Some package don't provide es5 version, or use module: es6version in package.json

Situation1 + Situation2 = the code send to uglify plugin would be es6 version, causing this problem.

All 5 comments

root is not a reserved keyword. The code works in es5 and es6-compliant browsers without any warnings about this.

Please file a bug in the uglify project.

So I guess root used to be a keyword in node versions pre v6, but doesn't seem to be the cause anyway. I have opened up an issue with UglifyJS. Thanks.

Resolved: UglifyJS 2.0.0 no longer supports ES6 so I will switch to ES5 version.

@djizco Hi! How did you solve the issue? What do you mean of switching to ES5 version?

No, you are wrong it's not a problem of Uglify plugin.

It's because many people use webpack this way:

      {
        test: /\.tsx?$/,
        loader: "ts-loader",
        exclude: /node_modules/,
        options: {
          configFile: path.resolve(__dirname, "../../src/tsconfig.json"),
        },
      },

so the const/let etc... will not be transpiled to es5.

add a rule:

      {
        test: /\.jsx?$/,
        loader: "ts-loader",
        include: /(autobind-decorator)|(superagent)/,
        options: {
          configFile: path.resolve(__dirname, "../../src/tsconfig.json"),
          transpileOnly: true,
        },
      },

will fix this problem.

Situation1: People usually use webpack to bundle with loaders exclude file in node_modules
Situation2: Some package don't provide es5 version, or use module: es6version in package.json

Situation1 + Situation2 = the code send to uglify plugin would be es6 version, causing this problem.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

littlee picture littlee  路  5Comments

tj picture tj  路  4Comments

srohde picture srohde  路  8Comments

ropez picture ropez  路  7Comments

t3hmrman picture t3hmrman  路  9Comments