-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathiife.ts
137 lines (127 loc) · 3.43 KB
/
iife.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import type { Bundle as MagicStringBundle } from 'magic-string';
import type { NormalizedOutputOptions } from '../rollup/types';
import { isLegal } from '../utils/identifierHelpers';
import { LOGLEVEL_WARN } from '../utils/logging';
import {
error,
logIllegalIdentifierAsName,
logMissingNameOptionForIifeExport
} from '../utils/logs';
import type { FinaliserOptions } from './index';
import { getExportBlock, getNamespaceMarkers } from './shared/getExportBlock';
import getInteropBlock from './shared/getInteropBlock';
import { keypath } from './shared/sanitize';
import setupNamespace from './shared/setupNamespace';
import trimEmptyImports from './shared/trimEmptyImports';
import warnOnBuiltins from './shared/warnOnBuiltins';
export default function iife(
magicString: MagicStringBundle,
{
accessedGlobals,
dependencies,
exports,
hasDefaultExport,
hasExports,
indent: t,
intro,
namedExportsMode,
log,
outro,
snippets
}: FinaliserOptions,
{
compact,
esModule,
extend,
freeze,
externalLiveBindings,
reexportProtoFromExternal,
globals,
interop,
name,
generatedCode: { symbols },
strict
}: NormalizedOutputOptions
): void {
const { _, getNonArrowFunctionIntro, getPropertyAccess, n } = snippets;
const isNamespaced = name && name.includes('.');
const useVariableAssignment = !extend && !isNamespaced;
if (name && useVariableAssignment && !isLegal(name)) {
return error(logIllegalIdentifierAsName(name));
}
warnOnBuiltins(log, dependencies);
const external = trimEmptyImports(dependencies);
const deps = external.map(dep => dep.globalName || 'null');
const parameters = external.map(m => m.name);
if (hasExports && !name) {
log(LOGLEVEL_WARN, logMissingNameOptionForIifeExport());
}
if (namedExportsMode && hasExports) {
if (extend) {
deps.unshift(
`this${keypath(name!, getPropertyAccess)}${_}=${_}this${keypath(
name!,
getPropertyAccess
)}${_}||${_}{}`
);
parameters.unshift('exports');
} else {
deps.unshift('{}');
parameters.unshift('exports');
}
}
const useStrict = strict ? `${t}'use strict';${n}` : '';
const interopBlock = getInteropBlock(
dependencies,
interop,
externalLiveBindings,
freeze,
symbols,
accessedGlobals,
t,
snippets
);
magicString.prepend(`${intro}${interopBlock}`);
let wrapperIntro = `(${getNonArrowFunctionIntro(parameters, {
isAsync: false,
name: null
})}{${n}${useStrict}${n}`;
if (hasExports) {
if (name && !(extend && namedExportsMode)) {
wrapperIntro =
(useVariableAssignment ? `var ${name}` : `this${keypath(name, getPropertyAccess)}`) +
`${_}=${_}${wrapperIntro}`;
}
if (isNamespaced) {
wrapperIntro = setupNamespace(name!, 'this', globals, snippets, compact, log) + wrapperIntro;
}
}
let wrapperOutro = `${n}${n}})(${deps.join(`,${_}`)});`;
if (hasExports && !extend && namedExportsMode) {
wrapperOutro = `${n}${n}${t}return exports;${wrapperOutro}`;
}
const exportBlock = getExportBlock(
exports,
dependencies,
namedExportsMode,
interop,
snippets,
t,
externalLiveBindings,
reexportProtoFromExternal
);
let namespaceMarkers = getNamespaceMarkers(
namedExportsMode && hasExports,
esModule === true || (esModule === 'if-default-prop' && hasDefaultExport),
symbols,
snippets
);
if (namespaceMarkers) {
namespaceMarkers = n + n + namespaceMarkers;
}
magicString
.append(`${exportBlock}${namespaceMarkers}${outro}`)
.indent(t)
.prepend(wrapperIntro)
.append(wrapperOutro);
}