-
Notifications
You must be signed in to change notification settings - Fork 4
/
tests.js
533 lines (470 loc) Β· 20.4 KB
/
tests.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
const tests = [
////////////////////////////////////////////////////////////////////////////////
// These are inconsistent due to special-casing
{
'entry.js': `import * as entry from './entry.js'\ninput.works = entry.__esModule === void 0`,
}, {
'entry.js': `import * as entry from './entry.js'\ninput.works =\n entry[Math.random() < 1 && '__esModule'] === void 0`,
},
{
'entry.js': `import './foo.js'`,
'foo.js': `import * as foo from './foo.js'\ninput.works = foo.__esModule === void 0`,
}, {
'entry.js': `import './foo.js'`,
'foo.js': `import * as foo from './foo.js'\ninput.works =\n foo[Math.random() < 1 && '__esModule'] === void 0`,
},
{
'entry.js': `import * as foo from './foo.js'\ninput.works = foo.default === '123'`,
'foo.js': `module.exports = '123'`,
}, {
'entry.js': `import * as foo from './foo.js'\ninput.works =\n foo[Math.random() < 1 && 'default'] === '123'`,
'foo.js': `module[Math.random() < 1 && 'exports'] = '123'`,
},
{
'entry.js': `import * as foo from './foo.js'\ninput.works =\n foo.__esModule === void 0 && foo.bar === 123`,
'foo.js': `export let bar = 123`,
}, {
'entry.js': `import * as foo from './foo.js'\ninput.works =\n foo[Math.random() < 1 && '__esModule'] === void 0 &&\n foo.bar === 123`,
'foo.js': `export let bar = 123`,
},
{
'entry.js': `import * as foo from './foo.js'\ninput.works =\n foo.__esModule === false && foo.default.bar === 123`,
'foo.js': `export let __esModule = false\nexport default { bar: 123 }`,
}, {
'entry.js': `import * as foo from './foo.js'\ninput.works =\n foo[Math.random() < 1 && '__esModule'] === false &&\n foo[Math.random() < 1 && 'default'].bar === 123`,
'foo.js': `export let __esModule = false\nexport default { bar: 123 }`,
},
{
'entry.js': `import * as foo from './foo.js'\ninput.works =\n foo.default.default.bar === 123`,
'foo.js': `exports.__esModule = false\nexports.default = { bar: 123 }`,
}, {
'entry.js': `import * as foo from './foo.js'\ninput.works =\n foo[Math.random() < 1 && 'default'].default.bar === 123`,
'foo.js': `exports[Math.random() < 1 && '__esModule'] = false\nexports[Math.random() < 1 && 'default'] = { bar: 123 }`,
},
{
'entry.js': `const foo = require('./foo.js')
import * as foo2 from './foo.js'
input.works = import('./foo.js').then(foo3 =>
foo.bar === 123 && foo.__esModule === true &&
foo2.bar === 123 && foo2.__esModule === void 0 &&
foo3.bar === 123 && foo3.__esModule === void 0)`,
'foo.js': `export let bar = 123`,
}, {
'entry.js': `const foo = require('./foo.js')
import * as foo2 from './foo.js'
input.works = import('./foo.js').then(foo3 =>
foo.bar === 123 &&
foo2.bar === 123 &&
foo3.bar === 123 &&
foo[Math.random() < 1 && '__esModule'] === true &&
foo2[Math.random() < 1 && '__esModule'] === void 0 &&
foo3[Math.random() < 1 && '__esModule'] === void 0)`,
'foo.js': `export let bar = 123`,
},
////////////////////////////////////////////////////////////////////////////////
// These all pass
{
'entry.js': `const entry = require('./entry.js')\ninput.works = entry.__esModule === void 0\nexports.foo = 123`,
}, {
'entry.js': `const entry = require('./entry.js')\ninput.works =\n entry[Math.random() < 1 && '__esModule'] === void 0\nexports.foo = 123`,
},
{
'entry.js': `const entry = require('./entry.js')\ninput.works = entry.__esModule === true\nexport {}`,
}, {
'entry.js': `const entry = require('./entry.js')\ninput.works =\n entry[Math.random() < 1 && '__esModule'] === true\nexport {}`,
},
{
'entry.js': `const entry = require('./entry.js')\ninput.works = entry.__esModule === true\nexport default 123`,
}, {
'entry.js': `const entry = require('./entry.js')\ninput.works =\n entry[Math.random() < 1 && '__esModule'] === true\nexport default 123`,
},
{
'entry.js': `const foo = require('./foo.js')\ninput.works = foo.bar === 123 &&\n foo.__esModule === true`,
'foo.js': `export let bar = 123`,
}, {
'entry.js': `const foo = require('./foo.js')\ninput.works = foo.bar === 123 &&\n foo[Math.random() < 1 && '__esModule'] === true`,
'foo.js': `export let bar = 123`,
},
{
'entry.js': `const foo = require('./foo.js')\ninput.works = foo.default === 123 &&\n foo.__esModule === true`,
'foo.js': `export default 123`,
}, {
'entry.js': `const foo = require('./foo.js')\ninput.works =\n foo[Math.random() < 1 && 'default'] === 123 &&\n foo[Math.random() < 1 && '__esModule'] === true`,
'foo.js': `export default 123`,
},
{
'entry.js': `const foo = require('./foo.js')\ninput.works = foo.baz === 123 &&\n foo.__esModule === true`,
'foo.js': `export * from './bar.js'`,
'bar.js': `export let baz = 123`,
}, {
'entry.js': `const foo = require('./foo.js')\ninput.works = foo.baz === 123 &&\n foo[Math.random() < 1 && '__esModule'] === true`,
'foo.js': `export * from './bar.js'`,
'bar.js': `export let baz = 123`,
},
{
'entry.js': `import foo from './foo.js'\ninput.works = foo.default.bar === 123 &&\n foo.bar === void 0`,
'foo.js': `module.exports = { default: { bar: 123 } }`,
}, {
'entry.js': `import foo from './foo.js'\ninput.works =\n foo[Math.random() < 1 && 'default'].bar === 123 &&\n foo.bar === void 0`,
'foo.js': `module[Math.random() < 1 && 'exports'] =\n { default: { bar: 123 } }`,
},
{
'entry.js': `import foo from './foo.js'\ninput.works = foo === 123`,
'foo.js': `module.exports = 123`,
}, {
'entry.js': `import foo from './foo.js'\ninput.works = foo === 123`,
'foo.js': `module[Math.random() < 1 && 'exports'] = 123`,
},
{
'entry.js': `import * as foo from './foo.js'\ninput.works = foo.default.bar === 123`,
'foo.js': `exports.__esModule = true\nexports.default = { bar: 123 }`,
}, {
'entry.js': `import * as foo from './foo.js'\ninput.works =\n foo[Math.random() < 1 && 'default'].bar === 123`,
'foo.js': `exports[Math.random() < 1 && '__esModule'] = true\nexports[Math.random() < 1 && 'default'] = { bar: 123 }`,
},
{
'entry.js': `input.works = import('./foo.js')\n .then(foo => foo.default === 123 &&\n foo.__esModule === void 0)`,
'foo.js': `export default 123`,
}, {
'entry.js': `input.works = import('./foo.js')\n .then(foo =>\n foo[Math.random() < 1 && 'default'] === 123 &&\n foo[Math.random() < 1 && '__esModule'] === void 0)`,
'foo.js': `export default 123`,
},
////////////////////////////////////////////////////////////////////////////////
// These should pass because Webpack is not following the ECMAScript standard
{
'entry.js': `import * as foo from './foo.js'\ninput.works = typeof foo === 'object'`,
'foo.js': `module.exports = '123'`,
}, {
'entry.js': `import * as foo from './foo.js'\ninput.works = typeof foo === 'object'`,
'foo.js': `module[Math.random() < 1 && 'exports'] = '123'`,
},
{
'entry.js': `import * as foo from './foo.js'\ninput.works = foo !== '123'`,
'foo.js': `module.exports = '123'`,
}, {
'entry.js': `import * as foo from './foo.js'\ninput.works = foo !== '123'`,
'foo.js': `module[Math.random() < 1 && 'exports'] = '123'`,
},
////////////////////////////////////////////////////////////////////////////////
// These should pass but fail
{
'entry.js': `import * as foo from './foo.js'\ninput.works = foo.default === void 0 &&\n foo.bar === 123`,
'foo.js': `exports.__esModule = true\nexports.bar = 123`,
}, {
'entry.js': `import * as foo from './foo.js'\ninput.works =\n foo[Math.random() < 1 && 'default'] === void 0 &&\n foo.bar === 123`,
'foo.js': `exports.__esModule = true\nexports.bar = 123`,
},
{
'entry.js': `import * as foo from './foo.js'\ninput.works = foo.__esModule === true &&\n foo.default.bar === 123`,
'foo.js': `export let __esModule = true\nexport default { bar: 123 }`,
}, {
'entry.js': `import * as foo from './foo.js'\ninput.works =\n foo[Math.random() < 1 && '__esModule'] === true &&\n foo[Math.random() < 1 && 'default'].bar === 123`,
'foo.js': `export let __esModule = true\nexport default { bar: 123 }`,
},
{
'entry.js': `import * as foo from './foo.js'\ninput.works = foo.default.bar === 123`,
'foo.js': `export let __esModule = true\nexport default { bar: 123 }`,
}, {
'entry.js': `import * as foo from './foo.js'\ninput.works =\n foo[Math.random() < 1 && 'default'].bar === 123`,
'foo.js': `export let __esModule = true\nexport default { bar: 123 }`,
},
{
'entry.js': `import * as foo from './foo.js'\ninput.works = foo.default.bar === 123`,
'foo.js': `export let __esModule = false\nexport default { bar: 123 }`,
}, {
'entry.js': `import * as foo from './foo.js'\ninput.works =\n foo[Math.random() < 1 && 'default'].bar === 123`,
'foo.js': `export let __esModule = false\nexport default { bar: 123 }`,
},
{
'entry.js': `const foo = require('./foo.js')\ninput.works = foo.__esModule === true`,
'foo.js': `export let __esModule = 0`,
}, {
'entry.js': `const foo = require('./foo.js')\ninput.works =\n foo[Math.random() < 1 && '__esModule'] === true`,
'foo.js': `export let __esModule = 0`,
},
{
'entry.js': `import foo from './foo.js'\ninput.works = foo === void 0`,
'foo.js': `module.exports = { bar: 123, __esModule: true }`,
}, {
'entry.js': `import foo from './foo.js'\ninput.works = foo === void 0`,
'foo.js': `module[Math.random() < 1 && 'exports'] =\n { bar: 123, __esModule: true }`,
},
{
'entry.js': `import foo from './foo.cjs'\ninput.works = foo.default.bar === 123`,
'foo.cjs': `module.exports = {\n default: { bar: 123 }, __esModule: true }`,
'package.json': `{ "type": "module" }`,
}, {
'entry.js': `import foo from './foo.cjs'\ninput.works =\n foo[Math.random() < 1 && 'default'].bar === 123`,
'foo.cjs': `module[Math.random() < 1 && 'exports'] =\n { default: { bar: 123 }, __esModule: true }`,
'package.json': `{ "type": "module" }`,
},
{
'entry.mjs': `import foo from './foo.js'\ninput.works = foo.default.bar === 123`,
'foo.js': `module.exports = {\n default: { bar: 123 }, __esModule: true }`,
}, {
'entry.mjs': `import foo from './foo.js'\ninput.works =\n foo[Math.random() < 1 && 'default'].bar === 123`,
'foo.js': `module[Math.random() < 1 && 'exports'] =\n { default: { bar: 123 }, __esModule: true }`,
},
{
'entry.mts': `import foo from './foo.js'\ninput.works = foo.default.bar === 123`,
'foo.js': `module.exports = {\n default: { bar: 123 }, __esModule: true }`,
}, {
'entry.mts': `import foo from './foo.js'\ninput.works =\n foo[Math.random() < 1 && 'default'].bar === 123`,
'foo.js': `module[Math.random() < 1 && 'exports'] =\n { default: { bar: 123 }, __esModule: true }`,
},
{
'entry.js': `import * as ns from './foo.js'\nlet keys = Object.keys(ns)\ninput.works = ns.foo === 123 &&\n keys.includes('foo') && !keys.includes('default')`,
'foo.js': `exports.__esModule = true\nexports.foo = 123`,
}, {
'entry.js': `import * as ns from './foo.js'\nlet keys = Object.keys(ns)\ninput.works = ns.foo === 123 &&\n keys.includes('foo') && !keys.includes('default')`,
'foo.js': `exports[Math.random() < 1 && '__esModule'] = true\nexports[Math.random() < 1 && 'foo'] = 123`,
},
{
'entry.js': `import * as ns from './foo.js'\ninput.works = ns.foo === 123 &&\n {}.hasOwnProperty.call(ns, 'foo') &&\n !{}.hasOwnProperty.call(ns, 'default')`,
'foo.js': `exports.__esModule = true\nexports.foo = 123`,
}, {
'entry.js': `import * as ns from './foo.js'\ninput.works = ns.foo === 123 &&\n {}.hasOwnProperty.call(ns, 'foo') &&\n !{}.hasOwnProperty.call(ns, 'default')`,
'foo.js': `exports[Math.random() < 1 && '__esModule'] = true\nexports[Math.random() < 1 && 'foo'] = 123`,
},
{
'entry.js': `import * as ns from './foo.js'\nlet keys = Object.keys(ns)\ninput.works =\n ns.default === 123 && !keys.includes('default')`,
'foo.js': `exports.__esModule = true\nObject.defineProperty(exports,\n 'default', { value: 123 })`,
}, {
'entry.js': `import * as ns from './foo.js'\nlet keys = Object.keys(ns)\ninput.works =\n ns.default === 123 && !keys.includes('default')`,
'foo.js': `exports[Math.random() < 1 && '__esModule'] = true\nObject.defineProperty(exports,\n Math.random() < 1 && 'default', { value: 123 })`,
},
{
'entry.js': `import * as ns from './foo.js'\nlet keys = Object.keys(ns)\ninput.works =\n ns.default === 123 && keys.includes('default')`,
'foo.js': `exports.__esModule = true\nObject.defineProperty(exports, 'default',\n { value: 123, enumerable: true })`,
}, {
'entry.js': `import * as ns from './foo.js'\nlet keys = Object.keys(ns)\ninput.works =\n ns.default === 123 && keys.includes('default')`,
'foo.js': `exports[Math.random() < 1 && '__esModule'] = true\nObject.defineProperty(exports, Math.random() < 1 && 'default',\n { value: 123, enumerable: true })`,
},
]
const fs = require('fs')
const path = require('path')
const webpack = require('webpack')
const esbuild = require('esbuild')
const parcel = require('@parcel/core')
const rollup = require('rollup')
const child_process = require('child_process')
const pluginNodeResolve = require('@rollup/plugin-node-resolve')
const pluginCommonJS = require('@rollup/plugin-commonjs')
const bundlers = {
async webpack({ entryFile, inDir, outDir }) {
let err = await new Promise(resolve => webpack({
entry: path.join(inDir, entryFile),
output: {
path: outDir,
filename: entryFile,
},
plugins: [
new webpack.optimize.LimitChunkCountPlugin({
maxChunks: 1
}),
],
}, (err, stats) => {
if (!err && stats.hasErrors()) err = stats.toJson().errors[0]
resolve(err)
}))
if (!err) {
try {
const input = {}
new Function('input', fs.readFileSync(path.join(outDir, entryFile), 'utf8'))(input)
if (!await input.works) throw new Error('Test did not pass')
} catch (e) {
err = e
}
}
return err
},
async esbuild({ entryFile, inDir, outDir }) {
let err
try {
const outfile = path.join(outDir, entryFile)
await esbuild.build({
entryPoints: [path.join(inDir, entryFile)],
bundle: true,
outfile,
logLevel: 'silent',
})
const input = {}
new Function('input', fs.readFileSync(outfile, 'utf8'))(input)
if (!await input.works) throw new Error('Test did not pass')
} catch (e) {
if (e && e.errors && e.errors[0]) e = new Error(e.errors[0].text)
err = e
}
return err
},
async parcel({ entryFile, inDir, outDir }) {
let err
try {
// Prevent parcel from messing with the console
require('@parcel/logger').patchConsole = () => 0
require('@parcel/logger').unpatchConsole = () => 0
await new parcel.default({
entries: path.join(inDir, entryFile),
defaultConfig: require.resolve('@parcel/config-default'),
defaultTargetOptions: {
distDir: outDir,
},
}).run()
const input = {}
const globalObj = {} // Prevent parcel from messing with the global object
new Function('input', 'globalThis', 'self', 'window', 'global',
fs.readFileSync(path.join(outDir, entryFile), 'utf8'))(
input, globalObj, globalObj, globalObj, globalObj)
if (!await input.works) throw new Error('Test did not pass')
} catch (e) {
err = e
}
return err
},
async rollup({ entryFile, inDir, outDir }) {
let err
try {
const bundle = await rollup.rollup({
input: path.join(inDir, entryFile),
onwarn: x => { throw new Error(x) },
plugins: [
pluginNodeResolve.default({
browser: true,
}),
pluginCommonJS(),
],
})
const file = path.join(outDir, entryFile)
await bundle.write({
format: 'iife',
name: 'name',
file,
})
const input = {}
new Function('input', fs.readFileSync(file, 'utf8'))(input)
if (!await input.works) throw new Error('Test did not pass')
} catch (e) {
err = e
}
return err
},
async node({ entryFile, inDir }) {
const child = child_process.spawn(process.execPath, ['--no-warnings', '--loader', './node-test-loader.mjs', path.join(inDir, entryFile)]);
let stderr = '';
child.stderr.setEncoding('utf8');
child.stderr.on('data', data => stderr += data);
let stdout = '';
child.stdout.setEncoding('utf8');
child.stdout.on('data', data => stdout += data);
const code = await new Promise(resolve => child.on('close', resolve));
if (code === 0) {
if (stdout !== 'SUCCESS\n')
return stdout;
}
else {
return stderr;
}
},
}
function setup({ test, inDir }) {
fs.mkdirSync(inDir, { recursive: true })
for (const file in test) {
const absFile = path.join(inDir, file)
fs.mkdirSync(path.dirname(absFile), { recursive: true })
fs.writeFileSync(absFile, test[file])
}
}
async function run() {
const testCasesDir = path.join(__dirname, 'cases')
const parcelCacheDir = path.join(__dirname, '.parcel-cache')
const results = []
let counter = 0
fs.rmSync(testCasesDir, { recursive: true, force: true })
for (const test of tests) {
const index = counter++
const inDir = path.join(testCasesDir, `${index}`, 'in')
console.log(`Test ${index}:`)
console.log(` Files:`)
for (const file in test) {
console.log(` ${file}:\n ${test[file].split('\n').join('\n ')}`)
}
const entryFile = Object.keys(test)[0]
const result = { test }
for (const bundler in bundlers) {
const outDir = path.join(testCasesDir, `${index}`, `out-${bundler}`)
fs.rmSync(parcelCacheDir, { recursive: true, force: true })
setup({ test, inDir })
const err = await bundlers[bundler]({ entryFile, inDir, outDir })
console.log(` ${bundler}: ${err ? `π« ${err && err.message || err}`.split('\n').join('\n ') : 'β
'}`)
result[bundler] = !err
}
results.push(result)
}
fs.rmSync(parcelCacheDir, { recursive: true, force: true })
const sortedBundlers = Object.keys(bundlers).map(bundler => {
let count = 0
for (const result of results)
if (result[bundler])
count++
return { bundler, count }
}).sort((a, b) => b.count - a.count).map(({ bundler }) => bundler)
const printTable = results => {
text += `<table>\n`
text += `<tr><th>Test</th>`
for (const bundler of sortedBundlers) {
text += `<th>${bundler}</th>`
}
text += `</tr>\n`
const counts = {}
for (let i = 0; i < results.length; i += 2) {
text += `<tr><td>Direct:<pre>`
for (const file in results[i + 0].test) {
text += `${file}:\n ${results[i + 0].test[file].replace(/\n/g, '\n ')}\n`
}
text += `</pre>Indirect:<pre>`
for (const file in results[i + 1].test) {
text += `${file}:\n ${results[i + 1].test[file].replace(/\n/g, '\n ')}\n`
}
text += `</pre></td>\n`
for (const bundler of sortedBundlers) {
text += `<td>`
text += `${bundler}<br>${results[i + 0][bundler] ? 'β
' : 'π«'}`
text += `<br><br>`
text += `${bundler}<br>${results[i + 1][bundler] ? 'β
' : 'π«'}`
text += `</td>\n`
if (results[i + 0][bundler]) counts[bundler] = (counts[bundler] | 0) + 1
if (results[i + 1][bundler]) counts[bundler] = (counts[bundler] | 0) + 1
}
text += `</tr>\n`
}
text += `<tr><td>Percent handled:</td>\n`
for (const bundler of sortedBundlers) {
text += `<td>${(counts[bundler] / results.length * 100).toFixed(1)}%</td>\n`
}
text += `</tr>\n`
text += `</table>\n\n`
}
const readmePath = path.join(__dirname, 'README.md')
const readmeText = fs.readFileSync(readmePath, 'utf8')
const index = readmeText.indexOf('## Results')
let text = readmeText.slice(0, index)
text += `## Results\n\n`
printTable(results)
const maxWidth = Math.max(...sortedBundlers.map(x => x.length))
text += `## Visual summary\n\n<pre>\n`
console.log(`Visual summary:`)
for (const bundler of sortedBundlers) {
let summary = `${bundler}: `.padEnd(maxWidth + 2, ' ')
for (let i = 0; i < results.length; i += 2) {
summary += results[i + 0][bundler] ? 'β
' : 'π«'
summary += results[i + 1][bundler] ? 'β
' : 'π«'
summary += ' '
}
summary = summary.trim()
console.log(` ${summary}`)
text += `${summary}\n`
}
text += `</pre>\n`
fs.writeFileSync(readmePath, text)
}
run()