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

Feature Request: replace/define constants #181

Closed
developit opened this issue Aug 1, 2018 · 14 comments
Closed

Feature Request: replace/define constants #181

developit opened this issue Aug 1, 2018 · 14 comments
Labels
has-fix A fix is available, but may not yet be released.

Comments

@developit
Copy link
Owner

Usage:

microbundle --define NODE_ENV=production

Result:

input:

if (NODE_ENV==='production') {
  console.log('prod');
}
else {
  console.log('dev');
}

output with --define NODE_ENV=production:

console.log('prod')

All we have to do is use rollup-plugin-replace to injec the value. Uglify handles the rest.

@ForsakenHarmony
Copy link
Collaborator

creator makes a feature request 👀

@Andarist
Copy link
Collaborator

Andarist commented Aug 1, 2018

Not sure if you like the idea but ive written this - https://github.com/Andarist/rollup-plugin-dotenv . Inspired by CRA which uses .env files to keep env vars organised (sorry that my plugin has no README 😉 yet...)

@developit
Copy link
Owner Author

@Andarist looks nice - it also supports inlining regular env vars? Could always have both.

@developit
Copy link
Owner Author

lol @ForsakenHarmony it was more of a "what do y'all think of this?" :P

@Andarist
Copy link
Collaborator

Andarist commented Aug 2, 2018

@Andarist looks nice - it also supports inlining regular env vars? Could always have both.

It doesn't - mainly because:

  • without special casing (like ur --define proposal) it's hard to tell which env vars should be used and I didnt want to include everything that can be found on process.env
  • when bundling libraries I don't want to have NODE_ENV replaced (leaving that to consumers, so a library can differentiate between dev/prod code paths)

It does support overriding with env vars what is specified in .env files though.

BTW. rollup supports this:

{
  "scripts": {
    "build": "rollup -c --environment INCLUDE_DEPS,BUILD:production"
  }
}

Not a fun of that syntax though.

@developit
Copy link
Owner Author

Makes sense. That being said, rollup's syntax is pretty much what I'm in need of - I have 2 builds from the same source, but want to define a variable that changes the output. Basically something like #ifdef:

// a bunch of these
if (PRETTY) {
  // whitespace handling stuff here
}
microbundle --define PRETTY=true && microbundle --define PRETTY=false

@Andarist
Copy link
Collaborator

Andarist commented Aug 2, 2018

if u have bunch of such variables (btw what they are? if thats not a mystery) it's imho good to gather them in .env files (which can act like a presets of different sets of those vars)

in the future i'd like to add special env vars to microbundle (need to rethink how it should work exactly) - such as REACT, PREACT, SERVER, NATIVE, BROWSER - multiple bundles could be outputted based on used package.json keys or smth.

I understand though that for other use cases than those from last paragraph and when having just 1 or 2 variables it's easier to use --define than creating .env files. Although I don't like rollup's syntax I'd prefer using it just to align to it as microbundle is built on top of rollup anyway and should be easier to explain this option (we could link to rollup's docs).

@balloob
Copy link

balloob commented Aug 7, 2018

I came here to see if this feature existed 👍 Would love it.

To keep it simple, you could use --replace instead of --define to indicate that we're doing variable replacement and that it's not related to environment variables. Otherwise people might also expect that they can check against process.env.NODE_ENV.

@developit
Copy link
Owner Author

I'm fine with the .env files, just it seems like it would make sense to support both. Presumably there's already a way to hydrate environment variables from a .env file like load-from-env .env microbundle?

@kutuluk
Copy link

kutuluk commented Sep 18, 2018

In my opinion this should have been done in the initial release. Tell me, please, how can I implement this manually, until this feature is implemented?

@kutuluk
Copy link

kutuluk commented Sep 21, 2018

Can define through a .js file:

.env

HOST=localhost

definitions.js

import dotenv from 'dotenv';

dotenv.config();

export default definitions => {
	definitions.PRETTY = true;
	definitions.HOST = process.env.HOST;
	definitions.DEVELOPMENT = process.env.NODE_ENV === 'development';
};

where the initial definitions are taken from the command line --define

@balloob
Copy link

balloob commented Sep 21, 2018

One thing to be cautious of is that if you expose all environment variables, one could accidentally leak credentials if they have just process.env in their code. Create React App requires env variables to be prefixed with REACT_APP_ for this reason.

@chopfitzroy
Copy link

Hey @developit I notice --define has been added, is it possible to define these values from an .env file?

@developit
Copy link
Owner Author

Not currently, no.

@developit developit added the has-fix A fix is available, but may not yet be released. label Dec 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
has-fix A fix is available, but may not yet be released.
Projects
None yet
Development

No branches or pull requests

6 participants