Skip to content

Commit c8711ca

Browse files
kermanxantfu
andauthored
perf: avoid loading parser in client and cache define (#1812)
Co-authored-by: Anthony Fu <anthonyfu117@hotmail.com>
1 parent 66bbde7 commit c8711ca

File tree

11 files changed

+50
-50
lines changed

11 files changed

+50
-50
lines changed

packages/client/builtin/KaTexBlockWrapper.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Learn more: https://sli.dev/guide/syntax.html#latex-line-highlighting
2121

2222
<script setup lang="ts">
2323
import type { PropType } from 'vue'
24-
import { parseRangeString } from '@slidev/parser'
24+
import { parseRangeString } from '@slidev/parser/utils'
2525
import { computed, onMounted, onUnmounted, ref, watchEffect } from 'vue'
2626
import { CLASS_VCLICK_HIDDEN, CLASS_VCLICK_TARGET, CLICKS_MAX } from '../constants'
2727
import { useSlideContext } from '../context'

packages/client/internals/PrintContainer.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script setup lang="ts">
2-
import { parseRangeString } from '@slidev/parser/core'
2+
import { parseRangeString } from '@slidev/parser/utils'
33
import { provideLocal } from '@vueuse/core'
44
import { computed } from 'vue'
55
import { useNav } from '../composables/useNav'

packages/parser/package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
"./fs": {
2424
"types": "./dist/fs.d.mts",
2525
"import": "./dist/fs.mjs"
26+
},
27+
"./utils": {
28+
"types": "./dist/utils.d.mts",
29+
"import": "./dist/utils.mjs"
2630
}
2731
},
2832
"main": "dist/index.mjs",
@@ -36,7 +40,7 @@
3640
"node": ">=18.0.0"
3741
},
3842
"scripts": {
39-
"build": "rimraf dist && tsup src/index.ts src/core.ts src/fs.ts",
43+
"build": "rimraf dist && tsup src/index.ts src/core.ts src/fs.ts src/utils.ts",
4044
"dev": "nr build --watch",
4145
"prepublishOnly": "npm run build"
4246
},

packages/slidev/node/options.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import mm from 'micromatch'
77
import { resolveAddons } from './integrations/addons'
88
import { getThemeMeta, resolveTheme } from './integrations/themes'
99
import { parser } from './parser'
10-
import { getRoots, resolveEntry } from './resolver'
10+
import { getRoots, resolveEntry, toAtFS } from './resolver'
1111
import setupIndexHtml from './setups/indexHtml'
1212
import setupShiki from './setups/shiki'
1313

