Browse Source

distinguish between default and namespace imports from external module (#637)

ghi-672
Rich-Harris 9 years ago
parent
commit
558b4cf355
  1. 2
      src/Declaration.js
  2. 5
      src/ExternalModule.js
  3. 26
      src/finalisers/cjs.js
  4. 4
      src/finalisers/es6.js
  5. 16
      src/finalisers/shared/getInteropBlock.js
  6. 3
      test/form/import-external-namespace-and-default/_config.js
  7. 9
      test/form/import-external-namespace-and-default/_expected/amd.js
  8. 8
      test/form/import-external-namespace-and-default/_expected/cjs.js
  9. 6
      test/form/import-external-namespace-and-default/_expected/es6.js
  10. 10
      test/form/import-external-namespace-and-default/_expected/iife.js
  11. 13
      test/form/import-external-namespace-and-default/_expected/umd.js
  12. 4
      test/form/import-external-namespace-and-default/main.js
  13. 3
      test/form/import-external-namespace-and-default/other.js

2
src/Declaration.js

@ -282,7 +282,7 @@ export class ExternalDeclaration {
}
if ( this.name === 'default' ) {
return !es6 && this.module.exportsNames ?
return this.module.exportsNamespace || ( !es6 && this.module.exportsNames ) ?
`${this.module.name}__default` :
this.module.name;
}

5
src/ExternalModule.js

@ -27,9 +27,8 @@ export default class ExternalModule {
}
traceExport ( name ) {
if ( name !== 'default' && name !== '*' ) {
this.exportsNames = true;
}
if ( name !== 'default' && name !== '*' ) this.exportsNames = true;
if ( name === '*' ) this.exportsNamespace = true;
return this.declarations[ name ] || (
this.declarations[ name ] = new ExternalDeclaration( this, name )

26
src/finalisers/cjs.js

@ -3,11 +3,12 @@ import getExportBlock from './shared/getExportBlock.js';
export default function cjs ( bundle, magicString, { exportMode }, options ) {
let intro = options.useStrict === false ? `` : `'use strict';\n\n`;
const hasDefaultImport = bundle.externalModules.some( mod => mod.declarations.default);
let needsInterop = false;
// const hasDefaultImport = bundle.externalModules.some( mod => mod.declarations.default);
if (hasDefaultImport) {
intro += `function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }\n\n`;
}
// if (hasDefaultImport) {
// intro += `function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }\n\n`;
// }
const varOrConst = bundle.varOrConst;
@ -15,18 +16,29 @@ export default function cjs ( bundle, magicString, { exportMode }, options ) {
const importBlock = bundle.externalModules
.map( module => {
if ( module.declarations.default ) {
if (module.exportsNames) {
if ( module.exportsNamespace ) {
return `${varOrConst} ${module.name} = require('${module.id}');` +
`\n${varOrConst} ${module.name}__default = ${module.name}['default'];`;
}
needsInterop = true;
if ( module.exportsNames ) {
return `${varOrConst} ${module.name} = require('${module.id}');` +
`\n${varOrConst} ${module.name}__default = _interopDefault(${module.name});`;
} else {
return `${varOrConst} ${module.name} = _interopDefault(require('${module.id}'));`;
}
return `${varOrConst} ${module.name} = _interopDefault(require('${module.id}'));`;
} else {
return `${varOrConst} ${module.name} = require('${module.id}');`;
}
})
.join( '\n' );
if ( needsInterop ) {
intro += `function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }\n\n`;
}
if ( importBlock ) {
intro += importBlock + '\n\n';
}

4
src/finalisers/es6.js

@ -19,8 +19,12 @@ export default function es6 ( bundle, magicString ) {
});
if ( module.declarations.default ) {
if ( module.exportsNamespace ) {
specifiersList.push([ `${module.name}__default` ]);
} else {
specifiers.push( module.name );
}
}
const namespaceSpecifier = module.declarations['*'] ? `* as ${module.name}` : null;
const namedSpecifier = importedNames.length ? `{ ${importedNames.join( ', ' )} }` : null;

16
src/finalisers/shared/getInteropBlock.js

@ -1,11 +1,17 @@
export default function getInteropBlock ( bundle ) {
return bundle.externalModules
.map( module => {
return module.declarations.default ?
( module.exportsNames ?
`${bundle.varOrConst} ${module.name}__default = 'default' in ${module.name} ? ${module.name}['default'] : ${module.name};` :
`${module.name} = 'default' in ${module.name} ? ${module.name}['default'] : ${module.name};` ) :
null;
if ( !module.declarations.default ) return null;
if ( module.exportsNamespace ) {
return `${bundle.varOrConst} ${module.name}__default = ${module.name}['default'];`;
}
if ( module.exportsNames ) {
return `${bundle.varOrConst} ${module.name}__default = 'default' in ${module.name} ? ${module.name}['default'] : ${module.name};`;
}
return `${module.name} = 'default' in ${module.name} ? ${module.name}['default'] : ${module.name};`;
})
.filter( Boolean )
.join( '\n' );

3
test/form/import-external-namespace-and-default/_config.js

@ -0,0 +1,3 @@
module.exports = {
description: 'disinguishes between external default and namespace (#637)'
};

9
test/form/import-external-namespace-and-default/_expected/amd.js

@ -0,0 +1,9 @@
define(['foo'], function (foo) { 'use strict';
var foo__default = foo['default'];
console.log( foo.bar );
console.log( foo__default );
});

8
test/form/import-external-namespace-and-default/_expected/cjs.js

@ -0,0 +1,8 @@
'use strict';
var foo = require('foo');
var foo__default = foo['default'];
console.log( foo.bar );
console.log( foo__default );

6
test/form/import-external-namespace-and-default/_expected/es6.js

@ -0,0 +1,6 @@
import * as foo from 'foo';
import foo__default from 'foo';
console.log( foo.bar );
console.log( foo__default );

10
test/form/import-external-namespace-and-default/_expected/iife.js

@ -0,0 +1,10 @@
(function (foo) {
'use strict';
var foo__default = foo['default'];
console.log( foo.bar );
console.log( foo__default );
}(foo));

13
test/form/import-external-namespace-and-default/_expected/umd.js

@ -0,0 +1,13 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(require('foo')) :
typeof define === 'function' && define.amd ? define(['foo'], factory) :
(factory(global.foo));
}(this, function (foo) { 'use strict';
var foo__default = foo['default'];
console.log( foo.bar );
console.log( foo__default );
}));

4
test/form/import-external-namespace-and-default/main.js

@ -0,0 +1,4 @@
import foo from 'foo';
import './other.js';
console.log( foo );

3
test/form/import-external-namespace-and-default/other.js

@ -0,0 +1,3 @@
import * as foo from 'foo';
console.log( foo.bar );
Loading…
Cancel
Save