Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Webpack5 ES5 Terser compression configuration is incorrect #16135

Closed
chenyulun opened this issue Aug 11, 2022 · 9 comments · Fixed by webpack-contrib/terser-webpack-plugin#509

Comments

@chenyulun
Copy link

Bug report

What is the current behavior?

Everything that can be configured is configured, but still cannot be converted to ES5

const path = require('path');

module.exports = {
  entry: './src/index.js',
  mode: 'production',
  target: ['web', 'es5'],
  output: {
    environment: {
      arrowFunction: false,
      const: false,
      destructuring: false,
      forOf: false
    },
    filename: 'main.js',
    path: path.resolve(__dirname, 'dist'),
  },
};

input code

const abc =  {
  c() {
    return {cc: 1}
  }
}
console.log(abc)

output code

console.log({c:()=>({cc:1})}); // Hopefully there's no arrow function

Webpack target should actively configure Terser's options.compress.arrows = False if ES5 is present

terserOptions = {
  //...
compress: {
    ecma: 5,
    // ...
    arrows: false,
  },
  // ..
}

If the current behavior is a bug, please provide the steps to reproduce.
Error recurrence project
https://github.com/chenyulun/webpack5-test

yarn build

What is the expected behavior?
// Hopefully there's no arrow function

Other relevant information:
webpack version: 5.74.0
Node.js version: v16.15.1
Operating System: macOS 12.3.1
Additional tools: null

@vankop
Copy link
Member

vankop commented Aug 11, 2022

hm.. yeah, we defaults should be updated..

@chenyulun
Copy link
Author

chenyulun commented Aug 11, 2022

Has Webpack5 always had this problem? I've got other projects that don't have 'arrows:false' configured either, don't see why
When will it be fixed?

@alexander-akait
Copy link
Member

We handle ecma here https://github.com/webpack-contrib/terser-webpack-plugin/blob/master/src/index.js#L490, so I will fix it on the plugin side

@chenyulun
Copy link
Author

Check that all Terser configurations are ES5 compliant,
tks

@chenyulun
Copy link
Author

Alternatively, it is recommended that the Terser compressor be compatible with an ESMA configuration of 5 without setting the arrows to False

@chenyulun
Copy link
Author

chenyulun commented Aug 11, 2022

test.mjs

import { minify } from "terser";

var code = `
(function() {
  var __webpack_exports__ = {};
var abc = {
  data() {
  return {
    a: 2
  };
}};
console.log(abc)
})();
`;
var result = await minify(code, { compress: { passes: 2 }, module: false, ecma: 5 }) // Webpack configuration of Terser
console.log(result.code); 
➜  node test.mjs
console.log({data:()=>({a:2})});

terser/terser#1241

@alexander-akait
Copy link
Member

@chenyulun

Alternatively, it is recommended that the Terser compressor be compatible with an ESMA configuration of 5 without setting the arrows to False

I agree that at least the ecma option should be respected (ideally browserslist).

@alexander-akait
Copy link
Member

@chenyulun Fix - webpack-contrib/terser-webpack-plugin#509 (swc affected too) + change ecma from 5 should enable more optimizations

@alexander-akait
Copy link
Member

Anyway will be great if terser handle it too

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants