Skip to content

Commit

Permalink
feat: update deps, use jiti v2
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Sep 27, 2024
1 parent 3a3cf6a commit 4a6578d
Show file tree
Hide file tree
Showing 9 changed files with 429 additions and 864 deletions.
4 changes: 2 additions & 2 deletions packages/client/setup/context-menu.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type { ContextMenuItem } from '@slidev/types'
/// <reference types="unplugin-icons/types/vue3" />

import type { ContextMenuItem } from '@slidev/types'
import type { ComputedRef } from 'vue'
import setups from '#slidev/setups/context-menu'
import IconApps from '~icons/carbon/apps'
import IconArrowDown from '~icons/carbon/arrow-down'
import IconArrowLeft from '~icons/carbon/arrow-left'
import IconArrowRight from '~icons/carbon/arrow-right'

import IconArrowUp from '~icons/carbon/arrow-up'
import IconMaximize from '~icons/carbon/maximize'
import IconMinimize from '~icons/carbon/minimize'
Expand Down
4 changes: 2 additions & 2 deletions packages/client/setup/monaco.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ const setup = createSingletonPromise(async () => {
monaco.languages.register({ id: 'typescript' })
monaco.languages.register({ id: 'javascript' })

const { shiki, langs, themes, shikiToMonaco } = await import('#slidev/shiki')
const { shiki, languages, themes, shikiToMonaco } = await import('#slidev/shiki')
const highlighter = await shiki

const editorOptions: MonacoSetupReturn['editorOptions'] & object = {}
Expand All @@ -114,7 +114,7 @@ const setup = createSingletonPromise(async () => {
})
}
// Register all languages, otherwise Monaco will not highlight them
for (const lang of langs) {
for (const lang of languages) {
monaco.languages.register({ id: lang })
}

Expand Down
10 changes: 5 additions & 5 deletions packages/slidev/node/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ const CONFIG_RESTART_FIELDS: (keyof SlidevConfig)[] = [
/**
* Files that triggers a restart when added or removed
*/
const FILES_CREATE_RESTART_GLOBS = [
const FILES_CREATE_RESTART = [
'global-bottom.vue',
'global-top.vue',
'uno.config.js',
Expand All @@ -44,7 +44,7 @@ const FILES_CREATE_RESTART_GLOBS = [
'unocss.config.ts',
]

const FILES_CHANGE_RESTART_GLOBS = [
const FILES_CHANGE_RESTART = [
'setup/shiki.ts',
'setup/katex.ts',
'setup/preparser.ts',
Expand Down Expand Up @@ -298,8 +298,8 @@ cli.command(
// Start watcher to restart server on file changes
const { watch } = await import('chokidar')
const watcher = watch([
...FILES_CREATE_RESTART_GLOBS,
...FILES_CHANGE_RESTART_GLOBS,
...FILES_CREATE_RESTART,
...FILES_CHANGE_RESTART,
], {
ignored: ['node_modules', '.git'],
ignoreInitial: true,
Expand All @@ -313,7 +313,7 @@ cli.command(
restartServer()
})
watcher.on('change', (file) => {
if (FILES_CREATE_RESTART_GLOBS.includes(file))
if (FILES_CREATE_RESTART.includes(file))
return
console.log(yellow(`\n file ${file} changed, restarting...\n`))
restartServer()
Expand Down
7 changes: 4 additions & 3 deletions packages/slidev/node/setups/load.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { Awaitable } from '@antfu/utils'
import { resolve } from 'node:path'
import { deepMergeWithArray } from '@antfu/utils'
import fs from 'fs-extra'
Expand All @@ -7,19 +8,19 @@ export async function loadSetups<F extends (...args: any) => any>(
roots: string[],
filename: string,
args: Parameters<F>,
extraLoader?: (root: string) => Awaited<ReturnType<F>>[],
extraLoader?: (root: string) => Awaitable<Awaited<ReturnType<F>>[]>,
) {
const returns: Awaited<ReturnType<F>>[] = []
for (const root of roots) {
const path = resolve(root, 'setup', filename)
if (fs.existsSync(path)) {
const { default: setup } = loadModule(path)
const { default: setup } = await loadModule(path) as { default: F }
const ret = await setup(...args)
if (ret)
returns.push(ret)
}
if (extraLoader)
returns.push(...extraLoader(root))
returns.push(...await extraLoader(root))
}
return returns
}
Expand Down
25 changes: 14 additions & 11 deletions packages/slidev/node/setups/unocss.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,19 @@ import { loadModule } from '../utils'
export default async function setupUnocss(
{ clientRoot, roots, data, utils }: ResolvedSlidevOptions,
) {
function loadFileConfigs(root: string): UserConfig<Theme>[] {
return [
resolve(root, 'uno.config.ts'),
resolve(root, 'unocss.config.ts'),
].map((i) => {
if (!existsSync(i))
return undefined
const loaded = loadModule(i)
return 'default' in loaded ? loaded.default : loaded
})
async function loadFileConfigs(root: string): Promise<UserConfig<Theme>[]> {
return (await Promise
.all([
resolve(root, 'uno.config.ts'),
resolve(root, 'unocss.config.ts'),
]
.map(async (i) => {
if (!existsSync(i))
return undefined
const loaded = await loadModule(i) as UserConfig<Theme> | { default: UserConfig<Theme> }
return 'default' in loaded ? loaded.default : loaded
})))
.filter(x => !!x)
}

const configs = [
Expand All @@ -35,7 +38,7 @@ export default async function setupUnocss(
}),
],
},
...loadFileConfigs(clientRoot),
...await loadFileConfigs(clientRoot),
...await loadSetups<UnoSetup>(roots, 'unocss.ts', [], loadFileConfigs),
].filter(Boolean) as UserConfig<Theme>[]

Expand Down
10 changes: 5 additions & 5 deletions packages/slidev/node/utils.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import type { ResolvedFontOptions, SlideInfo } from '@slidev/types'
import type { JITI } from 'jiti'
import type { Token } from 'markdown-it'
import type { Connect } from 'vite'
import { fileURLToPath } from 'node:url'
import createJiti from 'jiti'
import { createJiti } from 'jiti'
import YAML from 'yaml'

let jiti: JITI | undefined
export function loadModule(absolutePath: string) {
type Jiti = ReturnType<typeof createJiti>
let jiti: Jiti | undefined
export function loadModule<T = unknown>(absolutePath: string): Promise<T> {
jiti ??= createJiti(fileURLToPath(import.meta.url))
return jiti(absolutePath)
return jiti.import(absolutePath) as Promise<T>
}

export function stringifyMarkdownTokens(tokens: Token[]) {
Expand Down
2 changes: 1 addition & 1 deletion packages/types/client.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ declare module '#slidev/shiki' {

export { shikiToMonaco } from '@shikijs/monaco'

export const langs: BundledLanguage[]
export const languages: BundledLanguage[]
export const themes: BundledTheme | Record<string, BundledTheme>
export const shiki: Promise<ShikiHighlighterCore>
export function getHighlighter(): Promise<(code: string, lang: string, options?: Partial<CodeToHastOptions>) => string>
Expand Down
Loading

0 comments on commit 4a6578d

Please sign in to comment.