UNPKG

964 BJavaScriptView Raw
1import getExportBlock from './shared/getExportBlock.js';
2
3export default function cjs ( bundle, magicString, { exportMode }, options ) {
4 let intro = options.useStrict === false ? `` : `'use strict';\n\n`;
5
6 // TODO handle empty imports, once they're supported
7 const importBlock = bundle.externalModules
8 .map( module => {
9 let requireStatement = `var ${module.name} = require('${module.id}');`;
10
11 if ( module.declarations.default ) {
12 requireStatement += '\n' + ( module.exportsNames ? `var ${module.name}__default = ` : `${module.name} = ` ) +
13 `'default' in ${module.name} ? ${module.name}['default'] : ${module.name};`;
14 }
15
16 return requireStatement;
17 })
18 .join( '\n' );
19
20 if ( importBlock ) {
21 intro += importBlock + '\n\n';
22 }
23
24 magicString.prepend( intro );
25
26 const exportBlock = getExportBlock( bundle.entryModule, exportMode, 'module.exports =' );
27 if ( exportBlock ) magicString.append( '\n\n' + exportBlock );
28
29 return magicString;
30}