返回 slidev
options.ts
根目录 / packages / slidev / node / options.ts
1 import type { ResolvedSlidevOptions, ResolvedSlidevUtils, SlidevData, SlidevEntryOptions } from '@slidev/types'
2 import { objectMap, uniq } from '@antfu/utils'
3 import fg from 'fast-glob'
4 import { createDebug } from 'obug'
5 import path from 'pathe'
6 import pm from 'picomatch'
7 import { resolveAddons } from './integrations/addons'
8 import { getThemeMeta, resolveTheme } from './integrations/themes'
9 import { parser } from './parser'
10 import { getRoots, resolveEntry, toAtFS } from './resolver'
11 import setupIndexHtml from './setups/indexHtml'
12 import setupKatex from './setups/katex'
13 import setupShiki from './setups/shiki'
14
15 const RE_FILE_EXTENSION = /\.\w+$/
16
17 const debug = createDebug('slidev:options')
18
19 export async function resolveOptions(
20 entryOptions: SlidevEntryOptions,
21 mode: ResolvedSlidevOptions['mode'],
22 ): Promise<ResolvedSlidevOptions> {
23 const entry = await resolveEntry(entryOptions.entry)
24 const rootsInfo = await getRoots(entry)
25 const initialLoaded = await parser.load(
26 {
27 userRoot: rootsInfo.userRoot,
28 roots: [rootsInfo.userRoot],
29 allowedRoots: uniq([rootsInfo.userWorkspaceRoot, rootsInfo.userRoot]),
30 },
31 entry,
32 undefined,
33 mode,
34 )
35
36 // Load theme data first, because it may affect the config
37 let themeRaw = entryOptions.theme || initialLoaded.headmatter.theme as string | null | undefined
38 themeRaw = themeRaw === null
39 ? 'none'
40 : (themeRaw || 'default')
41 const [theme, themeRoot] = await resolveTheme(themeRaw, entry)
42 const themeRoots = themeRoot
43 ? [themeRoot]
44 : []
45 const themeMeta = themeRoot
46 ? await getThemeMeta(theme, themeRoot)
47 : undefined
48
49 const initialConfig = parser.resolveConfig(initialLoaded.headmatter, themeMeta, entryOptions.entry)
50 const addonRoots = await resolveAddons(initialConfig.addons)
51 const roots = uniq([
52 ...themeRoots,
53 ...addonRoots,
54 rootsInfo.userRoot,
55 ])
56 const loaded = await parser.load(
57 {
58 userRoot: rootsInfo.userRoot,
59 roots,
60 allowedRoots: uniq([rootsInfo.userWorkspaceRoot, ...roots]),
61 },
62 entry,
63 undefined,
64 mode,
65 )
66 const config = parser.resolveConfig(loaded.headmatter, themeMeta, entryOptions.entry)
67
68 // These fields selected the roots used for the final load. Keep the
69 // returned config aligned with the graph that was actually loaded if a
70 // preparser changes them.
71 config.theme = themeRaw
72 config.addons = initialConfig.addons
73
74 if (entryOptions.download)
75 config.download ||= entryOptions.download
76
77 if (entryOptions.routerMode)
78 config.routerMode = entryOptions.routerMode
79
80 debug({
81 ...rootsInfo,
82 ...entryOptions,
83 config,
84 mode,
85 entry,
86 themeRaw,
87 theme,
88 themeRoots,
89 addonRoots,
90 roots,
91 })
92
93 const data: SlidevData = {
94 ...loaded,
95 config,
96 themeMeta,
97 }
98
99 const resolved: Omit<ResolvedSlidevOptions, 'utils'> = {
100 ...rootsInfo,
101 ...entryOptions,
102 data,
103 mode,
104 entry,
105 themeRaw,
106 theme,
107 themeRoots,
108 addonRoots,
109 roots,
110 }
111
112 return {
113 ...resolved,
114 utils: await createDataUtils(resolved),
115 }
116 }
117
118 export async function createDataUtils(resolved: Omit<ResolvedSlidevOptions, 'utils'>): Promise<ResolvedSlidevUtils> {
119 const monacoTypesIgnorePackagesMatches = (resolved.data.config.monacoTypesIgnorePackages || [])
120 .map(i => pm.makeRe(i))
121
122 let _layouts_cache_time = 0
123 let _layouts_cache: Promise<Record<string, string>> | null = null
124
125 return {
126 ...await setupShiki(resolved.roots),
127 katexOptions: await setupKatex(resolved.roots),
128 indexHtml: await setupIndexHtml(resolved),
129 define: getDefine(resolved),
130 iconsResolvePath: [resolved.clientRoot, ...resolved.roots].reverse(),
131 isMonacoTypesIgnored: pkg => monacoTypesIgnorePackagesMatches.some(i => i.test(pkg)),
132 getLayouts: () => {
133 const now = Date.now()
134 if (_layouts_cache && now - _layouts_cache_time < 2000)
135 return _layouts_cache
136 _layouts_cache_time = now
137 return _layouts_cache = worker()
138
139 async function worker() {
140 const layouts: Record<string, string> = {}
141 const layoutPaths = await Promise.all(
142 [resolved.clientRoot, ...resolved.roots]
143 .map(root => fg('layouts/**/*.{vue,js,mjs,ts,mts}', {
144 cwd: root,
145 absolute: true,
146 suppressErrors: true,
147 })),
148 )
149 for (const layoutPath of layoutPaths.flat(1)) {
150 const layoutName = path.basename(layoutPath).replace(RE_FILE_EXTENSION, '')
151 layouts[layoutName] = layoutPath
152 }
153 return layouts
154 }
155 },
156 }
157 }
158
159 function getDefine(options: Omit<ResolvedSlidevOptions, 'utils'>): Record<string, string> {
160 const matchMode = (mode: string | boolean) => mode === true || mode === options.mode
161 return objectMap(
162 {
163 __DEV__: options.mode === 'dev',
164 __SLIDEV_CLIENT_ROOT__: toAtFS(options.clientRoot),
165 __SLIDEV_HASH_ROUTE__: options.data.config.routerMode === 'hash',
166 // Export navigates by URL, so memory routing (which ignores the URL) is
167 // disabled during export and falls back to history.
168 __SLIDEV_MEMORY_ROUTE__: options.data.config.routerMode === 'memory' && options.mode !== 'export',
169 __SLIDEV_FEATURE_DRAWINGS__: matchMode(options.data.config.drawings.enabled),
170 __SLIDEV_FEATURE_EDITOR__: options.mode === 'dev' && options.data.config.editor !== false,
171 __SLIDEV_FEATURE_DRAWINGS_PERSIST__: !!options.data.config.drawings.persist,
172 __SLIDEV_FEATURE_RECORD__: matchMode(options.data.config.record),
173 __SLIDEV_FEATURE_PRESENTER__: matchMode(options.data.config.presenter),
174 __SLIDEV_FEATURE_PRINT__: options.mode === 'export' || (options.mode === 'build' && [true, 'true', 'auto'].includes(options.data.config.download)),
175 __SLIDEV_FEATURE_BROWSER_EXPORTER__: matchMode(options.data.config.browserExporter),
176 __SLIDEV_FEATURE_WAKE_LOCK__: matchMode(options.data.config.wakeLock),
177 __SLIDEV_FEATURE_PWA__: matchMode(options.data.config.pwa),
178 __SLIDEV_HAS_SERVER__: options.mode !== 'build',
179 },
180 (v, k) => [
181 v,
182 JSON.stringify(k),
183 ],
184 )
185 }
186
186 lines TYPESCRIPT