Skip to content

Commit 821321b

Browse files
authored
feat: ViteSlidevPlugin pluginOptions argument now can be modified by themes and addons (#1210)
1 parent 80be226 commit 821321b

File tree

3 files changed

+47
-26
lines changed

3 files changed

+47
-26
lines changed

packages/slidev/node/build.ts

+11-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { join, resolve } from 'node:path'
33
import http from 'node:http'
44
import fs from 'fs-extra'
55
import type { InlineConfig, ResolvedConfig } from 'vite'
6-
import { resolveConfig, build as viteBuild } from 'vite'
6+
import { mergeConfig, build as viteBuild } from 'vite'
77
import connect from 'connect'
88
import sirv from 'sirv'
99
import { blue, yellow } from 'kolorist'
@@ -18,8 +18,6 @@ export async function build(
1818
args: BuildArgs,
1919
) {
2020
const indexPath = resolve(options.userRoot, 'index.html')
21-
const rawConfig = await resolveConfig({}, 'build', options.entry)
22-
const pluginOptions = rawConfig.slidev || {}
2321

2422
let originalIndexHTML: string | undefined
2523
if (fs.existsSync(indexPath))
@@ -29,13 +27,12 @@ export async function build(
2927
let config: ResolvedConfig = undefined!
3028

3129
try {
32-
const inlineConfig = await mergeViteConfigs(
30+
let inlineConfig = await mergeViteConfigs(
3331
options,
3432
viteConfig,
3533
<InlineConfig>({
3634
root: options.userRoot,
3735
plugins: [
38-
await ViteSlidevPlugin(options, pluginOptions),
3936
{
4037
name: 'resolve-config',
4138
configResolved(_config) {
@@ -50,6 +47,15 @@ export async function build(
5047
'build',
5148
)
5249

50+
inlineConfig = mergeConfig(
51+
inlineConfig,
52+
{
53+
plugins: [
54+
await ViteSlidevPlugin(options, inlineConfig.slidev || {}),
55+
],
56+
},
57+
)
58+
5359
await viteBuild(inlineConfig)
5460

5561
if (options.data.features.monaco) {

packages/slidev/node/common.ts

+16-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { existsSync, promises as fs } from 'node:fs'
22
import { join } from 'node:path'
33
import { uniq } from '@antfu/utils'
4-
import { loadConfigFromFile, mergeConfig } from 'vite'
4+
import { loadConfigFromFile, mergeConfig, resolveConfig } from 'vite'
55
import type { ConfigEnv, InlineConfig } from 'vite'
66
import type { ResolvedSlidevOptions } from './options'
77
import { generateGoogleFontsUrl, toAtFS } from './utils'
@@ -44,12 +44,17 @@ export async function getIndexHtml({ clientRoot, themeRoots, addonRoots, data, u
4444
return main
4545
}
4646

47-
export async function mergeViteConfigs({ addonRoots, themeRoots }: ResolvedSlidevOptions, viteConfig: InlineConfig, config: InlineConfig, command: 'serve' | 'build') {
47+
export async function mergeViteConfigs(
48+
{ addonRoots, themeRoots, entry }: ResolvedSlidevOptions,
49+
viteConfig: InlineConfig,
50+
config: InlineConfig,
51+
command: 'serve' | 'build',
52+
) {
4853
const configEnv: ConfigEnv = {
4954
mode: 'development',
5055
command,
5156
}
52-
57+
// Merge theme & addon configs
5358
const files = uniq([
5459
...themeRoots,
5560
...addonRoots,
@@ -64,5 +69,12 @@ export async function mergeViteConfigs({ addonRoots, themeRoots }: ResolvedSlide
6469
config = mergeConfig(config, viteConfig.config)
6570
}
6671

67-
return mergeConfig(viteConfig, config)
72+
// Merge viteConfig argument
73+
config = mergeConfig(config, viteConfig)
74+
75+
// Merge local config (slidev options only)
76+
const localConfig = await resolveConfig({}, command, entry)
77+
config = mergeConfig(config, { slidev: localConfig.slidev || {} })
78+
79+
return config
6880
}

packages/slidev/node/server.ts

+20-17
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { join } from 'node:path'
22
import process from 'node:process'
33
import type { InlineConfig } from 'vite'
4-
import { createServer as createViteServer, resolveConfig } from 'vite'
4+
import { createServer as createViteServer, mergeConfig } from 'vite'
55
import { mergeViteConfigs } from './common'
66
import type { ResolvedSlidevOptions, SlidevServerOptions } from './options'
77
import { ViteSlidevPlugin } from './plugins/preset'
@@ -11,28 +11,31 @@ export async function createServer(
1111
viteConfig: InlineConfig = {},
1212
serverOptions: SlidevServerOptions = {},
1313
) {
14-
const rawConfig = await resolveConfig({}, 'serve', options.entry)
15-
const pluginOptions = rawConfig.slidev || {}
16-
1714
// default open editor to code, #312
1815
process.env.EDITOR = process.env.EDITOR || 'code'
1916

17+
const config = await mergeViteConfigs(
18+
options,
19+
viteConfig,
20+
<InlineConfig>({
21+
root: options.userRoot,
22+
optimizeDeps: {
23+
entries: [
24+
join(options.clientRoot, 'main.ts'),
25+
],
26+
},
27+
}),
28+
'serve',
29+
)
30+
2031
const server = await createViteServer(
21-
await mergeViteConfigs(
22-
options,
23-
viteConfig,
24-
<InlineConfig>({
25-
root: options.userRoot,
26-
optimizeDeps: {
27-
entries: [
28-
join(options.clientRoot, 'main.ts'),
29-
],
30-
},
32+
mergeConfig(
33+
config,
34+
{
3135
plugins: [
32-
await ViteSlidevPlugin(options, pluginOptions, serverOptions),
36+
await ViteSlidevPlugin(options, config.slidev || {}, serverOptions),
3337
],
34-
}),
35-
'serve',
38+
},
3639
),
3740
)
3841

0 commit comments

Comments
 (0)