/
utils.js
297 lines (263 loc) Β· 6.56 KB
/
utils.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
const Bundler = require('parcel-bundler');
const assert = require('assert');
const vm = require('vm');
const fs = require('@parcel/fs');
const nodeFS = require('fs');
const path = require('path');
const WebSocket = require('ws');
const Module = require('module');
const {promisify} = require('@parcel/utils');
//const {sleep} = require('@parcel/test-utils');
const rimraf = promisify(require('rimraf'));
const ncp = promisify(require('ncp'));
const chalk = new (require('chalk')).constructor({enabled: true});
const warning = chalk.keyword('orange');
// eslint-disable-next-line no-console
console.warn = (...args) => {
// eslint-disable-next-line no-console
console.error(warning(...args));
};
async function removeDistDirectory(count = 0) {
try {
await rimraf(path.join(process.cwd(), 'test/dist'));
} catch (e) {
if (count > 8) {
// eslint-disable-next-line no-console
console.warn('WARNING: Unable to remove dist directory:', e.message);
return;
}
await sleep(250);
await removeDistDirectory(count + 1);
}
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
function symlinkPrivilegeWarning() {
// eslint-disable-next-line no-console
console.warn(
`-----------------------------------
Skipping symbolic link test(s) because you don't have the privilege.
Run tests with Administrator privilege.
If you don't know how, check here: https://bit.ly/2UmWsbD
-----------------------------------`
);
}
function bundler(file, opts) {
return new Bundler(
file,
Object.assign(
{
outDir: path.join(process.cwd(), 'test/dist'),
watch: false,
cache: false,
killWorkers: false,
hmr: false,
logLevel: 0,
throwErrors: true
},
opts
)
);
}
function bundle(file, opts) {
return bundler(file, opts).bundle();
}
async function run(bundle, globals, opts = {}) {
var ctx;
switch (bundle.entryAsset.options.target) {
case 'browser':
ctx = prepareBrowserContext(bundle, globals);
break;
case 'node':
ctx = prepareNodeContext(bundle, globals);
break;
case 'electron':
ctx = Object.assign(
prepareBrowserContext(bundle, globals),
prepareNodeContext(bundle, globals)
);
break;
}
vm.createContext(ctx);
vm.runInContext(await fs.readFile(bundle.name), ctx);
if (opts.require !== false) {
if (ctx.parcelRequire) {
return ctx.parcelRequire(bundle.entryAsset.id);
} else if (ctx.output) {
return ctx.output;
}
if (ctx.module) {
return ctx.module.exports;
}
}
return ctx;
}
async function assertBundleTree(bundle, tree) {
if (tree.name) {
assert.equal(
path.basename(bundle.name),
tree.name,
'bundle names mismatched'
);
}
if (tree.type) {
assert.equal(
bundle.type.toLowerCase(),
tree.type.toLowerCase(),
'bundle types mismatched'
);
}
if (tree.assets) {
assert.deepEqual(
Array.from(bundle.assets)
.map(a => a.basename)
.sort(),
tree.assets.sort()
);
}
let childBundles = Array.isArray(tree) ? tree : tree.childBundles;
if (childBundles) {
let children = Array.from(bundle.childBundles).sort(
(a, b) =>
Array.from(a.assets).sort()[0].basename <
Array.from(b.assets).sort()[0].basename
? -1
: 1
);
assert.equal(
bundle.childBundles.size,
childBundles.length,
'expected number of child bundles mismatched'
);
await Promise.all(
childBundles.map((b, i) => assertBundleTree(children[i], b))
);
}
if (/js|css/.test(bundle.type)) {
assert(await fs.exists(bundle.name), 'expected file does not exist');
}
}
function nextBundle(b) {
return new Promise(resolve => {
b.once('bundled', resolve);
});
}
function deferred() {
let resolve, reject;
let promise = new Promise((res, rej) => {
resolve = res;
reject = rej;
});
promise.resolve = resolve;
promise.reject = reject;
return promise;
}
function normaliseNewlines(text) {
return text.replace(/(\r\n|\n|\r)/g, '\n');
}
function prepareBrowserContext(bundle, globals) {
// for testing dynamic imports
const fakeElement = {
remove() {}
};
const fakeDocument = {
createElement(tag) {
return {tag};
},
getElementsByTagName() {
return [
{
appendChild(el) {
setTimeout(function() {
if (el.tag === 'script') {
vm.runInContext(
nodeFS.readFileSync(
path.join(path.dirname(bundle.name), el.src)
),
ctx
);
}
el.onload();
}, 0);
}
}
];
},
getElementById() {
return fakeElement;
},
body: {
appendChild() {
return null;
}
}
};
var exports = {};
var ctx = Object.assign(
{
exports,
module: {exports},
document: fakeDocument,
WebSocket,
console,
location: {hostname: 'localhost', reload() {}},
fetch(url) {
return Promise.resolve({
arrayBuffer() {
return Promise.resolve(
new Uint8Array(
nodeFS.readFileSync(path.join(path.dirname(bundle.name), url))
).buffer
);
},
text() {
return Promise.resolve(
nodeFS.readFileSync(
path.join(path.dirname(bundle.name), url),
'utf8'
)
);
}
});
}
},
globals
);
ctx.window = ctx;
return ctx;
}
function prepareNodeContext(bundle, globals) {
var mod = new Module(bundle.name);
mod.paths = [path.dirname(bundle.name) + '/node_modules'];
var ctx = Object.assign(
{
module: mod,
exports: module.exports,
__filename: bundle.name,
__dirname: path.dirname(bundle.name),
require: function(path) {
return mod.require(path);
},
console,
process: process,
setTimeout: setTimeout,
setImmediate: setImmediate
},
globals
);
ctx.global = ctx;
return ctx;
}
exports.sleep = sleep;
exports.removeDistDirectory = removeDistDirectory;
exports.symlinkPrivilegeWarning = symlinkPrivilegeWarning;
exports.bundler = bundler;
exports.bundle = bundle;
exports.run = run;
exports.assertBundleTree = assertBundleTree;
exports.nextBundle = nextBundle;
exports.deferred = deferred;
exports.rimraf = rimraf;
exports.ncp = ncp;
exports.normaliseNewlines = normaliseNewlines;