From ea825c90405d26958ff9283afa9be7803cb79ea6 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Thu, 6 Oct 2022 21:22:41 +0800 Subject: [PATCH] Fix "Bundler Friendly" test (#13584) --- .github/workflows/bundler-friendly.yml | 10 +- scripts/tools/bundle-test/index.js | 121 +++++++++++-------------- 2 files changed, 60 insertions(+), 71 deletions(-) diff --git a/.github/workflows/bundler-friendly.yml b/.github/workflows/bundler-friendly.yml index 882a322a9..e698f6f06 100644 --- a/.github/workflows/bundler-friendly.yml +++ b/.github/workflows/bundler-friendly.yml @@ -39,15 +39,17 @@ jobs: name: Bundle Prettier with webpack runs-on: ubuntu-latest needs: [build] - defaults: - run: - working-directory: scripts/tools/bundle-test steps: - name: Checkout uses: actions/checkout@v3.0.2 - name: Setup Node.js uses: actions/setup-node@v3.5.0 + with: + cache: "yarn" + + - name: Install Dependencies + run: yarn install --immutable - name: Download Artifact uses: actions/download-artifact@v3 @@ -57,6 +59,8 @@ jobs: - name: Install Dependencies run: yarn install --immutable + working-directory: scripts/tools/bundle-test - name: Test run: yarn test + working-directory: scripts/tools/bundle-test diff --git a/scripts/tools/bundle-test/index.js b/scripts/tools/bundle-test/index.js index 2150dc876..f557c84d5 100644 --- a/scripts/tools/bundle-test/index.js +++ b/scripts/tools/bundle-test/index.js @@ -1,9 +1,9 @@ -import { fileURLToPath } from "node:url"; +import url from "node:url"; import path from "node:path"; -import fs from "node:fs/promises"; import { createRequire } from "node:module"; import webpack from "webpack"; -import { DIST_DIR } from "../../../scripts/utils/index.mjs"; +import { DIST_DIR } from "../../utils/index.mjs"; +import files from "../../build/config.mjs"; function runWebpack(config) { return new Promise((resolve, reject) => { @@ -26,71 +26,56 @@ function runWebpack(config) { }); } -const TEMPORARY_DIRECTORY = fileURLToPath(new URL("./.tmp", import.meta.url)); +const TEMPORARY_DIRECTORY = url.fileURLToPath( + new URL("./.tmp", import.meta.url) +); /* `require` in `parser-typescript.js`, #12338 */ -(async () => { - const esmFilesDirectory = path.join(DIST_DIR, "esm"); - - const files = [ - (await fs.readdir(DIST_DIR)) - .filter( - (name) => - name.startsWith("parser-") || - name === "standalone.js" || - name === "doc.js" - ) - .map((name) => ({ - displayName: name, - name, - file: path.join(DIST_DIR, name), - })), - (await fs.readdir(esmFilesDirectory)).map((name) => ({ - displayName: `esm/${name}`, - name, - file: path.join(esmFilesDirectory, name), - })), - ].flat(); - - for (const { displayName, name, file } of files) { - console.log(`${displayName}: `); - const isEsmModule = name.endsWith(".mjs"); - - const stats = await runWebpack({ - mode: "production", - entry: file, - output: { - path: TEMPORARY_DIRECTORY, - filename: `${name}.[contenthash:7].${isEsmModule ? "mjs" : "cjs"}`, - }, - performance: { hints: false }, - optimization: { minimize: false }, - }); - const result = stats.toJson(); - const { warnings, assets } = result; - - if (warnings.length > 0) { - console.log(warnings); - throw new Error("Unexpected webpack warning."); - } - - if (assets.length > 1) { - console.log(assets); - throw new Error("Unexpected assets."); - } - - if (!isEsmModule) { - const outputFile = assets[0].name; - const require = createRequire(import.meta.url); - - try { - require(path.join(TEMPORARY_DIRECTORY, assets[0].name)); - } catch (error) { - console.log(`'${outputFile}' is not functional.`); - throw error; - } - } - - console.log(" Passed."); +for (const file of files) { + if (file.platform !== "universal") { + continue; } -})(); + console.log(`${file.output.file}: `); + + const stats = await runWebpack({ + mode: "production", + entry: path.join(DIST_DIR, file.output.file), + output: { + path: TEMPORARY_DIRECTORY, + filename: `${file.output.file}.[contenthash:7].${ + file.output.format === "esm" ? "mjs" : "cjs" + }`, + }, + performance: { hints: false }, + optimization: { minimize: false }, + }); + const result = stats.toJson(); + const { warnings, assets } = result; + + if (warnings.length > 0) { + console.log(warnings); + throw new Error("Unexpected webpack warning."); + } + + if (assets.length > 1) { + console.log(assets); + throw new Error("Unexpected assets."); + } + + const outputFileName = assets[0].name; + const outputFile = path.join(TEMPORARY_DIRECTORY, outputFileName); + const require = createRequire(import.meta.url); + + try { + if (file.output.format !== "esm") { + require(outputFile); + } else { + await import(url.pathToFileURL(outputFile)); + } + } catch (error) { + console.log(`'${outputFileName}' is not functional.`); + throw error; + } + + console.log(" Passed."); +}