Skip to content
This repository has been archived by the owner on Feb 7, 2024. It is now read-only.

Commit

Permalink
feat: auto module default resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Aug 11, 2023
1 parent 42a3272 commit de84711
Show file tree
Hide file tree
Showing 8 changed files with 208 additions and 207 deletions.
2 changes: 1 addition & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
src/vendor/languages
src/vendor
onig.js
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@ const shiki = await getHighlighter({
themes: [nord],
langs: [
// Or a getter if you want to do chunk splitting
() => import('shikiji/languages/javascript.mjs').then(m => m.default),
() => import('shikiji/languages/javascript.mjs')
],
loadWasm: getWasmInlined
})

// optionally, load themes and languages after creation
await shiki.loadTheme(() => import('shikiji/themes/vitesse-light.mjs').then(m => m.default))
await shiki.loadTheme(() => import('shikiji/themes/vitesse-light.mjs'))

const code = shiki.codeToHtml('const a = 1', { lang: 'javascript' })
```
Expand Down
4 changes: 2 additions & 2 deletions scripts/prepare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ for (const file of files) {
}, { spaces: 2 })
}

const languages = Object.fromEntries(BUNDLED_LANGUAGES.map(i => [i.id, `__()=>import('./languages/${i.id}.json').then(r=>r.default as unknown as LanguageRegistration)__`]))
const languages = Object.fromEntries(BUNDLED_LANGUAGES.map(i => [i.id, `__() => import('./languages/${i.id}.json') as unknown as Promise<{ default: LanguageRegistration }>__`]))

const themes = Object.fromEntries(BUNDLED_THEMES.map(i => [i, `__()=>import('shiki/themes/${i}.json').then(r=>r.default as unknown as ThemeRegisterationRaw)__`]))
const themes = Object.fromEntries(BUNDLED_THEMES.map(i => [i, `__() => import('shiki/themes/${i}.json') as unknown as Promise<{ default: ThemeRegisterationRaw }>__`]))

await fs.writeFile(
'src/vendor/langs.ts',
Expand Down
6 changes: 3 additions & 3 deletions src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ export async function getHighlighterCore(options: HighlighterCoreOptions) {
themes,
langs,
] = await Promise.all([
Promise.all(options.themes.map(async t => typeof t === 'function' ? await t() : t)),
Promise.all(options.langs.map(async t => typeof t === 'function' ? await t() : t)),
Promise.all(options.themes.map(normalizeGetter)),
Promise.all(options.langs.map(normalizeGetter)),
typeof options.loadWasm === 'function'
? Promise.resolve(options.loadWasm()).then(r => loadWasm(r))
: options.loadWasm
Expand Down Expand Up @@ -116,5 +116,5 @@ function isPlaintext(lang: string | null | undefined) {
}

async function normalizeGetter<T>(p: MaybeGetter<T>): Promise<T> {
return typeof p === 'function' ? (p as any)() : p
return typeof p === 'function' ? Promise.resolve((p as any)()).then(r => r.default || r) : p
}
3 changes: 2 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ export type BuiltinLanguages = keyof typeof bundledLanguages
export type BuiltinThemes = keyof typeof bundledThemes

export type Awaitable<T> = T | Promise<T>
export type MaybeGetter<T> = T | (() => Awaitable<T>)
export type MaybeGetter<T> = T | (() => Awaitable<MaybeModule<T>>)
export type MaybeModule<T> = T | { default: T }

export type ThemeInput = MaybeGetter<ThemeRegisteration | ThemeRegisterationRaw>
export type LanguageInput = MaybeGetter<LanguageRegistration>
Expand Down
334 changes: 167 additions & 167 deletions src/vendor/langs.ts

Large diffs are not rendered by default.

60 changes: 30 additions & 30 deletions src/vendor/themes.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
import type { ThemeRegisterationRaw } from '../types'

export const bundledThemes = {
'css-variables': () => import('shiki/themes/css-variables.json').then(r => r.default as unknown as ThemeRegisterationRaw),
'dark-plus': () => import('shiki/themes/dark-plus.json').then(r => r.default as unknown as ThemeRegisterationRaw),
'dracula-soft': () => import('shiki/themes/dracula-soft.json').then(r => r.default as unknown as ThemeRegisterationRaw),
'dracula': () => import('shiki/themes/dracula.json').then(r => r.default as unknown as ThemeRegisterationRaw),
'github-dark-dimmed': () => import('shiki/themes/github-dark-dimmed.json').then(r => r.default as unknown as ThemeRegisterationRaw),
'github-dark': () => import('shiki/themes/github-dark.json').then(r => r.default as unknown as ThemeRegisterationRaw),
'github-light': () => import('shiki/themes/github-light.json').then(r => r.default as unknown as ThemeRegisterationRaw),
'hc_light': () => import('shiki/themes/hc_light.json').then(r => r.default as unknown as ThemeRegisterationRaw),
'light-plus': () => import('shiki/themes/light-plus.json').then(r => r.default as unknown as ThemeRegisterationRaw),
'material-theme-darker': () => import('shiki/themes/material-theme-darker.json').then(r => r.default as unknown as ThemeRegisterationRaw),
'material-theme-lighter': () => import('shiki/themes/material-theme-lighter.json').then(r => r.default as unknown as ThemeRegisterationRaw),
'material-theme-ocean': () => import('shiki/themes/material-theme-ocean.json').then(r => r.default as unknown as ThemeRegisterationRaw),
'material-theme-palenight': () => import('shiki/themes/material-theme-palenight.json').then(r => r.default as unknown as ThemeRegisterationRaw),
'material-theme': () => import('shiki/themes/material-theme.json').then(r => r.default as unknown as ThemeRegisterationRaw),
'min-dark': () => import('shiki/themes/min-dark.json').then(r => r.default as unknown as ThemeRegisterationRaw),
'min-light': () => import('shiki/themes/min-light.json').then(r => r.default as unknown as ThemeRegisterationRaw),
'monokai': () => import('shiki/themes/monokai.json').then(r => r.default as unknown as ThemeRegisterationRaw),
'nord': () => import('shiki/themes/nord.json').then(r => r.default as unknown as ThemeRegisterationRaw),
'one-dark-pro': () => import('shiki/themes/one-dark-pro.json').then(r => r.default as unknown as ThemeRegisterationRaw),
'poimandres': () => import('shiki/themes/poimandres.json').then(r => r.default as unknown as ThemeRegisterationRaw),
'rose-pine-dawn': () => import('shiki/themes/rose-pine-dawn.json').then(r => r.default as unknown as ThemeRegisterationRaw),
'rose-pine-moon': () => import('shiki/themes/rose-pine-moon.json').then(r => r.default as unknown as ThemeRegisterationRaw),
'rose-pine': () => import('shiki/themes/rose-pine.json').then(r => r.default as unknown as ThemeRegisterationRaw),
'slack-dark': () => import('shiki/themes/slack-dark.json').then(r => r.default as unknown as ThemeRegisterationRaw),
'slack-ochin': () => import('shiki/themes/slack-ochin.json').then(r => r.default as unknown as ThemeRegisterationRaw),
'solarized-dark': () => import('shiki/themes/solarized-dark.json').then(r => r.default as unknown as ThemeRegisterationRaw),
'solarized-light': () => import('shiki/themes/solarized-light.json').then(r => r.default as unknown as ThemeRegisterationRaw),
'vitesse-dark': () => import('shiki/themes/vitesse-dark.json').then(r => r.default as unknown as ThemeRegisterationRaw),
'vitesse-light': () => import('shiki/themes/vitesse-light.json').then(r => r.default as unknown as ThemeRegisterationRaw),
}
"css-variables": () => import('shiki/themes/css-variables.json') as unknown as Promise<{ default: ThemeRegisterationRaw }>,
"dark-plus": () => import('shiki/themes/dark-plus.json') as unknown as Promise<{ default: ThemeRegisterationRaw }>,
"dracula-soft": () => import('shiki/themes/dracula-soft.json') as unknown as Promise<{ default: ThemeRegisterationRaw }>,
"dracula": () => import('shiki/themes/dracula.json') as unknown as Promise<{ default: ThemeRegisterationRaw }>,
"github-dark-dimmed": () => import('shiki/themes/github-dark-dimmed.json') as unknown as Promise<{ default: ThemeRegisterationRaw }>,
"github-dark": () => import('shiki/themes/github-dark.json') as unknown as Promise<{ default: ThemeRegisterationRaw }>,
"github-light": () => import('shiki/themes/github-light.json') as unknown as Promise<{ default: ThemeRegisterationRaw }>,
"hc_light": () => import('shiki/themes/hc_light.json') as unknown as Promise<{ default: ThemeRegisterationRaw }>,
"light-plus": () => import('shiki/themes/light-plus.json') as unknown as Promise<{ default: ThemeRegisterationRaw }>,
"material-theme-darker": () => import('shiki/themes/material-theme-darker.json') as unknown as Promise<{ default: ThemeRegisterationRaw }>,
"material-theme-lighter": () => import('shiki/themes/material-theme-lighter.json') as unknown as Promise<{ default: ThemeRegisterationRaw }>,
"material-theme-ocean": () => import('shiki/themes/material-theme-ocean.json') as unknown as Promise<{ default: ThemeRegisterationRaw }>,
"material-theme-palenight": () => import('shiki/themes/material-theme-palenight.json') as unknown as Promise<{ default: ThemeRegisterationRaw }>,
"material-theme": () => import('shiki/themes/material-theme.json') as unknown as Promise<{ default: ThemeRegisterationRaw }>,
"min-dark": () => import('shiki/themes/min-dark.json') as unknown as Promise<{ default: ThemeRegisterationRaw }>,
"min-light": () => import('shiki/themes/min-light.json') as unknown as Promise<{ default: ThemeRegisterationRaw }>,
"monokai": () => import('shiki/themes/monokai.json') as unknown as Promise<{ default: ThemeRegisterationRaw }>,
"nord": () => import('shiki/themes/nord.json') as unknown as Promise<{ default: ThemeRegisterationRaw }>,
"one-dark-pro": () => import('shiki/themes/one-dark-pro.json') as unknown as Promise<{ default: ThemeRegisterationRaw }>,
"poimandres": () => import('shiki/themes/poimandres.json') as unknown as Promise<{ default: ThemeRegisterationRaw }>,
"rose-pine-dawn": () => import('shiki/themes/rose-pine-dawn.json') as unknown as Promise<{ default: ThemeRegisterationRaw }>,
"rose-pine-moon": () => import('shiki/themes/rose-pine-moon.json') as unknown as Promise<{ default: ThemeRegisterationRaw }>,
"rose-pine": () => import('shiki/themes/rose-pine.json') as unknown as Promise<{ default: ThemeRegisterationRaw }>,
"slack-dark": () => import('shiki/themes/slack-dark.json') as unknown as Promise<{ default: ThemeRegisterationRaw }>,
"slack-ochin": () => import('shiki/themes/slack-ochin.json') as unknown as Promise<{ default: ThemeRegisterationRaw }>,
"solarized-dark": () => import('shiki/themes/solarized-dark.json') as unknown as Promise<{ default: ThemeRegisterationRaw }>,
"solarized-light": () => import('shiki/themes/solarized-light.json') as unknown as Promise<{ default: ThemeRegisterationRaw }>,
"vitesse-dark": () => import('shiki/themes/vitesse-dark.json') as unknown as Promise<{ default: ThemeRegisterationRaw }>,
"vitesse-light": () => import('shiki/themes/vitesse-light.json') as unknown as Promise<{ default: ThemeRegisterationRaw }>
}
2 changes: 1 addition & 1 deletion test/core.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ describe('should', () => {
},
})

await shiki.loadLanguage(() => import('../dist/languages/python.mjs').then(m => m.default))
await shiki.loadLanguage(() => import('../dist/languages/python.mjs'))

Check failure on line 33 in test/core.test.ts

View workflow job for this annotation

GitHub Actions / typecheck

Cannot find module '../dist/languages/python.mjs' or its corresponding type declarations.
await shiki.loadTheme(() => import('../dist/themes/vitesse-light.mjs').then(m => m.default))

Check failure on line 34 in test/core.test.ts

View workflow job for this annotation

GitHub Actions / typecheck

Cannot find module '../dist/themes/vitesse-light.mjs' or its corresponding type declarations.

expect(shiki.codeToHtml('print 1', { lang: 'python', theme: 'vitesse-light' }))
Expand Down

0 comments on commit de84711

Please sign in to comment.