rollup/rollup-plugin-replace

Plugin isn't replacing anything

sekoyo opened this issue · 4 comments

Hey, I'm a bit confused about what this plugin is supposed to do. I thought I could use it to remove unoptimised code from the bundle like in Webpack but it doesn't seem to do anything for my UMD or ESM builds?

config:

const extensions = [...DEFAULT_EXTENSIONS, '.ts', '.tsx'];
const globals = {
  react: 'React',
  'react-dom': 'ReactDOM',
};
const external = Object.keys(globals);
const input = 'src/index.tsx';

const plugins = [
  replace({
    'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
    ENVIRONMENT: JSON.stringify(process.env.NODE_ENV), // Tried this too
  }),
  nodeResolve({
    extensions,
    dedupe: external,
  }),
  commonjs({
    exclude: 'src/**',
  }),
  babel({
    exclude: 'node_modules/**',
    extensions,
  }),
];

export default [
  {
    input,
    output: {
      file: pkg.main,
      format: 'umd',
      name: 'flGrid',
      esModule: false,
      globals,
      sourcemap: true,
    },
    // plugins: plugins.concat(terser()),
    plugins,
    external,
  },
  {
    input,
    output: {
      file: pkg.module,
      format: 'esm',
      globals,
    },
    plugins,
    external,
  },
];

I have verified that process.env.NODE_ENV is production but if I look at the compiled code I see all the unstripped, unoptimised stuff. For example this is from Emotion the CSS lib:

Screenshot 2019-08-24 at 17 17 25

As you can see it seems that process.env.NODE_ENV hasn't been replaced and the unoptimised code is still there?

I am using the latest of all libraries as I just set this up.

Actually it's working in my userland code, but not for Emotion even with the babel emotion plugin hmm

Here's a minimal repo of the bug: https://github.com/DominicTobias/emotion-rollup-dead-code

Notice how in my own code the conditional "I should be eliminated" has successfully been removed, but the Emotion stuff hasn't:

Screenshot 2019-08-24 at 17 45 35

Try to put it after babel

Whoops thanks should have tried that, works!