Skip to content

Commit

Permalink
fix(inferencer): prism theme imports in ESM builds (#5836)
Browse files Browse the repository at this point in the history
  • Loading branch information
aliemir committed Apr 24, 2024
1 parent ba719f6 commit cc6285d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .changeset/happy-poets-smash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@refinedev/inferencer": patch
---

fix: inferencer now should work with ESM builds

Fixed the issue with `prism-react-renderer` theme imports not being resolved correctly in ESM builds.
2 changes: 2 additions & 0 deletions packages/inferencer/tsup.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { defineConfig } from "tsup";

import { lodashReplacePlugin } from "../shared/lodash-replace-plugin";
import { prismReactRendererThemeReplacePlugin } from "../shared/prism-react-renderer-theme-replace-plugin";
import { markAsExternalPlugin } from "../shared/mark-as-external-plugin";
import { removeTestIdsPlugin } from "../shared/remove-test-ids-plugin";
import { tablerCjsReplacePlugin } from "../shared/tabler-cjs-replace-plugin";
Expand All @@ -26,6 +27,7 @@ export default defineConfig({
tablerCjsReplacePlugin,
removeTestIdsPlugin,
lodashReplacePlugin,
prismReactRendererThemeReplacePlugin,
markAsExternalPlugin,
],
loader: {
Expand Down
29 changes: 29 additions & 0 deletions packages/shared/prism-react-renderer-theme-replace-plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Plugin } from "esbuild";

export const prismReactRendererThemeReplacePlugin: Plugin = {
name: "prismReactRendererThemeReplace",
setup: (build) => {
if (build.initialOptions.format === "esm") {
build.onEnd(async (args) => {
const prismReactRendererThemeImportRegexp =
/from\s?"prism-react-renderer\/themes\/(\w*?)"/g;
const prismReactRendererThemeEsmImport =
'from "prism-react-renderer/themes/$1/index.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(
prismReactRendererThemeImportRegexp,
prismReactRendererThemeEsmImport,
);
jsOutputFile.contents = new TextEncoder().encode(newStr);
}
});
}
},
};

0 comments on commit cc6285d

Please sign in to comment.