|
|
@ -26,23 +26,36 @@ export default function umd ( bundle, magicString, exportMode, options ) { |
|
|
|
( has( options, 'moduleId' ) ? `['${options.moduleId}'], ` : `` ) + |
|
|
|
( amdDeps.length ? `[${amdDeps.join( ', ' )}], ` : `` ); |
|
|
|
|
|
|
|
const defaultExport = exportMode === 'default' ? `global.${options.moduleName} = ` : ''; |
|
|
|
|
|
|
|
const intro = |
|
|
|
`(function (global, factory) {
|
|
|
|
typeof exports === 'object' && typeof module !== 'undefined' ? factory(${cjsDeps.join( ', ' )}) : |
|
|
|
typeof define === 'function' && define.amd ? define(${amdParams}factory) : |
|
|
|
factory(${globalDeps}); |
|
|
|
${defaultExport}factory(${globalDeps}); |
|
|
|
}(this, function (${args}) { 'use strict'; |
|
|
|
|
|
|
|
`.replace( /^\t\t/gm, '' ).replace( /^\t/gm, indentStr );
|
|
|
|
|
|
|
|
const exports = bundle.entryModule.exports; |
|
|
|
|
|
|
|
const exportBlock = '\n\n' + Object.keys( exports ).map( name => { |
|
|
|
return `exports.${name} = ${exports[name].localName};`; |
|
|
|
let exportBlock; |
|
|
|
|
|
|
|
if ( exportMode === 'default' ) { |
|
|
|
const canonicalName = bundle.entryModule.getCanonicalName( 'default' ); |
|
|
|
exportBlock = `return ${canonicalName};`; |
|
|
|
} else { |
|
|
|
exportBlock = Object.keys( exports ).map( name => { |
|
|
|
const canonicalName = bundle.entryModule.getCanonicalName( exports[ name ].localName ); |
|
|
|
return `exports.${name} = ${canonicalName};`; |
|
|
|
}).join( '\n' ); |
|
|
|
} |
|
|
|
|
|
|
|
if ( exportBlock ) { |
|
|
|
magicString.append( '\n\n' + exportBlock ); |
|
|
|
} |
|
|
|
|
|
|
|
return magicString |
|
|
|
.append( exportBlock ) |
|
|
|
.trim() |
|
|
|
.indent() |
|
|
|
.append( '\n\n}));' ) |
|
|
|