/
pluginContainer.spec.ts
166 lines (139 loc) · 4.38 KB
/
pluginContainer.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import { beforeEach, describe, expect, it } from 'vitest'
import type { UserConfig } from '../../config'
import { resolveConfig } from '../../config'
import type { Plugin } from '../../plugin'
import { ModuleGraph } from '../moduleGraph'
import type { PluginContainer } from '../pluginContainer'
import { createPluginContainer } from '../pluginContainer'
let resolveId: (id: string) => any
let moduleGraph: ModuleGraph
describe('plugin container', () => {
describe('getModuleInfo', () => {
beforeEach(() => {
moduleGraph = new ModuleGraph((id) => resolveId(id))
})
it('can pass metadata between hooks', async () => {
const entryUrl = '/x.js'
const metaArray: any[] = []
const plugin: Plugin = {
name: 'p1',
resolveId(id) {
if (id === entryUrl) {
// The module hasn't been resolved yet, so its info is null.
const moduleInfo = this.getModuleInfo(entryUrl)
expect(moduleInfo).toEqual(null)
return { id, meta: { x: 1 } }
}
},
load(id) {
if (id === entryUrl) {
const { meta } = this.getModuleInfo(entryUrl)
metaArray.push(meta)
return { code: 'export {}', meta: { x: 2 } }
}
},
transform(code, id) {
if (id === entryUrl) {
const { meta } = this.getModuleInfo(entryUrl)
metaArray.push(meta)
return { meta: { x: 3 } }
}
},
buildEnd() {
const { meta } = this.getModuleInfo(entryUrl)
metaArray.push(meta)
},
}
const container = await getPluginContainer({
plugins: [plugin],
})
const entryModule = await moduleGraph.ensureEntryFromUrl(entryUrl, false)
expect(entryModule.meta).toEqual({ x: 1 })
const loadResult: any = await container.load(entryUrl)
expect(loadResult?.meta).toEqual({ x: 2 })
await container.transform(loadResult.code, entryUrl)
await container.close()
expect(metaArray).toEqual([{ x: 1 }, { x: 2 }, { x: 3 }])
})
it('can pass metadata between plugins', async () => {
const entryUrl = '/x.js'
const plugin1: Plugin = {
name: 'p1',
resolveId(id) {
if (id === entryUrl) {
return { id, meta: { x: 1 } }
}
},
}
const plugin2: Plugin = {
name: 'p2',
load(id) {
if (id === entryUrl) {
const { meta } = this.getModuleInfo(entryUrl)
expect(meta).toEqual({ x: 1 })
return null
}
},
}
const container = await getPluginContainer({
plugins: [plugin1, plugin2],
})
await moduleGraph.ensureEntryFromUrl(entryUrl, false)
await container.load(entryUrl)
expect.assertions(1)
})
it('can pass custom resolve opts between plugins', async () => {
const entryUrl = '/x.js'
const plugin1: Plugin = {
name: 'p1',
resolveId(id) {
if (id === entryUrl) {
return this.resolve('foobar', 'notreal', {
custom: { p1: 'success' },
isEntry: true,
})
}
},
}
const plugin2: Plugin = {
name: 'p2',
resolveId(id, importer, opts) {
if (id === 'foobar') {
expect(importer).toBe('notreal')
expect(opts).toEqual(
expect.objectContaining({
custom: { p1: 'success' },
isEntry: true,
}),
)
return entryUrl
}
},
load(id) {
if (id === entryUrl) {
return null
}
},
}
const container = await getPluginContainer({
plugins: [plugin1, plugin2],
})
await moduleGraph.ensureEntryFromUrl(entryUrl, false)
await container.load(entryUrl)
expect.assertions(2)
})
})
})
async function getPluginContainer(
inlineConfig?: UserConfig,
): Promise<PluginContainer> {
const config = await resolveConfig(
{ configFile: false, ...inlineConfig },
'serve',
)
// @ts-expect-error This plugin requires a ViteDevServer instance.
config.plugins = config.plugins.filter((p) => !/pre-alias/.test(p.name))
resolveId = (id) => container.resolveId(id)
const container = await createPluginContainer(config, moduleGraph)
return container
}