Skip to content

Commit

Permalink
Uses mergeOptions to validate the config accross Node API, CLI and wa…
Browse files Browse the repository at this point in the history
…tch.
  • Loading branch information
ankeetmaini committed Dec 20, 2017
1 parent 996fea6 commit 5afda86
Show file tree
Hide file tree
Showing 11 changed files with 115 additions and 161 deletions.
14 changes: 2 additions & 12 deletions bin/src/run/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { realpathSync } from 'fs';
import relative from 'require-relative';
import { handleError } from '../logging.js';
import mergeOptions from '../../../src/utils/mergeOptions';
import batchWarnings from '../../../src/utils/batchWarnings';
import mergeOptions from '../../../src/utils/mergeOptions.js';
import batchWarnings from '../../../src/utils/batchWarnings.js';
import loadConfigFile from './loadConfigFile.js';
import sequence from '../utils/sequence.js';
import build from './build.js';
Expand Down Expand Up @@ -84,18 +84,8 @@ function execute ( configFile, configs, command ) {
} else {
return sequence( configs, config => {
const { inputOptions, outputOptions, deprecations } = mergeOptions( config, command );

const warnings = batchWarnings();

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

if (deprecations.length) {
inputOptions.onwarn({
code: 'DEPRECATED_OPTIONS',
Expand Down
2 changes: 1 addition & 1 deletion bin/src/run/loadConfigFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import buble from 'rollup-plugin-buble';
import path from 'path';
import chalk from 'chalk';
import * as rollup from 'rollup';
import batchWarnings from '../../../src/utils/batchWarnings';
import batchWarnings from '../../../src/utils/batchWarnings.js';
import relativeId from '../../../src/utils/relativeId.js';
import { handleError, stderr } from '../logging.js';

Expand Down
4 changes: 2 additions & 2 deletions bin/src/run/watch.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import chalk from 'chalk';
import ms from 'pretty-ms';
import onExit from 'signal-exit';
import dateTime from 'date-time';
import mergeOptions from '../../../src/utils/mergeOptions';
import batchWarnings from '../../../src/utils/batchWarnings';
import mergeOptions from '../../../src/utils/mergeOptions.js';
import batchWarnings from '../../../src/utils/batchWarnings.js';
import alternateScreen from './alternateScreen.js';
import loadConfigFile from './loadConfigFile.js';
import relativeId from '../../../src/utils/relativeId.js';
Expand Down
25 changes: 9 additions & 16 deletions src/rollup/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import { assign, keys } from '../utils/object.js';
import { mapSequence } from '../utils/promise.js';
import error from '../utils/error.js';
import { SOURCEMAPPING_URL } from '../utils/sourceMappingURL.js';
import mergeOptions from '../utils/mergeOptions';
import batchWarnings from '../utils/batchWarnings';
import mergeOptions from '../utils/mergeOptions.js';
import Bundle from '../Bundle.js';

export const VERSION = '<@VERSION@>';
Expand Down Expand Up @@ -60,18 +59,7 @@ export default function rollup ( _inputOptions ) {
throw new Error( 'You must supply an options object to rollup' );
}
const { inputOptions, deprecations } = mergeOptions(_inputOptions, {}, { input: true });
const warnings = batchWarnings();
const onwarn = inputOptions.onwarn;
let warn;

if (onwarn) {
warn = warning => {
onwarn(warning, warnings.add);
};
} else {
warn = warning => console.warn(warning.message); // eslint-disable-line no-console
}
if ( deprecations.length ) addDeprecations(deprecations, warn);
if ( deprecations.length ) addDeprecations(deprecations, inputOptions.onwarn);
checkInputOptions( inputOptions );
const bundle = new Bundle( inputOptions );

Expand All @@ -84,13 +72,18 @@ export default function rollup ( _inputOptions ) {
if ( !_outputOptions ) {
throw new Error( 'You must supply an options object' );
}
const mergedOptions = mergeOptions({ output: _outputOptions }, {}, { output: true });
const mergedOptions = mergeOptions(
// just for backward compatiblity to fallback on root
// if the option isn't present in `output`
Object.assign({}, { output: Object.assign({}, _outputOptions, inputOptions.output) }),
{},
{ output: true });

// now outputOptions is an array, but rollup.rollup API doesn't support arrays
const outputOptions = mergedOptions.outputOptions[0];
const deprecations = mergedOptions.deprecations;

if ( deprecations.length ) addDeprecations(deprecations, warn);
if ( deprecations.length ) addDeprecations(deprecations, inputOptions.onwarn);
checkOutputOptions( outputOptions );

timeStart( '--GENERATE--' );
Expand Down
4 changes: 2 additions & 2 deletions src/utils/batchWarnings.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import chalk from 'chalk';
import { stderr } from '../../bin/src/logging';
import relativeId from './relativeId';
import { stderr } from '../../bin/src/logging.js';
import relativeId from './relativeId.js';

export default function batchWarnings () {
let allWarnings = new Map();
Expand Down
91 changes: 43 additions & 48 deletions src/utils/deprecateOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,53 +10,27 @@ export default function deprecateOptions ( options, deprecateConfig ) {
return deprecations;

function deprecateInputOptions () {
if ( options.entry ) {
deprecations.push( { old: 'entry', new: 'input' } );
options.input = options.entry; // don't delete, as plugins sometimes depend on this...
}

if ( options.pureExternalModules ) {
deprecations.push( { old: 'pureExternalModules', new: 'treeshake.pureExternalModules' } );
if ( options.treeshake === undefined ) {
options.treeshake = {};
}
if ( options.treeshake ) {
options.treeshake.pureExternalModules = options.pureExternalModules;
}
delete options.pureExternalModules;
}
}

function deprecateOutputOptions () {
if ( options.moduleName ) {
deprecations.push( { old: 'moduleName', new: 'output.name' } );
options.name = options.moduleName;
delete options.moduleName;
}

if ( options.sourceMap ) {
deprecations.push( { old: 'sourceMap', new: 'output.sourcemap' } );
options.sourcemap = options.sourceMap;
delete options.sourceMap;
}

if ( options.sourceMapFile ) {
deprecations.push( { old: 'sourceMapFile', new: 'output.sourcemapFile' } );
options.sourcemapFile = options.sourceMapFile;
delete options.sourceMapFile;
}

if ( options.useStrict ) {
deprecations.push( { old: 'useStrict', new: 'output.strict' } );
options.strict = options.useStrict;
delete options.useStrict;
}
if (options.entry) deprecate('entry', 'input');
if (options.moduleName) deprecate('moduleName', 'output.name', true);
if (options.extend) deprecate('extend', 'output.extend', true);
if (options.globals) deprecate('globals', 'output.globals', true);
if (options.indent) deprecate('indent', 'output.indent', true);
if (options.noConflict) deprecate('noConflict', 'output.noConflict', true);
if (options.paths) deprecate('paths', 'output.paths', true);
if (options.sourcemap) deprecate('sourcemap', 'output.sourcemap', true);
if (options.sourceMap) deprecate('sourceMap', 'output.sourcemap', true);
if (options.sourceMapFile) deprecate('sourceMapFile', 'output.sourcemapFile', true);
if (options.useStrict) deprecate('useStrict', 'output.strict', true);
if (options.format) deprecate('format', 'output.format', true);

if ( options.targets ) {
deprecations.push( { old: 'targets', new: 'output' } );
options.output = options.targets;

// as targets is an array and we need to merge other output options
// like sourcemap etc.
options.output = options.targets.map(t => Object.assign({}, t, options.output));
delete options.targets;

let deprecatedDest = false;
options.output.forEach( output => {
if ( output.dest ) {
Expand All @@ -77,16 +51,37 @@ export default function deprecateOptions ( options, deprecateConfig ) {
delete options.dest;
}

if ( options.format ) {
if ( !options.output ) options.output = { format: options.format };
deprecations.push( { old: 'format', new: 'output.format' } );
delete options.format;
if ( options.pureExternalModules ) {
deprecations.push( { old: 'pureExternalModules', new: 'treeshake.pureExternalModules' } );
if ( options.treeshake === undefined ) {
options.treeshake = {};
}
if ( options.treeshake ) {
options.treeshake.pureExternalModules = options.pureExternalModules;
}
delete options.pureExternalModules;
}

}

function deprecateOutputOptions () {
if (options.output && options.output.moduleId) {
options.output.amd = { id: options.moduleId };
deprecations.push( { old: 'moduleId', new: 'amd' } );
delete options.output.moduleId;
}
}

// a utility function to add deprecations for straightforward options
function deprecate (oldOption, newOption, shouldDelete) {
deprecations.push({ new: newOption, old: oldOption });

if (newOption.indexOf('output') > -1) {
options.output = options.output || {};
options.output[newOption.replace(/output\./, '')] = options[oldOption];
} else {
options[newOption] = options[oldOption];
}

if (shouldDelete) delete options[oldOption];
}
}
65 changes: 43 additions & 22 deletions src/utils/mergeOptions.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import ensureArray from './ensureArray.js';
import deprecateOptions from './deprecateOptions.js';
import batchWarnings from './batchWarnings.js';

function normalizeObjectOptionValue ( optionValue ) {
if ( !optionValue ) {
Expand All @@ -11,12 +12,15 @@ function normalizeObjectOptionValue ( optionValue ) {
return optionValue;
}

export default function mergeOptions ( config, command, deprecateConfig) {
export default function mergeOptions ( config, command = {}, deprecateConfig) {
const deprecations = deprecate( config, command, deprecateConfig );

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

const getInputOption = getOption(config);
const getOutputOption = getOption(config.output || {});

function getObjectOption ( name ) {
const commandOption = normalizeObjectOptionValue( command[ name ] );
Expand All @@ -29,16 +33,30 @@ export default function mergeOptions ( config, command, deprecateConfig) {
return configOption;
}

const warnings = batchWarnings();
const onwarn = config.onwarn;
let warn;

if (onwarn) {
warn = warning => {
onwarn(warning, warnings.add);
};
} else {
warn = warning => console.warn(warning.message); // eslint-disable-line no-console
}

const inputOptions = {
input: getOption( 'input' ),
legacy: getOption( 'legacy' ),
input: getInputOption( 'input' ),
legacy: getInputOption( 'legacy' ),
treeshake: getObjectOption( 'treeshake' ),
acorn: config.acorn,
context: config.context,
moduleContext: config.moduleContext,
plugins: config.plugins,
onwarn: config.onwarn,
watch: config.watch
onwarn: warn,
watch: config.watch,
cache: getInputOption( 'cache' ),
preferConst: getInputOption( 'preferConst' ),
};

// legacy, to ensure e.g. commonjs plugin still works
Expand Down Expand Up @@ -76,22 +94,25 @@ export default function mergeOptions ( config, command, deprecateConfig) {
}

const baseOutputOptions = {
extend: getOption( 'extend' ),
extend: getOutputOption( 'extend' ),
amd: Object.assign( {}, config.amd, command.amd ),
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' ),
freeze: getOption( 'freeze' ),
indent: getOption( 'indent' ),
strict: getOption( 'strict' ),
noConflict: getOption( 'noConflict' ),
paths: getOption( 'paths' )
banner: getOutputOption( 'banner' ),
footer: getOutputOption( 'footer' ),
intro: getOutputOption( 'intro' ),
format: getOutputOption( 'format' ),
outro: getOutputOption( 'outro' ),
sourcemap: getOutputOption( 'sourcemap' ),
sourcemapFile: getOutputOption( 'sourcemapFile' ),
name: getOutputOption( 'name' ),
globals: getOutputOption( 'globals' ),
interop: getOutputOption( 'interop' ),
legacy: getOutputOption( 'legacy' ),
freeze: getOutputOption( 'freeze' ),
indent: getOutputOption( 'indent' ),
strict: getOutputOption( 'strict' ),
noConflict: getOutputOption( 'noConflict' ),
paths: getOutputOption( 'paths' ),
exports: getOutputOption( 'exports' ),
};

let mergedOutputOptions;
Expand Down
13 changes: 0 additions & 13 deletions src/utils/validateKeys.js

This file was deleted.

Loading

0 comments on commit 5afda86

Please sign in to comment.