diff --git a/src/Module.js b/src/Module.js index 3a5350e..dd507a8 100644 --- a/src/Module.js +++ b/src/Module.js @@ -208,6 +208,7 @@ export default class Module { // export { foo, bar, baz } // export var foo = 42; + // export var a = 1, b = 2, c = 3; // export function foo () {} else if ( node.type === 'ExportNamedDeclaration' ) { if ( node.specifiers.length ) { @@ -449,6 +450,33 @@ export default class Module { ast.body.forEach( node => { if ( node.type === 'EmptyStatement' ) return; + if ( + node.type === 'ExportNamedDeclaration' && + node.declaration && + node.declaration.type === 'VariableDeclaration' && + node.declaration.declarations && + node.declaration.declarations.length > 1 + ) { + // push a synthetic export declaration + const syntheticNode = { + type: 'ExportNamedDeclaration', + specifiers: node.declaration.declarations.map( declarator => { + const id = { name: declarator.id.name }; + return { + local: id, + exported: id + }; + }), + isSynthetic: true + }; + + const statement = new Statement( syntheticNode, this, node.start, node.start ); + statements.push( statement ); + + this.magicString.remove( node.start, node.declaration.start ); + node = node.declaration; + } + // special case - top-level var declarations with multiple declarators // should be split up. Otherwise, we may end up including code we // don't need, just because an unwanted declarator is included @@ -498,10 +526,12 @@ export default class Module { } }); - statements.forEach( ( statement, i ) => { - const nextStatement = statements[ i + 1 ]; - statement.next = nextStatement ? nextStatement.start : statement.end; - }); + let i = statements.length; + let next = this.source.length; + while ( i-- ) { + statements[i].next = next; + if ( !statements[i].isSynthetic ) next = statements[i].start; + } return statements; } @@ -519,6 +549,8 @@ export default class Module { // skip `export { foo, bar, baz }` if ( statement.node.type === 'ExportNamedDeclaration' ) { + if ( statement.node.isSynthetic ) return; + // skip `export { foo, bar, baz }` if ( statement.node.specifiers.length ) { magicString.remove( statement.start, statement.next ); diff --git a/test/form/export-multiple-vars/_config.js b/test/form/export-multiple-vars/_config.js new file mode 100644 index 0000000..6e84035 --- /dev/null +++ b/test/form/export-multiple-vars/_config.js @@ -0,0 +1,3 @@ +module.exports = { + description: 'splits up multiple vars in a single export declaration' +}; diff --git a/test/form/export-multiple-vars/_expected/amd.js b/test/form/export-multiple-vars/_expected/amd.js new file mode 100644 index 0000000..37a2678 --- /dev/null +++ b/test/form/export-multiple-vars/_expected/amd.js @@ -0,0 +1,13 @@ +define(function () { 'use strict'; + + var a = 1; + + var e = 5; + + var i = 9; + + assert.equal( a, 1 ); + assert.equal( e, 5 ); + assert.equal( i, 9 ); + +}); diff --git a/test/form/export-multiple-vars/_expected/cjs.js b/test/form/export-multiple-vars/_expected/cjs.js new file mode 100644 index 0000000..9953a6f --- /dev/null +++ b/test/form/export-multiple-vars/_expected/cjs.js @@ -0,0 +1,11 @@ +'use strict'; + +var a = 1; + +var e = 5; + +var i = 9; + +assert.equal( a, 1 ); +assert.equal( e, 5 ); +assert.equal( i, 9 ); diff --git a/test/form/export-multiple-vars/_expected/es6.js b/test/form/export-multiple-vars/_expected/es6.js new file mode 100644 index 0000000..4b58759 --- /dev/null +++ b/test/form/export-multiple-vars/_expected/es6.js @@ -0,0 +1,9 @@ +var a = 1; + +var e = 5; + +var i = 9; + +assert.equal( a, 1 ); +assert.equal( e, 5 ); +assert.equal( i, 9 ); diff --git a/test/form/export-multiple-vars/_expected/iife.js b/test/form/export-multiple-vars/_expected/iife.js new file mode 100644 index 0000000..c1c798a --- /dev/null +++ b/test/form/export-multiple-vars/_expected/iife.js @@ -0,0 +1,13 @@ +(function () { 'use strict'; + + var a = 1; + + var e = 5; + + var i = 9; + + assert.equal( a, 1 ); + assert.equal( e, 5 ); + assert.equal( i, 9 ); + +})(); diff --git a/test/form/export-multiple-vars/_expected/umd.js b/test/form/export-multiple-vars/_expected/umd.js new file mode 100644 index 0000000..15b3b4b --- /dev/null +++ b/test/form/export-multiple-vars/_expected/umd.js @@ -0,0 +1,17 @@ +(function (global, factory) { + typeof exports === 'object' && typeof module !== 'undefined' ? factory() : + typeof define === 'function' && define.amd ? define(factory) : + factory(); +}(this, function () { 'use strict'; + + var a = 1; + + var e = 5; + + var i = 9; + + assert.equal( a, 1 ); + assert.equal( e, 5 ); + assert.equal( i, 9 ); + +})); diff --git a/test/form/export-multiple-vars/bar.js b/test/form/export-multiple-vars/bar.js new file mode 100644 index 0000000..8f4e2ff --- /dev/null +++ b/test/form/export-multiple-vars/bar.js @@ -0,0 +1 @@ +export var d = 4, e = 5, f = 6; diff --git a/test/form/export-multiple-vars/baz.js b/test/form/export-multiple-vars/baz.js new file mode 100644 index 0000000..e9d4343 --- /dev/null +++ b/test/form/export-multiple-vars/baz.js @@ -0,0 +1 @@ +export var g = 7, h = 8, i = 9; diff --git a/test/form/export-multiple-vars/foo.js b/test/form/export-multiple-vars/foo.js new file mode 100644 index 0000000..75df6fb --- /dev/null +++ b/test/form/export-multiple-vars/foo.js @@ -0,0 +1 @@ +export var a = 1, b = 2, c = 3; diff --git a/test/form/export-multiple-vars/main.js b/test/form/export-multiple-vars/main.js new file mode 100644 index 0000000..2800674 --- /dev/null +++ b/test/form/export-multiple-vars/main.js @@ -0,0 +1,7 @@ +import { a } from './foo'; +import { e } from './bar'; +import { i } from './baz'; + +assert.equal( a, 1 ); +assert.equal( e, 5 ); +assert.equal( i, 9 );