egoist / bili

Bili makes it easier to bundle JavaScript libraries.

Home Page:https://bili.egoist.sh

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Replacing `process.env.NODE_ENV`

chopfitzroy opened this issue Β· comments

I am aware of #19 however this functionality appears to have been removed.

I have tried implementing this manually using babel-plugin-transform-define by adding the following to my babel.config.js plugins config:

["babel-plugin-transform-define", {
      "process.env.NODE_ENV": "production",
}]

However this seems to be having no effect on the compiled output, I could do this via @rollup/plugin-replace as demonstrated in this comment but doing it via babel will convert things like process.env.NODE_ENV === "production" to true in the compiled output which I prefer.

Cheers.

UPDATE

Using:

  plugins: {
    vue: true,
    json: true,
    image: true,
    replace: {
      'process.env.NODE_ENV': JSON.stringify( 'production' )
    }
  },

Still does not effect compiled output.

commented

Yes, this options does not exist in v4 (not sure whether exists in v3). Should be fixed.

commented

@CrashyBang hi, I study the source code a bit and find a workaround to bypass this problem under current version. Bili can resolve custom plugins with options, but rollup-plugin-replace is a built-in plugin of Bili and has some internal options. Here:

    pluginsOptions.replace = {
      ...config.plugins.replace,
      values: {
        ...Object.keys(env).reduce((res: Env, name) => {
          res[`process.env.${name}`] = JSON.stringify(env[name])
          return res
        }, {}),
        ...(config.plugins.replace && config.plugins.replace.values)
      }
    }

in rollup-plugin-replace (source), values takes precedence over replace so the plugins.replace is ignored. But you can use plugins.replace.values to make it work.

  plugins: {
    replace: {
      values: {
        'process.env.NODE_ENV': JSON.stringify('production'),
      },
    },
  },

BTW, this is really a trap. Should be pointed out in documentation. πŸ˜…

update:

We will upgrade to @rollup/plugin-replace. And stoo using replace.values as replace is enough.

πŸŽ‰ This issue has been resolved in version 4.9.2 πŸŽ‰

The release is available on:

Your semantic-release bot πŸ“¦πŸš€