@@ -80,6 +80,7 @@ export async function createDataUtils(resolved: Omit<ResolvedSlidevOptions, 'uti
8080
return {
8181
...await setupShiki(resolved.roots),
8282
indexHtml: setupIndexHtml(resolved),
83+
define: getDefine(resolved),
8384
iconsResolvePath: [resolved.clientRoot, ...resolved.roots].reverse(),
8485
isMonacoTypesIgnored: pkg => monacoTypesIgnorePackagesMatches.some(i => i(pkg)),
8586
getLayouts: () => {
@@ -109,3 +110,19 @@ export async function createDataUtils(resolved: Omit<ResolvedSlidevOptions, 'uti
109110
},
110111
}
111112
}
113+
114+
function getDefine(options: Omit<ResolvedSlidevOptions, 'utils'>): Record<string, string> {
115+
return {
116+
__DEV__: options.mode === 'dev' ? 'true' : 'false',
117+
__SLIDEV_CLIENT_ROOT__: JSON.stringify(toAtFS(options.clientRoot)),
118+
__SLIDEV_HASH_ROUTE__: JSON.stringify(options.data.config.routerMode === 'hash'),
119+
__SLIDEV_FEATURE_DRAWINGS__: JSON.stringify(options.data.config.drawings.enabled === true || options.data.config.drawings.enabled === options.mode),
120+
__SLIDEV_FEATURE_EDITOR__: JSON.stringify(options.mode === 'dev' && options.data.config.editor !== false),
121+
__SLIDEV_FEATURE_DRAWINGS_PERSIST__: JSON.stringify(!!options.data.config.drawings.persist === true),
122+
__SLIDEV_FEATURE_RECORD__: JSON.stringify(options.data.config.record === true || options.data.config.record === options.mode),
123+
__SLIDEV_FEATURE_PRESENTER__: JSON.stringify(options.data.config.presenter === true || options.data.config.presenter === options.mode),
124+
__SLIDEV_FEATURE_PRINT__: JSON.stringify(options.mode === 'export' || (options.mode === 'build' && [true, 'true', 'auto'].includes(options.data.config.download))),
125+
__SLIDEV_FEATURE_WAKE_LOCK__: JSON.stringify(options.data.config.wakeLock === true || options.data.config.wakeLock === options.mode),
126+
__SLIDEV_HAS_SERVER__: options.mode !== 'build' ? 'true' : 'false',
127+
}
128+
}
Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,26 @@
11
import type { ResolvedSlidevOptions } from '@slidev/types'
22
import type { Plugin } from 'vite'
33
import { objectEntries } from '@antfu/utils'
4-
import { getDefine } from './extendConfig'
54

65
/**
76
* Replace compiler flags like `__DEV__` in Vue SFC
87
*/
98
export function createVueCompilerFlagsPlugin(
109
options: ResolvedSlidevOptions,
11-
): Plugin[] {
12-
const define = objectEntries(getDefine(options))
13-
return [
14-
{
15-
name: 'slidev:flags',
16-
enforce: 'pre',
17-
transform(code, id) {
18-
if (id.match(/\.vue($|\?)/)) {
19-
const original = code
20-
define.forEach(([from, to]) => {
21-
code = code.replace(new RegExp(from, 'g'), to)
22-
})
23-
if (original !== code)
24-
return code
25-
}
26-
},
10+
): Plugin {
11+
const define = objectEntries(options.utils.define)
12+
return {
13+
name: 'slidev:flags',
14+
enforce: 'pre',
15+
transform(code, id) {
16+
if (!id.match(/\.vue($|\?)/) && !id.includes('?vue&'))
17+
return
18+
const original = code
19+
define.forEach(([from, to]) => {
20+
code = code.replaceAll(from, to)
21+
})
22+
if (original !== code)
23+
return code
2724
},
28-
]
25+
}
2926
}

packages/slidev/node/vite/extendConfig.ts

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { ResolvedSlidevOptions } from '@slidev/types'
2-
import type { InlineConfig, Plugin } from 'vite'
2+
import type { Plugin, UserConfig } from 'vite'
33
import { join } from 'node:path'
44
import { fileURLToPath, pathToFileURL } from 'node:url'
55
import { slash, uniq } from '@antfu/utils'
@@ -69,8 +69,8 @@ export function createConfigPlugin(options: ResolvedSlidevOptions): Plugin {
6969
return {
7070
name: 'slidev:config',
7171
async config(config) {
72-
const injection: InlineConfig = {
73-
define: getDefine(options),
72+
const injection: UserConfig = {
73+
define: options.utils.define,
7474
resolve: {
7575
alias: [
7676
{
@@ -171,6 +171,7 @@ export function createConfigPlugin(options: ResolvedSlidevOptions): Plugin {
171171
},
172172
},
173173
},
174+
cacheDir: isInstalledGlobally.value ? join(options.cliRoot, 'node_modules/.vite') : undefined,
174175
}
175176

176177
function isSlidevClient(id: string) {
@@ -183,11 +184,6 @@ export function createConfigPlugin(options: ResolvedSlidevOptions): Plugin {
183184
// return nodeModuelsMatch[nodeModuelsMatch.length - 1][1]
184185
// }
185186

186-
if (isInstalledGlobally.value) {
187-
injection.cacheDir = join(options.cliRoot, 'node_modules/.vite')
188-
injection.root = options.cliRoot
189-
}
190-
191187
return mergeConfig(injection, config)
192188
},
193189
configureServer(server) {
@@ -206,19 +202,3 @@ export function createConfigPlugin(options: ResolvedSlidevOptions): Plugin {
206202
},
207203
}
208204
}
209-
210-
export function getDefine(options: ResolvedSlidevOptions): Record<string, string> {
211-
return {
212-
__DEV__: options.mode === 'dev' ? 'true' : 'false',
213-
__SLIDEV_CLIENT_ROOT__: JSON.stringify(toAtFS(options.clientRoot)),
214-
__SLIDEV_HASH_ROUTE__: JSON.stringify(options.data.config.routerMode === 'hash'),
215-
__SLIDEV_FEATURE_DRAWINGS__: JSON.stringify(options.data.config.drawings.enabled === true || options.data.config.drawings.enabled === options.mode),
216-
__SLIDEV_FEATURE_EDITOR__: JSON.stringify(options.mode === 'dev' && options.data.config.editor !== false),
217-
__SLIDEV_FEATURE_DRAWINGS_PERSIST__: JSON.stringify(!!options.data.config.drawings.persist === true),
218-
__SLIDEV_FEATURE_RECORD__: JSON.stringify(options.data.config.record === true || options.data.config.record === options.mode),
219-
__SLIDEV_FEATURE_PRESENTER__: JSON.stringify(options.data.config.presenter === true || options.data.config.presenter === options.mode),
220-
__SLIDEV_FEATURE_PRINT__: JSON.stringify(options.mode === 'export' || (options.mode === 'build' && [true, 'true', 'auto'].includes(options.data.config.download))),
221-
__SLIDEV_FEATURE_WAKE_LOCK__: JSON.stringify(options.data.config.wakeLock === true || options.data.config.wakeLock === options.mode),
222-
__SLIDEV_HAS_SERVER__: options.mode !== 'build' ? 'true' : 'false',
223-
}
224-
}

packages/slidev/node/vite/hmrPatch.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { regexSlideSourceId } from './common'
66
*/
77
export function createHmrPatchPlugin(): Plugin {
88
return {
9-
name: 'slidev:slide-transform:post',
9+
name: 'slidev:hmr-patch',
1010
transform(code, id) {
1111
if (!id.match(regexSlideSourceId))
1212
return

packages/slidev/node/vite/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { createStaticCopyPlugin } from './staticCopy'
1818
import { createUnocssPlugin } from './unocss'
1919
import { createVuePlugin } from './vue'
2020

21-
export async function ViteSlidevPlugin(
21+
export function ViteSlidevPlugin(
2222
options: ResolvedSlidevOptions,
2323
pluginOptions: SlidevPluginOptions = {},
2424
serverOptions: SlidevServerOptions = {},

packages/slidev/node/vite/layoutWrapper.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export function createLayoutWrapperPlugin(
1717
if (type !== 'md')
1818
return
1919
const index = +no - 1
20-
const layouts = await utils.getLayouts()
20+
const layouts = utils.getLayouts()
2121
const rawLayoutName = data.slides[index]?.frontmatter?.layout ?? data.slides[0]?.frontmatter?.defaults?.layout
2222
let layoutName = rawLayoutName || (index === 0 ? 'cover' : 'default')
2323
if (!layouts[layoutName]) {

packages/slidev/node/vite/vue.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,13 @@ export async function createVuePlugin(
4848
exclude: [],
4949
...vueOptions,
5050
template: {
51+
...vueOptions?.template,
5152
compilerOptions: {
53+
...vueOptions?.template?.compilerOptions,
5254
isCustomElement(tag) {
5355
return customElements.has(tag) || vueOptions?.template?.compilerOptions?.isCustomElement?.(tag)
5456
},
5557
},
56-
...vueOptions?.template,
5758
},
5859
})
5960
const VueJsxPlugin = VueJsx(vuejsxOptions)

0 commit comments

Comments
 (0)