Skip to content

Commit

Permalink
Merge pull request #1768 from tivac/cli-multiple-envs
Browse files Browse the repository at this point in the history
feat: let --environment be specified multiple times
  • Loading branch information
lukastaegert committed Dec 5, 2017
2 parents e63b11e + fe4bb17 commit eadf109
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 8 deletions.
20 changes: 12 additions & 8 deletions bin/src/run/index.js
Expand Up @@ -28,14 +28,18 @@ export default function runRollup ( command ) {
}

if ( command.environment ) {
command.environment.split( ',' ).forEach( pair => {
const index = pair.indexOf( ':' );
if ( ~index ) {
process.env[ pair.slice( 0, index ) ] = pair.slice( index + 1 );
} else {
process.env[ pair ] = true;
}
});
const environment = Array.isArray(command.environment) ? command.environment : [ command.environment ];

environment.forEach( arg => {
arg.split( ',' ).forEach( pair => {
const [ key, value ] = pair.split( ':' );
if ( value ) {
process.env[ key ] = value;
} else {
process.env[ key ] = true;
}
});
})
}

let configFile = command.config === true ? 'rollup.config.js' : command.config;
Expand Down
5 changes: 5 additions & 0 deletions test/cli/samples/config-env-multiple/_config.js
@@ -0,0 +1,5 @@
module.exports = {
description: 'passes environment variables to config file via multiple --environment values',
command: 'rollup --config --environment PRODUCTION,FOO:bar --environment SECOND,KEY:value --environment FOO:foo',
execute: true
};
4 changes: 4 additions & 0 deletions test/cli/samples/config-env-multiple/main.js
@@ -0,0 +1,4 @@
assert.equal( '__ENVIRONMENT__', 'production' );
assert.equal( '__FOO__', 'foo' );
assert.equal('__SECOND__', 'true');
assert.equal('__KEY__', "value");
16 changes: 16 additions & 0 deletions test/cli/samples/config-env-multiple/rollup.config.js
@@ -0,0 +1,16 @@
var replace = require( 'rollup-plugin-replace' );

module.exports = {
input: 'main.js',
output: {
format: 'cjs'
},
plugins: [
replace( {
__ENVIRONMENT__: process.env.PRODUCTION ? 'production' : 'development',
__FOO__: process.env.FOO,
__SECOND__: process.env.SECOND,
__KEY__: process.env.KEY
} )
]
};

0 comments on commit eadf109

Please sign in to comment.