From 0c7a6df2b51245453fe7a13f22194521a74e1fe5 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Wed, 23 Mar 2022 10:44:50 +0800 Subject: [PATCH 1/5] Add test for #12493 --- scripts/tools/bundle-test/index.js | 31 ++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/scripts/tools/bundle-test/index.js b/scripts/tools/bundle-test/index.js index 7bac6724d10c..2150dc876da4 100644 --- a/scripts/tools/bundle-test/index.js +++ b/scripts/tools/bundle-test/index.js @@ -1,6 +1,7 @@ import { fileURLToPath } 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"; @@ -39,7 +40,11 @@ const TEMPORARY_DIRECTORY = fileURLToPath(new URL("./.tmp", import.meta.url)); name === "standalone.js" || name === "doc.js" ) - .map((name) => ({ name, file: path.join(DIST_DIR, name) })), + .map((name) => ({ + displayName: name, + name, + file: path.join(DIST_DIR, name), + })), (await fs.readdir(esmFilesDirectory)).map((name) => ({ displayName: `esm/${name}`, name, @@ -48,26 +53,44 @@ const TEMPORARY_DIRECTORY = fileURLToPath(new URL("./.tmp", import.meta.url)); ].flat(); for (const { displayName, name, file } of files) { - console.log(`${displayName || name}: `); + console.log(`${displayName}: `); + const isEsmModule = name.endsWith(".mjs"); const stats = await runWebpack({ mode: "production", entry: file, output: { path: TEMPORARY_DIRECTORY, - filename: `${name}.[contenthash:7].js`, + filename: `${name}.[contenthash:7].${isEsmModule ? "mjs" : "cjs"}`, }, performance: { hints: false }, optimization: { minimize: false }, }); const result = stats.toJson(); - const { warnings } = result; + 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."); } })(); From 942b60a57f001845b02858c651ca53687e77626b Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Wed, 23 Mar 2022 10:56:53 +0800 Subject: [PATCH 2/5] Add fix from 12506 --- scripts/build/bundler.mjs | 5 ++--- scripts/build/config.mjs | 7 ------- 2 files changed, 2 insertions(+), 10 deletions(-) diff --git a/scripts/build/bundler.mjs b/scripts/build/bundler.mjs index 001e274087c7..f8aa0de6c8b5 100644 --- a/scripts/build/bundler.mjs +++ b/scripts/build/bundler.mjs @@ -76,11 +76,10 @@ const bundledFiles = [ function* getEsbuildOptions(bundle, buildOptions) { const replaceModule = [ - // `tslib` exports global variables + // #12493 { module: require.resolve("tslib"), - find: "factory(createExporter(root", - replacement: "factory(createExporter({}", + path: require.resolve("tslib").replace(/tslib\.js$/, "tslib.es6.js"), }, ]; diff --git a/scripts/build/config.mjs b/scripts/build/config.mjs index 77aaf6720716..5241d8255812 100644 --- a/scripts/build/config.mjs +++ b/scripts/build/config.mjs @@ -235,13 +235,6 @@ const parsers = [ }, { input: "src/language-yaml/parser-yaml.js", - replaceModule: [ - // Use `tslib.es6.js`, so we can avoid `globalThis` shim - { - module: require.resolve("tslib"), - path: require.resolve("tslib").replace(/tslib\.js$/, "tslib.es6.js"), - }, - ], }, ].map((bundle) => { const { name } = bundle.input.match( From c691b0055629483a48efff5c78e59226d64b5200 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Wed, 23 Mar 2022 11:10:12 +0800 Subject: [PATCH 3/5] Add a simple comment --- scripts/build/bundler.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/build/bundler.mjs b/scripts/build/bundler.mjs index f8aa0de6c8b5..5a0ade6d9db9 100644 --- a/scripts/build/bundler.mjs +++ b/scripts/build/bundler.mjs @@ -76,7 +76,7 @@ const bundledFiles = [ function* getEsbuildOptions(bundle, buildOptions) { const replaceModule = [ - // #12493 + // #12493, not sure what the problem is, but replace the cjs version with esm version seems fix it { module: require.resolve("tslib"), path: require.resolve("tslib").replace(/tslib\.js$/, "tslib.es6.js"), From 287ca64a3ff461041bc409333ff8ccb40157da05 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Wed, 23 Mar 2022 11:27:40 +0800 Subject: [PATCH 4/5] Update changelog --- changelog_unreleased/misc/12485.md | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/changelog_unreleased/misc/12485.md b/changelog_unreleased/misc/12485.md index ba690a3bb216..aeff152a7b40 100644 --- a/changelog_unreleased/misc/12485.md +++ b/changelog_unreleased/misc/12485.md @@ -1,3 +1,6 @@ -#### Make artifact friendly for `webpack` (#12485 by @fisker) +#### Make artifact friendly for webpack (#12485, #12511 by @fisker) -Previously, when bundling our UMD files `standalone.js`, `parser-typescript.js`, `webpack` warn about "Critical dependency: the request of a dependency is an expression", now this is fixed. +Fixes two problems when bundling our UMD files with webpack: + +- A error `` `....__exportStar` is not a function`` throws when running the bundles. +- Some files cause warning about `"Critical dependency: the request of a dependency is an expression"`. From 5bbb4370c581e76f41249b5db05cbae0896cb9b6 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Wed, 23 Mar 2022 11:29:17 +0800 Subject: [PATCH 5/5] Update changelog --- changelog_unreleased/misc/12485.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/changelog_unreleased/misc/12485.md b/changelog_unreleased/misc/12485.md index aeff152a7b40..a2372ffb62c8 100644 --- a/changelog_unreleased/misc/12485.md +++ b/changelog_unreleased/misc/12485.md @@ -2,5 +2,5 @@ Fixes two problems when bundling our UMD files with webpack: -- A error `` `....__exportStar` is not a function`` throws when running the bundles. +- A error `` "`....__exportStar` is not a function" `` throws when running the bundles. - Some files cause warning about `"Critical dependency: the request of a dependency is an expression"`.