diff --git a/bin/help.md b/bin/help.md index 5ec5719..85aa7e0 100644 --- a/bin/help.md +++ b/bin/help.md @@ -20,14 +20,23 @@ Basic options: -m, --sourcemap Generate sourcemap (`-m inline` for inline map) --no-strict Don't emit a `"use strict";` in the generated modules. --no-indent Don't indent result +--environment Settings passed to config file (see example) Examples: +# use settings in config file rollup -c +# in config file, process.env.INCLUDE_DEPS === 'true' +# and process.env.BUILD === 'production' +rollup -c --environment INCLUDE_DEPS,BUILD:production + +# create CommonJS bundle.js from src/main.js rollup --format=cjs --output=bundle.js -- src/main.js -rollup -f iife --globals jquery:jQuery,angular:ng \ +# create self-executing IIFE using `window.jQuery` +# and `window._` as external globals +rollup -f iife --globals jquery:jQuery,lodash:_ \ -i src/app.js -o build/app.js -m build/app.js.map Notes: diff --git a/bin/runRollup.js b/bin/runRollup.js index bff21a1..d789ba8 100644 --- a/bin/runRollup.js +++ b/bin/runRollup.js @@ -20,6 +20,17 @@ module.exports = function ( command ) { command.input = command._[0]; } + if ( command.environment ) { + command.environment.split( ',' ).forEach( function ( pair ) { + var index = pair.indexOf( ':' ); + if ( ~index ) { + process.env[ pair.slice( 0, index ) ] = pair.slice( index + 1 ); + } else { + process.env[ pair ] = true; + } + }); + } + var config = command.config === true ? 'rollup.config.js' : command.config; if ( config ) { diff --git a/test/cli/config-env/_config.js b/test/cli/config-env/_config.js new file mode 100644 index 0000000..5b5f493 --- /dev/null +++ b/test/cli/config-env/_config.js @@ -0,0 +1,5 @@ +module.exports = { + description: 'passes environment variables to config file', + command: 'rollup --config --environment PRODUCTION,FOO:bar', + execute: true +}; diff --git a/test/cli/config-env/main.js b/test/cli/config-env/main.js new file mode 100644 index 0000000..b18f7a9 --- /dev/null +++ b/test/cli/config-env/main.js @@ -0,0 +1,2 @@ +assert.equal( '__ENVIRONMENT__', 'production' ); +assert.equal( '__FOO__', 'bar' ); diff --git a/test/cli/config-env/rollup.config.js b/test/cli/config-env/rollup.config.js new file mode 100644 index 0000000..7d4fb0b --- /dev/null +++ b/test/cli/config-env/rollup.config.js @@ -0,0 +1,12 @@ +var replace = require( 'rollup-plugin-replace' ); + +module.exports = { + entry: 'main.js', + format: 'cjs', + plugins: [ + replace({ + __ENVIRONMENT__: process.env.PRODUCTION ? 'production' : 'development', + __FOO__: process.env.FOO + }) + ] +};