Browse Source

use canonical names for exports

contingency-plan
Rich Harris 10 years ago
parent
commit
9ca27a768c
  1. 19
      src/Bundle.js
  2. 4
      src/finalisers/cjs.js
  3. 6
      test/samples/export-deconflicted-names/_config.js
  4. 3
      test/samples/export-deconflicted-names/bar.js
  5. 5
      test/samples/export-deconflicted-names/baz.js
  6. 5
      test/samples/export-deconflicted-names/foo.js
  7. 5
      test/samples/export-deconflicted-names/main.js

19
src/Bundle.js

@ -85,10 +85,19 @@ export default class Bundle {
})
.then( () => {
entryModule.ast.body.forEach( node => {
// exclude imports and exports, include everything else
if ( !/^(?:Im|Ex)port/.test( node.type ) ) {
this.body.push( node );
// Exclude imports
if ( /^Import/.test( node.type ) ) return;
if ( node.type === 'ExportNamedDeclaration' ) {
// Exclude specifier exports
if ( node.specifiers.length ) return;
// Remove the `export` from everything else
node._source.remove( node.start, node.declaration.start );
}
// Include everything else
this.body.push( node );
});
});
})
@ -157,7 +166,7 @@ export default class Bundle {
// TODO we shouldn't be adding export statements back into the entry
// module, they shouldn't be removed in the first place
this.entryModule.exportStatements.forEach( statement => {
/*this.entryModule.exportStatements.forEach( statement => {
if ( statement.specifiers.length ) {
// we don't need to include `export { foo }`, it's already handled
return;
@ -170,7 +179,7 @@ export default class Bundle {
}
this.body.push( statement );
});
});*/
// Apply new names and add to the output bundle
this.body.forEach( statement => {

4
src/finalisers/cjs.js

@ -19,7 +19,9 @@ export default function cjs ( bundle, magicString ) {
const exportBlock = keys( bundle.entryModule.exports )
.map( key => {
const specifier = bundle.entryModule.exports[ key ];
return `exports.${key} = ${specifier.localName};`;
const name = bundle.entryModule.getCanonicalName( specifier.localName );
return `exports.${key} = ${name};`;
})
.join( '\n' );

6
test/samples/export-deconflicted-names/_config.js

@ -0,0 +1,6 @@
module.exports = {
description: 'correctly exports deconflicted names',
exports: function ( exports, assert ) {
assert.equal( exports.bar(), 'bar' );
}
};

3
test/samples/export-deconflicted-names/bar.js

@ -0,0 +1,3 @@
export default function bar () {
return 'bar';
}

5
test/samples/export-deconflicted-names/baz.js

@ -0,0 +1,5 @@
var bar = 'baz';
export default function baz () {
return bar;
}

5
test/samples/export-deconflicted-names/foo.js

@ -0,0 +1,5 @@
var bar = 'foo';
export default function foo () {
return bar;
}

5
test/samples/export-deconflicted-names/main.js

@ -0,0 +1,5 @@
import foo from './foo';
import bar from './bar';
import baz from './baz';
export { bar };
Loading…
Cancel
Save