Browse Source

import defaults from external modules, with an interop layer

contingency-plan
Rich Harris 10 years ago
parent
commit
859d294b71
  1. 5
      src/ExternalModule.js
  2. 6
      src/Module.js
  3. 11
      src/finalisers/cjs.js
  4. 3
      test/samples/allows-external-modules/_config.js
  5. 3
      test/samples/import-default-from-external/_config.js
  6. 7
      test/samples/import-default-from-external/main.js
  7. 3
      test/samples/import-named-from-external/_config.js
  8. 0
      test/samples/import-named-from-external/main.js

5
src/ExternalModule.js

@ -8,11 +8,14 @@ export default class ExternalModule {
this.canonicalNames = {}; this.canonicalNames = {};
this.defaultExportName = null; this.defaultExportName = null;
this.needsDefault = null;
this.needsNamed = null;
} }
getCanonicalName ( name ) { getCanonicalName ( name ) {
if ( name === 'default' ) { if ( name === 'default' ) {
return `${this.name}__default`; // TODO... return this.needsNamed ? `${this.name}__default` : this.name;
} }
if ( name === '*' ) { if ( name === '*' ) {

6
src/Module.js

@ -190,6 +190,12 @@ export default class Module {
} }
if ( module.isExternal ) { if ( module.isExternal ) {
if ( importDeclaration.name === 'default' ) {
module.needsDefault = true;
} else {
module.needsNamed = true;
}
module.importedByBundle.push( importDeclaration ); module.importedByBundle.push( importDeclaration );
return emptyArrayPromise; return emptyArrayPromise;
} }

11
src/finalisers/cjs.js

@ -6,7 +6,16 @@ export default function cjs ( bundle, magicString ) {
// TODO handle ambiguous default imports // TODO handle ambiguous default imports
// TODO handle empty imports, once they're supported // TODO handle empty imports, once they're supported
const importBlock = bundle.externalModules const importBlock = bundle.externalModules
.map( module => `var ${module.name} = require('${module.id}');` ) .map( module => {
let requireStatement = `var ${module.name} = require('${module.id}');`;
if ( module.needsDefault ) {
requireStatement += '\n' + ( module.needsNamed ? `var ${module.name}__default = ` : `${module.name} = ` ) +
`'default' in ${module.name} ? ${module.name}['default'] : ${module.name};`;
}
return requireStatement;
})
.join( '\n' ); .join( '\n' );
if ( importBlock ) { if ( importBlock ) {

3
test/samples/allows-external-modules/_config.js

@ -1,3 +0,0 @@
module.exports = {
description: 'Non-absolute and non-relative modules are assumed to be external'
};

3
test/samples/import-default-from-external/_config.js

@ -0,0 +1,3 @@
module.exports = {
description: 'imports default from external module'
};

7
test/samples/import-default-from-external/main.js

@ -0,0 +1,7 @@
// would be a namespace import in real life, obvs
import path from 'path';
var path1 = 'foo/bar/baz';
var path2 = 'foo/baz/bar';
assert.equal( path.relative( path1, path2 ), '../../baz/bar' );

3
test/samples/import-named-from-external/_config.js

@ -0,0 +1,3 @@
module.exports = {
description: 'imports names from an external module'
};

0
test/samples/allows-external-modules/main.js → test/samples/import-named-from-external/main.js

Loading…
Cancel
Save