From e0e4dc1cd2f7abd0564432370189fe606e67eda3 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Tue, 21 Jun 2016 18:16:59 -0400 Subject: [PATCH] support rollup --config node:whatever (#736) --- bin/src/handleError.js | 4 ++++ bin/src/runRollup.js | 21 +++++++++++++++++-- test/cli/node-config-auto-prefix/_config.js | 5 +++++ test/cli/node-config-auto-prefix/_expected.js | 11 ++++++++++ test/cli/node-config-auto-prefix/main.js | 1 + .../rollup-config-foo/lib/config.js | 9 ++++++++ .../rollup-config-foo/package.json | 3 +++ test/cli/node-config/_config.js | 5 +++++ test/cli/node-config/_expected.js | 11 ++++++++++ test/cli/node-config/main.js | 1 + .../node_modules/foo/lib/config.js | 9 ++++++++ .../node-config/node_modules/foo/package.json | 3 +++ 12 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 test/cli/node-config-auto-prefix/_config.js create mode 100644 test/cli/node-config-auto-prefix/_expected.js create mode 100644 test/cli/node-config-auto-prefix/main.js create mode 100644 test/cli/node-config-auto-prefix/node_modules/rollup-config-foo/lib/config.js create mode 100644 test/cli/node-config-auto-prefix/node_modules/rollup-config-foo/package.json create mode 100644 test/cli/node-config/_config.js create mode 100644 test/cli/node-config/_expected.js create mode 100644 test/cli/node-config/main.js create mode 100644 test/cli/node-config/node_modules/foo/lib/config.js create mode 100644 test/cli/node-config/node_modules/foo/package.json diff --git a/bin/src/handleError.js b/bin/src/handleError.js index 243592c..80892fd 100644 --- a/bin/src/handleError.js +++ b/bin/src/handleError.js @@ -9,6 +9,10 @@ const handlers = { stderr( chalk.red( 'Config file must export an options object. See https://github.com/rollup/rollup/wiki/Command-Line-Interface#using-a-config-file' ) ); }, + MISSING_EXTERNAL_CONFIG: err => { + stderr( chalk.red( `Could not resolve config file ${err.config}` ) ); + }, + MISSING_INPUT_OPTION: () => { stderr( chalk.red( 'You must specify an --input (-i) option' ) ); }, diff --git a/bin/src/runRollup.js b/bin/src/runRollup.js index ff89f41..ce82d80 100644 --- a/bin/src/runRollup.js +++ b/bin/src/runRollup.js @@ -38,7 +38,24 @@ export default function runRollup ( command ) { let config = command.config === true ? 'rollup.config.js' : command.config; if ( config ) { - config = resolve( config ); + if ( config.slice( 0, 5 ) === 'node:' ) { + const pkgName = config.slice( 5 ); + try { + config = relative.resolve( pkgName, process.cwd() ); + } catch ( err ) { + try { + config = relative.resolve( `rollup-config-${pkgName}`, process.cwd() ); + } catch ( err ) { + if ( err.code === 'MODULE_NOT_FOUND' ) { + handleError({ code: 'MISSING_EXTERNAL_CONFIG', config }); + } + + throw err; + } + } + } else { + config = resolve( config ); + } rollup.rollup({ entry: config, @@ -225,4 +242,4 @@ function bundle ( options ) { process.stdout.write( code ); }); -} \ No newline at end of file +} diff --git a/test/cli/node-config-auto-prefix/_config.js b/test/cli/node-config-auto-prefix/_config.js new file mode 100644 index 0000000..88b1368 --- /dev/null +++ b/test/cli/node-config-auto-prefix/_config.js @@ -0,0 +1,5 @@ +module.exports = { + description: 'uses config file installed from npm', + command: 'rollup --config node:foo', + execute: true +}; diff --git a/test/cli/node-config-auto-prefix/_expected.js b/test/cli/node-config-auto-prefix/_expected.js new file mode 100644 index 0000000..b5aa08a --- /dev/null +++ b/test/cli/node-config-auto-prefix/_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/node-config-auto-prefix/main.js b/test/cli/node-config-auto-prefix/main.js new file mode 100644 index 0000000..df16c1b --- /dev/null +++ b/test/cli/node-config-auto-prefix/main.js @@ -0,0 +1 @@ +assert.equal( ANSWER, 42 ); diff --git a/test/cli/node-config-auto-prefix/node_modules/rollup-config-foo/lib/config.js b/test/cli/node-config-auto-prefix/node_modules/rollup-config-foo/lib/config.js new file mode 100644 index 0000000..3cc995e --- /dev/null +++ b/test/cli/node-config-auto-prefix/node_modules/rollup-config-foo/lib/config.js @@ -0,0 +1,9 @@ +var replace = require( 'rollup-plugin-replace' ); + +module.exports = { + entry: 'main.js', + format: 'cjs', + plugins: [ + replace({ 'ANSWER': 42 }) + ] +}; diff --git a/test/cli/node-config-auto-prefix/node_modules/rollup-config-foo/package.json b/test/cli/node-config-auto-prefix/node_modules/rollup-config-foo/package.json new file mode 100644 index 0000000..fbd4901 --- /dev/null +++ b/test/cli/node-config-auto-prefix/node_modules/rollup-config-foo/package.json @@ -0,0 +1,3 @@ +{ + "main": "lib/config.js" +} diff --git a/test/cli/node-config/_config.js b/test/cli/node-config/_config.js new file mode 100644 index 0000000..88b1368 --- /dev/null +++ b/test/cli/node-config/_config.js @@ -0,0 +1,5 @@ +module.exports = { + description: 'uses config file installed from npm', + command: 'rollup --config node:foo', + execute: true +}; diff --git a/test/cli/node-config/_expected.js b/test/cli/node-config/_expected.js new file mode 100644 index 0000000..b5aa08a --- /dev/null +++ b/test/cli/node-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/node-config/main.js b/test/cli/node-config/main.js new file mode 100644 index 0000000..df16c1b --- /dev/null +++ b/test/cli/node-config/main.js @@ -0,0 +1 @@ +assert.equal( ANSWER, 42 ); diff --git a/test/cli/node-config/node_modules/foo/lib/config.js b/test/cli/node-config/node_modules/foo/lib/config.js new file mode 100644 index 0000000..3cc995e --- /dev/null +++ b/test/cli/node-config/node_modules/foo/lib/config.js @@ -0,0 +1,9 @@ +var replace = require( 'rollup-plugin-replace' ); + +module.exports = { + entry: 'main.js', + format: 'cjs', + plugins: [ + replace({ 'ANSWER': 42 }) + ] +}; diff --git a/test/cli/node-config/node_modules/foo/package.json b/test/cli/node-config/node_modules/foo/package.json new file mode 100644 index 0000000..fbd4901 --- /dev/null +++ b/test/cli/node-config/node_modules/foo/package.json @@ -0,0 +1,3 @@ +{ + "main": "lib/config.js" +}