rollup-plugin-esbuild icon indicating copy to clipboard operation
rollup-plugin-esbuild copied to clipboard

`define` not work like @rollup/plugin-replace

Open Mini-ghost opened this issue 2 years ago • 1 comments

I try to build my plugin, this is original rollup config of plugins

configs.push({
  // ignore
  plugins: [
    replace({
      preventAssignment: true,
      values: {
        __DEV__:
          format === 'esm'
            ? `(process.env.NODE_ENV !== 'production')`
            : 'false',
      },
    }),
    typescript({
      clean: true,
    }),
    resolve(),
    commonjs(),
    format !== 'esm' &&
      terser({
        output: { comments: false },
        compress: {
          drop_console: true,
        },
      }),
  ],
});

Then try to use rollup-plugin-esbuild to replace @rollup/plugin-replace and rollup-plugin-typescript2, like below

configs.push({
  // ignore
  plugins: [
    esbuild({
      minify: process.env.NODE_ENV === 'production',
      define: {
        __DEV__:
          format === 'esm'
            ? '"(process.env.NODE_ENV !== \'production\')"'
            : 'false',
      },
    }),
    resolve(),
    commonjs(),
    format !== 'esm' &&
      terser({
        output: { comments: false },
        compress: {
          drop_console: true,
        },
      }),
  ],
});

My expected result is that when format === 'esm', __DEV__ is replaced with process.env.NODE_ENV !== 'production', for example:

// input
const foo = __DEV__ ? 'is development' : 'is production' 

// output
const foo = process.env.NODE_ENV !== 'production' ? 'is development' : 'is production' 

But current, I got below

// input
const foo = __DEV__ ? 'is development' : 'is production' 

// output
const foo = 'is development'

I'm not sure how to set it up correctly, is there something I'm doing wrong?

Mini-ghost avatar May 28 '22 07:05 Mini-ghost

i have the same problem

takegine avatar Jun 02 '22 09:06 takegine

me too

fantasy525 avatar Oct 21 '22 10:10 fantasy525

same too

takegine avatar Mar 23 '23 12:03 takegine

See https://github.com/evanw/esbuild/issues/630#issuecomment-752689237

However, You can still use @rollup/plugin-replace in rollup plugins.

sxzz avatar Sep 21 '23 09:09 sxzz