Skip to content

Commit 50c4daa

Browse files
committed
Demos: Cover all 29 bundler variants in Node.js
Previously only the 5 .cjs.js outputs were tested in Node.js. * Rename .es.js to .es.mjs output, because otherwise `node --import=` will reject it. The rejection error suggest placing `{ type: 'module' }` in package.json, but we can't given the Webpack caveat (see build.mjs). * Use readdirSync(), since 29 is too many to hardcode. We have to know the list of test cases at the top-level, thus run readdirSync() top-level, thus run build.mjs before it, which is ESM and async, thus to import and await it, the test file has to be an ESM file, convert to demos/bundlers.mjs accordingly.
1 parent b0db826 commit 50c4daa

File tree

2 files changed

+39
-37
lines changed

2 files changed

+39
-37
lines changed

demos/bundlers.js renamed to demos/bundlers.mjs

+23-23
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,34 @@
1-
const cp = require('child_process');
2-
const path = require('path');
3-
const DIR = path.join(__dirname, 'bundlers');
1+
import cp from 'node:child_process';
2+
import fs from 'node:fs';
3+
import path from 'node:path';
4+
import url from 'node:url';
5+
6+
const dirname = path.dirname(url.fileURLToPath(import.meta.url));
7+
const DIR = path.join(dirname, 'bundlers');
8+
9+
// Prepare
10+
// cp.execSync('npm install --no-audit --update-notifier=false', { cwd: DIR, encoding: 'utf8' });
11+
await import('./bundlers/build.mjs');
12+
13+
const tmpJsFiles = fs.readdirSync(path.join(DIR, 'tmp'))
14+
.filter(name => name.endsWith('.js') || name.endsWith('.mjs'));
15+
const directFiles = tmpJsFiles.filter(name => !name.includes('-indirect'));
16+
const indirectFiles = tmpJsFiles.filter(name => name.includes('-indirect'));
417

518
function normalize (str) {
619
return str
720
.replace(/^localhost:\d+/g, 'localhost:8000')
821
.replace(/\b\d+ms\b/g, '42ms');
922
}
1023

11-
QUnit.module('bundlers', {
12-
before: async (assert) => {
13-
assert.timeout(60_000);
14-
15-
cp.execSync('npm install --no-audit --update-notifier=false', { cwd: DIR, encoding: 'utf8' });
24+
QUnit.module('bundlers');
1625

17-
await import('./bundlers/build.mjs');
18-
}
19-
});
20-
21-
QUnit.test.each('test in Node.js [direct]', [
22-
'./tmp/import-default.cjs.js',
23-
'./tmp/import-named.cjs.js',
24-
'./tmp/require-default.cjs.js'
25-
], function (assert, fileName) {
26+
QUnit.test.each('test in Node.js [direct]', directFiles, function (assert, fileName) {
2627
const actual = cp.execFileSync(process.execPath,
2728
[
2829
'--input-type=module',
2930
'-e',
30-
`import ${JSON.stringify(fileName)}; QUnit.start();`
31+
`import ${JSON.stringify('./tmp/' + fileName)}; QUnit.start();`
3132
],
3233
{ cwd: DIR, env: { qunit_config_reporters_tap: 'true' }, encoding: 'utf8' }
3334
);
@@ -41,15 +42,12 @@ QUnit.test.each('test in Node.js [direct]', [
4142
assert.pushResult({ result: actual.includes(expected), actual, expected }, 'stdout');
4243
});
4344

44-
QUnit.test.each('test in Node.js [indirect]', [
45-
'./tmp/import-indirect.cjs.js',
46-
'./tmp/require-indirect.cjs.js'
47-
], function (assert, fileName) {
45+
QUnit.test.each('test in Node.js [indirect]', indirectFiles, function (assert, fileName) {
4846
const actual = cp.execFileSync(process.execPath,
4947
[
5048
'--input-type=module',
5149
'-e',
52-
`import ${JSON.stringify(fileName)}; QUnit.start();`
50+
`import ${JSON.stringify('./tmp/' + fileName)}; QUnit.start();`
5351
],
5452
{ cwd: DIR, env: { qunit_config_reporters_tap: 'true' }, encoding: 'utf8' }
5553
);
@@ -64,6 +62,8 @@ QUnit.test.each('test in Node.js [indirect]', [
6462
});
6563

6664
QUnit.test('test in browser', function (assert) {
65+
assert.timeout(60_000);
66+
6767
const expected = `Running "connect:all" (connect) task
6868
Started connect web server on http://localhost:8000
6969

demos/bundlers/build.mjs

+16-14
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ const htmlTemplate = `<!DOCTYPE html>
3535
const rollupOutputs = [
3636
{
3737
dir: tmpDir,
38-
entryFileNames: '[name].[format].js',
38+
entryFileNames: '[name].[format].mjs',
3939
format: 'es'
4040
},
4141
{
@@ -107,20 +107,22 @@ await (async function main () {
107107
for await (const fileName of gRollup) {
108108
console.log('... built ' + fileName);
109109

110-
if (!fileName.endsWith('.cjs.js')) {
111-
const html = htmlTemplate
112-
.replace('{{title}}', fileName)
113-
.replace('{{scriptTag}}', (
114-
fileName.endsWith('.es.js')
115-
? `<script src="./${fileName}" type="module"></script>`
116-
: `<script src="./${fileName}"></script>`
117-
));
118-
119-
fs.writeFileSync(
120-
`${tmpDir}/test-${fileName.replace('.js', '')}.html`,
121-
html
122-
);
110+
if (fileName.endsWith('.cjs.js')) {
111+
continue;
123112
}
113+
114+
const html = htmlTemplate
115+
.replace('{{title}}', fileName)
116+
.replace('{{scriptTag}}', (
117+
fileName.endsWith('.mjs')
118+
? `<script src="./${fileName}" type="module"></script>`
119+
: `<script src="./${fileName}"></script>`
120+
));
121+
122+
fs.writeFileSync(
123+
`${tmpDir}/test-${fileName.replace(/\.(js|mjs)$/, '')}.html`,
124+
html
125+
);
124126
}
125127
for await (const fileName of gWebpack) {
126128
console.log('... built ' + fileName);

0 commit comments

Comments
 (0)