-
-
Notifications
You must be signed in to change notification settings - Fork 360
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
1.18.0 seems causing bundle errors on Next.js >= 14.2.x #784
Comments
Hmm I'm not quite sure what does the logic in Here's my example tested in CodeSandbox: "use client";
import { useEffect, useState } from "react";
import { BundledLanguage, createHighlighter } from "shiki";
const snippets = [
{
lang: "ts",
code: `export const hello: string = "Hello"`,
},
{
lang: "java",
code: `System.out.println("Hello")`,
},
{
lang: "py",
code: `print(f"Hello")`,
},
];
export default function Page() {
return <div>{snippets.map(item => <Highlight key={item.lang} {...item} />)}</div>;
}
const highlighter = createHighlighter({
langs: ["ts"], // preload language
themes: ["vitesse-dark"],
});
function Highlight({ code, lang }: { code: string; lang: string }) {
const [html, setHtml] = useState<string>();
useEffect(() => {
// TEST: do it on browser only
async function run() {
const instance = await highlighter;
if (!instance.getLoadedLanguages().includes(lang)) {
console.log('load', lang)
// the language may not exist, we assume it is always valid
await instance.loadLanguage(lang as BundledLanguage);
}
setHtml(instance.codeToHtml(code, {
lang,
theme: 'vitesse-dark'
}))
}
void run()
}, [code, lang]);
if (!html) return null
return <div dangerouslySetInnerHTML={{ __html: html }} />
} Notice that the function As you are using React, I would suggest you to check https://shiki.style/packages/next#custom-components, which illustrates the way to render JSX instead of HTML with Shiki. |
Shiki v1.18 does not introduce any functionality changes (v1.17.7...v1.18.0). The only thing notable is #781 - which seems not related to your case. The example and repo has too much React related logic for me to take a look, can you provide a minimal reproduction instead? |
I'm not sure if it's the same issue, but we had to revert to
|
So my issue was specifically related to #781 and the The v1.17.7 version of Object.freeze({ "displayName": ... }) And the v1.18.0 version (backticks with serialized JSON): Object.freeze(JSON.parse(`{"displayName": ... `)) And the Next.js bundled version of v1.18.0 (single quotes with corrupted serialized JSON): Object.freeze(JSON.parse('{"displayName": ... ')) Both of Shikijs versions are valid, but the Next.js bundler corrupts the
|
Thanks for the investigation! I guess that sounds more like a Webpack bug? Would you create a repro to webpack instead? |
I tried briefly with Webpack 5.87.0, it seems to bundle fine, maybe related to Next.js' specific setup? |
Oh you are right, also encountered the same problem when using Shiki in a Next.js app. I was using Shiki in a package in my monorepo. I found installing shiki to the app workspace can fix this issue, it’s very likely some production build optimisation that bundles peer dependency. I’m not familiar with Next.js bundler so perhaps opening an issue on Next.js helps. Also maybe we can revert this change for now? It’s a performance improvement so reverting the change shouldn’t affect anything. |
If this is confirmed as a bug of Next.js, I think it would be a rather severe one. It would be better to not revert on our side which would shallow the bug for them. |
As most of you have already noticed I created a bug in Next.js, but really have no idea when it will get fixed (might need to get pushed further upstream to webpack and/or babel and that could take a while). It might be worth updating the issue title to specifically mention |
I can confirm this is fixed by PR #795 on the latest release of Shiki, tested on Next.js 14.2.13. |
Same, #795 fixed the issue for me as well. |
I have checked with v1.21.0. And it seems work now. and I have unpinned the shiki and upgrade to the latest version.(lobehub/lobe-chat#4218) I think this issue can be closed now. Thanks for your all great job! 🎉 |
Validations
Describe the bug
Thank you for bring this amazing work! LobeChat have this beautiful code highlight all credit to you~
But it seems there is a breaking change for 1.18.0 which case the code highlight invalid.
So I pin to 1.17.7 temporarily with the PR to fix it.
Our usage code is:
and the code in
useHighlight
is:the source is here: https://github.com/lobehub/lobe-ui/blob/master/src/hooks/useHighlight.ts#L41
I dig it a little and find that the dom seems different:
v1.17.7 's dom:
but in the v1.18.0's dom:
it means that in the v1.18.0 the control flow is go to the condition of
isLoading || !data ? ( <div className={cx(styles.unshiki, className)} style={style}> <pre> <code>{children.trim()}</code> </pre> </div> )
.and then I try to log the error, and it show up with:
Why it's worked in v1.17.7 but breaking in the 1.18.0 ?
Reproduction
https://github.com/lobehub/lobe-chat/tree/reproduction/shiki-1.18.0
Contributes
The text was updated successfully, but these errors were encountered: