Skip to content

Commit

Permalink
Merge pull request #1566 from rollup/gh-1479
Browse files Browse the repository at this point in the history
normalise options
  • Loading branch information
Rich-Harris committed Aug 20, 2017
2 parents df48042 + 0eb4fc1 commit 6fc2088
Show file tree
Hide file tree
Showing 121 changed files with 798 additions and 514 deletions.
49 changes: 25 additions & 24 deletions bin/src/help.md
Expand Up @@ -5,30 +5,31 @@ Usage: rollup [options] <entry file>

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)
-w, --watch Watch files in bundle and rebuild on changes
-i, --input Input (alternative to <entry file>)
-o, --output <output> Output (if absent, prints to stdout)
-f, --format [es] Type of output (amd, cjs, es, iife, umd)
-e, --external Comma-separate list of module IDs to exclude
-g, --globals Comma-separate list of `module ID:Global` pairs
Any module IDs defined here are added to external
-n, --name Name for UMD export
-u, --id ID for AMD module (default is anonymous)
-m, --sourcemap Generate sourcemap (`-m inline` for inline map)
--no-strict Don't emit a `"use strict";` in the generated modules.
--no-indent Don't indent result
--environment <values> Settings passed to config file (see example)
--no-conflict Generate a noConflict method for UMD globals
--silent Don't print warnings
--intro Content to insert at top of bundle (inside wrapper)
--outro Content to insert at end of bundle (inside wrapper)
--banner Content to insert at top of bundle (outside wrapper)
--footer Content to insert at end of bundle (outside wrapper)
--interop Include interop block (true by default)
-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)
-w, --watch Watch files in bundle and rebuild on changes
-i, --input Input (alternative to <entry file>)
-o, --output.file <output> Output (if absent, prints to stdout)
-f, --output.format [es] Type of output (amd, cjs, es, iife, umd)
-e, --external Comma-separate list of module IDs to exclude
-g, --globals Comma-separate list of `module ID:Global` pairs
Any module IDs defined here are added to external
-n, --name Name for UMD export
-m, --sourcemap Generate sourcemap (`-m inline` for inline map)
--amd.id ID for AMD module (default is anonymous)
--amd.define Function to use in place of `define`
--no-strict Don't emit a `"use strict";` in the generated modules.
--no-indent Don't indent result
--environment <values> Settings passed to config file (see example)
--no-conflict Generate a noConflict method for UMD globals
--silent Don't print warnings
--intro Content to insert at top of bundle (inside wrapper)
--outro Content to insert at end of bundle (inside wrapper)
--banner Content to insert at top of bundle (outside wrapper)
--footer Content to insert at end of bundle (outside wrapper)
--interop Include interop block (true by default)

Examples:

