Skip to content

Commit

Permalink
feat!: add unwasm condition in exports (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu authored Jan 14, 2024
1 parent da394fb commit 9630092
Show file tree
Hide file tree
Showing 14 changed files with 34 additions and 36 deletions.
9 changes: 3 additions & 6 deletions docs/guide/install.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ When importing `shikiji`, all the themes and languages are bundled as async chun
import { getHighlighterCore } from 'shikiji/core'

// `shikiji/wasm` contains the wasm binary inlined as base64 string.
import { getWasmInlined } from 'shikiji/wasm'
import getWasm from 'shikiji/wasm'

// directly import the theme and language modules, only the ones you imported will be bundled.
import nord from 'shikiji/themes/nord.mjs'
Expand All @@ -174,7 +174,7 @@ const highlighter = await getHighlighterCore({
// or a getter that returns custom grammar
async () => JSON.parse(await fs.readFile('my-grammar.json', 'utf-8'))
],
loadWasm: getWasmInlined
loadWasm: getWasm
})

// optionally, load themes and languages after creation
Expand Down Expand Up @@ -276,10 +276,7 @@ import nord from 'shikiji/themes/nord.mjs'
import js from 'shikiji/langs/javascript.mjs'

// import wasm as assets
import wasm from 'shikiji/onig.wasm'

// load wasm outside of `fetch` so it can be reused
await loadWasm(obj => WebAssembly.instantiate(wasm, obj))
await loadWasm(import('shikiji/onig.wasm'))

