From dd9a93f094ca4c1dd2268791b34f1549c5c6beaf Mon Sep 17 00:00:00 2001 From: Rich-Harris Date: Mon, 2 Jan 2017 11:30:01 -0500 Subject: [PATCH] more error stuff --- src/Bundle.js | 7 ++++++- src/Module.js | 16 ++++++++++++---- src/finalisers/iife.js | 6 +++++- src/finalisers/umd.js | 6 +++++- src/rollup.js | 14 +++++++++----- src/utils/collapseSourcemaps.js | 5 ++++- src/utils/defaults.js | 9 ++++++++- 7 files changed, 49 insertions(+), 14 deletions(-) diff --git a/src/Bundle.js b/src/Bundle.js index c945184..0cfe895 100644 --- a/src/Bundle.js +++ b/src/Bundle.js @@ -474,7 +474,12 @@ export default class Bundle { const indentString = getIndentString( magicString, options ); const finalise = finalisers[ options.format ]; - if ( !finalise ) throw new Error( `You must specify an output type - valid options are ${keys( finalisers ).join( ', ' )}` ); + if ( !finalise ) { + error({ + code: 'INVALID_OPTION', + message: `You must specify an output type - valid options are ${keys( finalisers ).join( ', ' )}` + }); + } timeStart( 'render format' ); diff --git a/src/Module.js b/src/Module.js index dadd5a5..ce59dee 100644 --- a/src/Module.js +++ b/src/Module.js @@ -121,7 +121,10 @@ export default class Module { const name = specifier.exported.name; if ( this.exports[ name ] || this.reexports[ name ] ) { - throw new Error( `A module cannot have multiple exports with the same name ('${name}')` ); + this.error({ + code: 'DUPLICATE_EXPORT', + message: `A module cannot have multiple exports with the same name ('${name}')` + }, specifier.start ); } this.reexports[ name ] = { @@ -141,8 +144,10 @@ export default class Module { const identifier = ( node.declaration.id && node.declaration.id.name ) || node.declaration.name; if ( this.exports.default ) { - // TODO indicate location - throw new Error( 'A module can only have one default export' ); + this.error({ + code: 'DUPLICATE_EXPORT', + message: `A module can only have one default export` + }, node.start ); } this.exports.default = { @@ -182,7 +187,10 @@ export default class Module { const exportedName = specifier.exported.name; if ( this.exports[ exportedName ] || this.reexports[ exportedName ] ) { - throw new Error( `A module cannot have multiple exports with the same name ('${exportedName}')` ); + this.error({ + code: 'DUPLICATE_EXPORT', + message: `A module cannot have multiple exports with the same name ('${exportedName}')` + }, specifier.start ); } this.exports[ exportedName ] = { localName }; diff --git a/src/finalisers/iife.js b/src/finalisers/iife.js index 2d6121e..aad4491 100644 --- a/src/finalisers/iife.js +++ b/src/finalisers/iife.js @@ -1,5 +1,6 @@ import { blank } from '../utils/object.js'; import { getName } from '../utils/map-helpers.js'; +import error from '../utils/error.js'; import getInteropBlock from './shared/getInteropBlock.js'; import getExportBlock from './shared/getExportBlock.js'; import getGlobalNameMaker from './shared/getGlobalNameMaker.js'; @@ -36,7 +37,10 @@ export default function iife ( bundle, magicString, { exportMode, indentString, const args = bundle.externalModules.map( getName ); if ( exportMode !== 'none' && !name ) { - throw new Error( 'You must supply options.moduleName for IIFE bundles' ); + error({ + code: 'INVALID_OPTION', + message: `You must supply options.moduleName for IIFE bundles` + }); } if ( exportMode === 'named' ) { diff --git a/src/finalisers/umd.js b/src/finalisers/umd.js index 655c77a..b624307 100644 --- a/src/finalisers/umd.js +++ b/src/finalisers/umd.js @@ -1,5 +1,6 @@ import { blank } from '../utils/object.js'; import { getName, quotePath, req } from '../utils/map-helpers.js'; +import error from '../utils/error.js'; import getInteropBlock from './shared/getInteropBlock.js'; import getExportBlock from './shared/getExportBlock.js'; import getGlobalNameMaker from './shared/getGlobalNameMaker.js'; @@ -28,7 +29,10 @@ const wrapperOutro = '\n\n})));'; export default function umd ( bundle, magicString, { exportMode, indentString, intro, outro }, options ) { if ( exportMode !== 'none' && !options.moduleName ) { - throw new Error( 'You must supply options.moduleName for UMD bundles' ); + error({ + code: 'INVALID_OPTION', + message: 'You must supply options.moduleName for UMD bundles' + }); } warnOnBuiltins( bundle ); diff --git a/src/rollup.js b/src/rollup.js index c3e6379..a87fcf8 100644 --- a/src/rollup.js +++ b/src/rollup.js @@ -4,6 +4,7 @@ import { writeFile } from './utils/fs.js'; import { assign, keys } from './utils/object.js'; import { mapSequence } from './utils/promise.js'; import validateKeys from './utils/validateKeys.js'; +import error from './utils/error.js'; import { SOURCEMAPPING_URL } from './utils/sourceMappingURL.js'; import Bundle from './Bundle.js'; @@ -50,15 +51,15 @@ function checkOptions ( options ) { return 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( keys(options), ALLOWED_KEYS ); - if ( error ) return error; + const err = validateKeys( keys(options), ALLOWED_KEYS ); + if ( err ) return err; return null; } export function rollup ( options ) { - const error = checkOptions ( options ); - if ( error ) return Promise.reject( error ); + const err = checkOptions ( options ); + if ( err ) return Promise.reject( err ); const bundle = new Bundle( options ); @@ -105,7 +106,10 @@ export function rollup ( options ) { generate, write: options => { if ( !options || !options.dest ) { - throw new Error( 'You must supply options.dest to bundle.write' ); + error({ + code: 'MISSING_OPTION', + message: 'You must supply options.dest to bundle.write' + }); } const dest = options.dest; diff --git a/src/utils/collapseSourcemaps.js b/src/utils/collapseSourcemaps.js index 122a148..bd65cf4 100644 --- a/src/utils/collapseSourcemaps.js +++ b/src/utils/collapseSourcemaps.js @@ -1,4 +1,5 @@ import { encode } from 'sourcemap-codec'; +import error from './error.js'; import { dirname, relative, resolve } from './path.js'; class Source { @@ -51,7 +52,9 @@ class Link { } else if ( sourcesContent[ sourceIndex ] == null ) { sourcesContent[ sourceIndex ] = traced.source.content; } else if ( traced.source.content != null && sourcesContent[ sourceIndex ] !== traced.source.content ) { - throw new Error( `Multiple conflicting contents for sourcemap source ${source.filename}` ); + error({ + message: `Multiple conflicting contents for sourcemap source ${source.filename}` + }); } segment[1] = sourceIndex; diff --git a/src/utils/defaults.js b/src/utils/defaults.js index c5dd054..d2757dd 100644 --- a/src/utils/defaults.js +++ b/src/utils/defaults.js @@ -1,6 +1,7 @@ import { lstatSync, readdirSync, readFileSync, realpathSync } from './fs.js'; // eslint-disable-line import { basename, dirname, isAbsolute, resolve } from './path.js'; import { blank } from './object.js'; +import error from './error.js'; export function load ( id ) { return readFileSync( id, 'utf-8' ); @@ -27,7 +28,13 @@ function addJsExtensionIfNecessary ( file ) { } export function resolveId ( importee, importer ) { - if ( typeof process === 'undefined' ) throw new Error( `It looks like you're using Rollup in a non-Node.js environment. This means you must supply a plugin with custom resolveId and load functions. See https://github.com/rollup/rollup/wiki/Plugins for more information` ); + if ( typeof process === 'undefined' ) { + error({ + code: 'MISSING_PROCESS', + message: `It looks like you're using Rollup in a non-Node.js environment. This means you must supply a plugin with custom resolveId and load functions`, + url: 'https://github.com/rollup/rollup/wiki/Plugins' + }); + } // absolute paths are left untouched if ( isAbsolute( importee ) ) return addJsExtensionIfNecessary( resolve( importee ) );