-
-
Notifications
You must be signed in to change notification settings - Fork 410
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 import to React #110
Comments
Shiki is a server-side library, not for client-side. You have to use it in a NodeJS environment. |
As of v0.9 - shiki can run in a browser, so maybe someone can work on validating it works in React? |
I was able use it with React.js, prepared a very basic demo with CRA: https://github.com/i9or/react-shiki-demo Great work, thanks for the browser support! Although should mention a few things, which I might not get right:
Any help would be appreciated. |
@i9or Thanks for the detailed description and repro. For 2. and 3. should be fixed in the recent commits. For 1. You can use your current solution or via third-party CDNs like unpkg or jsdelivr. If you'd like to bundle those file, you can do them this way: import * as shiki from 'shiki';
import themeNord from 'shiki/themes/nord.json'
import langTS from 'shiki/languages/typescript.tmLanguage.json'
import langTSX from 'shiki/languages/tsx.tmLanguage.json'
import onigasm from 'arraybuffer-loader!shiki/dist/onigasm.wasm'
shiki.setOnigasmWASM(onigasm)
const highlighter = await shiki.getHighlighter({
theme: 'nord',
themes: [themeNord],
langs: [{
id: 'typescript',
scopeName: langTS.scopeName,
grammar: langTS,
aliases: ['ts']
}, {
id: 'tsx',
scopeName: langTSX.scopeName,
grammar: langTSX
}],
})
const highlightedCode = highlighter.codeToHtml(SAMPLE_CODE, 'tsx'); |
@orta Fixed some bugs and types for this, can you have a look and maybe draft a new release? Thanks. |
I can, and I have - this is shipped in 0.9.2 👍🏻 |
Thanks for a quick fix and suggestions, @antfu. import onigasm from 'arraybuffer-loader!shiki/dist/onigasm.wasm' in CRA generated app, issues with the loader usage (something "magic header not detected"). Haven't tried to eject the app though. Most probably a webpack configuration issue. Anyways, decided to quickly update my demo with minimal Webpack setup and now it works perfectly fine (again the link for anyone interested: https://github.com/i9or/react-shiki-demo) |
Was able to add TypeScript support with one minor issue. Once I set declare module "*.json"; Not a big issue for me personally, but still wanted to share. |
The original issues seems to have been fixed. |
Hi, I am not able to import shiki into a react app. I am getting
Uncaught TypeError: fs.readFileSync is not a function
error.Here's my code for reference.
The text was updated successfully, but these errors were encountered: