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

Replacing process.env.NODE_ENV #274

Closed
chopfitzroy opened this issue Dec 10, 2019 · 3 comments
Closed

Replacing process.env.NODE_ENV #274

chopfitzroy opened this issue Dec 10, 2019 · 3 comments

Comments

@chopfitzroy
Copy link

chopfitzroy commented Dec 10, 2019

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.

@fi3ework
Copy link
Collaborator

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

@fi3ework fi3ework added the bug label Apr 23, 2020
@fi3ework
Copy link
Collaborator

fi3ework commented Apr 25, 2020

@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.

@egoist
Copy link
Owner

egoist commented Apr 26, 2020

🎉 This issue has been resolved in version 4.9.2 🎉

The release is available on:

Your semantic-release bot 📦🚀

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

No branches or pull requests

3 participants