UNPKG

1.16 kBJavaScriptView Raw
1import { getName, quoteId } from '../utils/map-helpers.js';
2import getInteropBlock from './shared/getInteropBlock.js';
3import getExportBlock from './shared/getExportBlock.js';
4
5export default function amd ( bundle, magicString, { exportMode, indentString }, options ) {
6 let deps = bundle.externalModules.map( quoteId );
7 let args = bundle.externalModules.map( getName );
8
9 if ( exportMode === 'named' ) {
10 args.unshift( `exports` );
11 deps.unshift( `'exports'` );
12 }
13
14 const params =
15 ( options.moduleId ? `'${options.moduleId}', ` : `` ) +
16 ( deps.length ? `[${deps.join( ', ' )}], ` : `` );
17
18 const useStrict = options.useStrict !== false ? ` 'use strict';` : ``;
19 const intro = `define(${params}function (${args.join( ', ' )}) {${useStrict}\n\n`;
20
21 // var foo__default = 'default' in foo ? foo['default'] : foo;
22 const interopBlock = getInteropBlock( bundle );
23 if ( interopBlock ) magicString.prepend( interopBlock + '\n\n' );
24
25 const exportBlock = getExportBlock( bundle.entryModule, exportMode );
26 if ( exportBlock ) magicString.append( '\n\n' + exportBlock );
27
28 return magicString
29 .indent( indentString )
30 .append( '\n\n});' )
31 .prepend( intro );
32}