mirror of https://github.com/lukechilds/rollup.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
59 lines
2.1 KiB
59 lines
2.1 KiB
import { blank } from '../utils/object';
|
|
import { getName, quoteId, req } from '../utils/map-helpers';
|
|
import getInteropBlock from './shared/getInteropBlock';
|
|
import getExportBlock from './shared/getExportBlock';
|
|
|
|
export default function umd ( bundle, magicString, { exportMode, indentString }, options ) {
|
|
if ( exportMode !== 'none' && !options.moduleName ) {
|
|
throw new Error( 'You must supply options.moduleName for UMD bundles' );
|
|
}
|
|
|
|
const globalNames = options.globals || blank();
|
|
|
|
let amdDeps = bundle.externalModules.map( quoteId );
|
|
let cjsDeps = bundle.externalModules.map( req );
|
|
let globalDeps = bundle.externalModules.map( module => {
|
|
return 'global.' + (globalNames[ module.id ] || module.name);
|
|
});
|
|
|
|
let args = bundle.externalModules.map( getName );
|
|
|
|
if ( exportMode === 'named' ) {
|
|
amdDeps.unshift( `'exports'` );
|
|
cjsDeps.unshift( `exports` );
|
|
globalDeps.unshift( `(global.${options.moduleName} = {})` );
|
|
|
|
args.unshift( 'exports' );
|
|
}
|
|
|
|
const amdParams =
|
|
( options.moduleId ? `'${options.moduleId}', ` : `` ) +
|
|
( amdDeps.length ? `[${amdDeps.join( ', ' )}], ` : `` );
|
|
|
|
const cjsExport = exportMode === 'default' ? `module.exports = ` : ``;
|
|
const defaultExport = exportMode === 'default' ? `global.${options.moduleName} = ` : '';
|
|
|
|
const useStrict = options.useStrict !== false ? ` 'use strict';` : ``;
|
|
|
|
const intro =
|
|
`(function (global, factory) {
|
|
typeof exports === 'object' && typeof module !== 'undefined' ? ${cjsExport}factory(${cjsDeps.join( ', ' )}) :
|
|
typeof define === 'function' && define.amd ? define(${amdParams}factory) :
|
|
${defaultExport}factory(${globalDeps});
|
|
}(this, function (${args}) {${useStrict}
|
|
|
|
`.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 exportBlock = getExportBlock( bundle, exportMode );
|
|
if ( exportBlock ) magicString.append( '\n\n' + exportBlock );
|
|
|
|
return magicString
|
|
.trim()
|
|
.indent( indentString )
|
|
.append( '\n\n}));' )
|
|
.prepend( intro );
|
|
}
|
|
|