Browse Source

add --config option to CLI (#226)

better-aggressive
Rich Harris 9 years ago
parent
commit
1225d0aef7
  1. 4
      bin/help.md
  2. 1
      bin/rollup
  3. 101
      bin/runRollup.js
  4. 1
      package.json
  5. 5
      test/cli/config-es6/_config.js
  6. 1
      test/cli/config-es6/main.js
  7. 9
      test/cli/config-es6/rollup.config.js
  8. 5
      test/cli/config-override/_config.js
  9. 1
      test/cli/config-override/main.js
  10. 9
      test/cli/config-override/rollup.config.js
  11. 5
      test/cli/config-true/_config.js
  12. 1
      test/cli/config-true/main.js
  13. 9
      test/cli/config-true/rollup.config.js
  14. 5
      test/cli/config/_config.js
  15. 11
      test/cli/config/_expected.js
  16. 1
      test/cli/config/main.js
  17. 9
      test/cli/config/rollup.config.js

4
bin/help.md

@ -7,6 +7,8 @@ Basic options:
-v, --version Show version number -v, --version Show version number
-h, --help Show this help message -h, --help Show this help message
-c, --config Use this config file (if argument is used but value
is unspecified, defaults to rollup.config.js)
-i, --input Input (alternative to <entry file>) -i, --input Input (alternative to <entry file>)
-o, --output <output> Output (if absent, prints to stdout) -o, --output <output> Output (if absent, prints to stdout)
-f, --format [es6] Type of output (amd, cjs, es6, iife, umd) -f, --format [es6] Type of output (amd, cjs, es6, iife, umd)
@ -21,6 +23,8 @@ Basic options:
Examples: Examples:
rollup -c
rollup --format=cjs --output=bundle.js -- src/main.js rollup --format=cjs --output=bundle.js -- src/main.js
rollup -f iife --globals jquery:jQuery,angular:ng \ rollup -f iife --globals jquery:jQuery,angular:ng \

1
bin/rollup

@ -9,6 +9,7 @@ command = minimist( process.argv.slice( 2 ), {
strict: 'useStrict', strict: 'useStrict',
// Short options // Short options
c: 'config',
d: 'indent', d: 'indent',
e: 'external', e: 'external',
f: 'format', f: 'format',

101
bin/runRollup.js

@ -1,27 +1,72 @@
require( 'source-map-support' ).install(); require( 'source-map-support' ).install();
var path = require( 'path' );
var handleError = require( './handleError' ); var handleError = require( './handleError' );
var rollup = require( '../' ); var rollup = require( '../' );
module.exports = function ( options ) { module.exports = function ( command ) {
if ( options._.length > 1 ) { if ( command._.length > 1 ) {
handleError({ code: 'ONE_AT_A_TIME' }); handleError({ code: 'ONE_AT_A_TIME' });
} }
if ( options._.length === 1 ) { if ( command._.length === 1 ) {
if ( options.input ) { if ( command.input ) {
handleError({ code: 'DUPLICATE_IMPORT_OPTIONS' }); handleError({ code: 'DUPLICATE_IMPORT_OPTIONS' });
} }
options.input = options._[0]; command.input = command._[0];
} }
var external = options.external ? options.external.split( ',' ) : []; var config = command.config === true ? 'rollup.config.js' : command.config;
if ( config ) {
config = path.resolve( config );
rollup.rollup({
entry: config,
onwarn: function () {}
}).then( function ( bundle ) {
var code = bundle.generate({
format: 'cjs'
}).code;
// temporarily override require
var defaultLoader = require.extensions[ '.js' ];
require.extensions[ '.js' ] = function ( m, filename ) {
if ( filename === config ) {
m._compile( code, filename );
} else {
defaultLoader( m, filename );
}
};
const options = require( path.resolve( config ) );
execute( options, command );
require.extensions[ '.js' ] = defaultLoader;
});
} else {
execute( {}, command );
}
};
var equivalents = {
input: 'entry',
output: 'dest',
name: 'moduleName',
format: 'format',
globals: 'globals',
id: 'moduleId',
sourcemap: 'sourceMap'
};
function execute ( options, command ) {
var external = command.external ? command.external.split( ',' ) : [];
if ( options.globals ) { if ( command.globals ) {
var globals = Object.create( null ); var globals = Object.create( null );
options.globals.split( ',' ).forEach(function ( str ) { command.globals.split( ',' ).forEach(function ( str ) {
var names = str.split( ':' ); var names = str.split( ':' );
globals[ names[0] ] = names[1]; globals[ names[0] ] = names[1];
@ -31,51 +76,45 @@ module.exports = function ( options ) {
} }
}); });
options.globals = globals; command.globals = globals;
} }
options.external = external; options.external = external;
options.indent = command.indent !== false;
Object.keys( equivalents ).forEach( function ( cliOption ) {
if ( command[ cliOption ] ) {
options[ equivalents[ cliOption ] ] = command[ cliOption ];
}
});
try { try {
bundle( options ).catch( handleError ); bundle( options ).catch( handleError );
} catch ( err ) { } catch ( err ) {
handleError( err ); handleError( err );
} }
}; }
function bundle ( options, method ) { function bundle ( options ) {
if ( !options.input ) { if ( !options.entry ) {
handleError({ code: 'MISSING_INPUT_OPTION' }); handleError({ code: 'MISSING_INPUT_OPTION' });
} }
return rollup.rollup({ return rollup.rollup( options ).then( function ( bundle ) {
entry: options.input, if ( options.dest ) {
external: options.external return bundle.write( options );
}).then( function ( bundle ) {
var generateOptions = {
dest: options.output,
format: options.format,
globals: options.globals,
moduleId: options.id,
moduleName: options.name,
sourceMap: options.sourcemap,
indent: options.indent !== false
};
if ( options.output ) {
return bundle.write( generateOptions );
} }
if ( options.sourcemap && options.sourcemap !== 'inline' ) { if ( options.sourceMap && options.sourceMap !== 'inline' ) {
handleError({ code: 'MISSING_OUTPUT_OPTION' }); handleError({ code: 'MISSING_OUTPUT_OPTION' });
} }
var result = bundle.generate( generateOptions ); var result = bundle.generate( options );
var code = result.code, var code = result.code,
map = result.map; map = result.map;
if ( options.sourcemap === 'inline' ) { if ( options.sourceMap === 'inline' ) {
code += '\n//# sourceMappingURL=' + map.toUrl(); code += '\n//# sourceMappingURL=' + map.toUrl();
} }

1
package.json

@ -55,6 +55,7 @@
"magic-string": "^0.8.0", "magic-string": "^0.8.0",
"mocha": "^2.3.3", "mocha": "^2.3.3",
"remap-istanbul": "^0.3.1", "remap-istanbul": "^0.3.1",
"rollup-plugin-replace": "^1.0.1",
"sander": "^0.4.0", "sander": "^0.4.0",
"source-map": "^0.5.1" "source-map": "^0.5.1"
}, },

5
test/cli/config-es6/_config.js

@ -0,0 +1,5 @@
module.exports = {
description: 'uses ES6 module config file',
command: 'rollup --config rollup.config.js',
execute: true
};

1
test/cli/config-es6/main.js

@ -0,0 +1 @@
assert.equal( ANSWER, 42 );

9
test/cli/config-es6/rollup.config.js

@ -0,0 +1,9 @@
import replace from 'rollup-plugin-replace';
export default {
entry: 'main.js',
format: 'cjs',
plugins: [
replace({ 'ANSWER': 42 })
]
};

5
test/cli/config-override/_config.js

@ -0,0 +1,5 @@
module.exports = {
description: 'overrides config file with command line arguments',
command: 'rollup -c -i main.js -f cjs',
execute: true
};

1
test/cli/config-override/main.js

@ -0,0 +1 @@
assert.equal( ANSWER, 42 );

9
test/cli/config-override/rollup.config.js

@ -0,0 +1,9 @@
import replace from 'rollup-plugin-replace';
export default {
entry: 'nope.js',
format: 'amd',
plugins: [
replace({ 'ANSWER': 42 })
]
};

5
test/cli/config-true/_config.js

@ -0,0 +1,5 @@
module.exports = {
description: 'defaults to rollup.config.js',
command: 'rollup -c',
execute: true
};

1
test/cli/config-true/main.js

@ -0,0 +1 @@
assert.equal( ANSWER, 42 );

9
test/cli/config-true/rollup.config.js

@ -0,0 +1,9 @@
import replace from 'rollup-plugin-replace';
export default {
entry: 'main.js',
format: 'cjs',
plugins: [
replace({ 'ANSWER': 42 })
]
};

5
test/cli/config/_config.js

@ -0,0 +1,5 @@
module.exports = {
description: 'uses config file',
command: 'rollup --config rollup.config.js',
execute: true
};

11
test/cli/config/_expected.js

@ -0,0 +1,11 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
global.myBundle = factory();
}(this, function () { 'use strict';
var main = 42;
return main;
}));

1
test/cli/config/main.js

@ -0,0 +1 @@
assert.equal( ANSWER, 42 );

9
test/cli/config/rollup.config.js

@ -0,0 +1,9 @@
var replace = require( 'rollup-plugin-replace' );
module.exports = {
entry: 'main.js',
format: 'cjs',
plugins: [
replace({ 'ANSWER': 42 })
]
};
Loading…
Cancel
Save