badcafe / rollup-plugin-inject-process-env

Inject environment variables in process.env with Rollup
MIT License
51 stars 5 forks source link

Is this compatible with rollup-plugin-babel? #3

Closed schester44 closed 4 years ago

schester44 commented 4 years ago

I get an error when trying to use this plugin with core-js and babel's @babel/preset-env

SyntaxError: /node_modules/core-js/internals/engine-v8-version.js: Identifier 'process' has already been declared (7:4)
schester44 commented 4 years ago

ended up using the @rollup/plugin-replace plugin to get around this.

import replace from "@rollup/plugin-replace";

plugins: [replace(envKeys())]
const envKeys = (envRaw) => {
  return Object.keys(envRaw).reduce(
    (envValues, envValue) => ({
      ...envValues,
      [`process.env.${envValue}`]: JSON.stringify(envRaw[envValue])
    }),
    {}
  );
};
ppoulard commented 4 years ago

Please can you send me a minimal conf that fails with core-js and babel, I need to test my update with such conf. Thanks

schester44 commented 4 years ago

Give this a shot https://github.com/schester44/rollup-plugin-inject-process-env-reproduction

The errors i'm seeing

[!] Error: Identifier 'process' has already been declared
node_modules/core-js/modules/es.promise.js (38:4)
36: var TypeError = global.TypeError;
37: var document = global.document;
38: var process = global.process;
        ^
39: var $fetch = getBuiltIn('fetch');
40: var newPromiseCapability = newPromiseCapabilityModule.f;
Error: Identifier 'process' has already been declared
schester44 commented 4 years ago

Looks like its happening when I use @rollup/plugin-node-resolve .. Maybe I need to exclude core-js from that?

    resolve({
      include: "node_modules/**",
    })
ppoulard commented 4 years ago

Hi, there is a new version of the plugin, with a test that covers your issue.

I play it on you code, changing it with :

async function main() {
  await Promise.resolve({});
}
console.log(process.env.ACCOUNT);
main();

Compile result :

$ ./node_modules/.bin/yarn run dev
rollup v2.6.1
bundles test.js → output.js...
created output.js in 2.9s

Running :

$ node output.js
test