Browse Source

ensure reassigned exports are declared in an ES bundle (#755)

semi-dynamic-namespace-imports
Rich-Harris 9 years ago
parent
commit
dbfa280fcd
  1. 18
      src/Module.js
  2. 4
      test/form/assignment-to-exports-class-declaration/_expected/es.js
  3. 6
      test/form/assignment-to-exports/_config.js
  4. 7
      test/form/assignment-to-exports/_expected/amd.js
  5. 5
      test/form/assignment-to-exports/_expected/cjs.js
  6. 4
      test/form/assignment-to-exports/_expected/es.js
  7. 6
      test/form/assignment-to-exports/_expected/iife.js
  8. 11
      test/form/assignment-to-exports/_expected/umd.js
  9. 2
      test/form/assignment-to-exports/main.js

18
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 );
}

4
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 };
export { Foo };

6
test/form/assignment-to-exports/_config.js

@ -0,0 +1,6 @@
module.exports = {
description: 'declares updated variable in ES output (#755)',
options: {
moduleName: 'bundle'
}
};

7
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 });
});

5
test/form/assignment-to-exports/_expected/cjs.js

@ -0,0 +1,5 @@
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
exports.foo = 1;

4
test/form/assignment-to-exports/_expected/es.js

@ -0,0 +1,4 @@
var foo;
foo = 1;
export { foo };

6
test/form/assignment-to-exports/_expected/iife.js

@ -0,0 +1,6 @@
(function (exports) {
'use strict';
exports.foo = 1;
}((this.bundle = this.bundle || {})));

11
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 });
}));

2
test/form/assignment-to-exports/main.js

@ -0,0 +1,2 @@
export var foo;
foo = 1;
Loading…
Cancel
Save