/
cjs.ts
104 lines (93 loc) · 2.96 KB
/
cjs.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import { Bundle as MagicStringBundle } from 'magic-string';
import { OutputOptions } from '../rollup/types';
import { INTEROP_DEFAULT_VARIABLE, INTEROP_NAMESPACE_VARIABLE } from '../utils/variableNames';
import { FinaliserOptions } from './index';
import { compactEsModuleExport, esModuleExport } from './shared/esModuleExport';
import getExportBlock from './shared/getExportBlock';
import { getInteropNamespace } from './shared/getInteropNamespace';
export default function cjs(
magicString: MagicStringBundle,
{
accessedGlobals,
dependencies,
exports,
hasExports,
indentString: t,
intro,
isEntryModuleFacade,
namedExportsMode,
outro,
varOrConst
}: FinaliserOptions,
options: OutputOptions
) {
const n = options.compact ? '' : '\n';
const _ = options.compact ? '' : ' ';
intro =
(options.strict === false ? intro : `'use strict';${n}${n}${intro}`) +
(namedExportsMode && hasExports && isEntryModuleFacade && options.esModule
? `${options.compact ? compactEsModuleExport : esModuleExport}${n}${n}`
: '');
let needsInterop = false;
const interop = options.interop !== false;
let importBlock: string;
let definingVariable = false;
importBlock = '';
for (const {
id,
namedExportsMode,
isChunk,
name,
reexports,
imports,
exportsNames,
exportsDefault
} of dependencies) {
if (!reexports && !imports) {
if (importBlock) {
importBlock += !options.compact || definingVariable ? `;${n}` : ',';
}
definingVariable = false;
importBlock += `require('${id}')`;
} else {
importBlock +=
options.compact && definingVariable ? ',' : `${importBlock ? `;${n}` : ''}${varOrConst} `;
definingVariable = true;
if (!interop || isChunk || !exportsDefault || !namedExportsMode) {
importBlock += `${name}${_}=${_}require('${id}')`;
} else {
needsInterop = true;
if (exportsNames)
importBlock += `${name}${_}=${_}require('${id}')${
options.compact ? ',' : `;\n${varOrConst} `
}${name}__default${_}=${_}${INTEROP_DEFAULT_VARIABLE}(${name})`;
else importBlock += `${name}${_}=${_}${INTEROP_DEFAULT_VARIABLE}(require('${id}'))`;
}
}
}
if (importBlock) importBlock += ';';
if (needsInterop) {
const ex = options.compact ? 'e' : 'ex';
intro +=
`function ${INTEROP_DEFAULT_VARIABLE}${_}(${ex})${_}{${_}return${_}` +
`(${ex}${_}&&${_}(typeof ${ex}${_}===${_}'object')${_}&&${_}'default'${_}in ${ex})${_}` +
`?${_}${ex}['default']${_}:${_}${ex}${options.compact ? '' : '; '}}${n}${n}`;
}
if (accessedGlobals.has(INTEROP_NAMESPACE_VARIABLE)) {
intro += getInteropNamespace(_, n, t, options.externalLiveBindings !== false);
}
if (importBlock) intro += importBlock + n + n;
const exportBlock = getExportBlock(
exports,
dependencies,
namedExportsMode,
options.interop as boolean,
options.compact as boolean,
t,
`module.exports${_}=${_}`
);
magicString.prepend(intro);
if (exportBlock) magicString.append(n + n + exportBlock);
if (outro) magicString.append(outro);
return magicString;
}