-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(inferencer): prism theme imports in ESM builds (#5836)
- Loading branch information
Showing
3 changed files
with
38 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
packages/shared/prism-react-renderer-theme-replace-plugin.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}); | ||
} | ||
}, | ||
}; |