Skip to content

Commit

Permalink
fix: mark engine required in createHighlighterCore
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Jan 20, 2025
1 parent 0c571d3 commit 1212f47
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 24 deletions.
4 changes: 2 additions & 2 deletions bench/bundle-test/index-wasm.ts
Original file line number Diff line number Diff line change
@@ -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(
{
Expand All @@ -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')),
},
)

Expand Down
10 changes: 5 additions & 5 deletions packages/core/src/constructors/highlighter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<HighlighterCore> {
export async function createHighlighterCore(options: HighlighterCoreOptions<false>): Promise<HighlighterCore> {
const internal = await createShikiInternal(options)

return {
Expand All @@ -39,7 +39,7 @@ export async function createHighlighterCore(options: HighlighterCoreOptions = {}
*
* @see http://shiki.style/guide/bundles#fine-grained-bundle
*/
export function createHighlighterCoreSync(options: HighlighterCoreOptions<true> = {}): HighlighterCore {
export function createHighlighterCoreSync(options: HighlighterCoreOptions<true>): HighlighterCore {
const internal = createShikiInternalSync(options)

return {
Expand All @@ -56,11 +56,11 @@ export function createHighlighterCoreSync(options: HighlighterCoreOptions<true>

export function makeSingletonHighlighterCore(
createHighlighter: typeof createHighlighterCore,
): (options?: Partial<HighlighterCoreOptions>) => Promise<HighlighterCore> {
): (options: HighlighterCoreOptions) => Promise<HighlighterCore> {
let _shiki: ReturnType<typeof createHighlighterCore>

async function getSingletonHighlighterCore(
options: Partial<HighlighterCoreOptions> = {},
options: HighlighterCoreOptions,
): Promise<HighlighterCore> {
if (!_shiki) {
_shiki = createHighlighter({
Expand Down Expand Up @@ -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<HighlighterCore> {
export function getHighlighterCore(options: HighlighterCoreOptions): Promise<HighlighterCore> {
warnDeprecated('`getHighlighterCore` is deprecated. Use `createHighlighterCore` or `getSingletonHighlighterCore` instead.')
return createHighlighterCore(options)
}
7 changes: 5 additions & 2 deletions packages/core/src/constructors/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@ import { createShikiInternalSync } from './internal-sync'
/**
* Get the minimal shiki context for rendering.
*/
export async function createShikiInternal(options: HighlighterCoreOptions = {}): Promise<ShikiInternal> {
export async function createShikiInternal(options: HighlighterCoreOptions): Promise<ShikiInternal> {
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,
Expand All @@ -38,7 +41,7 @@ export async function createShikiInternal(options: HighlighterCoreOptions = {}):
/**
* @deprecated Use `createShikiInternal` instead.
*/
export function getShikiInternal(options: HighlighterCoreOptions = {}): Promise<ShikiInternal> {
export function getShikiInternal(options: HighlighterCoreOptions): Promise<ShikiInternal> {
warnDeprecated('`getShikiInternal` is deprecated. Use `createShikiInternal` instead.')
return createShikiInternal(options)
}
15 changes: 12 additions & 3 deletions packages/core/test/core.test.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -67,6 +67,7 @@ describe('should', () => {
langs: [
import('@shikijs/langs/cpp'),
],
engine: createJavaScriptRegexEngine(),
})

expect(shiki.getLoadedLanguages().sort())
Expand All @@ -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)
Expand All @@ -101,6 +104,7 @@ describe('should', () => {
mylang: 'javascript',
mylang2: 'js', // nested alias
},
engine: createJavaScriptRegexEngine(),
})

await shiki.loadLanguage(js)
Expand All @@ -117,6 +121,7 @@ describe('should', () => {
langAlias: {
js: 'typescript',
},
engine: createJavaScriptRegexEngine(),
})

await shiki.loadLanguage(ts)
Expand All @@ -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' }))
Expand All @@ -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' }))
Expand Down Expand Up @@ -175,6 +182,7 @@ describe('errors', () => {
mylang: 'mylang2',
mylang2: 'mylang',
},
engine: createJavaScriptRegexEngine(),
})

await shiki.loadLanguage(js)
Expand All @@ -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' }))
Expand Down
5 changes: 4 additions & 1 deletion packages/core/test/injections.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -35,6 +35,7 @@ it('injections-side-effects vue', async () => {
langs: [
html,
],
engine: createJavaScriptRegexEngine(),
})

const code = `<h1 v-if="count == 1 ? true : 'str'.toUpperCase()">{{ count * 2 }}</h1>`
Expand All @@ -60,6 +61,7 @@ it('injections-side-effects angular-html', async () => {
langs: [
html,
],
engine: createJavaScriptRegexEngine(),
})

const code = `<h2>Hero List</h2>
Expand Down Expand Up @@ -126,6 +128,7 @@ it('injections-side-effects angular-ts', async () => {
langs: [
ts,
],
engine: createJavaScriptRegexEngine(),
})

const code = `
Expand Down
13 changes: 5 additions & 8 deletions packages/shiki/test/cf.ts
Original file line number Diff line number Diff line change
@@ -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(
Expand Down
3 changes: 2 additions & 1 deletion packages/shiki/test/get-highlighter.test.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -17,6 +17,7 @@ it('getSingletonHighlighterCore', async () => {

const shiki2 = await getSingletonHighlighterCore({
themes: [mtp],
engine: createJavaScriptRegexEngine(),
})

expect(shiki1).toBe(shiki2)
Expand Down
8 changes: 6 additions & 2 deletions packages/types/src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export interface HighlighterCoreOptions<Sync extends boolean = false> {
/**
* Custom RegExp engine.
*/
engine?: Sync extends true ? RegexEngine : Awaitable<RegexEngine>
engine: Sync extends true ? RegexEngine : Awaitable<RegexEngine>
/**
* Theme names, or theme registration objects to be loaded upfront.
*/
Expand Down Expand Up @@ -38,7 +38,11 @@ export interface HighlighterCoreOptions<Sync extends boolean = false> {
loadWasm?: Sync extends true ? never : LoadWasmOptions
}

export interface BundledHighlighterOptions<L extends string, T extends string> extends Pick<HighlighterCoreOptions, 'warnings' | 'engine'> {
export interface BundledHighlighterOptions<L extends string, T extends string> extends Pick<HighlighterCoreOptions, 'warnings'> {
/**
* Custom RegExp engine.
*/
engine?: Awaitable<RegexEngine>
/**
* Theme registation
*
Expand Down

0 comments on commit 1212f47

Please sign in to comment.