TrySound / rollup-plugin-uglify

Rollup plugin to minify generated bundle
MIT License
260 stars 42 forks source link

Unexpected token error when using format esm #65

Closed johannes-z closed 4 years ago

johannes-z commented 5 years ago

When I build my package using umd the uglify plugin works. It doesn't using esm. I already am using uglify-es:

import { minify } from 'uglify-es'
...
uglify({}, minify),

Here are the dependencies in my package.json:

    "rollup": "^1.20.3",
    "rollup-plugin-babel": "^4.3.3",
    "rollup-plugin-babel-minify": "^9.0.0",
    "rollup-plugin-commonjs": "^10.1.0",
    "rollup-plugin-copy": "^3.1.0",
    "rollup-plugin-delete": "^1.1.0",
    "rollup-plugin-json": "^4.0.0",
    "rollup-plugin-node-resolve": "^5.2.0",
    "rollup-plugin-typescript2": "^0.24.0",
    "rollup-plugin-uglify": "^6.0.3",
    "uglify-es": "^3.3.9"

my rollup.config.js:

const formats = ['umd', 'esm']

module.exports = formats.map(format => ({
  input: './src/main.ts',
  external: Object.keys(pkg.dependencies || {}),
  output: {
    name: 'test',
    file: `dist/test.${format}.js`,
    format: format,
    globals: {
      'jquery': '$'
    },
    sourcemap: true,
  },
  plugins: [
    json(),
    typescript({
      useTsconfigDeclarationDir: true,
    }),
    resolve(),
    commonjs(),
    babel({
      exclude: 'node_modules/**',
      extensions: ['.js', '.ts'],
      runtimeHelpers: true,
      sourceMap: true,
    }),
    uglify({}, minify),
  ],
}))

This is the error message I get:

./src/main.ts → dist/c4.core.esm.js...
> 1 | import $ from 'jquery';
    |       ^ Unexpected token: name «$», expected: punc «;»

[!] (plugin uglify) Error: Unexpected token: name «$», expected: punc «;»
SyntaxError: Unexpected token: name «$», expected: punc «;»
    at JS_Parse_Error.get (eval at <anonymous> (...\node_modules\uglify-js\tools\node.js:20:1), <anonymous>:71:23)
    at reportError (...\packages\test\node_modules\jest-worker\build\workers\processChild.js:107:11)
    at reportClientError (...\packages\test\node_modules\jest-worker\build\workers\processChild.js:87:10)
    at execFunction (...\packages\test\node_modules\jest-worker\build\workers\processChild.js:157:5)
    at execHelper (...\packages\test\node_modules\jest-worker\build\workers\processChild.js:139:5)
    at execMethod (...\packages\test\node_modules\jest-worker\build\workers\processChild.js:143:5)
    at process.on.request (...\packages\test\node_modules\jest-worker\build\workers\processChild.js:64:7)
    at process.emit (events.js:193:13)
    at emit (internal/child_process.js:850:12)
    at processTicksAndRejections (internal/process/task_queues.js:81:17)
TrySound commented 5 years ago

Hi, custom minifier is not supported anymore. Also uglify-es is not longer maintained. Please use rollup-plugin-terser instead. Terser is an actively maintained fork of uglify-es.

FoxDaxian commented 4 years ago

I create a new pr for this issue:

renderChunk(code, chunk, outputConf) {
      const illegalFormat = ["es"];
      if (illegalFormat.indexOf(outputConf.format) !== -1) {
        return code;
      }
      return this.worker.transform(code, minifierOptions).catch(error => {
        const { message, line, col: column } = error;
        console.error(
          codeFrameColumns(code, { start: { line, column } }, { message })
        );
        throw error;
      });
    },
TrySound commented 4 years ago

@FoxDaxian What's the point? This is lying that everything works. Just use terser.