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