diff --git a/src/Bundle.js b/src/Bundle.js index f5fedc8..d91479f 100644 --- a/src/Bundle.js +++ b/src/Bundle.js @@ -167,6 +167,12 @@ export default class Bundle { msg += `: ${err.message}`; throw new Error( msg ); }) + .then( source => { + if ( typeof source === 'string' ) return source; + if ( source && typeof source === 'object' && source.code ) return source; + + throw new Error( `Error loading ${id}: load hook should return a string, a { code, map } object, or nothing/null` ); + }) .then( source => transform( source, id, this.transformers ) ) .then( source => { const { code, originalCode, ast, sourceMapChain } = source; diff --git a/src/ast/attachScopes.js b/src/ast/attachScopes.js index 92798e8..51fc28a 100644 --- a/src/ast/attachScopes.js +++ b/src/ast/attachScopes.js @@ -29,7 +29,7 @@ export default function attachScopes ( statement ) { let newScope; // create new function scope - if ( /Function/.test( node.type ) ) { + if ( /(Function|Class)/.test( node.type ) ) { newScope = new Scope({ parent: scope, block: false, @@ -38,7 +38,7 @@ export default function attachScopes ( statement ) { // named function expressions - the name is considered // part of the function's scope - if ( node.type === 'FunctionExpression' && node.id ) { + if ( /(Function|Class)Expression/.test( node.type ) && node.id ) { newScope.addDeclaration( node, false, false ); } } diff --git a/test/form/assignment-to-exports-class-declaration/_config.js b/test/form/assignment-to-exports-class-declaration/_config.js new file mode 100644 index 0000000..0f3d2ef --- /dev/null +++ b/test/form/assignment-to-exports-class-declaration/_config.js @@ -0,0 +1,6 @@ +module.exports = { + description: 'does not rewrite class declaration IDs', + options: { + moduleName: 'myModule' + } +}; diff --git a/test/form/assignment-to-exports-class-declaration/_expected/amd.js b/test/form/assignment-to-exports-class-declaration/_expected/amd.js new file mode 100644 index 0000000..c2d0691 --- /dev/null +++ b/test/form/assignment-to-exports-class-declaration/_expected/amd.js @@ -0,0 +1,6 @@ +define(['exports'], function (exports) { 'use strict'; + + exports.Foo = class Foo {} + exports.Foo = lol( exports.Foo ); + +}); diff --git a/test/form/assignment-to-exports-class-declaration/_expected/cjs.js b/test/form/assignment-to-exports-class-declaration/_expected/cjs.js new file mode 100644 index 0000000..b062474 --- /dev/null +++ b/test/form/assignment-to-exports-class-declaration/_expected/cjs.js @@ -0,0 +1,4 @@ +'use strict'; + +exports.Foo = class Foo {} +exports.Foo = lol( exports.Foo ); diff --git a/test/form/assignment-to-exports-class-declaration/_expected/es6.js b/test/form/assignment-to-exports-class-declaration/_expected/es6.js new file mode 100644 index 0000000..08e82c9 --- /dev/null +++ b/test/form/assignment-to-exports-class-declaration/_expected/es6.js @@ -0,0 +1,4 @@ +Foo = class Foo {} +Foo = lol( Foo ); + +export { Foo }; \ No newline at end of file diff --git a/test/form/assignment-to-exports-class-declaration/_expected/iife.js b/test/form/assignment-to-exports-class-declaration/_expected/iife.js new file mode 100644 index 0000000..fa37538 --- /dev/null +++ b/test/form/assignment-to-exports-class-declaration/_expected/iife.js @@ -0,0 +1,7 @@ +(function (exports) { + 'use strict'; + + exports.Foo = class Foo {} + exports.Foo = lol( exports.Foo ); + +}((this.myModule = this.myModule || {}))); diff --git a/test/form/assignment-to-exports-class-declaration/_expected/umd.js b/test/form/assignment-to-exports-class-declaration/_expected/umd.js new file mode 100644 index 0000000..bbefc87 --- /dev/null +++ b/test/form/assignment-to-exports-class-declaration/_expected/umd.js @@ -0,0 +1,10 @@ +(function (global, factory) { + typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : + typeof define === 'function' && define.amd ? define(['exports'], factory) : + (factory((global.myModule = global.myModule || {}))); +}(this, function (exports) { 'use strict'; + + exports.Foo = class Foo {} + exports.Foo = lol( exports.Foo ); + +})); diff --git a/test/form/assignment-to-exports-class-declaration/main.js b/test/form/assignment-to-exports-class-declaration/main.js new file mode 100644 index 0000000..ceae376 --- /dev/null +++ b/test/form/assignment-to-exports-class-declaration/main.js @@ -0,0 +1,2 @@ +export let Foo = class Foo {} +Foo = lol( Foo ); diff --git a/test/function/load-returns-string-or-null/_config.js b/test/function/load-returns-string-or-null/_config.js new file mode 100644 index 0000000..ea80699 --- /dev/null +++ b/test/function/load-returns-string-or-null/_config.js @@ -0,0 +1,15 @@ +var assert = require( 'assert' ); + +module.exports = { + description: 'throws error if load returns something wacky', + options: { + plugins: [{ + load: function () { + return 42; + } + }] + }, + error: function ( err ) { + assert.ok( /load hook should return a string, a \{ code, map \} object, or nothing\/null/.test( err.message ) ); + } +};