diff --git a/bin/help.md b/bin/help.md index cb68a80..5ec5719 100644 --- a/bin/help.md +++ b/bin/help.md @@ -7,6 +7,8 @@ Basic options: -v, --version Show version number -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 ) -o, --output Output (if absent, prints to stdout) -f, --format [es6] Type of output (amd, cjs, es6, iife, umd) @@ -21,6 +23,8 @@ Basic options: Examples: +rollup -c + rollup --format=cjs --output=bundle.js -- src/main.js rollup -f iife --globals jquery:jQuery,angular:ng \ diff --git a/bin/rollup b/bin/rollup index 8132e92..200c4bb 100755 --- a/bin/rollup +++ b/bin/rollup @@ -9,6 +9,7 @@ command = minimist( process.argv.slice( 2 ), { strict: 'useStrict', // Short options + c: 'config', d: 'indent', e: 'external', f: 'format', diff --git a/bin/runRollup.js b/bin/runRollup.js index 1717c0c..4870180 100644 --- a/bin/runRollup.js +++ b/bin/runRollup.js @@ -1,27 +1,72 @@ require( 'source-map-support' ).install(); +var path = require( 'path' ); var handleError = require( './handleError' ); var rollup = require( '../' ); -module.exports = function ( options ) { - if ( options._.length > 1 ) { +module.exports = function ( command ) { + if ( command._.length > 1 ) { handleError({ code: 'ONE_AT_A_TIME' }); } - if ( options._.length === 1 ) { - if ( options.input ) { + if ( command._.length === 1 ) { + if ( command.input ) { 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 ); - options.globals.split( ',' ).forEach(function ( str ) { + command.globals.split( ',' ).forEach(function ( str ) { var names = str.split( ':' ); globals[ names[0] ] = names[1]; @@ -31,51 +76,45 @@ module.exports = function ( options ) { } }); - options.globals = globals; + command.globals = globals; } options.external = external; + options.indent = command.indent !== false; + + Object.keys( equivalents ).forEach( function ( cliOption ) { + if ( command[ cliOption ] ) { + options[ equivalents[ cliOption ] ] = command[ cliOption ]; + } + }); try { bundle( options ).catch( handleError ); } catch ( err ) { handleError( err ); } -}; +} -function bundle ( options, method ) { - if ( !options.input ) { +function bundle ( options ) { + if ( !options.entry ) { handleError({ code: 'MISSING_INPUT_OPTION' }); } - return rollup.rollup({ - entry: options.input, - external: options.external - }).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 ); + return rollup.rollup( options ).then( function ( bundle ) { + if ( options.dest ) { + return bundle.write( options ); } - if ( options.sourcemap && options.sourcemap !== 'inline' ) { + if ( options.sourceMap && options.sourceMap !== 'inline' ) { handleError({ code: 'MISSING_OUTPUT_OPTION' }); } - var result = bundle.generate( generateOptions ); + var result = bundle.generate( options ); var code = result.code, map = result.map; - if ( options.sourcemap === 'inline' ) { + if ( options.sourceMap === 'inline' ) { code += '\n//# sourceMappingURL=' + map.toUrl(); } diff --git a/package.json b/package.json index 60fce40..b402a34 100644 --- a/package.json +++ b/package.json @@ -55,6 +55,7 @@ "magic-string": "^0.8.0", "mocha": "^2.3.3", "remap-istanbul": "^0.3.1", + "rollup-plugin-replace": "^1.0.1", "sander": "^0.4.0", "source-map": "^0.5.1" }, diff --git a/test/cli/config-es6/_config.js b/test/cli/config-es6/_config.js new file mode 100644 index 0000000..0db10ef --- /dev/null +++ b/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 +}; diff --git a/test/cli/config-es6/main.js b/test/cli/config-es6/main.js new file mode 100644 index 0000000..df16c1b --- /dev/null +++ b/test/cli/config-es6/main.js @@ -0,0 +1 @@ +assert.equal( ANSWER, 42 ); diff --git a/test/cli/config-es6/rollup.config.js b/test/cli/config-es6/rollup.config.js new file mode 100644 index 0000000..0cafd0c --- /dev/null +++ b/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 }) + ] +}; diff --git a/test/cli/config-override/_config.js b/test/cli/config-override/_config.js new file mode 100644 index 0000000..49fc481 --- /dev/null +++ b/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 +}; diff --git a/test/cli/config-override/main.js b/test/cli/config-override/main.js new file mode 100644 index 0000000..df16c1b --- /dev/null +++ b/test/cli/config-override/main.js @@ -0,0 +1 @@ +assert.equal( ANSWER, 42 ); diff --git a/test/cli/config-override/rollup.config.js b/test/cli/config-override/rollup.config.js new file mode 100644 index 0000000..0274345 --- /dev/null +++ b/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 }) + ] +}; diff --git a/test/cli/config-true/_config.js b/test/cli/config-true/_config.js new file mode 100644 index 0000000..95252f5 --- /dev/null +++ b/test/cli/config-true/_config.js @@ -0,0 +1,5 @@ +module.exports = { + description: 'defaults to rollup.config.js', + command: 'rollup -c', + execute: true +}; diff --git a/test/cli/config-true/main.js b/test/cli/config-true/main.js new file mode 100644 index 0000000..df16c1b --- /dev/null +++ b/test/cli/config-true/main.js @@ -0,0 +1 @@ +assert.equal( ANSWER, 42 ); diff --git a/test/cli/config-true/rollup.config.js b/test/cli/config-true/rollup.config.js new file mode 100644 index 0000000..0cafd0c --- /dev/null +++ b/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 }) + ] +}; diff --git a/test/cli/config/_config.js b/test/cli/config/_config.js new file mode 100644 index 0000000..4dff8e5 --- /dev/null +++ b/test/cli/config/_config.js @@ -0,0 +1,5 @@ +module.exports = { + description: 'uses config file', + command: 'rollup --config rollup.config.js', + execute: true +}; diff --git a/test/cli/config/_expected.js b/test/cli/config/_expected.js new file mode 100644 index 0000000..b5aa08a --- /dev/null +++ b/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; + +})); diff --git a/test/cli/config/main.js b/test/cli/config/main.js new file mode 100644 index 0000000..df16c1b --- /dev/null +++ b/test/cli/config/main.js @@ -0,0 +1 @@ +assert.equal( ANSWER, 42 ); diff --git a/test/cli/config/rollup.config.js b/test/cli/config/rollup.config.js new file mode 100644 index 0000000..3cc995e --- /dev/null +++ b/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 }) + ] +};