Browse Source

support rollup --config node:whatever (#736)

semi-dynamic-namespace-imports
Rich Harris 9 years ago
parent
commit
e0e4dc1cd2
  1. 4
      bin/src/handleError.js
  2. 19
      bin/src/runRollup.js
  3. 5
      test/cli/node-config-auto-prefix/_config.js
  4. 11
      test/cli/node-config-auto-prefix/_expected.js
  5. 1
      test/cli/node-config-auto-prefix/main.js
  6. 9
      test/cli/node-config-auto-prefix/node_modules/rollup-config-foo/lib/config.js
  7. 3
      test/cli/node-config-auto-prefix/node_modules/rollup-config-foo/package.json
  8. 5
      test/cli/node-config/_config.js
  9. 11
      test/cli/node-config/_expected.js
  10. 1
      test/cli/node-config/main.js
  11. 9
      test/cli/node-config/node_modules/foo/lib/config.js
  12. 3
      test/cli/node-config/node_modules/foo/package.json

4
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' ) );
},

19
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,

5
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
};

11
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;
}));

1
test/cli/node-config-auto-prefix/main.js

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

9
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 })
]
};

3
test/cli/node-config-auto-prefix/node_modules/rollup-config-foo/package.json

@ -0,0 +1,3 @@
{
"main": "lib/config.js"
}

5
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
};

11
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;
}));

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

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

9
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 })
]
};

3
test/cli/node-config/node_modules/foo/package.json

@ -0,0 +1,3 @@
{
"main": "lib/config.js"
}
Loading…
Cancel
Save