diff --git a/src/Bundle.js b/src/Bundle.js index accd9ae..c6a2386 100644 --- a/src/Bundle.js +++ b/src/Bundle.js @@ -63,7 +63,7 @@ export default class Bundle { this.onwarn = options.onwarn || makeOnwarn(); // TODO strictly speaking, this only applies with non-ES6, non-default-only bundles - [ 'module', 'exports' ].forEach( global => this.assumedGlobals[ global ] = true ); + [ 'module', 'exports', '_interopDefault' ].forEach( global => this.assumedGlobals[ global ] = true ); } build () { diff --git a/src/finalisers/cjs.js b/src/finalisers/cjs.js index 4651409..c71f59d 100644 --- a/src/finalisers/cjs.js +++ b/src/finalisers/cjs.js @@ -6,18 +6,19 @@ export default function cjs ( bundle, magicString, { exportMode }, options ) { const hasDefaultImport = bundle.externalModules.some( mod => mod.declarations.default); if (hasDefaultImport) { - intro += `function _interopRequire (id) { var ex = require(id); return 'default' in ex ? ex['default'] : ex; }\n\n`; + intro += `function _interopDefault (ex) { return 'default' in ex ? ex['default'] : ex; }\n\n`; } // TODO handle empty imports, once they're supported const importBlock = bundle.externalModules .map( module => { if ( module.declarations.default ) { - let importStatement = `var ${module.name} = _interopRequire('${module.id}');`; if (module.exportsNames) { - importStatement += `\nvar ${module.name}__default = ${module.name};`; + return `var ${module.name} = require('${module.id}');` + + `\nvar ${module.name}__default = _interopDefault(${module.name});`; + } else { + return `var ${module.name} = _interopDefault(require('${module.id}'));`; } - return importStatement; } else { return `var ${module.name} = require('${module.id}');`; } diff --git a/test/form/external-imports-custom-names/_expected/cjs.js b/test/form/external-imports-custom-names/_expected/cjs.js index 7c31c60..8f6506e 100644 --- a/test/form/external-imports-custom-names/_expected/cjs.js +++ b/test/form/external-imports-custom-names/_expected/cjs.js @@ -1,9 +1,9 @@ 'use strict'; -function _interopRequire (id) { var ex = require(id); return 'default' in ex ? ex['default'] : ex; } +function _interopDefault (ex) { return 'default' in ex ? ex['default'] : ex; } -var $ = _interopRequire('jquery'); +var $ = _interopDefault(require('jquery')); $( function () { $( 'body' ).html( '