privatenumber / esbuild-loader

💠 Speed up your Webpack with esbuild ⚡️
MIT License
3.56k stars 108 forks source link

Define property causes bundle to minify #331

Closed pavel-krasnov closed 1 year ago

pavel-krasnov commented 1 year ago

Bug description

  1. Given the provided template, I run npm start and get a minified bundle under the dist folder.
  2. I set optimization.minimize to false to disable Webpack minimization.
  3. I run npm start and get a not minified bundle under the dist folder.
  4. I add the following to replace values in the bundle:
new EsbuildPlugin({
  define: {
    'process.env.ENV': JSON.stringify('value'),
  },
}),
  1. I run npm start and get a minified bundle under the dist folder.
  2. Adding minify: false to the plugin options constructor argument doesn't change anything.

It looks like uing EsbuildPlugin to define values causes minification no matter what.

Minimal reproduction link

https://stackblitz.com/edit/node-dawpgq

Environment

System:
    OS: Linux 5.0 undefined
    CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 0 Bytes / 0 Bytes
    Shell: 1.0 - /bin/jsh
  Binaries:
    Node: 16.20.0 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 9.4.2 - /usr/local/bin/npm
  npmPackages:
    esbuild-loader: ^3.0.0 => 3.0.0 
    webpack: ^5.75.0 => 5.75.0

Can you contribute a fix?

privatenumber commented 1 year ago

Good catch.

You can turn off minification by setting one of these properties to false: https://github.com/esbuild-kit/esbuild-loader/blob/1a758b41a850897072560f853d44a1607328a255/src/plugin.ts#L132-L136

Previously, it was called the "EsbuildMinifyPlugin". I should've also changed this default behavior when I renamed it to the generic "EsbuildPlugin".

I will change this default behavior but it will have to be in the next breaking.

pavel-krasnov commented 1 year ago

Thank you, it works. Here is what I did:

// No need of this anymore.
// optimization: {
//   minimize: false,
//   minimizer: [
//     // Use esbuild to minify
//     new EsbuildPlugin({}),
//   ],
// },

plugins: [
  new EsbuildPlugin({
    define: {
      'process.env.ENV': JSON.stringify('value'),
    },
    // This is enough.
    minifyIdentifiers: false,
  }),
],
privatenumber commented 1 year ago

Preparing the v4 release where this is fixed. Would you mind helping test?

npm install -D 'esbuild-kit/esbuild-loader#npm/v4'
pavel-krasnov commented 1 year ago

It worked without the minify option after installing the new version, thank you.

privatenumber commented 1 year ago

:tada: This issue has been resolved in version 4.0.0 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

pavel-krasnov commented 1 year ago

@privatenumber I'm afraid now using the plugin causes bandle to not minify even in cases I want to minify.

privatenumber commented 1 year ago

If you can explain, please file a new issue.

I'm locking this thread as the original issue is resolved.