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

support --environment in CLI #388

Merged
merged 1 commit into from
Dec 30, 2015
Merged

support --environment in CLI #388

merged 1 commit into from
Dec 30, 2015

Conversation

Rich-Harris
Copy link
Contributor

Closes #376. Adds an --environment option to the CLI with the following syntax, modelled after the syntax for -g:

rollup -c --environment PRODUCTION,FOO:bar,BAZ:qux

This results in the following conditions inside the rollup.config.js file:

process.env.PRODUCTION === 'true'; // all process.env properties must be strings
process.env.FOO === 'bar';
process.env.BAZ === 'qux';

@eventualbuddha
Copy link
Contributor

👍🏻
On Tue, Dec 29, 2015 at 6:13 PM Rich Harris notifications@github.com
wrote:

Closes #376 #376. Adds an
--environment option to the CLI with the following syntax, modelled after
the syntax for -g:

rollup -c --environment PRODUCTION,FOO:bar,BAZ:qux

This results in the following conditions inside the rollup.config.js file:

process.env.PRODUCTION === 'true'; // all process.env properties must be stringsprocess.env.FOO === 'bar';process.env.BAZ === 'qux';


You can view, comment on, or merge this pull request online at:

#388
Commit Summary

File Changes

Patch Links:


Reply to this email directly or view it on GitHub
#388.

Rich-Harris added a commit that referenced this pull request Dec 30, 2015
support --environment in CLI
@Rich-Harris Rich-Harris merged commit ad78d48 into master Dec 30, 2015
@Rich-Harris Rich-Harris deleted the gh-376 branch December 30, 2015 16:15
@himalayafan
Copy link

@Rich-Harris Is this only for CLI? If you want to do the same inside rollup.rollup{}, how you do it?

I was re-thinking Rollup, and now with the new commits, I start to like it! Maybe it's should be a step-by-step intro page for nobs for me so people can get into this quickly?

@Rich-Harris
Copy link
Contributor Author

Is this only for CLI?

Yep, the assumption is that if you're using the JavaScript API you already have full control over the environment. So you can vary any of the options based on whatever conditions are relevant. Is there something specific you're unsure how to do?

I was re-thinking Rollup, and now with the new commits, I start to like it! Maybe it's should be a step-by-step intro page for nobs for me so people can get into this quickly?

Welcome back! #385 has some anecdotes from other people who struggled initially; it seems like some intro tutorials or screencasts would be a good idea. Hopefully we'll get round to that soon

@himalayafan
Copy link

Issue for me is Mac vs Windows. If I use Rollup config file and want it to react on a variable environment, I have to use two build commands in the script section in the package.json. One for mac and one for windows.

"NODE_ENV=development babel-node scripts/build-dist"
"set NODE_ENV=development babel-node scripts/build-dist"

I didn't found a workaround for this, or can I use Rollup CLI here, and call the config file with it?

Yep! Would be awsome if you get some tutorials and screencasts. Would helped me and others a lot!

@thisconnect
Copy link

@himalayafan I'd love if NPM would provide a first class way to set NODE_ENV and others cross-platform. FYI there is https://www.npmjs.com/package/cross-env

@Rich-Harris
Copy link
Contributor Author

You could certainly use the Rollup CLI here...

{
  "scripts": {
    "build": "rollup -c --environment ENV:development"
  }
}

...unless your build script has a lot of custom stuff (since rollup.config.js is basically just returning a straightforward options object – it gives you a lot of flexibility, but not total flexibility). If you wanted to set environment variables inside scripts/build-dist you should be able to do it like so:

{
  "scripts": {
    "build": "babel-node scripts/build-dist dev"
  }
}

...and then inside build-dist.js you would have something along these lines:

var target = process.argv[2] === 'dev' ? 'development' : 'production';

/* rest of the build script */

@thisconnect
Copy link

"babel-node scripts/build-dist dev" was not enough for me

(function (exports, require, module, __filename, __dirname) { import { rollup } from 'rollup';
                                                              ^^^^^^
SyntaxError: Unexpected token import

"babel-node --presets es2015 scripts/build-dist dev" did work for me.
Note: it has to be es2015, (not es2015-rollup). Since there are many references in the wiki, I would like to have a short "run babel" section within the JavaScript API is that ok?

@Rich-Harris
Copy link
Contributor Author

That makes sense, running Rollup with Babel seems to be a common point of confusion. Until we have such a guide, I recommend having two .babelrc files – one in the root of your project that includes a modules transformer...

// my-project/.babelrc
{
  "presets": ["es2015"]
}

...and one that goes in the src folder (or wherever you keep the files that Rollup is bundling) that doesn't:

// my-project/src/.babelrc
{
  "presets": ["es2015-rollup"]
}

This way, the appropriate configuration will be applied at the appropriate time.

@thisconnect
Copy link

Makes sence. I guess I always disabled the .babelrc to have all the configs in one place. Personally I prefer to use the cli arg and rollup configs over having multiple .babelrc's

@Rich-Harris
Copy link
Contributor Author

@thisconnect you have to be careful though – options passed in directly will be merged with the nearest .babelrc, with unexpected results. I don't know if that behaviour has changed in more recent versions, but I got some pretty wild results with duplicated plugins at one point

@thisconnect
Copy link

@Rich-Harris good to know, thanks. 'babelrc': false, will protect me from the next .babelrc that wants to merge with my options.

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

Successfully merging this pull request may close these issues.

4 participants