Skip to content

Commit 7862813

Browse files
committed
feat(core): pass isServer flag to alias and define hooks
1 parent 4e4a72d commit 7862813

File tree

13 files changed

+66
-59
lines changed

13 files changed

+66
-59
lines changed

docs/reference/plugin-api.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ The following hooks will be processed in dev / build:
7575

7676
### alias
7777

78-
- Type: `Record<string, any> | ((app: App) => Record<string, any>)`
78+
- Type: `Record<string, any> | ((app: App, isServer: boolean) => Record<string, any>)`
7979

8080
- Details:
8181

@@ -128,7 +128,7 @@ export default {
128128

129129
### define
130130

131-
- Type: `Record<string, any> | ((app: App) => Record<string, any>)`
131+
- Type: `Record<string, any> | ((app: App, isServer: boolean) => Record<string, any>)`
132132

133133
- Details:
134134

docs/zh/reference/plugin-api.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575

7676
### alias
7777

78-
- 类型: `Record<string, any> | ((app: App) => Record<string, any>)`
78+
- 类型: `Record<string, any> | ((app: App, isServer: boolean) => Record<string, any>)`
7979

8080
- 详情:
8181

@@ -128,7 +128,7 @@ export default {
128128

129129
### define
130130

131-
- 类型: `Record<string, any> | ((app: App) => Record<string, any>)`
131+
- 类型: `Record<string, any> | ((app: App, isServer: boolean) => Record<string, any>)`
132132

133133
- 详情:
134134

packages/bundler-vite/src/plugins/mainPlugin.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ const resolveAlias = async ({
140140
}
141141

142142
// plugin hook: alias
143-
const aliasResult = await app.pluginApi.hooks.alias.process(app)
143+
const aliasResult = await app.pluginApi.hooks.alias.process(app, isServer)
144144

145145
aliasResult.forEach((aliasObject) =>
146146
Object.entries(aliasObject).forEach(([key, value]) => {
@@ -196,7 +196,7 @@ const resolveDefine = async ({
196196
}
197197

198198
// plugin hook: define
199-
const defineResult = await app.pluginApi.hooks.define.process(app)
199+
const defineResult = await app.pluginApi.hooks.define.process(app, isServer)
200200

201201
defineResult.forEach((defineObject) =>
202202
Object.entries(defineObject).forEach(([key, value]) => {

packages/bundler-webpack/src/config/createBaseConfig.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export const createBaseConfig = async ({
4747
/**
4848
* resolve
4949
*/
50-
await handleResolve({ app, config })
50+
await handleResolve({ app, config, isServer })
5151

5252
/**
5353
* module

packages/bundler-webpack/src/config/handlePluginDefine.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export const handlePluginDefine = async ({
3030
])
3131

3232
// plugin hook: define
33-
const defineResult = await app.pluginApi.hooks.define.process(app)
33+
const defineResult = await app.pluginApi.hooks.define.process(app, isServer)
3434

3535
// tap the arguments of DefinePlugin
3636
config.plugin('define').tap(([options]) => {

packages/bundler-webpack/src/config/handleResolve.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ import type Config from 'webpack-chain'
77
export const handleResolve = async ({
88
app,
99
config,
10+
isServer,
1011
}: {
1112
app: App
1213
config: Config
14+
isServer: boolean
1315
}): Promise<void> => {
1416
// aliases
1517
config.resolve.alias
@@ -28,7 +30,7 @@ export const handleResolve = async ({
2830
])
2931

3032
// plugin hook: alias
31-
const aliasResult = await app.pluginApi.hooks.alias.process(app)
33+
const aliasResult = await app.pluginApi.hooks.alias.process(app, isServer)
3234

3335
// set aliases
3436
aliasResult.forEach((aliasObject) =>

packages/core/src/pluginApi/createPluginApiRegisterHooks.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { HooksName, PluginApi } from '../types/index.js'
2+
import { normalizeAliasDefineHook } from './normalizeAliasDefineHook.js'
23
import { normalizeClientConfigFileHook } from './normalizeClientConfigFileHook.js'
3-
import { normalizeReturnObjectHook } from './normalizeReturnObjectHook.js'
44

55
export const createPluginApiRegisterHooks =
66
(
@@ -24,14 +24,14 @@ export const createPluginApiRegisterHooks =
2424
if (alias) {
2525
hooks.alias.add({
2626
pluginName,
27-
hook: normalizeReturnObjectHook(alias),
27+
hook: normalizeAliasDefineHook(alias),
2828
})
2929
}
3030

3131
if (define) {
3232
hooks.define.add({
3333
pluginName,
34-
hook: normalizeReturnObjectHook(define),
34+
hook: normalizeAliasDefineHook(define),
3535
})
3636
}
3737

packages/core/src/pluginApi/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ export * from './createHookQueue.js'
22
export * from './createPluginApi.js'
33
export * from './createPluginApiHooks.js'
44
export * from './createPluginApiRegisterHooks.js'
5+
export * from './normalizeAliasDefineHook.js'
56
export * from './normalizeClientConfigFileHook.js'
6-
export * from './normalizeReturnObjectHook.js'
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { isFunction } from '@vuepress/shared'
2+
import type { AliasDefineHook } from '../types/index.js'
3+
4+
/**
5+
* Normalize alias and define hook
6+
*/
7+
export const normalizeAliasDefineHook =
8+
(hook: AliasDefineHook['exposed']): AliasDefineHook['normalized'] =>
9+
async (app, isServer) =>
10+
isFunction(hook) ? hook(app, isServer) : hook

packages/core/src/pluginApi/normalizeReturnObjectHook.ts

Lines changed: 0 additions & 10 deletions
This file was deleted.

packages/core/src/types/pluginApi/hooks.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,11 @@ export type ClientConfigFileHook = Hook<
3737
(app: App) => Promise<string>
3838
>
3939

40-
// hook that returns an object
41-
export type ReturnObjectHook = Hook<
42-
Record<string, any> | ((app: App) => PromiseOrNot<Record<string, any>>),
43-
(app: App) => Promise<Record<string, any>>
40+
// alias and define hook
41+
export type AliasDefineHook = Hook<
42+
| Record<string, any>
43+
| ((app: App, isServer: boolean) => PromiseOrNot<Record<string, any>>),
44+
(app: App, isServer: boolean) => Promise<Record<string, any>>
4445
>
4546

4647
/**
@@ -57,8 +58,8 @@ export interface Hooks {
5758
extendsPage: ExtendsHook<Page>
5859
extendsBundlerOptions: ExtendsHook<any>
5960
clientConfigFile: ClientConfigFileHook
60-
alias: ReturnObjectHook
61-
define: ReturnObjectHook
61+
alias: AliasDefineHook
62+
define: AliasDefineHook
6263
}
6364

6465
/**
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { path } from '@vuepress/utils'
2+
import { describe, expect, it, vi } from 'vitest'
3+
import { createBaseApp, normalizeAliasDefineHook } from '../../src/index.js'
4+
import type { AliasDefineHook } from '../../src/index.js'
5+
6+
const app = createBaseApp({
7+
source: path.resolve(__dirname, 'fake-source'),
8+
theme: { name: 'test' },
9+
bundler: {} as any,
10+
})
11+
12+
describe('core > pluginApi > normalizeAliasDefineHook', () => {
13+
it('should keep function as is', async () => {
14+
const rawHook: AliasDefineHook['exposed'] = vi.fn((app, isServer) => ({
15+
foo: 'bar',
16+
isServer,
17+
}))
18+
const normalizedHook = normalizeAliasDefineHook(rawHook)
19+
expect(await normalizedHook(app, true)).toEqual({
20+
foo: 'bar',
21+
isServer: true,
22+
})
23+
expect(rawHook).toHaveBeenCalledTimes(1)
24+
expect(rawHook).toHaveBeenCalledWith(app, true)
25+
})
26+
27+
it('should wrap object with a function', async () => {
28+
const rawHook: AliasDefineHook['exposed'] = {
29+
foo: 'bar',
30+
}
31+
const normalizedHook = normalizeAliasDefineHook(rawHook)
32+
expect(await normalizedHook(app, true)).toEqual({ foo: 'bar' })
33+
})
34+
})

packages/core/tests/pluginApi/normalizeReturnObjectHook.spec.ts

Lines changed: 0 additions & 30 deletions
This file was deleted.

0 commit comments

Comments
 (0)