diff --git a/src/finalisers/amd.js b/src/finalisers/amd.js index dd86c57..02a947b 100644 --- a/src/finalisers/amd.js +++ b/src/finalisers/amd.js @@ -1,4 +1,5 @@ import { getName, quoteId } from '../utils/map-helpers'; +import getInteropBlock from './shared/getInteropBlock'; export default function amd ( bundle, magicString, { exportMode, indentString }, options ) { 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`; + // 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; let exportBlock; @@ -27,9 +32,7 @@ export default function amd ( bundle, magicString, { exportMode, indentString }, }).join( '\n' ); } - if ( exportBlock ) { - magicString.append( '\n\n' + exportBlock ); - } + if ( exportBlock ) magicString.append( '\n\n' + exportBlock ); return magicString .indent( indentString ) diff --git a/src/finalisers/es6.js b/src/finalisers/es6.js index 1bf26d3..f785e9e 100644 --- a/src/finalisers/es6.js +++ b/src/finalisers/es6.js @@ -1,7 +1,42 @@ import { keys } from '../utils/object'; 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 exportBlock = keys( exports ).map( exportedName => { diff --git a/src/finalisers/iife.js b/src/finalisers/iife.js index 98a5ff3..dd6b6d0 100644 --- a/src/finalisers/iife.js +++ b/src/finalisers/iife.js @@ -1,5 +1,6 @@ import { blank } from '../utils/object'; import { getName } from '../utils/map-helpers'; +import getInteropBlock from './shared/getInteropBlock'; export default function iife ( bundle, magicString, { exportMode, indentString }, options ) { 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 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' ) { intro = `var ${options.moduleName} = ${intro}`; magicString.append( `\n\nreturn ${bundle.entryModule.getCanonicalName('default')};` ); diff --git a/src/finalisers/shared/getInteropBlock.js b/src/finalisers/shared/getInteropBlock.js new file mode 100644 index 0000000..2fadc3a --- /dev/null +++ b/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' ); +} diff --git a/src/finalisers/umd.js b/src/finalisers/umd.js index 61696db..8dc20ff 100644 --- a/src/finalisers/umd.js +++ b/src/finalisers/umd.js @@ -1,5 +1,6 @@ import { blank } from '../utils/object'; import { getName, quoteId, req } from '../utils/map-helpers'; +import getInteropBlock from './shared/getInteropBlock'; export default function umd ( bundle, magicString, { exportMode, indentString }, options ) { 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() ); + // 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; let exportBlock;