-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathvite-plugin.ts
More file actions
296 lines (243 loc) · 8.09 KB
/
Copy pathvite-plugin.ts
File metadata and controls
296 lines (243 loc) · 8.09 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
import Path from 'node:path'
import { AddressInfo } from 'node:net'
import { Str } from '@supercharge/strings'
import { HotReloadFile } from './vite-hotfile.js'
import { DevServerUrl, PluginConfigContract } from './vite-plugin-types.js'
import { ConfigEnv, Plugin, ResolvedConfig, UserConfig, ViteDevServer } from 'vite'
/**
* Supercharge plugin for Vite.
*/
export function supercharge (config: string | string[] | PluginConfigContract): Plugin {
const pluginConfig = resolvePluginConfig(config)
return resolveSuperchargePlugin(pluginConfig)
}
/**
* Returns the validated plugin configuration, with default values where necessary.
*/
function resolvePluginConfig (config: string | string[] | PluginConfigContract): Required<PluginConfigContract> {
if (!config) {
throw new Error('supercharge-vite-plugin: missing configuration object')
}
if (typeof config === 'string') {
config = [config]
}
if (Array.isArray(config)) {
config = { entrypoints: config }
}
if (!config.entrypoints && config.input) {
config.entrypoints = config.input
}
if (!config.entrypoints) {
throw new Error('supercharge-vite-plugin: missing "entrypoints" configuration')
}
if (typeof config.publicDirectory === 'string') {
config.publicDirectory = Str(config.publicDirectory).trim().ltrim('/').get()
if (config.publicDirectory === '') {
throw new Error('supercharge-vite-plugin: the "publicDirectory" option must be a subdirectory, like "public"')
}
}
if (typeof config.buildDirectory === 'string') {
config.buildDirectory = Str(config.buildDirectory).trim().ltrim('/').rtrim('/').get()
if (config.buildDirectory === '') {
throw new Error('supercharge-vite-plugin: the "buildDirectory" option must be a subdirectory, like "build"')
}
}
if (typeof config.ssrOutputDirectory === 'string') {
config.ssrOutputDirectory = Str(config.ssrOutputDirectory).trim().ltrim('/').rtrim('/').get()
if (config.ssrOutputDirectory === '') {
throw new Error('supercharge-vite-plugin: the "ssrOutputDirectory" option must be a subdirectory, like "ssr"')
}
}
const publicDirectory = config.publicDirectory ?? 'public'
const buildDirectory = config.buildDirectory ?? 'build'
return {
input: config.input,
publicDirectory,
buildDirectory,
ssr: config.ssr ?? config.input,
ssrOutputDirectory: config.ssrOutputDirectory ?? 'bootstrap/ssr',
hotFilePath: config.hotFilePath ?? Path.join(publicDirectory, buildDirectory, '.vite', 'hot.json'),
}
}
/**
* Returns the resolved Supercharge plugin config.
*/
function resolveSuperchargePlugin (pluginConfig: Required<PluginConfigContract>): Plugin {
let viteDevServerUrl: DevServerUrl
let resolvedConfig: ResolvedConfig
return {
name: 'supercharge',
enforce: 'post',
/**
* Hook into the Vite configuration before it is resolved. This adjusts the
* configuration for a project using the Supercharge directory structure.
*/
config (userConfig: UserConfig, { command }: ConfigEnv): UserConfig {
const isSsrBuild = !!userConfig.build?.ssr
return {
base: userConfig.base ?? (command === 'build' ? resolveBase(pluginConfig) : '/'),
publicDir: userConfig.publicDir ?? false,
build: {
manifest: !isSsrBuild,
outDir: userConfig.build?.outDir ?? resolveOutDir(pluginConfig, isSsrBuild),
rollupOptions: {
input: userConfig.build?.rollupOptions?.input ?? resolveInput(pluginConfig, isSsrBuild)
},
assetsInlineLimit: userConfig.build?.assetsInlineLimit ?? 0,
},
server: {
origin: '__supercharge_vite_placeholder__',
...userConfig.server
},
ssr: {
noExternal: noExternalInertiaHelpers(userConfig),
},
}
},
/**
* This hook stores the final, resolved Vite config.
*/
configResolved (config) {
resolvedConfig = config
},
/**
* Hook into Vite’s code transform lifecycle setp.
*/
transform (code: string) {
if (resolvedConfig.command !== 'serve') {
return
}
return Str(code)
.replaceAll('__supercharge_vite_placeholder__', viteDevServerUrl)
.get()
},
/**
* Configure the Vite server.
*/
configureServer (server: ViteDevServer) {
const hotfile = new HotReloadFile(
Path.join(resolvedConfig.root, pluginConfig.hotFilePath)
)
server.httpServer?.once('listening', () => {
const address = server.httpServer?.address()
if (isAddressInfo(address)) {
viteDevServerUrl = resolveDevServerUrl(address, server.config)
hotfile.writeFileSync({ viteDevServerUrl })
}
})
}
}
}
/**
* Returns the resolved base option based on the build directory.
*/
function resolveBase (pluginConfig: Required<PluginConfigContract>): string {
return `/${pluginConfig.buildDirectory}/`
}
/**
* Returns the output path for the compiled assets.
*/
function resolveOutDir (pluginConfig: Required<PluginConfigContract>, useSsr: boolean): string {
const { publicDirectory, buildDirectory, ssrOutputDirectory } = pluginConfig
return useSsr
? ssrOutputDirectory
: Path.join(publicDirectory, buildDirectory)
}
/**
* Returns the input path for the Vite configuration.
*/
function resolveInput (pluginConfig: Required<PluginConfigContract>, useSsr: boolean): string | string[] | undefined {
return useSsr
? pluginConfig.ssr
: pluginConfig.input
}
/**
* Determine whether the given `address` is an `AddressInfo` object.
*/
function isAddressInfo (address: string | AddressInfo | null | undefined): address is AddressInfo {
return typeof address === 'object'
}
/**
* Returns the resolved Vite dev server URL.
*/
function resolveDevServerUrl (address: AddressInfo, config: ResolvedConfig): DevServerUrl {
return `${protocol(config)}://${host(address, config)}:${port(address, config)}`
}
/**
* Returns the dev server protocol.
*/
function protocol (config: ResolvedConfig): 'http' | 'https' {
return clientProtocol(config) ?? serverProtocol(config)
}
/**
* Returns the client protocol.
*/
function clientProtocol (config: ResolvedConfig): 'https' | 'http' | undefined {
const configHmrProtocol = typeof config.server.hmr === 'object'
? config.server.hmr.protocol
: null
if (!configHmrProtocol) {
return
}
return configHmrProtocol === 'wss'
? 'https'
: 'http'
}
/**
* Returns the server protocol.
*/
function serverProtocol (config: ResolvedConfig): 'https' | 'http' {
return config.server.https
? 'https'
: 'http'
}
/**
* Returns the server’s host address.
*/
function host (address: AddressInfo, config: ResolvedConfig): string {
const configHmrHost = typeof config.server.hmr === 'object'
? config.server.hmr.host
: null
const configHost = typeof config.server.host === 'string'
? config.server.host
: null
const serverAddress = isIpv6(address)
? `[${address.address}]`
: address.address
return configHmrHost ?? configHost ?? serverAddress
}
/**
* Determine whether the given `address` uses an IPv6 address.
*/
function isIpv6 (address: AddressInfo): boolean {
if (typeof address.family === 'string') {
return address.family === 'IPv6'
}
// In Node.js >=18.0 <18.4 this was an integer value. This was changed in a minor version.
return address.family === 6
}
/**
* Returns the server’s port.
*/
function port (address: AddressInfo, config: ResolvedConfig): number {
const configHmrPort = typeof config.server.hmr === 'object'
? config.server.hmr.clientPort
: null
return configHmrPort ?? address.port
}
/**
* Returns the values for Vite’s `ssr.noExternal` configuration option.
*/
function noExternalInertiaHelpers (userConfig: UserConfig): true | Array<string | RegExp> {
const userNoExternal = (userConfig.ssr)?.noExternal
const pluginNoExternal = ['supercharge-vite-plugin']
if (userNoExternal === true) {
return true
}
if (userNoExternal == null) {
return pluginNoExternal
}
return ([] as Array<string | RegExp>)
.concat(userNoExternal)
.concat(pluginNoExternal)
}