Skip to content

Commit

Permalink
fix external namespace imports in es output (#1511)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Aug 11, 2017
1 parent 8e7cbe0 commit 6ba6440
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 8 deletions.
6 changes: 1 addition & 5 deletions src/finalisers/es.js
Expand Up @@ -6,10 +6,6 @@ function notDefault ( name ) {

export default function es ( bundle, magicString, { intro, outro } ) {
const importBlock = bundle.externalModules
.filter( module => {
const imported = keys( module.declarations );
return imported.length !== 1 || imported[0][0] !== '*';
})
.map( module => {
const specifiers = [];
const specifiersList = [specifiers];
Expand All @@ -36,7 +32,7 @@ export default function es ( bundle, magicString, { intro, outro } ) {
}
}

const namespaceSpecifier = module.declarations['*'] ? `* as ${module.name}` : null; // TODO prevent unnecessary namespace import, e.g form/external-imports
const namespaceSpecifier = module.declarations['*'] && module.declarations['*'].activated ? `* as ${module.name}` : null; // TODO prevent unnecessary namespace import, e.g form/external-imports
const namedSpecifier = importedNames.length ? `{ ${importedNames.sort().join( ', ' )} }` : null;

if ( namespaceSpecifier && namedSpecifier ) {
Expand Down
1 change: 0 additions & 1 deletion test/form/samples/external-imports/_expected/es.js
@@ -1,7 +1,6 @@
import factory from 'factory';
import { bar, foo } from 'baz';
import { forEach, port } from 'shipping-port';
import * as containers from 'shipping-port';
import alphabet, { a } from 'alphabet';

factory( null );
Expand Down
@@ -1,6 +1,5 @@
import { bar } from 'foo';
import foo__default from 'foo';
import * as foo from 'foo';

console.log( bar );

Expand Down
5 changes: 5 additions & 0 deletions test/form/samples/import-namespace/_config.js
@@ -0,0 +1,5 @@
module.exports = {
options: {
external: ['foo', 'bar']
}
};
6 changes: 6 additions & 0 deletions test/form/samples/import-namespace/_expected/amd.js
@@ -0,0 +1,6 @@
define(['foo', 'bar'], function (foo, bar) { 'use strict';

foo.x();
console.log(bar);

});
7 changes: 7 additions & 0 deletions test/form/samples/import-namespace/_expected/cjs.js
@@ -0,0 +1,7 @@
'use strict';

var foo = require('foo');
var bar = require('bar');

foo.x();
console.log(bar);
5 changes: 5 additions & 0 deletions test/form/samples/import-namespace/_expected/es.js
@@ -0,0 +1,5 @@
import { x } from 'foo';
import * as bar from 'bar';

x();
console.log(bar);
7 changes: 7 additions & 0 deletions test/form/samples/import-namespace/_expected/iife.js
@@ -0,0 +1,7 @@
(function (foo,bar) {
'use strict';

foo.x();
console.log(bar);

}(foo,bar));
10 changes: 10 additions & 0 deletions test/form/samples/import-namespace/_expected/umd.js
@@ -0,0 +1,10 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(require('foo'), require('bar')) :
typeof define === 'function' && define.amd ? define(['foo', 'bar'], factory) :
(factory(global.foo,global.bar));
}(this, (function (foo,bar) { 'use strict';

foo.x();
console.log(bar);

})));
5 changes: 5 additions & 0 deletions test/form/samples/import-namespace/main.js
@@ -0,0 +1,5 @@
import * as foo from 'foo';
import * as bar from 'bar';

foo.x();
console.log(bar);
1 change: 0 additions & 1 deletion test/form/samples/no-treeshake/_expected/es.js
@@ -1,5 +1,4 @@
import { value } from 'external';
import * as external from 'external';

var foo = 'unused';

Expand Down

0 comments on commit 6ba6440

Please sign in to comment.