openpanel/packages/shared/dayjs-esm-replace-plugin.ts
Stefan Pejcic 09f9f9502d packages
2024-11-07 19:03:37 +01:00

25 lines
809 B
TypeScript

import type { Plugin } from "esbuild";
export const dayJsEsmReplacePlugin: Plugin = {
name: "dayJsEsmReplace",
setup: (build) => {
if (build.initialOptions.format === "esm") {
build.onEnd((args) => {
const dayJsImportRegexp = /from\s?"dayjs\/plugin\/(\w*?)"/g;
const dayJsEsmImport = 'from "dayjs/plugin/$1.js"';
const jsOutputFiles =
args.outputFiles?.filter(
(el) => el.path.endsWith(".mjs") || el.path.endsWith(".js"),
) ?? [];
for (const jsOutputFile of jsOutputFiles) {
const str = new TextDecoder("utf-8").decode(jsOutputFile.contents);
const newStr = str.replace(dayJsImportRegexp, dayJsEsmImport);
jsOutputFile.contents = new TextEncoder().encode(newStr);
}
});
}
},
};