From 50c4daa470ad0520a2e253aade356169e25695ab Mon Sep 17 00:00:00 2001 From: Timo Tijhof Date: Tue, 30 Jul 2024 22:21:48 +0100 Subject: [PATCH] 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. --- demos/{bundlers.js => bundlers.mjs} | 46 ++++++++++++++--------------- demos/bundlers/build.mjs | 30 ++++++++++--------- 2 files changed, 39 insertions(+), 37 deletions(-) rename demos/{bundlers.js => bundlers.mjs} (80%) diff --git a/demos/bundlers.js b/demos/bundlers.mjs similarity index 80% rename from demos/bundlers.js rename to demos/bundlers.mjs index c64d39b8e..787206542 100644 --- a/demos/bundlers.js +++ b/demos/bundlers.mjs @@ -1,6 +1,19 @@ -const cp = require('child_process'); -const path = require('path'); -const DIR = path.join(__dirname, 'bundlers'); +import cp from 'node:child_process'; +import fs from 'node:fs'; +import path from 'node:path'; +import url from 'node:url'; + +const dirname = path.dirname(url.fileURLToPath(import.meta.url)); +const DIR = path.join(dirname, 'bundlers'); + +// Prepare +// cp.execSync('npm install --no-audit --update-notifier=false', { cwd: DIR, encoding: 'utf8' }); +await import('./bundlers/build.mjs'); + +const tmpJsFiles = fs.readdirSync(path.join(DIR, 'tmp')) + .filter(name => name.endsWith('.js') || name.endsWith('.mjs')); +const directFiles = tmpJsFiles.filter(name => !name.includes('-indirect')); +const indirectFiles = tmpJsFiles.filter(name => name.includes('-indirect')); function normalize (str) { return str @@ -8,26 +21,14 @@ function normalize (str) { .replace(/\b\d+ms\b/g, '42ms'); } -QUnit.module('bundlers', { - before: async (assert) => { - assert.timeout(60_000); - - cp.execSync('npm install --no-audit --update-notifier=false', { cwd: DIR, encoding: 'utf8' }); +QUnit.module('bundlers'); - await import('./bundlers/build.mjs'); - } -}); - -QUnit.test.each('test in Node.js [direct]', [ - './tmp/import-default.cjs.js', - './tmp/import-named.cjs.js', - './tmp/require-default.cjs.js' -], function (assert, fileName) { +QUnit.test.each('test in Node.js [direct]', directFiles, function (assert, fileName) { const actual = cp.execFileSync(process.execPath, [ '--input-type=module', '-e', - `import ${JSON.stringify(fileName)}; QUnit.start();` + `import ${JSON.stringify('./tmp/' + fileName)}; QUnit.start();` ], { cwd: DIR, env: { qunit_config_reporters_tap: 'true' }, encoding: 'utf8' } ); @@ -41,15 +42,12 @@ QUnit.test.each('test in Node.js [direct]', [ assert.pushResult({ result: actual.includes(expected), actual, expected }, 'stdout'); }); -QUnit.test.each('test in Node.js [indirect]', [ - './tmp/import-indirect.cjs.js', - './tmp/require-indirect.cjs.js' -], function (assert, fileName) { +QUnit.test.each('test in Node.js [indirect]', indirectFiles, function (assert, fileName) { const actual = cp.execFileSync(process.execPath, [ '--input-type=module', '-e', - `import ${JSON.stringify(fileName)}; QUnit.start();` + `import ${JSON.stringify('./tmp/' + fileName)}; QUnit.start();` ], { cwd: DIR, env: { qunit_config_reporters_tap: 'true' }, encoding: 'utf8' } ); @@ -64,6 +62,8 @@ QUnit.test.each('test in Node.js [indirect]', [ }); QUnit.test('test in browser', function (assert) { + assert.timeout(60_000); + const expected = `Running "connect:all" (connect) task Started connect web server on http://localhost:8000 diff --git a/demos/bundlers/build.mjs b/demos/bundlers/build.mjs index a14bc4e54..577082cb3 100644 --- a/demos/bundlers/build.mjs +++ b/demos/bundlers/build.mjs @@ -35,7 +35,7 @@ const htmlTemplate = ` const rollupOutputs = [ { dir: tmpDir, - entryFileNames: '[name].[format].js', + entryFileNames: '[name].[format].mjs', format: 'es' }, { @@ -107,20 +107,22 @@ await (async function main () { for await (const fileName of gRollup) { console.log('... built ' + fileName); - if (!fileName.endsWith('.cjs.js')) { - const html = htmlTemplate - .replace('{{title}}', fileName) - .replace('{{scriptTag}}', ( - fileName.endsWith('.es.js') - ? `` - : `` - )); - - fs.writeFileSync( - `${tmpDir}/test-${fileName.replace('.js', '')}.html`, - html - ); + if (fileName.endsWith('.cjs.js')) { + continue; } + + const html = htmlTemplate + .replace('{{title}}', fileName) + .replace('{{scriptTag}}', ( + fileName.endsWith('.mjs') + ? `` + : `` + )); + + fs.writeFileSync( + `${tmpDir}/test-${fileName.replace(/\.(js|mjs)$/, '')}.html`, + html + ); } for await (const fileName of gWebpack) { console.log('... built ' + fileName);