-
-
Notifications
You must be signed in to change notification settings - Fork 380
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
Bundling for browser usage #22
Comments
It should be possible, but I haven't had time for it. You should start by making Onigasm runnable in browsers. |
If you're using Vue and gridsome, there's a plugin for it. https://github.com/EldoranDev/gridsome-plugin-remark-shiki @ipelekhan |
That library actually still runs on node. |
Hmm, I looked into this a bit but I'm left a bit confused. As it is One idea could be to introduce |
@canibanoglu Even if that's doable, is it desirable at all? Loading several MB of WASM and then the grammars to do syntax highlighting on client side? |
Oh, I quite agree with you there. I was just thinking that changing the way Instead of reading the WASM file from the file system with On a kinda related note: Is there a reason for not using |
vscode-textmate depends on either onigasm or oniguruma so I was using it. |
Related: microsoft/monaco-editor#1915 |
Blocked by zikaari/onigasm#20 Hopefully |
Author of It works perfectly fine with Input // src/index.ts
await loadWASM(await import('onigasm/lib/onigasm.wasm'))` Output // out/bundle.eu21df1.js
loadWASM('build/onigasm.8ug81e3.wasm').then(...)`
|
This is in with 0.9.0 - see the readme and #109 |
So if anyone arrives here having troubles ✅ Bundling and self-hosting with Webpack 5 / Next jsthis is a way to make it work. When you simply import and use shiki like this import { getHighlighter } from "shiki";
getHighlighter().then(highlighter => /* do stuff */); You might get the error
This is not really a MIME-Type-problem. What is happening here is that the browser bundle loads a wasm file (and some other files) from the default location
and those files are not there. You can change the path as described here, but you might also want to serve those files yourself. For this, you can copy the npm package to your static files in next
const path = require("path");
const CopyPlugin = require("copy-webpack-plugin");
const nextConfig = {
webpack: (config, { webpack }) => {
/**
* Copying the whole npm package of shiki to static/shiki because it
* loads some files from a "cdn" in the browser (semi-hacky)
* @see https://github.com/shikijs/shiki#specify-a-custom-root-directory
*/
config.plugins.push(
new CopyPlugin({
patterns: [
{
from: path.resolve(path.dirname(require.resolve("shiki")), ".."),
to: "static/shiki/",
},
],
})
);
return config;
},
};
module.exports = nextConfig; And then set the CDN to your own served folder - import { getHighlighter } from "shiki";
+ import { getHighlighter, setCDN } from "shiki";
+ setCDN("/_next/static/shiki/");
getHighlighter().then(highlighter => /* do stuff */); |
I'm trying to do like you suggested, but I'm getting a lot of errors in nextjs. |
It outputs no file nord.json at .... .next/server/app/(blog)/article/themes/nord.json |
next 13 server components |
I found this issue looking for how to use shiki with Next.js, if you did as well the discussion you are looking for is likely here: #398 |
If the problem is WASM bundling in Webpack this works - zikaari/onigasm#2 (comment) |
This worked for me! Thanks @LukasBombach |
Hi, not really an issue, just wondering if it's actually possible to use Shiki in browser instead of node?
I have tried to bundle it with webpack and https://github.com/Jam3/babel-plugin-static-fs plugin, but still getting errors related to
fs
module calls forOnigasm
and etc.The text was updated successfully, but these errors were encountered: