Browse Source

add interop blocks for default imports from external modules

contingency-plan
Rich Harris 10 years ago
parent
commit
9ceb75de91
  1. 9
      src/finalisers/amd.js
  2. 37
      src/finalisers/es6.js
  3. 5
      src/finalisers/iife.js
  4. 6
      src/finalisers/shared/getInteropBlock.js
  5. 5
      src/finalisers/umd.js

9
src/finalisers/amd.js

@ -1,4 +1,5 @@
import { getName, quoteId } from '../utils/map-helpers'; import { getName, quoteId } from '../utils/map-helpers';
import getInteropBlock from './shared/getInteropBlock';
export default function amd ( bundle, magicString, { exportMode, indentString }, options ) { export default function amd ( bundle, magicString, { exportMode, indentString }, options ) {
let deps = bundle.externalModules.map( quoteId ); let deps = bundle.externalModules.map( quoteId );
@ -15,6 +16,10 @@ export default function amd ( bundle, magicString, { exportMode, indentString },
const intro = `define(${params}function (${args.join( ', ' )}) { 'use strict';\n\n`; const intro = `define(${params}function (${args.join( ', ' )}) { 'use strict';\n\n`;
// var foo__default = 'default' in foo ? foo['default'] : foo;
const interopBlock = getInteropBlock( bundle );
if ( interopBlock ) magicString.prepend( interopBlock + '\n\n' );
const exports = bundle.entryModule.exports; const exports = bundle.entryModule.exports;
let exportBlock; let exportBlock;
@ -27,9 +32,7 @@ export default function amd ( bundle, magicString, { exportMode, indentString },
}).join( '\n' ); }).join( '\n' );
} }
if ( exportBlock ) { if ( exportBlock ) magicString.append( '\n\n' + exportBlock );
magicString.append( '\n\n' + exportBlock );
}
return magicString return magicString
.indent( indentString ) .indent( indentString )

37
src/finalisers/es6.js

@ -1,7 +1,42 @@
import { keys } from '../utils/object'; import { keys } from '../utils/object';
export default function es6 ( bundle, magicString, { exportMode }, options ) { export default function es6 ( bundle, magicString, { exportMode }, options ) {
const introBlock = ''; // TODO... const importBlock = bundle.externalModules
.map( module => {
let defaultSpecifier = null;
let namedSpecifiers = null;
if ( module.needsDefault ) {
const defaultImportDeclaration = module.importedByBundle.filter( declaration => declaration.name === 'default' )[0];
defaultSpecifier = defaultImportDeclaration.localName;
}
if ( module.needsNamed ) {
namedSpecifiers = '{ ' + module.importedByBundle
.filter( declaration => declaration.name !== 'default' )
.map( declaration => {
const { name, localName } = declaration;
return name === localName ?
name :
`${name} as ${localName}`;
})
.join( ', ' ) + ' }';
}
const specifiers = module.needsDefault && module.needsNamed ?
`${defaultSpecifier}, ${namedSpecifiers}` :
( defaultSpecifier || namedSpecifiers );
return specifiers ?
`import ${specifiers} from '${module.id}';` :
`import '${module.id}';`;
})
.join( '\n' );
if ( importBlock ) {
magicString.prepend( importBlock + '\n\n' );
}
const exports = bundle.entryModule.exports; const exports = bundle.entryModule.exports;
const exportBlock = keys( exports ).map( exportedName => { const exportBlock = keys( exports ).map( exportedName => {

5
src/finalisers/iife.js

@ -1,5 +1,6 @@
import { blank } from '../utils/object'; import { blank } from '../utils/object';
import { getName } from '../utils/map-helpers'; import { getName } from '../utils/map-helpers';
import getInteropBlock from './shared/getInteropBlock';
export default function iife ( bundle, magicString, { exportMode, indentString }, options ) { export default function iife ( bundle, magicString, { exportMode, indentString }, options ) {
const globalNames = options.globals || blank(); const globalNames = options.globals || blank();
@ -22,6 +23,10 @@ export default function iife ( bundle, magicString, { exportMode, indentString }
let intro = `(function (${args}) { 'use strict';\n\n`; let intro = `(function (${args}) { 'use strict';\n\n`;
let outro = `\n\n})(${dependencies});`; let outro = `\n\n})(${dependencies});`;
// var foo__default = 'default' in foo ? foo['default'] : foo;
const interopBlock = getInteropBlock( bundle );
if ( interopBlock ) magicString.prepend( interopBlock + '\n\n' );
if ( exportMode === 'default' ) { if ( exportMode === 'default' ) {
intro = `var ${options.moduleName} = ${intro}`; intro = `var ${options.moduleName} = ${intro}`;
magicString.append( `\n\nreturn ${bundle.entryModule.getCanonicalName('default')};` ); magicString.append( `\n\nreturn ${bundle.entryModule.getCanonicalName('default')};` );

6
src/finalisers/shared/getInteropBlock.js

@ -0,0 +1,6 @@
export default function getInteropBlock ( bundle ) {
return bundle.externalModules
.filter( module => module.needsDefault && module.needsNamed )
.map( module => `var ${module.name}__default = 'default' in ${module.name} ? ${module.name}['default'] : ${module.name};` )
.join( '\n' );
}

5
src/finalisers/umd.js

@ -1,5 +1,6 @@
import { blank } from '../utils/object'; import { blank } from '../utils/object';
import { getName, quoteId, req } from '../utils/map-helpers'; import { getName, quoteId, req } from '../utils/map-helpers';
import getInteropBlock from './shared/getInteropBlock';
export default function umd ( bundle, magicString, { exportMode, indentString }, options ) { export default function umd ( bundle, magicString, { exportMode, indentString }, options ) {
if ( exportMode !== 'none' && !options.moduleName ) { if ( exportMode !== 'none' && !options.moduleName ) {
@ -40,6 +41,10 @@ export default function umd ( bundle, magicString, { exportMode, indentString },
`.replace( /^\t\t/gm, '' ).replace( /^\t/gm, magicString.getIndentString() ); `.replace( /^\t\t/gm, '' ).replace( /^\t/gm, magicString.getIndentString() );
// var foo__default = 'default' in foo ? foo['default'] : foo;
const interopBlock = getInteropBlock( bundle );
if ( interopBlock ) magicString.prepend( interopBlock + '\n\n' );
const exports = bundle.entryModule.exports; const exports = bundle.entryModule.exports;
let exportBlock; let exportBlock;

Loading…
Cancel
Save