Expand Down
10 changes: 8 additions & 2 deletions bin/src/index.js
Expand Up @@ -12,14 +12,14 @@ const command = minimist( process.argv.slice( 2 ), {
c: 'config',
d: 'indent',
e: 'external',
f: 'format',
f: 'output.format',
g: 'globals',
h: 'help',
i: 'input',
l: 'legacy',
m: 'sourcemap',
n: 'name',
o: 'output',
o: 'output.file',
u: 'id',
v: 'version',
w: 'watch'
Expand All @@ -35,5 +35,11 @@ else if ( command.version ) {
}

else {
try {
require('source-map-support').install();
} catch (err) {
// do nothing
}

run( command );
}
7 changes: 7 additions & 0 deletions bin/src/run/batchWarnings.js
Expand Up @@ -74,6 +74,13 @@ export default function batchWarnings () {
}

const immediateHandlers = {
DEPRECATED_OPTIONS: warning => {
title( `Some options have been renamed` );
warning.deprecations.forEach(option => {
stderr( `${chalk.bold(option.old)} is now ${option.new}` );
});
},

MISSING_NODE_BUILTINS: warning => {
title( `Missing shims for Node.js built-ins` );

Expand Down
35 changes: 12 additions & 23 deletions bin/src/run/build.js
Expand Up @@ -6,51 +6,40 @@ import relativeId from '../../../src/utils/relativeId.js';
import { mapSequence } from '../../../src/utils/promise.js';
import SOURCEMAPPING_URL from '../sourceMappingUrl.js';

export default function build ( options, warnings, silent ) {
const useStdout = !options.targets && !options.dest;
const targets = options.targets ? options.targets : [{ dest: options.dest, format: options.format }];
export default function build ( inputOptions, outputOptions, warnings, silent ) {
const useStdout = outputOptions.length === 1 && !outputOptions[0].file;

const start = Date.now();
const dests = useStdout ? [ 'stdout' ] : targets.map( t => relativeId( t.dest ) );
if ( !silent ) stderr( chalk.cyan( `\n${chalk.bold( options.entry )}${chalk.bold( dests.join( ', ' ) )}...` ) );
const files = useStdout ? [ 'stdout' ] : outputOptions.map( t => relativeId( t.file ) );
if ( !silent ) stderr( chalk.cyan( `\n${chalk.bold( inputOptions.input )}${chalk.bold( files.join( ', ' ) )}...` ) );

return rollup.rollup( options )
return rollup.rollup( inputOptions )
.then( bundle => {
if ( useStdout ) {
if ( options.sourceMap && options.sourceMap !== 'inline' ) {
const output = outputOptions[0];
if ( output.sourcemap && output.sourcemap !== 'inline' ) {
handleError({
code: 'MISSING_OUTPUT_OPTION',
message: 'You must specify an --output (-o) option when creating a file with a sourcemap'
});
}

return bundle.generate(options).then( ({ code, map }) => {
if ( options.sourceMap === 'inline' ) {
return bundle.generate(output).then( ({ code, map }) => {
if ( output.sourcemap === 'inline' ) {
code += `\n//# ${SOURCEMAPPING_URL}=${map.toUrl()}\n`;
}

process.stdout.write( code );
});
}

return mapSequence( targets, target => {
return bundle.write( assign( clone( options ), target ) );
return mapSequence( outputOptions, output => {
return bundle.write( output );
});
})
.then( () => {
warnings.flush();
if ( !silent ) stderr( chalk.green( `created ${chalk.bold( dests.join( ', ' ) )} in ${chalk.bold(ms( Date.now() - start))}` ) );
if ( !silent ) stderr( chalk.green( `created ${chalk.bold( files.join( ', ' ) )} in ${chalk.bold(ms( Date.now() - start))}` ) );
})
.catch( handleError );
}

function clone ( object ) {
return assign( {}, object );
}

function assign ( target, source ) {
Object.keys( source ).forEach( key => {
target[ key ] = source[ key ];
});
return target;
}
18 changes: 13 additions & 5 deletions bin/src/run/index.js
Expand Up @@ -79,20 +79,28 @@ function execute ( configFile, configs, command ) {
watch( configFile, configs, command, command.silent );
} else {
return sequence( configs, config => {
const options = mergeOptions( config, command );
const { inputOptions, outputOptions, deprecations } = mergeOptions( config, command );

const warnings = batchWarnings();

const onwarn = options.onwarn;
const onwarn = inputOptions.onwarn;
if ( onwarn ) {
options.onwarn = warning => {
inputOptions.onwarn = warning => {
onwarn( warning, warnings.add );
};
} else {
options.onwarn = warnings.add;
inputOptions.onwarn = warnings.add;
}

return build( options, warnings, command.silent );
if (deprecations.length) {
inputOptions.onwarn({
code: 'DEPRECATED_OPTIONS',
message: `The following options have been renamed — please update your config: ${deprecations.map(option => `${option.old} -> ${option.new}`).join(', ')}`,
deprecations
});
}

return build( inputOptions, outputOptions, warnings, command.silent );
});
}
}
2 changes: 1 addition & 1 deletion bin/src/run/loadConfigFile.js
Expand Up @@ -9,7 +9,7 @@ export default function loadConfigFile (configFile, silent) {
const warnings = batchWarnings();

return rollup.rollup({
entry: configFile,
input: configFile,
external: id => {
return (id[0] !== '.' && !path.isAbsolute(id)) || id.slice(-5,id.length) === '.json';
},
Expand Down
130 changes: 87 additions & 43 deletions bin/src/run/mergeOptions.js
@@ -1,31 +1,30 @@
import batchWarnings from './batchWarnings.js';

const equivalents = {
useStrict: 'useStrict',
banner: 'banner',
footer: 'footer',
format: 'format',
globals: 'globals',
id: 'moduleId',
indent: 'indent',
interop: 'interop',
input: 'entry',
intro: 'intro',
legacy: 'legacy',
name: 'moduleName',
output: 'dest',
outro: 'outro',
sourcemap: 'sourceMap',
treeshake: 'treeshake'
};
import ensureArray from '../../../src/utils/ensureArray.js';
import deprecateOptions from '../../../src/utils/deprecateOptions.js';

export default function mergeOptions ( config, command ) {
const options = Object.assign( {}, config );
// deprecations... TODO
const deprecations = deprecate( config, command );

let external;
function getOption(name) {
return command[name] !== undefined ? command[name] : config[name];
}

const inputOptions = {
input: getOption('input'),
legacy: getOption('legacy'),
treeshake: getOption('treeshake'),
acorn: config.acorn,
context: config.context,
moduleContext: config.moduleContext,
plugins: config.plugins,
onwarn: config.onwarn
};

// legacy, to ensure e.g. commonjs plugin still works
inputOptions.entry = inputOptions.input;

const commandExternal = ( command.external || '' ).split( ',' );
const optionsExternal = options.external;
const configExternal = config.external;

if ( command.globals ) {
const globals = Object.create( null );
Expand All @@ -43,34 +42,79 @@ export default function mergeOptions ( config, command ) {
command.globals = globals;
}

if ( typeof optionsExternal === 'function' ) {
external = id => {
return optionsExternal( id ) || ~commandExternal.indexOf( id );
if ( typeof configExternal === 'function' ) {
inputOptions.external = id => {
return configExternal( id ) || ~commandExternal.indexOf( id );
};
} else {
external = ( optionsExternal || [] ).concat( commandExternal );
}

if (typeof command.extend !== 'undefined') {
options.extend = command.extend;
inputOptions.external = ( configExternal || [] ).concat( commandExternal );
}

if (command.silent) {
options.onwarn = () => {};
inputOptions.onwarn = () => {};
}

options.external = external;
const baseOutputOptions = {
extend: command.extend !== undefined ? command.extend : config.extend,
amd: Object.assign({}, config.amd, command.amd),

// Use any options passed through the CLI as overrides.
Object.keys( equivalents ).forEach( cliOption => {
if ( command.hasOwnProperty( cliOption ) ) {
options[ equivalents[ cliOption ] ] = command[ cliOption ];
}
});
banner: getOption('banner'),
footer: getOption('footer'),
intro: getOption('intro'),
outro: getOption('outro'),
sourcemap: getOption('sourcemap'),
name: getOption('name'),
globals: getOption('globals'),
interop: getOption('interop'),
legacy: getOption('legacy'),
indent: getOption('indent'),
strict: getOption('strict'),
noConflict: getOption('noConflict')
};

const targets = options.dest ? [{ dest: options.dest, format: options.format }] : options.targets;
options.targets = targets;
delete options.dest;
const outputOptions = (
(command.output || config.output) ?
ensureArray(command.output || config.output) :
[{
file: command.output ? command.output.file : null,
format: command.output ? command.output.format : null
}]
).map(output => {
return Object.assign({}, baseOutputOptions, output);
});

return options;
return { inputOptions, outputOptions, deprecations };
}

function deprecate( config, command ) {
const deprecations = [];

// CLI
if ( command.id ) {
deprecations.push({
old: '-u/--id',
new: '--amd.id'
});
(command.amd || (command.amd = {})).id = command.id;
}

if ( typeof command.output === 'string' ) {
deprecations.push({
old: '--output',
new: '--output.file'
});
command.output = { file: command.output };
}

if ( command.format ) {
deprecations.push({
old: '--format',
new: '--output.format'
});
(command.output || (command.output = {})).format = command.format;
}

// config file
deprecations.push(...deprecateOptions(config));
return deprecations;
}
11 changes: 6 additions & 5 deletions bin/src/run/watch.js
Expand Up @@ -15,17 +15,18 @@ export default function watch(configFile, configs, command, silent) {

configs = configs.map(options => {
const merged = mergeOptions(options, command);

const onwarn = merged.onwarn;
const onwarn = merged.inputOptions.onwarn;
if ( onwarn ) {
merged.onwarn = warning => {
merged.inputOptions.onwarn = warning => {
onwarn( warning, warnings.add );
};
} else {
merged.onwarn = warnings.add;
merged.inputOptions.onwarn = warnings.add;
}

return merged;
return Object.assign({}, merged.inputOptions, {
output: merged.outputOptions
});
});

let watcher;
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6fc2088

Please sign in to comment.