/
amd.ts
102 lines (95 loc) · 2.35 KB
/
amd.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
import { Bundle as MagicStringBundle } from 'magic-string';
import { NormalizedOutputOptions } from '../rollup/types';
import { FinaliserOptions } from './index';
import { getExportBlock, getNamespaceMarkers } from './shared/getExportBlock';
import getInteropBlock from './shared/getInteropBlock';
import removeExtensionFromRelativeAmdId from './shared/removeExtensionFromRelativeAmdId';
import warnOnBuiltins from './shared/warnOnBuiltins';
export default function amd(
magicString: MagicStringBundle,
{
accessedGlobals,
dependencies,
exports,
hasExports,
indentString: t,
intro,
isEntryFacade,
isModuleFacade,
namedExportsMode,
outro,
varOrConst,
warn
}: FinaliserOptions,
{
amd: { define: amdDefine, id: amdId },
compact,
esModule,
externalLiveBindings,
freeze,
interop,
namespaceToStringTag,
strict
}: NormalizedOutputOptions
) {
warnOnBuiltins(warn, dependencies);
const deps = dependencies.map(m => `'${removeExtensionFromRelativeAmdId(m.id)}'`);
const args = dependencies.map(m => m.name);
const n = compact ? '' : '\n';
const s = compact ? '' : ';';
const _ = compact ? '' : ' ';
if (namedExportsMode && hasExports) {
args.unshift(`exports`);
deps.unshift(`'exports'`);
}
if (accessedGlobals.has('require')) {
args.unshift('require');
deps.unshift(`'require'`);
}
if (accessedGlobals.has('module')) {
args.unshift('module');
deps.unshift(`'module'`);
}
const params =
(amdId ? `'${amdId}',${_}` : ``) + (deps.length ? `[${deps.join(`,${_}`)}],${_}` : ``);
const useStrict = strict ? `${_}'use strict';` : '';
magicString.prepend(
`${intro}${getInteropBlock(
dependencies,
varOrConst,
interop,
externalLiveBindings,
freeze,
namespaceToStringTag,
accessedGlobals,
_,
n,
s,
t
)}`
);
const exportBlock = getExportBlock(
exports,
dependencies,
namedExportsMode,
interop,
compact,
t,
externalLiveBindings
);
let namespaceMarkers = getNamespaceMarkers(
namedExportsMode && hasExports,
isEntryFacade && esModule,
isModuleFacade && namespaceToStringTag,
_,
n
);
if (namespaceMarkers) {
namespaceMarkers = n + n + namespaceMarkers;
}
magicString.append(`${exportBlock}${namespaceMarkers}${outro}`);
return magicString
.indent(t)
.prepend(`${amdDefine}(${params}function${_}(${args.join(`,${_}`)})${_}{${useStrict}${n}${n}`)
.append(`${n}${n}});`);
}