Skip to content

Feature Request: replace/define constants #181

Closed

Description

@developit

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.

Activity

  1. ForsakenHarmony commented on Aug 1, 2018

    @ForsakenHarmony
    Collaborator

    creator makes a feature request 👀

  2. Andarist commented on Aug 1, 2018

    @Andarist
    Collaborator

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

  3. developit commented on Aug 1, 2018

    @developit
    OwnerAuthor

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

  4. developit commented on Aug 1, 2018

    @developit
    OwnerAuthor

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

  5. Andarist commented on Aug 2, 2018

    @Andarist
    Collaborator

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

  6. developit commented on Aug 2, 2018

    @developit
    OwnerAuthor

    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
  7. Andarist commented on Aug 2, 2018

    @Andarist
    Collaborator

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

  8. balloob commented on Aug 7, 2018

    @balloob

    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.

  9. developit commented on Aug 14, 2018

    @developit
    OwnerAuthor

    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?

  10. kutuluk commented on Sep 18, 2018

    @kutuluk

    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?

  11. kutuluk commented on Sep 21, 2018

    @kutuluk

    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

  12. balloob commented on Sep 21, 2018

    @balloob

    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.

  13. chopfitzroy commented on Jul 27, 2020

    @chopfitzroy

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

  14. developit commented on Jul 30, 2020

    @developit
    OwnerAuthor

    Not currently, no.

  15. added
    has-fix
    on Dec 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    has-fix

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions