Files
test-repo/scripts/build/esbuild-plugins/replace-module.mjs
T
dependabot[bot]andfisker Cheung 7b480b1400 Build(deps-dev): Bump esbuild from 0.14.11 to 0.14.13 (#12129)
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: fisker Cheung <lionkay@gmail.com>
2022-01-24 17:29:31 +08:00

42 lines
988 B
JavaScript

export default function esbuildPluginReplaceModule(replacements = {}) {
return {
name: "replace-module",
setup(build) {
build.onLoad({ filter: /./ }, ({ path }) => {
if (!Reflect.has(replacements, path)) {
return;
}
let replacement = replacements[path];
if (typeof replacement === "string") {
replacement = { path: replacement };
}
if (replacement.external) {
// Prevent `esbuild` to resolve
return {
contents: `
module.exports = require.call(undefined, ${JSON.stringify(
`${replacement.path}`
)});
`,
};
}
if (replacement.path) {
replacement = {
contents: `
module.exports = require(${JSON.stringify(
`${replacement.path}`
)});
`,
};
}
return replacement;
});
},
};
}