export default {
async fetch() {
Expand Down
4 changes: 2 additions & 2 deletions docs/packages/markdown-it.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ By default, the full bundle of `shikiji` will be imported. If you are using a [f
import MarkdownIt from 'markdown-it'
import { fromHighlighter } from 'markdown-it-shikiji/core'
import { getHighlighterCore } from 'shikiji/core'
import { getWasmInlined } from 'shikiji/wasm'
import getWasm from 'shikiji/wasm'

const highlighter = await getHighlighterCore({
themes: [
Expand All @@ -44,7 +44,7 @@ const highlighter = await getHighlighterCore({
langs: [
import('shikiji/langs/javascript.mjs'),
],
loadWasm: getWasmInlined
loadWasm: getWasm
})

const md = MarkdownIt()
Expand Down
4 changes: 2 additions & 2 deletions docs/packages/rehype.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ import rehypeShikijiFromHighlighter from 'rehype-shikiji/core'

import { fromHighlighter } from 'markdown-it-shikiji/core'
import { getHighlighterCore } from 'shikiji/core'
import { getWasmInlined } from 'shikiji/wasm'
import getWasm from 'shikiji/wasm'

const highlighter = await getHighlighterCore({
themes: [
Expand All @@ -61,7 +61,7 @@ const highlighter = await getHighlighterCore({
langs: [
import('shikiji/langs/javascript.mjs'),
],
loadWasm: getWasmInlined
loadWasm: getWasm
})

const raw = await fs.readFile('./input.md')
Expand Down
6 changes: 3 additions & 3 deletions packages/shikiji-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
"types": "./dist/index.d.mts",
"default": "./dist/index.mjs"
},
"./wasm": {
"types": "./dist/wasm.d.mts",
"default": "./dist/wasm.mjs"
"./wasm-inlined": {
"types": "./dist/wasm-inlined.d.mts",
"default": "./dist/wasm-inlined.mjs"
},
"./textmate": {
"types": "./dist/textmate.d.mts",
Expand Down
2 changes: 1 addition & 1 deletion packages/shikiji-core/rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const entries = [
'src/index.ts',
'src/types.ts',
'src/textmate.ts',
'src/wasm.ts',
'src/wasm-inlined.ts',
]

const plugins = [
Expand Down
9 changes: 9 additions & 0 deletions packages/shikiji-core/src/wasm-inlined.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { WebAssemblyInstantiator } from './oniguruma'

const getWasm: WebAssemblyInstantiator = async (info) => {
// @ts-expect-error this will be compiled to ArrayBuffer
const binray: ArrayBuffer = await import('vscode-oniguruma/release/onig.wasm').then(m => m.default)
return WebAssembly.instantiate(binray, info).then(wasm => wasm.instance.exports)
}

export default getWasm
11 changes: 0 additions & 11 deletions packages/shikiji-core/src/wasm.ts

This file was deleted.

2 changes: 2 additions & 0 deletions packages/shikiji/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"default": "./dist/core.mjs"
},
"./wasm": {
"unwasm": "./dist/onig.wasm",
"types": "./dist/wasm.d.mts",
"default": "./dist/wasm.mjs"
},
Expand All @@ -49,6 +50,7 @@
"types": "./dist/bundle-web.d.mts",
"default": "./dist/bundle-web.mjs"
},
"./onig.wasm": "./dist/onig.wasm",
"./dist/*": "./dist/*",
"./*": "./dist/*"
},
Expand Down
3 changes: 2 additions & 1 deletion packages/shikiji/rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ const entries = [

const external = [
'shikiji-core',
'shikiji-core/wasm',
'shikiji-core/wasm-inlined',
'shikiji-core/types',
'shikiji/wasm',
]

const plugins = [
Expand Down
6 changes: 3 additions & 3 deletions packages/shikiji/src/bundle-full.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import type { HighlighterGeneric } from 'shikiji-core'
import getWasm from 'shikiji/wasm'
import { createSingletonShorthands, createdBundledHighlighter } from './core'
import type { BundledLanguage } from './assets/langs-bundle-full'
import type { BundledTheme } from './themes'
import { bundledLanguages } from './assets/langs-bundle-full'
import { bundledThemes } from './themes'
import { getWasmInlined } from './wasm'

export * from './core'
export * from './themes'
export * from './assets/langs-bundle-full'
export * from './wasm'
export { default as getWasmInlined } from 'shikiji/wasm'

export type Highlighter = HighlighterGeneric<BundledLanguage, BundledTheme>

Expand All @@ -29,7 +29,7 @@ export const getHighlighter = /* @__PURE__ */ createdBundledHighlighter<
>(
bundledLanguages,
bundledThemes,
getWasmInlined,
getWasm,
)

export const {
Expand Down
6 changes: 3 additions & 3 deletions packages/shikiji/src/bundle-web.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import type { HighlighterGeneric } from 'shikiji-core'
import getWasm from 'shikiji/wasm'
import { createSingletonShorthands, createdBundledHighlighter } from './core'
import type { BundledLanguage } from './assets/langs-bundle-web'
import type { BundledTheme } from './themes'
import { bundledLanguages } from './assets/langs-bundle-web'
import { bundledThemes } from './themes'
import { getWasmInlined } from './wasm'

export * from './core'
export * from './themes'
export * from './assets/langs-bundle-web'
export * from './wasm'
export { default as getWasmInlined } from 'shikiji/wasm'

export type Highlighter = HighlighterGeneric<BundledLanguage, BundledTheme>

Expand All @@ -29,7 +29,7 @@ export const getHighlighter = /* @__PURE__ */ createdBundledHighlighter<
>(
bundledLanguages,
bundledThemes,
getWasmInlined,
getWasm,
)

export const {
Expand Down
3 changes: 1 addition & 2 deletions packages/shikiji/src/wasm.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export { default } from 'shikiji-core/wasm'
export * from 'shikiji-core/wasm'
export { default } from 'shikiji-core/wasm-inlined'
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
"moduleResolution": "Bundler",
"paths": {
"shikiji-core/types": ["./packages/shikiji-core/src/types.ts"],
"shikiji-core/wasm": ["./packages/shikiji-core/src/wasm.ts"],
"shikiji-core/wasm-inlined": ["./packages/shikiji-core/src/wasm.ts"],
"shikiji-core": ["./packages/shikiji-core/src/index.ts"],
"shikiji-transformers": ["./packages/shikiji-transformers/src/index.ts"],
"shikiji-twoslash": ["./packages/shikiji-twoslash/src/index.ts"],
"vitepress-plugin-twoslash/client": ["./packages/vitepress-plugin-twoslash/src/client.ts"],
"vitepress-plugin-twoslash": ["./packages/vitepress-plugin-twoslash/src/index.ts"],
"shikiji/core": ["./packages/shikiji/src/core.ts"],
"shikiji/wasm": ["./packages/shikiji/src/wasm.ts"],
"shikiji/langs": ["./packages/shikiji/src/langs.ts"],
"shikiji/themes": ["./packages/shikiji/src/themes.ts"],
"shikiji": ["./packages/shikiji/src/index.ts"],
Expand Down
2 changes: 1 addition & 1 deletion vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default defineConfig({
],
resolve: {
alias: {
'shikiji-core/wasm': fileURLToPath(new URL('./packages/shikiji-core/src/wasm.ts', import.meta.url)),
'shikiji-core/wasm-inlined': fileURLToPath(new URL('./packages/shikiji-core/src/wasm-inlined.ts', import.meta.url)),
'shikiji-core/types': fileURLToPath(new URL('./packages/shikiji-core/src/types.ts', import.meta.url)),
'shikiji-core': fileURLToPath(new URL('./packages/shikiji-core/src/index.ts', import.meta.url)),
'shikiji-transformers': fileURLToPath(new URL('./packages/shikiji-transformers/src/index.ts', import.meta.url)),
Expand Down

0 comments on commit 9630092

Please sign in to comment.