Skip to content

Commit

Permalink
normalise options
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Aug 19, 2017
1 parent df48042 commit 31de491
Show file tree
Hide file tree
Showing 110 changed files with 338 additions and 274 deletions.
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.options.forEach(({ key, replacement }) => {
stderr( `${chalk.bold(key)} is now ${replacement}` );
});
},

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

Expand Down
12 changes: 6 additions & 6 deletions bin/src/run/build.js
Expand Up @@ -7,25 +7,25 @@ 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 }];
const useStdout = !options.targets && !options.output;
const targets = options.targets ? options.targets : [{ output: options.output, format: options.format }];

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 dests = useStdout ? [ 'stdout' ] : targets.map( t => relativeId( t.output ) );
if ( !silent ) stderr( chalk.cyan( `\n${chalk.bold( options.input )}${chalk.bold( dests.join( ', ' ) )}...` ) );

return rollup.rollup( options )
.then( bundle => {
if ( useStdout ) {
if ( options.sourceMap && options.sourceMap !== 'inline' ) {
if ( options.sourcemap && options.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' ) {
if ( options.sourcemap === 'inline' ) {
code += `\n//# ${SOURCEMAPPING_URL}=${map.toUrl()}\n`;
}

Expand Down
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
16 changes: 7 additions & 9 deletions bin/src/run/mergeOptions.js
@@ -1,21 +1,19 @@
import batchWarnings from './batchWarnings.js';

const equivalents = {
useStrict: 'useStrict',
strict: 'strict',
banner: 'banner',
footer: 'footer',
format: 'format',
globals: 'globals',
id: 'moduleId',
indent: 'indent',
interop: 'interop',
input: 'entry',
input: 'input',
intro: 'intro',
legacy: 'legacy',
name: 'moduleName',
output: 'dest',
name: 'name',
output: 'output',
outro: 'outro',
sourcemap: 'sourceMap',
sourcemap: 'sourcemap',
treeshake: 'treeshake'
};

Expand Down Expand Up @@ -68,9 +66,9 @@ export default function mergeOptions ( config, command ) {
}
});

const targets = options.dest ? [{ dest: options.dest, format: options.format }] : options.targets;
const targets = options.output ? [{ output: options.output, format: options.format }] : options.targets;
options.targets = targets;
delete options.dest;
delete options.output;

return options;
}
20 changes: 10 additions & 10 deletions src/Bundle.js
Expand Up @@ -37,11 +37,11 @@ export default class Bundle {
return acc;
}, options );

if ( !options.entry ) {
throw new Error( 'You must supply options.entry to rollup' );
if ( !options.input ) {
throw new Error( 'You must supply options.input to rollup' );
}

this.entry = options.entry;
this.entry = options.input;
this.entryId = null;
this.entryModule = null;

Expand Down Expand Up @@ -338,15 +338,15 @@ export default class Bundle {
return transform( this, source, id, this.plugins );
} )
.then( source => {
const { code, originalCode, originalSourceMap, ast, sourceMapChain, resolvedIds } = source;
const { code, originalCode, originalSourcemap, ast, sourcemapChain, resolvedIds } = source;

const module = new Module( {
id,
code,
originalCode,
originalSourceMap,
originalSourcemap,
ast,
sourceMapChain,
sourcemapChain,
resolvedIds,
bundle: this
} );
Expand Down Expand Up @@ -545,10 +545,10 @@ export default class Bundle {
const bundleSourcemapChain = [];

return transformBundle( prevCode, this.plugins, bundleSourcemapChain, options ).then( code => {
if ( options.sourceMap ) {
timeStart( 'sourceMap' );
if ( options.sourcemap ) {
timeStart( 'sourcemap' );

let file = options.sourceMapFile || options.dest;
let file = options.sourcemapFile || options.output;
if ( file ) file = resolve( typeof process !== 'undefined' ? process.cwd() : '', file );

if ( this.hasLoaders || find( this.plugins, plugin => plugin.transform || plugin.transformBundle ) ) {
Expand All @@ -563,7 +563,7 @@ export default class Bundle {

map.sources = map.sources.map( normalize );

timeEnd( 'sourceMap' );
timeEnd( 'sourcemap' );
}

if ( code[ code.length - 1 ] !== '\n' ) code += '\n';
Expand Down
10 changes: 5 additions & 5 deletions src/Module.js
Expand Up @@ -32,13 +32,13 @@ function tryParse ( module, acornOptions ) {
}

export default class Module {
constructor ( { id, code, originalCode, originalSourceMap, ast, sourceMapChain, resolvedIds, resolvedExternalIds, bundle } ) {
constructor ( { id, code, originalCode, originalSourcemap, ast, sourcemapChain, resolvedIds, resolvedExternalIds, bundle } ) {
this.code = code;
this.id = id;
this.bundle = bundle;
this.originalCode = originalCode;
this.originalSourceMap = originalSourceMap;
this.sourceMapChain = sourceMapChain;
this.originalSourcemap = originalSourcemap;
this.sourcemapChain = sourcemapChain;

this.comments = [];

Expand Down Expand Up @@ -363,9 +363,9 @@ export default class Module {
dependencies: this.dependencies.map( module => module.id ),
code: this.code,
originalCode: this.originalCode,
originalSourceMap: this.originalSourceMap,
originalSourcemap: this.originalSourcemap,
ast: this.astClone,
sourceMapChain: this.sourceMapChain,
sourcemapChain: this.sourcemapChain,
resolvedIds: this.resolvedIds,
resolvedExternalIds: this.resolvedExternalIds
};
Expand Down
2 changes: 1 addition & 1 deletion src/finalisers/amd.js
Expand Up @@ -20,7 +20,7 @@ export default function amd ( bundle, magicString, { exportMode, indentString, i
( amdOptions.id ? `'${amdOptions.id}', ` : `` ) +
( deps.length ? `[${deps.join( ', ' )}], ` : `` );

const useStrict = options.useStrict !== false ? ` 'use strict';` : ``;
const useStrict = options.strict !== false ? ` 'use strict';` : ``;
const define = amdOptions.define || 'define';
const wrapperStart = `${define}(${params}function (${args.join( ', ' )}) {${useStrict}\n\n`;

Expand Down
2 changes: 1 addition & 1 deletion src/finalisers/cjs.js
Expand Up @@ -2,7 +2,7 @@ import getExportBlock from './shared/getExportBlock.js';
import esModuleExport from './shared/esModuleExport.js';

export default function cjs ( bundle, magicString, { exportMode, intro, outro }, options ) {
intro = ( options.useStrict === false ? intro : `'use strict';\n\n${intro}` ) +
intro = ( options.strict === false ? intro : `'use strict';\n\n${intro}` ) +
( exportMode === 'named' && options.legacy !== true ? `${esModuleExport}\n\n` : '' );

let needsInterop = false;
Expand Down
8 changes: 4 additions & 4 deletions src/finalisers/iife.js
Expand Up @@ -26,14 +26,14 @@ const thisProp = name => `this${keypath( name )}`;
export default function iife ( bundle, magicString, { exportMode, indentString, intro, outro }, options ) {
const globalNameMaker = getGlobalNameMaker( options.globals || blank(), bundle, 'null' );

const { extend, moduleName: name } = options;
const { extend, name } = options;
const isNamespaced = name && name.indexOf( '.' ) !== -1;
const possibleVariableAssignment = !extend && !isNamespaced;

if ( name && possibleVariableAssignment && !isLegal(name) ) {
error({
code: 'ILLEGAL_IDENTIFIER_AS_NAME',
message: `Given moduleName (${name}) is not legal JS identifier. If you need this you can try --extend option`
message: `Given name (${name}) is not legal JS identifier. If you need this you can try --extend option`
});
}

Expand All @@ -46,7 +46,7 @@ export default function iife ( bundle, magicString, { exportMode, indentString,
if ( exportMode !== 'none' && !name ) {
error({
code: 'INVALID_OPTION',
message: `You must supply options.moduleName for IIFE bundles`
message: `You must supply options.name for IIFE bundles`
});
}

Expand All @@ -58,7 +58,7 @@ export default function iife ( bundle, magicString, { exportMode, indentString,
args.unshift( 'exports' );
}

const useStrict = options.useStrict !== false ? `${indentString}'use strict';\n\n` : ``;
const useStrict = options.strict !== false ? `${indentString}'use strict';\n\n` : ``;

let wrapperIntro = `(function (${args}) {\n${useStrict}`;

Expand Down
16 changes: 8 additions & 8 deletions src/finalisers/umd.js
Expand Up @@ -37,10 +37,10 @@ function safeAccess ( name ) {
const wrapperOutro = '\n\n})));';

export default function umd ( bundle, magicString, { exportMode, indentString, intro, outro }, options ) {
if ( exportMode !== 'none' && !options.moduleName ) {
if ( exportMode !== 'none' && !options.name ) {
error({
code: 'INVALID_OPTION',
message: 'You must supply options.moduleName for UMD bundles'
message: 'You must supply options.name for UMD bundles'
});
}

Expand All @@ -58,7 +58,7 @@ export default function umd ( bundle, magicString, { exportMode, indentString, i
if ( exportMode === 'named' ) {
amdDeps.unshift( `'exports'` );
cjsDeps.unshift( `exports` );
globalDeps.unshift( `(${setupNamespace(options.moduleName)} = ${options.extend ? `${globalProp(options.moduleName)} || ` : '' }{})` );
globalDeps.unshift( `(${setupNamespace(options.name)} = ${options.extend ? `${globalProp(options.name)} || ` : '' }{})` );

args.unshift( 'exports' );
}
Expand All @@ -72,9 +72,9 @@ export default function umd ( bundle, magicString, { exportMode, indentString, i
const define = amdOptions.define || 'define';

const cjsExport = exportMode === 'default' ? `module.exports = ` : ``;
const defaultExport = exportMode === 'default' ? `${setupNamespace(options.moduleName)} = ` : '';
const defaultExport = exportMode === 'default' ? `${setupNamespace(options.name)} = ` : '';

const useStrict = options.useStrict !== false ? ` 'use strict';` : ``;
const useStrict = options.strict !== false ? ` 'use strict';` : ``;

let globalExport;

Expand All @@ -89,10 +89,10 @@ export default function umd ( bundle, magicString, { exportMode, indentString, i
factory(${['exports'].concat(globalDeps)});`;
}
globalExport = `(function() {
var current = ${safeAccess(options.moduleName)};
var current = ${safeAccess(options.name)};
${factory}
${globalProp(options.moduleName)} = exports;
exports.noConflict = function() { ${globalProp(options.moduleName)} = current; return exports; };
${globalProp(options.name)} = exports;
exports.noConflict = function() { ${globalProp(options.name)} = current; return exports; };
})()`;
} else {
globalExport = `(${defaultExport}factory(${globalDeps}))`;
Expand Down

0 comments on commit 31de491

Please sign in to comment.