OpenPanel/packages/shared/remove-test-ids-plugin.ts
Stefan Pejcic 8595a9f4e5 back
2024-05-08 19:58:53 +02:00

25 lines
825 B
TypeScript

import { Plugin } from "esbuild";
export const removeTestIdsPlugin: Plugin = {
name: "react-remove-testids",
setup(build) {
build.onEnd(async (args) => {
// data-testid regexp
const regexp = /("data-testid":)(.*?)(?:(,)|(}))/gi;
// output files with `*.js`
const jsOutputFiles =
args.outputFiles?.filter((el) => 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);
}
});
},
};