Browse Source

Merge pull request #348 from rollup/validate-options

Prevent invalid keys from being passed in options.
gh-384
Rich Harris 9 years ago
parent
commit
ea5970f0d9
  1. 28
      src/rollup.js
  2. 17
      src/utils/validateKeys.js
  3. 8
      test/test.js

28
src/rollup.js

@ -2,11 +2,33 @@ import Promise from 'es6-promise/lib/es6-promise/promise.js';
import { basename } from './utils/path.js';
import { writeFile } from './utils/fs.js';
import { keys } from './utils/object.js';
import validateKeys from './utils/validateKeys.js';
import SOURCEMAPPING_URL from './utils/sourceMappingURL.js';
import Bundle from './Bundle.js';
export const VERSION = '<@VERSION@>';
const ALLOWED_KEYS = [
'entry',
'dest',
'plugins',
'external',
'onwarn',
'indent',
'format',
'moduleName',
'sourceMap',
'intro',
'outro',
'banner',
'footer',
'globals',
'transform',
'load',
'resolveId',
'resolveExternal'
];
export function rollup ( options ) {
if ( !options || !options.entry ) {
return Promise.reject( new Error( 'You must supply options.entry to rollup' ) );
@ -16,6 +38,12 @@ export function rollup ( options ) {
return Promise.reject( new Error( 'The `transform`, `load`, `resolveId` and `resolveExternal` options are deprecated in favour of a unified plugin API. See https://github.com/rollup/rollup/wiki/Plugins for details' ) );
}
const error = validateKeys( options, ALLOWED_KEYS );
if ( error ) {
return Promise.reject( error );
}
const bundle = new Bundle( options );
return bundle.build().then( () => {

17
src/utils/validateKeys.js

@ -0,0 +1,17 @@
import { keys } from './object.js';
export default function validateKeys ( object, allowedKeys ) {
const actualKeys = keys( object );
let i = actualKeys.length;
while ( i-- ) {
const key = actualKeys[i];
if ( allowedKeys.indexOf( key ) === -1 ) {
return new Error(
`Unexpected key '${ key }' found, expected one of: ${ allowedKeys.join( ', ' ) }`
);
}
}
}

8
test/test.js

@ -69,6 +69,14 @@ describe( 'rollup', function () {
assert.equal( 'You must supply options.entry to rollup', err.message );
});
});
it( 'fails with invalid keys', function () {
return rollup.rollup({ entry: 'x', plUgins: [] }).then( function () {
throw new Error( 'Missing expected error' );
}, function ( err ) {
assert.equal( 'Unexpected key \'plUgins\' found, expected one of: entry, dest, plugins, external, onwarn, indent, format, moduleName, sourceMap, intro, outro, banner, footer, globals, transform, load, resolveId, resolveExternal', err.message );
});
});
});
describe( 'bundle.write()', function () {

Loading…
Cancel
Save