forked from rollup/rollup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
umd.js
233 lines (210 loc) · 6.53 KB
/
umd.js
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
const assert = require('node:assert');
const { rollup } = require('../../dist/rollup');
const { loader } = require('../utils.js');
function runTestCode(code, thisValue, globals) {
const globalThisDesc = Object.getOwnPropertyDescriptor(global, 'globalThis');
delete global.globalThis;
const globalsWithAssert = { ...globals, assert };
const globalKeys = Object.keys(globalsWithAssert);
const function_ = new Function(globalKeys, code);
function_.apply(
thisValue,
globalKeys.map(key => globalsWithAssert[key])
);
if (globalThisDesc) {
Object.defineProperty(global, 'globalThis', globalThisDesc);
}
}
function runNodeTest(code) {
const module = { exports: {} };
runTestCode(code, module.exports, {
module,
exports: module.exports,
require: importee => {
if (importee !== 'external') {
throw new Error(`Tried to import unknown "${importee}".`);
}
return runNodeTest('module.exports = "external";');
}
});
return module.exports;
}
function runAmdTest(code) {
let defineArguments;
function define(...parameters) {
defineArguments = parameters;
}
define.amd = true;
runTestCode(code, undefined, { define });
if (typeof defineArguments[0] === 'function') {
return defineArguments[0]() || {};
}
const module = { exports: {} };
const result =
defineArguments[1](
...defineArguments[0].map(injection => {
switch (injection) {
case 'module': {
return module;
}
case 'exports': {
return module.exports;
}
case 'external': {
return 'external';
}
default: {
throw new Error(`unexpected AMD injection ${injection}`);
}
}
})
) || {};
return defineArguments[0].includes('exports') ? module.exports : result;
}
function runIifeTestWithThis(code, outputOptions) {
const global = { external: 'external' };
runTestCode(code, global, {});
return getAndCheckIifeExports(global, outputOptions);
}
function runStrictIifeTestWithSelf(code, outputOptions) {
const global = { external: 'external' };
runTestCode('"use strict";' + code, undefined, { self: global });
return getAndCheckIifeExports(global, outputOptions);
}
function runStrictIifeTestWithGlobalThis(code, outputOptions) {
const global = { external: 'external' };
runTestCode('"use strict";' + code, undefined, { globalThis: global });
return getAndCheckIifeExports(global, outputOptions);
}
function runIifeWithExistingValuesTest(code, outputOptions) {
const global = {
external: 'external',
bundle: 'previous',
my: { '@nested/value': { bundle: 'previous' } }
};
runTestCode(code, global, {});
const exports = getAndCheckIifeExports(global, outputOptions);
if (outputOptions.noConflict) {
// As "noConflict" was already called when getting the exports, the previous globals should be restored
assert.deepEqual(
global,
{ external: 'external', bundle: 'previous', my: { '@nested/value': { bundle: 'previous' } } },
'noConflict restores global'
);
}
return exports === 'previous' ? {} : exports;
}
function getAndCheckIifeExports(global, outputOptions) {
const exports = getIifeExports(global, outputOptions);
if (outputOptions.noConflict) {
assert.deepEqual(exports.noConflict(), exports, 'noConflict() returns exports');
delete exports.noConflict;
}
return exports;
}
function getIifeExports(global, outputOptions) {
if (outputOptions.name) {
return outputOptions.name
.split('.')
.reduce((currentVariable, nextKey) => currentVariable[nextKey] || {}, global);
}
return {};
}
async function getUmdCode(inputCode, outputOptions) {
const bundle = await rollup({
input: 'input',
external: ['external'],
plugins: [loader({ input: inputCode })]
});
const { output } = await bundle.generate({
format: 'umd',
globals: { external: 'external' },
...outputOptions
});
return output[0].code;
}
function runTestsWithCode(code, outputOptions, expectedExports) {
let umdCodePromise;
function getUmdCodePromise() {
if (umdCodePromise) {
return umdCodePromise;
}
return (umdCodePromise = getUmdCode(code, outputOptions));
}
for (const { environmentName, runTest } of [
{
environmentName: 'node',
runTest: runNodeTest
},
{
environmentName: 'amd',
runTest: runAmdTest
},
{
environmentName: 'iife with this',
runTest: runIifeTestWithThis
},
{
environmentName: 'strict mode iife with self',
runTest: runStrictIifeTestWithSelf
},
{
environmentName: 'strict mode iife with globalThis',
runTest: runStrictIifeTestWithGlobalThis
},
{
environmentName: 'iife with existing globals',
runTest: runIifeWithExistingValuesTest
}
])
it(`works in ${environmentName} environment`, async () => {
assert.deepEqual(
runTest(await getUmdCodePromise(), outputOptions),
expectedExports,
'expected exports are returned'
);
});
}
for (const name of ['bundle', 'my.@nested/value.bundle'])
for (const compact of [false, true]) {
for (const noConflict of [false, true])
describe(`The UMD wrapper with name="${name}", compact=${compact}, noConflict=${noConflict}`, () => {
const outputOptions = { compact, name, noConflict };
describe('creating a bundle with neither exports nor imports', () =>
runTestsWithCode('assert.ok(true);', outputOptions, {}));
describe('creating a bundle with named exports', () =>
runTestsWithCode('export const x = 42;', outputOptions, { x: 42 }));
describe('creating a bundle with a default export', () =>
runTestsWithCode('export default {value: 42};', outputOptions, { value: 42 }));
describe('creating a bundle with an external import', () =>
runTestsWithCode(
'import value from "external"; assert.equal(value, "external");',
outputOptions,
{}
));
describe('creating a bundle with an external import and named exports', () =>
runTestsWithCode('import value from "external"; export const x = value;', outputOptions, {
x: 'external'
}));
describe('creating a bundle with an external import and a default export', () =>
runTestsWithCode('import value from "external"; export default {value};', outputOptions, {
value: 'external'
}));
});
}
describe('The UMD wrapper with output name as reserved keyword', () => {
it('Set output name as toString.value', () => {
getUmdCode('export const x = 42;', { name: 'toString.value' }).then(code => {
assert.deepEqual(
code,
'this.toString = this.toString || {};\n' +
'this.toString.value = (function (exports) {\n' +
"'use strict';\n\n" +
'const x = 42;\n\n' +
'exports.x = x;\n\n' +
'return exports;\n\n' +
'})({});'
);
});
});
});