-
-
Notifications
You must be signed in to change notification settings - Fork 330
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
Can't load shiki vscode-oniguruma in Next 13 react server component #398
Comments
For the minimal reproduction see github.com/selenecodes/next13-rsc-shiki-wasm-not-found |
I'm expecting same issue. |
Another workaround is to enable the experimental serverComponentsExternalPackages option in your next.config.js: /** @type {import('next').NextConfig} */
const nextConfig = {
experimental: {
appDir: true,
serverComponentsExternalPackages: ['vscode-oniguruma', 'shiki'],
},
};
module.exports = nextConfig; lib/shiki.js: import { getHighlighter } from 'shiki'
const highlighterPromise = getHighlighter({
theme: 'nord',
langs: ['javascript', 'python'],
})
export async function hightlight(code, language) {
const highlighter = await highlighterPromise
const output = highlighter.codeToHtml(code, { lang: language })
return output
} app/page.js import { highlight } from 'lib/shiki'
const sampleCode = `import * as React from 'react';
import './App.css';
import Hello from './components/Hello';
const logo = require('./logo.svg');
function App() {
return (
<div className="App">
<div className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<h2>Welcome to React</h2>
</div>
<p className="App-intro">
To get started, edit <code>src/App.tsx</code> and save to reload.
</p>
<Hello name="TypeScript" />
</div>
);
}
export default App;`
export default async function Page() {
const html = await highlight(sampleCode, 'js')
return <div dangerouslySetInnerHTML={{ __html: html }}/>
} |
Can confirm this works perfectly, thanks @ISnackable |
I also confirm this works |
Thanks so much! This worked. I've been trying to get this to work for a while. There is a small typo in the example you shared: // lib/shiki.js:
- export async function hightlight(code, language) {
+ export async function highlight(code, language) { I put together a minimal app to test this as I was having some small issues getting it set up in my project, hopefully it is useful for someone :) |
Shiki and vscode-oniguruma currently use node.js-specific APIs and require the user to add these two packages to their `next.config.js` in the `serverComponentsExternalPackages` section. Related issues: - shikijs/shiki#398 - vercel#44316
…44968) fixes #44316 fixes shikijs/shiki#398 Co-authored-by: JJ Kasper <jj@jjsweb.site>
Unfortunately the above workaround doesn't seem to work if the NextJS app is part of an NPM workspace 😢 Here is a workaround script I used to symlink my subfolder
const symlinkDir = require("symlink-dir");
const path = require("path");
const rootModules = path.resolve(__dirname, "../../../../node_modules");
const siteModules = path.resolve(__dirname, "../../../node_modules");
const modules = ["vscode-oniguruma", "shiki"];
modules.forEach((module) => {
const from = path.resolve(`${rootModules}/${module}`);
const to = path.resolve(`${siteModules}/${module}`);
console.log(`Linking ${from} to ${to}`);
symlinkDir(from, to).catch((err) => console.error(err));
});
{
"scripts": {
"prepare": "npm run link-shiki",
"link-shiki": "node ./src/lib/ci/symlink.js"
},
} |
I'm using a component that does client-side codeblocks: To make that work I do the following: cd public
ln -s ../node_modules/shiki/dist dist
ln -s ../node_modules/shiki/themes themes
ln -s ../node_modules/shiki/languages languages
git add public
git commit -m "Add symlinks for shiki client-side integration" |
I did this pretty simply - zikaari/onigasm#2 (comment) |
I'm trying to use Shiki in a react server component; however, the oniguruma wasm file can't be found. I've tried setting the wasm path; however, when reading through the documentation, this isn't possible for the node.js environment.
Issue:
What I've tried to do to resolve it:
onig.wasm
to my src folderonig.wasm
in theshiki.getHighlighter
optionsPossible solutions:
onig.wasm
from in the node.js environment. (see theshiki.getHighlighter
paths -> wasm section. (this option currently only works in browser environments*)The text was updated successfully, but these errors were encountered: