Browse Source

Merge pull request #356 from rollup/fix-generated-namespace-imports

Ensure namespace imports are not combined with other import types.
gh-384
Rich Harris 9 years ago
parent
commit
b33f14f2b0
  1. 26
      src/finalisers/es6.js
  2. 2
      test/form/external-imports/_expected/amd.js
  3. 2
      test/form/external-imports/_expected/cjs.js
  4. 3
      test/form/external-imports/_expected/es6.js
  5. 2
      test/form/external-imports/_expected/iife.js
  6. 2
      test/form/external-imports/_expected/umd.js
  7. 3
      test/form/external-imports/main.js

26
src/finalisers/es6.js

@ -8,6 +8,7 @@ export default function es6 ( bundle, magicString ) {
const importBlock = bundle.externalModules
.map( module => {
const specifiers = [];
const specifiersList = [specifiers];
const importedNames = keys( module.declarations )
.filter( name => name !== '*' && name !== 'default' );
@ -15,17 +16,26 @@ export default function es6 ( bundle, magicString ) {
specifiers.push( module.name );
}
if ( module.declarations['*'] ) {
specifiers.push( `* as ${module.name}` );
}
const namespaceSpecifier = module.declarations['*'] ? `* as ${module.name}` : null;
const namedSpecifier = importedNames.length ? `{ ${importedNames.join( ', ' )} }` : null;
if ( importedNames.length ) {
specifiers.push( `{ ${importedNames.join( ', ' )} }` );
if ( namespaceSpecifier && namedSpecifier ) {
// Namespace and named specifiers cannot be combined.
specifiersList.push( [namespaceSpecifier] );
specifiers.push( namedSpecifier );
} else if ( namedSpecifier ) {
specifiers.push( namedSpecifier );
} else if ( namespaceSpecifier ) {
specifiers.push( namespaceSpecifier );
}
return specifiers.length ?
`import ${specifiers.join( ', ' )} from '${module.id}';` :
`import '${module.id}';`;
return specifiersList
.map( specifiers =>
specifiers.length ?
`import ${specifiers.join( ', ' )} from '${module.id}';` :
`import '${module.id}';`
)
.join( '\n' );
})
.join( '\n' );

2
test/form/external-imports/_expected/amd.js

@ -4,7 +4,7 @@ define(['factory', 'baz', 'shipping-port', 'alphabet'], function (factory, baz,
var alphabet__default = 'default' in alphabet ? alphabet['default'] : alphabet;
factory( null );
baz.foo( baz.bar );
baz.foo( baz.bar, containers.port );
containers.forEach( console.log, console );
console.log( alphabet.a );
console.log( alphabet__default.length );

2
test/form/external-imports/_expected/cjs.js

@ -8,7 +8,7 @@ var alphabet = require('alphabet');
var alphabet__default = 'default' in alphabet ? alphabet['default'] : alphabet;
factory( null );
baz.foo( baz.bar );
baz.foo( baz.bar, containers.port );
containers.forEach( console.log, console );
console.log( alphabet.a );
console.log( alphabet__default.length );

3
test/form/external-imports/_expected/es6.js

@ -1,10 +1,11 @@
import factory from 'factory';
import { bar, foo } from 'baz';
import { port } from 'shipping-port';
import * as containers from 'shipping-port';
import alphabet, { a } from 'alphabet';
factory( null );
foo( bar );
foo( bar, port );
containers.forEach( console.log, console );
console.log( a );
console.log( alphabet.length );

2
test/form/external-imports/_expected/iife.js

@ -4,7 +4,7 @@
var alphabet__default = 'default' in alphabet ? alphabet['default'] : alphabet;
factory( null );
baz.foo( baz.bar );
baz.foo( baz.bar, containers.port );
containers.forEach( console.log, console );
console.log( alphabet.a );
console.log( alphabet__default.length );

2
test/form/external-imports/_expected/umd.js

@ -8,7 +8,7 @@
var alphabet__default = 'default' in alphabet ? alphabet['default'] : alphabet;
factory( null );
baz.foo( baz.bar );
baz.foo( baz.bar, containers.port );
containers.forEach( console.log, console );
console.log( alphabet.a );
console.log( alphabet__default.length );

3
test/form/external-imports/main.js

@ -1,10 +1,11 @@
import factory from 'factory';
import { foo, bar } from 'baz';
import * as containers from 'shipping-port';
import { port } from 'shipping-port';
import alphabet, { a, b } from 'alphabet';
factory( null );
foo( bar );
foo( bar, port );
containers.forEach( console.log, console );
console.log( a );
console.log( alphabet.length );

Loading…
Cancel
Save