diff --git a/scripts/build/bundler.mjs b/scripts/build/bundler.mjs index 63246e362..78eba011a 100644 --- a/scripts/build/bundler.mjs +++ b/scripts/build/bundler.mjs @@ -15,10 +15,11 @@ import esbuildPluginUmd from "./esbuild-plugins/umd.mjs"; import esbuildPluginVisualizer from "./esbuild-plugins/visualizer.mjs"; import bundles from "./config.mjs"; -const { __dirname, json } = createEsmUtils(import.meta); +const { __dirname, json, require } = createEsmUtils(import.meta); const packageJson = json.loadSync("../../package.json"); const umdTarget = browserslistToEsbuild(packageJson.browserslist); +const EMPTY_MODULE_REPLACEMENT = { contents: "" }; function getBabelConfig(bundle) { const config = { @@ -67,7 +68,7 @@ function getBabelConfig(bundle) { return config; } -async function* getEsbuildOptions(bundle, buildOptions) { +function* getEsbuildOptions(bundle, buildOptions) { const replaceStrings = { // `tslib` exports global variables "createExporter(root": "createExporter({}", @@ -113,9 +114,8 @@ async function* getEsbuildOptions(bundle, buildOptions) { // Universal bundle only use version info from package.json // Replace package.json with `{version: "{VERSION}"}` replaceModule[path.join(PROJECT_ROOT, "package.json")] = { - contents: `module.exports = ${JSON.stringify({ - version: packageJson.version, - })};`, + contents: JSON.stringify({ version: packageJson.version }), + loader: "json", }; // Replace parser getters with `undefined` @@ -128,13 +128,11 @@ async function* getEsbuildOptions(bundle, buildOptions) { "src/language-markdown/parsers.js", "src/language-yaml/parsers.js", ]) { - replaceModule[path.join(PROJECT_ROOT, file)] = { contents: "" }; + replaceModule[path.join(PROJECT_ROOT, file)] = EMPTY_MODULE_REPLACEMENT; } // Prevent `esbuildPluginNodeModulePolyfills` include shim for this module - replaceModule.assert = { - contents: await fs.readFile(path.join(__dirname, "./shims/assert.cjs")), - }; + replaceModule.assert = require.resolve("./shims/assert.cjs"); } let shouldMinify = buildOptions.minify; @@ -246,7 +244,7 @@ async function runBuild(bundle, esbuildOptions, buildOptions) { } async function* createBundle(bundle, buildOptions) { - for await (const esbuildOptions of getEsbuildOptions(bundle, buildOptions)) { + for (const esbuildOptions of getEsbuildOptions(bundle, buildOptions)) { const { outfile: file } = esbuildOptions; if ( diff --git a/scripts/build/esbuild-plugins/replace-module.mjs b/scripts/build/esbuild-plugins/replace-module.mjs index 2b1147f56..7f166f6d8 100644 --- a/scripts/build/esbuild-plugins/replace-module.mjs +++ b/scripts/build/esbuild-plugins/replace-module.mjs @@ -1,40 +1,55 @@ +import { dirname } from "node:path"; + export default function esbuildPluginReplaceModule(replacements = {}) { return { name: "replace-module", setup(build) { - build.onLoad({ filter: /./ }, ({ path }) => { - if (!Reflect.has(replacements, path)) { + build.onLoad({ filter: /./ }, ({ path, namespace }) => { + let options = replacements[path]; + + if (!options) { return; } - let replacement = replacements[path]; + options = + typeof options === "string" ? { path: options } : { ...options }; - if (typeof replacement === "string") { - replacement = { path: replacement }; + let { + path: file, + resolveDir, + contents, + loader = "js", + external: isExternal, + } = options; + + // Make this work with `@esbuild-plugins/node-modules-polyfill` plugin + if ( + !resolveDir && + namespace === "node-modules-polyfills-commonjs" && + file + ) { + resolveDir = dirname(file); } - if (replacement.external) { + if (isExternal) { + if (!file) { + throw new Error("Missing external module path."); + } + // Prevent `esbuild` to resolve - return { - contents: ` - module.exports = require.call(undefined, ${JSON.stringify( - `${replacement.path}` - )}); - `, - }; + contents = ` + const entry = ${JSON.stringify(`${file}`)}; + module.exports = require(entry); + `; + } else { + if (file) { + contents = ` + module.exports = require(${JSON.stringify(`${file}`)}); + `; + } } - if (replacement.path) { - replacement = { - contents: ` - module.exports = require(${JSON.stringify( - `${replacement.path}` - )}); - `, - }; - } - - return replacement; + return { contents, loader, resolveDir }; }); }, };