Files
test-repo/scripts/build/esbuild-plugins/evaluate.mjs
T
2022-09-27 11:13:44 +08:00

37 lines
861 B
JavaScript

import createEsmUtils from "esm-utils";
const { importModule } = createEsmUtils(import.meta);
export default function esbuildPluginEvaluate() {
return {
name: "evaluate",
setup(build) {
build.onLoad({ filter: /\.evaluate\.c?js$/ }, async ({ path }) => {
let data = await importModule(path);
if (Object.hasOwn(data, "default")) {
if (Object.keys(data).length !== 1) {
throw new TypeError("Mixed export not allowed.");
}
data = data.default;
}
const json = JSON.stringify(
data,
(_, v) => {
if (typeof v === "function") {
throw new TypeError("Cannot evaluate functions.");
}
return v;
},
2
);
return { loader: "json", contents: json };
});
},
};
}