/
umd.ts
175 lines (152 loc) · 5.73 KB
/
umd.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import { Bundle as MagicStringBundle } from 'magic-string';
import { GlobalsOption, OutputOptions } from '../rollup/types';
import { error } from '../utils/error';
import { FinaliserOptions } from './index';
import { compactEsModuleExport, esModuleExport } from './shared/esModuleExport';
import getExportBlock from './shared/getExportBlock';
import getInteropBlock from './shared/getInteropBlock';
import { keypath, property } from './shared/sanitize';
import { assignToDeepVariable } from './shared/setupNamespace';
import trimEmptyImports from './shared/trimEmptyImports';
import warnOnBuiltins from './shared/warnOnBuiltins';
function globalProp(name: string, globalVar: string) {
if (!name) return 'null';
return `${globalVar}${keypath(name)}`;
}
function safeAccess(name: string, globalVar: string, _: string) {
const parts = name.split('.');
let acc = globalVar;
return parts.map(part => ((acc += property(part)), acc)).join(`${_}&&${_}`);
}
export default function umd(
magicString: MagicStringBundle,
{
dependencies,
exports,
hasExports,
indentString: t,
intro,
namedExportsMode,
outro,
varOrConst,
warn
}: FinaliserOptions,
options: OutputOptions
) {
const _ = options.compact ? '' : ' ';
const n = options.compact ? '' : '\n';
const factoryVar = options.compact ? 'f' : 'factory';
const globalVar = options.compact ? 'g' : 'global';
if (hasExports && !options.name) {
return error({
code: 'MISSING_NAME_OPTION_FOR_IIFE_EXPORT',
message:
'You must supply "output.name" for UMD bundles that have exports so that the exports are accessible in environments without a module loader.'
});
}
warnOnBuiltins(warn, dependencies);
const amdDeps = dependencies.map(m => `'${m.id}'`);
const cjsDeps = dependencies.map(m => `require('${m.id}')`);
const trimmedImports = trimEmptyImports(dependencies);
const globalDeps = trimmedImports.map(module => globalProp(module.globalName, globalVar));
const factoryArgs = trimmedImports.map(m => m.name);
if (namedExportsMode && (hasExports || options.noConflict === true)) {
amdDeps.unshift(`'exports'`);
cjsDeps.unshift(`exports`);
globalDeps.unshift(
assignToDeepVariable(
options.name!,
globalVar,
options.globals as GlobalsOption,
options.compact!,
`${options.extend ? `${globalProp(options.name!, globalVar)}${_}||${_}` : ''}{}`
)
);
factoryArgs.unshift('exports');
}
const amdOptions = options.amd || {};
const amdParams =
(amdOptions.id ? `'${amdOptions.id}',${_}` : ``) +
(amdDeps.length ? `[${amdDeps.join(`,${_}`)}],${_}` : ``);
const define = amdOptions.define || 'define';
const cjsExport = !namedExportsMode && hasExports ? `module.exports${_}=${_}` : ``;
const useStrict = options.strict !== false ? `${_}'use strict';${n}` : ``;
let iifeExport;
if (options.noConflict === true) {
const noConflictExportsVar = options.compact ? 'e' : 'exports';
let factory;
if (!namedExportsMode && hasExports) {
factory = `var ${noConflictExportsVar}${_}=${_}${assignToDeepVariable(
options.name!,
globalVar,
options.globals,
options.compact,
`${factoryVar}(${globalDeps.join(`,${_}`)})`
)};`;
} else if (namedExportsMode) {
const module = globalDeps.shift();
factory =
`var ${noConflictExportsVar}${_}=${_}${module};${n}` +
`${t}${t}${factoryVar}(${[noConflictExportsVar].concat(globalDeps).join(`,${_}`)});`;
}
iifeExport =
`(function${_}()${_}{${n}` +
`${t}${t}var current${_}=${_}${safeAccess(options.name!, globalVar, _)};${n}` +
`${t}${t}${factory}${n}` +
`${t}${t}${noConflictExportsVar}.noConflict${_}=${_}function${_}()${_}{${_}` +
`${globalProp(options.name!, globalVar)}${_}=${_}current;${_}return ${noConflictExportsVar}${
options.compact ? '' : '; '
}};${n}` +
`${t}}())`;
} else {
iifeExport = `${factoryVar}(${globalDeps.join(`,${_}`)})`;
if (!namedExportsMode && hasExports) {
iifeExport = assignToDeepVariable(
options.name!,
globalVar,
options.globals as GlobalsOption,
options.compact!,
iifeExport
);
}
}
const iifeNeedsGlobal =
hasExports || (options.noConflict === true && namedExportsMode) || globalDeps.length > 0;
const globalParam = iifeNeedsGlobal ? `${globalVar},${_}` : '';
const globalArg = iifeNeedsGlobal ? `this,${_}` : '';
const iifeStart = iifeNeedsGlobal ? `(${globalVar}${_}=${_}${globalVar}${_}||${_}self,${_}` : '';
const iifeEnd = iifeNeedsGlobal ? ')' : '';
const cjsIntro = iifeNeedsGlobal
? `${t}typeof exports${_}===${_}'object'${_}&&${_}typeof module${_}!==${_}'undefined'${_}?` +
`${_}${cjsExport}${factoryVar}(${cjsDeps.join(`,${_}`)})${_}:${n}`
: '';
// factory function should be wrapped by parentheses to avoid lazy parsing
const wrapperIntro =
`(function${_}(${globalParam}${factoryVar})${_}{${n}` +
cjsIntro +
`${t}typeof ${define}${_}===${_}'function'${_}&&${_}${define}.amd${_}?${_}${define}(${amdParams}${factoryVar})${_}:${n}` +
`${t}${iifeStart}${iifeExport}${iifeEnd};${n}` +
`}(${globalArg}(function${_}(${factoryArgs.join(', ')})${_}{${useStrict}${n}`;
const wrapperOutro = n + n + '})));';
// var foo__default = 'default' in foo ? foo['default'] : foo;
const interopBlock = getInteropBlock(dependencies, options, varOrConst);
if (interopBlock) magicString.prepend(interopBlock + n + n);
if (intro) magicString.prepend(intro);
const exportBlock = getExportBlock(
exports,
dependencies,
namedExportsMode,
options.interop,
options.compact,
t
);
if (exportBlock) magicString.append(n + n + exportBlock);
if (namedExportsMode && hasExports && options.esModule)
magicString.append(n + n + (options.compact ? compactEsModuleExport : esModuleExport));
if (outro) magicString.append(outro);
return magicString
.trim()
.indent(t)
.append(wrapperOutro)
.prepend(wrapperIntro);
}