From dbfa280fcd8badec6f16bae800c63f65d0d7e9e9 Mon Sep 17 00:00:00 2001 From: Rich-Harris Date: Sat, 9 Jul 2016 16:26:58 -0400 Subject: [PATCH 1/2] ensure reassigned exports are declared in an ES bundle (#755) --- src/Module.js | 18 ++++++++++++++---- .../_expected/es.js | 4 ++-- test/form/assignment-to-exports/_config.js | 6 ++++++ .../assignment-to-exports/_expected/amd.js | 7 +++++++ .../assignment-to-exports/_expected/cjs.js | 5 +++++ .../form/assignment-to-exports/_expected/es.js | 4 ++++ .../assignment-to-exports/_expected/iife.js | 6 ++++++ .../assignment-to-exports/_expected/umd.js | 11 +++++++++++ test/form/assignment-to-exports/main.js | 2 ++ 9 files changed, 57 insertions(+), 6 deletions(-) create mode 100644 test/form/assignment-to-exports/_config.js create mode 100644 test/form/assignment-to-exports/_expected/amd.js create mode 100644 test/form/assignment-to-exports/_expected/cjs.js create mode 100644 test/form/assignment-to-exports/_expected/es.js create mode 100644 test/form/assignment-to-exports/_expected/iife.js create mode 100644 test/form/assignment-to-exports/_expected/umd.js create mode 100644 test/form/assignment-to-exports/main.js diff --git a/src/Module.js b/src/Module.js index 4dd1b53..0830a8c 100644 --- a/src/Module.js +++ b/src/Module.js @@ -462,7 +462,7 @@ export default class Module { if ( statement.node.isSynthetic ) return; // skip `export { foo, bar, baz }` - if ( statement.node.specifiers.length ) { + if ( statement.node.declaration === null ) { magicString.remove( statement.start, statement.next ); return; } @@ -555,11 +555,21 @@ export default class Module { const name = extractNames( statement.node.declaration.declarations[ 0 ].id )[ 0 ]; const declaration = this.declarations[ name ]; + // TODO is this even possible? if ( !declaration ) throw new Error( `Missing declaration for ${name}!` ); - const end = declaration.exportName && declaration.isReassigned ? - statement.node.declaration.declarations[0].start : - statement.node.declaration.start; + let end; + + if ( es ) { + end = statement.node.declaration.start; + } else { + if ( declaration.exportName && declaration.isReassigned ) { + const declarator = statement.node.declaration.declarations[0]; + end = declarator.init ? declarator.start : statement.next; + } else { + end = statement.node.declaration.start + } + } magicString.remove( statement.node.start, end ); } diff --git a/test/form/assignment-to-exports-class-declaration/_expected/es.js b/test/form/assignment-to-exports-class-declaration/_expected/es.js index 08e82c9..2631630 100644 --- a/test/form/assignment-to-exports-class-declaration/_expected/es.js +++ b/test/form/assignment-to-exports-class-declaration/_expected/es.js @@ -1,4 +1,4 @@ -Foo = class Foo {} +let Foo = class Foo {} Foo = lol( Foo ); -export { Foo }; \ No newline at end of file +export { Foo }; diff --git a/test/form/assignment-to-exports/_config.js b/test/form/assignment-to-exports/_config.js new file mode 100644 index 0000000..3c9f6d4 --- /dev/null +++ b/test/form/assignment-to-exports/_config.js @@ -0,0 +1,6 @@ +module.exports = { + description: 'declares updated variable in ES output (#755)', + options: { + moduleName: 'bundle' + } +}; diff --git a/test/form/assignment-to-exports/_expected/amd.js b/test/form/assignment-to-exports/_expected/amd.js new file mode 100644 index 0000000..6f8d4d8 --- /dev/null +++ b/test/form/assignment-to-exports/_expected/amd.js @@ -0,0 +1,7 @@ +define(['exports'], function (exports) { 'use strict'; + + exports.foo = 1; + + Object.defineProperty(exports, '__esModule', { value: true }); + +}); diff --git a/test/form/assignment-to-exports/_expected/cjs.js b/test/form/assignment-to-exports/_expected/cjs.js new file mode 100644 index 0000000..9de4215 --- /dev/null +++ b/test/form/assignment-to-exports/_expected/cjs.js @@ -0,0 +1,5 @@ +'use strict'; + +Object.defineProperty(exports, '__esModule', { value: true }); + +exports.foo = 1; diff --git a/test/form/assignment-to-exports/_expected/es.js b/test/form/assignment-to-exports/_expected/es.js new file mode 100644 index 0000000..e6c6e5f --- /dev/null +++ b/test/form/assignment-to-exports/_expected/es.js @@ -0,0 +1,4 @@ +var foo; +foo = 1; + +export { foo }; diff --git a/test/form/assignment-to-exports/_expected/iife.js b/test/form/assignment-to-exports/_expected/iife.js new file mode 100644 index 0000000..3ac6c5f --- /dev/null +++ b/test/form/assignment-to-exports/_expected/iife.js @@ -0,0 +1,6 @@ +(function (exports) { + 'use strict'; + + exports.foo = 1; + +}((this.bundle = this.bundle || {}))); diff --git a/test/form/assignment-to-exports/_expected/umd.js b/test/form/assignment-to-exports/_expected/umd.js new file mode 100644 index 0000000..cc3fb1f --- /dev/null +++ b/test/form/assignment-to-exports/_expected/umd.js @@ -0,0 +1,11 @@ +(function (global, factory) { + typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : + typeof define === 'function' && define.amd ? define(['exports'], factory) : + (factory((global.bundle = global.bundle || {}))); +}(this, function (exports) { 'use strict'; + + exports.foo = 1; + + Object.defineProperty(exports, '__esModule', { value: true }); + +})); diff --git a/test/form/assignment-to-exports/main.js b/test/form/assignment-to-exports/main.js new file mode 100644 index 0000000..bd12ed8 --- /dev/null +++ b/test/form/assignment-to-exports/main.js @@ -0,0 +1,2 @@ +export var foo; +foo = 1; From b699a46978c774591e953ab859c2142e4603ee97 Mon Sep 17 00:00:00 2001 From: Rich-Harris Date: Sat, 9 Jul 2016 16:30:01 -0400 Subject: [PATCH 2/2] lint --- src/Module.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Module.js b/src/Module.js index 0830a8c..4d57a19 100644 --- a/src/Module.js +++ b/src/Module.js @@ -567,7 +567,7 @@ export default class Module { const declarator = statement.node.declaration.declarations[0]; end = declarator.init ? declarator.start : statement.next; } else { - end = statement.node.declaration.start + end = statement.node.declaration.start; } }