openpanel/packages/shared/remove-test-ids-plugin.ts
Stefan Pejcic 09f9f9502d packages
2024-11-07 19:03:37 +01:00

28 lines
815 B
TypeScript

import type { Plugin } from "esbuild";
export const removeTestIdsPlugin: Plugin = {
name: "react-remove-testids",
setup(build) {
build.onEnd((args) => {
// data-testid regexp
const regexp = /("data-testid":)(.*?)(?:(,)|(})|(\n))/gi;
// output files with `*.js`
const jsOutputFiles =
args.outputFiles?.filter(
(el) =>
el.path.endsWith(".cjs") ||
el.path.endsWith(".mjs") ||
el.path.endsWith(".js"),
) ?? [];
// replace data-testid in output files
for (const jsOutputFile of jsOutputFiles) {
const str = new TextDecoder("utf-8").decode(jsOutputFile.contents);
const newStr = str.replace(regexp, "$4");
jsOutputFile.contents = new TextEncoder().encode(newStr);
}
});
},
};