diff --git a/bench/bundle-test/index-wasm.ts b/bench/bundle-test/index-wasm.ts index e31309b1..2ad36d8f 100644 --- a/bench/bundle-test/index-wasm.ts +++ b/bench/bundle-test/index-wasm.ts @@ -1,5 +1,5 @@ import { codeToHtml, createShikiInternal } from '@shikijs/core' -import { createWasmOnigEngine } from '@shikijs/engine-oniguruma' +import { createOnigurumaEngine } from '@shikijs/engine-oniguruma' const shiki = createShikiInternal( { @@ -9,7 +9,7 @@ const shiki = createShikiInternal( themes: [ import('@shikijs/themes/vitesse-dark'), ], - engine: createWasmOnigEngine(import('@shikijs/engine-oniguruma/wasm-inlined')), + engine: createOnigurumaEngine(import('@shikijs/engine-oniguruma/wasm-inlined')), }, ) diff --git a/packages/core/src/constructors/highlighter.ts b/packages/core/src/constructors/highlighter.ts index 19821608..5c8ec3c1 100644 --- a/packages/core/src/constructors/highlighter.ts +++ b/packages/core/src/constructors/highlighter.ts @@ -16,7 +16,7 @@ import { createShikiInternalSync } from './internal-sync' * * @see http://shiki.style/guide/bundles#fine-grained-bundle */ -export async function createHighlighterCore(options: HighlighterCoreOptions = {}): Promise { +export async function createHighlighterCore(options: HighlighterCoreOptions): Promise { const internal = await createShikiInternal(options) return { @@ -39,7 +39,7 @@ export async function createHighlighterCore(options: HighlighterCoreOptions = {} * * @see http://shiki.style/guide/bundles#fine-grained-bundle */ -export function createHighlighterCoreSync(options: HighlighterCoreOptions = {}): HighlighterCore { +export function createHighlighterCoreSync(options: HighlighterCoreOptions): HighlighterCore { const internal = createShikiInternalSync(options) return { @@ -56,11 +56,11 @@ export function createHighlighterCoreSync(options: HighlighterCoreOptions export function makeSingletonHighlighterCore( createHighlighter: typeof createHighlighterCore, -): (options?: Partial) => Promise { +): (options: HighlighterCoreOptions) => Promise { let _shiki: ReturnType async function getSingletonHighlighterCore( - options: Partial = {}, + options: HighlighterCoreOptions, ): Promise { if (!_shiki) { _shiki = createHighlighter({ @@ -89,7 +89,7 @@ export const getSingletonHighlighterCore = /* @__PURE__ */ makeSingletonHighligh * @deprecated Use `createHighlighterCore` or `getSingletonHighlighterCore` instead. */ /* v8 ignore next 5 */ -export function getHighlighterCore(options: HighlighterCoreOptions = {}): Promise { +export function getHighlighterCore(options: HighlighterCoreOptions): Promise { warnDeprecated('`getHighlighterCore` is deprecated. Use `createHighlighterCore` or `getSingletonHighlighterCore` instead.') return createHighlighterCore(options) } diff --git a/packages/core/src/constructors/internal.ts b/packages/core/src/constructors/internal.ts index 75085c15..4e0b3bcb 100644 --- a/packages/core/src/constructors/internal.ts +++ b/packages/core/src/constructors/internal.ts @@ -11,10 +11,13 @@ import { createShikiInternalSync } from './internal-sync' /** * Get the minimal shiki context for rendering. */ -export async function createShikiInternal(options: HighlighterCoreOptions = {}): Promise { +export async function createShikiInternal(options: HighlighterCoreOptions): Promise { if (options.loadWasm) { warnDeprecated('`loadWasm` option is deprecated. Use `engine: createOnigurumaEngine(loadWasm)` instead.') } + if (!options.engine) { + throw new Error('`engine` option is required.') + } const [ themes, @@ -38,7 +41,7 @@ export async function createShikiInternal(options: HighlighterCoreOptions = {}): /** * @deprecated Use `createShikiInternal` instead. */ -export function getShikiInternal(options: HighlighterCoreOptions = {}): Promise { +export function getShikiInternal(options: HighlighterCoreOptions): Promise { warnDeprecated('`getShikiInternal` is deprecated. Use `createShikiInternal` instead.') return createShikiInternal(options) } diff --git a/packages/core/test/core.test.ts b/packages/core/test/core.test.ts index da61b1d8..5f58dace 100644 --- a/packages/core/test/core.test.ts +++ b/packages/core/test/core.test.ts @@ -1,7 +1,7 @@ +import { createJavaScriptRegexEngine } from '@shikijs/engine-javascript' import { createOnigurumaEngine } from '@shikijs/engine-oniguruma' import { wasmBinary } from '@shikijs/engine-oniguruma/wasm-inlined' - import js from '@shikijs/langs/javascript' import ts from '@shikijs/langs/typescript' import mtp from '@shikijs/themes/material-theme-palenight' @@ -67,6 +67,7 @@ describe('should', () => { langs: [ import('@shikijs/langs/cpp'), ], + engine: createJavaScriptRegexEngine(), }) expect(shiki.getLoadedLanguages().sort()) @@ -85,7 +86,9 @@ describe('should', () => { }) it('works without no initial langs and themes', async () => { - using shiki = await createHighlighterCore() + using shiki = await createHighlighterCore({ + engine: createJavaScriptRegexEngine(), + }) await shiki.loadLanguage(js) await shiki.loadTheme(nord) @@ -101,6 +104,7 @@ describe('should', () => { mylang: 'javascript', mylang2: 'js', // nested alias }, + engine: createJavaScriptRegexEngine(), }) await shiki.loadLanguage(js) @@ -117,6 +121,7 @@ describe('should', () => { langAlias: { js: 'typescript', }, + engine: createJavaScriptRegexEngine(), }) await shiki.loadLanguage(ts) @@ -132,6 +137,7 @@ describe('errors', () => { using shiki = await createHighlighterCore({ themes: [nord], langs: [js as any], + engine: createJavaScriptRegexEngine(), }) await expect(() => shiki.codeToHtml('console.log("Hi")', { lang: 'javascript', theme: 'invalid' })) @@ -142,6 +148,7 @@ describe('errors', () => { using shiki = await createHighlighterCore({ themes: [nord], langs: [js as any], + engine: createJavaScriptRegexEngine(), }) await expect(() => shiki.codeToHtml('console.log("Hi")', { lang: 'abc', theme: 'nord' })) @@ -175,6 +182,7 @@ describe('errors', () => { mylang: 'mylang2', mylang2: 'mylang', }, + engine: createJavaScriptRegexEngine(), }) await shiki.loadLanguage(js) @@ -187,7 +195,8 @@ describe('errors', () => { it('throw on using disposed instance', async () => { using shiki = await createHighlighterCore({ themes: [nord], - langs: [js as any], + langs: [js], + engine: createJavaScriptRegexEngine(), }) expect(shiki.codeToHtml('console.log("Hi")', { lang: 'javascript', theme: 'nord' })) diff --git a/packages/core/test/injections.test.ts b/packages/core/test/injections.test.ts index cc930160..ccade94d 100644 --- a/packages/core/test/injections.test.ts +++ b/packages/core/test/injections.test.ts @@ -4,7 +4,7 @@ import html from '@shikijs/langs/html' import ts from '@shikijs/langs/typescript' import vue from '@shikijs/langs/vue' import vl from '@shikijs/themes/vitesse-light' -import { codeToHtml, createHighlighterCore } from 'shiki' +import { codeToHtml, createHighlighterCore, createJavaScriptRegexEngine } from 'shiki' import { expect, it } from 'vitest' // Basically we need to make sure that the syntax inside `v-if` and `{{}}` is highlighted correctly. @@ -35,6 +35,7 @@ it('injections-side-effects vue', async () => { langs: [ html, ], + engine: createJavaScriptRegexEngine(), }) const code = `

{{ count * 2 }}

` @@ -60,6 +61,7 @@ it('injections-side-effects angular-html', async () => { langs: [ html, ], + engine: createJavaScriptRegexEngine(), }) const code = `

Hero List

@@ -126,6 +128,7 @@ it('injections-side-effects angular-ts', async () => { langs: [ ts, ], + engine: createJavaScriptRegexEngine(), }) const code = ` diff --git a/packages/shiki/test/cf.ts b/packages/shiki/test/cf.ts index 5a6a83c8..6f97b043 100644 --- a/packages/shiki/test/cf.ts +++ b/packages/shiki/test/cf.ts @@ -1,24 +1,21 @@ import type { LanguageRegistration } from '@shikijs/types' -import { loadWasm } from '@shikijs/engine-oniguruma' +import { createOnigurumaEngine } from '@shikijs/engine-oniguruma' import js from '@shikijs/langs/javascript' import nord from '@shikijs/themes/nord' -import { createHighlighterCore } from 'shiki/core' +import { createHighlighterCore } from 'shiki/core' // @ts-expect-error no types // eslint-disable-next-line antfu/no-import-dist import wasm from '../dist/onig.wasm' -// eslint-disable-next-line antfu/no-top-level-await -await loadWasm(wasm) - -// cloudflare also supports dynamic import -// await loadWasm(import('../dist/onig.wasm')) - export default { async fetch() { const highlighter = await createHighlighterCore({ themes: [nord], langs: [js as LanguageRegistration[]], + // cloudflare also supports dynamic import + // engine: createOnigurumaEngine(import('shiki/onig.wasm')), + engine: createOnigurumaEngine(wasm), }) return new Response( diff --git a/packages/shiki/test/get-highlighter.test.ts b/packages/shiki/test/get-highlighter.test.ts index 1264ffcd..71cc5026 100644 --- a/packages/shiki/test/get-highlighter.test.ts +++ b/packages/shiki/test/get-highlighter.test.ts @@ -1,4 +1,4 @@ -import { createOnigurumaEngine, getSingletonHighlighter } from 'shiki' +import { createJavaScriptRegexEngine, createOnigurumaEngine, getSingletonHighlighter } from 'shiki' import { expect, it } from 'vitest' import { getSingletonHighlighterCore } from '../src/core' import js from '../src/langs/javascript.mjs' @@ -17,6 +17,7 @@ it('getSingletonHighlighterCore', async () => { const shiki2 = await getSingletonHighlighterCore({ themes: [mtp], + engine: createJavaScriptRegexEngine(), }) expect(shiki1).toBe(shiki2) diff --git a/packages/types/src/options.ts b/packages/types/src/options.ts index d8bb632f..2c95c878 100644 --- a/packages/types/src/options.ts +++ b/packages/types/src/options.ts @@ -10,7 +10,7 @@ export interface HighlighterCoreOptions { /** * Custom RegExp engine. */ - engine?: Sync extends true ? RegexEngine : Awaitable + engine: Sync extends true ? RegexEngine : Awaitable /** * Theme names, or theme registration objects to be loaded upfront. */ @@ -38,7 +38,11 @@ export interface HighlighterCoreOptions { loadWasm?: Sync extends true ? never : LoadWasmOptions } -export interface BundledHighlighterOptions extends Pick { +export interface BundledHighlighterOptions extends Pick { + /** + * Custom RegExp engine. + */ + engine?: Awaitable /** * Theme registation *