forked from rollup/rollup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
amd.ts
111 lines (104 loc) · 2.75 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
103
104
105
106
107
108
109
110
111
import type { Bundle as MagicStringBundle } from 'magic-string';
import type { NormalizedOutputOptions } from '../rollup/types';
import type { FinaliserOptions } from './index';
import getCompleteAmdId from './shared/getCompleteAmdId';
import { getExportBlock, getNamespaceMarkers } from './shared/getExportBlock';
import getInteropBlock from './shared/getInteropBlock';
import updateExtensionForRelativeAmdId from './shared/updateExtensionForRelativeAmdId';
import warnOnBuiltins from './shared/warnOnBuiltins';
export default function amd(
magicString: MagicStringBundle,
{
accessedGlobals,
dependencies,
exports,
hasDefaultExport,
hasExports,
id,
indent: t,
intro,
isEntryFacade,
isModuleFacade,
namedExportsMode,
log,
outro,
snippets
}: FinaliserOptions,
{
amd,
esModule,
externalLiveBindings,
freeze,
generatedCode: { symbols },
interop,
reexportProtoFromExternal,
strict
}: NormalizedOutputOptions
): void {
warnOnBuiltins(log, dependencies);
const deps = dependencies.map(
m => `'${updateExtensionForRelativeAmdId(m.importPath, amd.forceJsExtensionForImports)}'`
);
const parameters = dependencies.map(m => m.name);
const { n, getNonArrowFunctionIntro, _ } = snippets;
if (namedExportsMode && hasExports) {
parameters.unshift(`exports`);
deps.unshift(`'exports'`);
}
if (accessedGlobals.has('require')) {
parameters.unshift('require');
deps.unshift(`'require'`);
}
if (accessedGlobals.has('module')) {
parameters.unshift('module');
deps.unshift(`'module'`);
}
const completeAmdId = getCompleteAmdId(amd, id);
const defineParameters =
(completeAmdId ? `'${completeAmdId}',${_}` : ``) +
(deps.length > 0 ? `[${deps.join(`,${_}`)}],${_}` : ``);
const useStrict = strict ? `${_}'use strict';` : '';
magicString.prepend(
`${intro}${getInteropBlock(
dependencies,
interop,
externalLiveBindings,
freeze,
symbols,
accessedGlobals,
t,
snippets
)}`
);
const exportBlock = getExportBlock(
exports,
dependencies,
namedExportsMode,
interop,
snippets,
t,
externalLiveBindings,
reexportProtoFromExternal
);
let namespaceMarkers = getNamespaceMarkers(
namedExportsMode && hasExports,
isEntryFacade && (esModule === true || (esModule === 'if-default-prop' && hasDefaultExport)),
isModuleFacade && symbols,
snippets
);
if (namespaceMarkers) {
namespaceMarkers = n + n + namespaceMarkers;
}
magicString
.append(`${exportBlock}${namespaceMarkers}${outro}`)
.indent(t)
// factory function should be wrapped by parentheses to avoid lazy parsing,
// cf. https://v8.dev/blog/preparser#pife
.prepend(
`${amd.define}(${defineParameters}(${getNonArrowFunctionIntro(parameters, {
isAsync: false,
name: null
})}{${useStrict}${n}${n}`
)
.append(`${n}${n}}));`);
}