forked from laravel/vite-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
377 lines (316 loc) · 12.1 KB
/
index.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
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
import fs from 'fs'
import { AddressInfo } from 'net'
import path from 'path'
import colors from 'picocolors'
import { Plugin, loadEnv, UserConfig, ConfigEnv, ResolvedConfig, SSROptions, PluginOption } from 'vite'
import fullReload, { Config as FullReloadConfig } from 'vite-plugin-full-reload'
interface PluginConfig {
/**
* The path or paths of the entry points to compile.
*/
input: string|string[]
/**
* Laravel's public directory.
*
* @default 'public'
*/
publicDirectory?: string
/**
* The public subdirectory where compiled assets should be written.
*
* @default 'build'
*/
buildDirectory?: string
/**
* The path of the SSR entry point.
*/
ssr?: string|string[]
/**
* The directory where the SSR bundle should be written.
*
* @default 'bootstrap/ssr'
*/
ssrOutputDirectory?: string
/**
* Configuration for performing full page refresh on blade (or other) file changes.
*
* {@link https://github.com/ElMassimo/vite-plugin-full-reload}
* @default false
*/
refresh?: boolean|string|string[]|RefreshConfig|RefreshConfig[]
}
interface RefreshConfig {
paths: string[],
config?: FullReloadConfig,
}
interface LaravelPlugin extends Plugin {
config: (config: UserConfig, env: ConfigEnv) => UserConfig
}
type DevServerUrl = `${'http'|'https'}://${string}:${number}`
let exitHandlersBound = false
export const refreshPaths = [
'app/View/Components/**',
'resources/views/**',
'routes/**',
]
/**
* Laravel plugin for Vite.
*
* @param config - A config object or relative path(s) of the scripts to be compiled.
*/
export default function laravel(config: string|string[]|PluginConfig): [LaravelPlugin, ...Plugin[]] {
const pluginConfig = resolvePluginConfig(config)
return [
resolveLaravelPlugin(pluginConfig),
...resolveFullReloadConfig(pluginConfig) as Plugin[],
];
}
/**
* Resolve the Laravel Plugin configuration.
*/
function resolveLaravelPlugin(pluginConfig: Required<PluginConfig>): LaravelPlugin {
let viteDevServerUrl: DevServerUrl
let resolvedConfig: ResolvedConfig
const defaultAliases: Record<string, string> = {
'@': '/resources/js',
};
return {
name: 'laravel',
enforce: 'post',
config: (userConfig, { command, mode }) => {
const ssr = !! userConfig.build?.ssr
const env = loadEnv(mode, userConfig.envDir || process.cwd(), '')
const assetUrl = env.ASSET_URL ?? ''
return {
base: command === 'build' ? resolveBase(pluginConfig, assetUrl) : '',
publicDir: false,
build: {
manifest: !ssr,
outDir: userConfig.build?.outDir ?? resolveOutDir(pluginConfig, ssr),
rollupOptions: {
input: userConfig.build?.rollupOptions?.input ?? resolveInput(pluginConfig, ssr)
},
},
server: {
origin: '__laravel_vite_placeholder__',
...(process.env.LARAVEL_SAIL ? {
host: userConfig.server?.host ?? '0.0.0.0',
port: userConfig.server?.port ?? (env.VITE_PORT ? parseInt(env.VITE_PORT) : 5173),
strictPort: userConfig.server?.strictPort ?? true,
} : undefined)
},
resolve: {
alias: Array.isArray(userConfig.resolve?.alias)
? [
...userConfig.resolve?.alias ?? [],
...Object.keys(defaultAliases).map(alias => ({
find: alias,
replacement: defaultAliases[alias]
}))
]
: {
...defaultAliases,
...userConfig.resolve?.alias,
}
},
ssr: {
noExternal: noExternalInertiaHelpers(userConfig),
},
}
},
configResolved(config) {
resolvedConfig = config
},
transform(code) {
if (resolvedConfig.command === 'serve') {
return code.replace(/__laravel_vite_placeholder__/g, viteDevServerUrl)
}
},
configureServer(server) {
const hotFile = path.join(pluginConfig.publicDirectory, 'hot')
const envDir = resolvedConfig.envDir || process.cwd()
const appUrl = loadEnv('', envDir, 'APP_URL').APP_URL
server.httpServer?.once('listening', () => {
const address = server.httpServer?.address()
const isAddressInfo = (x: string|AddressInfo|null|undefined): x is AddressInfo => typeof x === 'object'
if (isAddressInfo(address)) {
viteDevServerUrl = resolveDevServerUrl(address, server.config)
fs.writeFileSync(hotFile, viteDevServerUrl)
setTimeout(() => {
server.config.logger.info(`\n ${colors.red(`${colors.bold('LARAVEL')} ${laravelVersion()}`)} ${colors.dim('plugin')} ${colors.bold(`v${pluginVersion()}`)}`)
server.config.logger.info('')
server.config.logger.info(` ${colors.green('➜')} ${colors.bold('APP_URL')}: ${colors.cyan(appUrl.replace(/:(\d+)/, (_, port) => `:${colors.bold(port)}`))}`)
}, 100)
}
})
if (exitHandlersBound) {
return
}
const clean = () => {
if (fs.existsSync(hotFile)) {
fs.rmSync(hotFile)
}
}
process.on('exit', clean)
process.on('SIGINT', process.exit)
process.on('SIGTERM', process.exit)
process.on('SIGHUP', process.exit)
exitHandlersBound = true
return () => server.middlewares.use((req, res, next) => {
if (req.url === '/index.html') {
server.config.logger.warn(
"\n" + colors.bgYellow(
colors.black(`The Vite server should not be accessed directly. Your Laravel application's configured APP_URL is: ${appUrl}`)
)
)
res.statusCode = 404
res.end(
fs.readFileSync(path.join(__dirname, 'dev-server-index.html')).toString().replace(/{{ APP_URL }}/g, appUrl)
)
}
next()
})
}
}
}
/**
* The version of Laravel being run.
*/
function laravelVersion(): string {
try {
const composer = JSON.parse(fs.readFileSync('composer.lock').toString())
return composer.packages?.find((composerPackage: {name: string}) => composerPackage.name === 'laravel/framework')?.version ?? ''
} catch {
return ''
}
}
/**
* The version of the Laravel Vite plugin being run.
*/
function pluginVersion(): string {
try {
return JSON.parse(fs.readFileSync(path.join(__dirname, '../package.json')).toString())?.version
} catch {
return ''
}
}
/**
* Convert the users configuration into a standard structure with defaults.
*/
function resolvePluginConfig(config: string|string[]|PluginConfig): Required<PluginConfig> {
if (typeof config === 'undefined') {
throw new Error('laravel-vite-plugin: missing configuration.')
}
if (typeof config === 'string' || Array.isArray(config)) {
config = { input: config, ssr: config }
}
if (typeof config.input === 'undefined') {
throw new Error('laravel-vite-plugin: missing configuration for "input".')
}
if (typeof config.publicDirectory === 'string') {
config.publicDirectory = config.publicDirectory.trim().replace(/^\/+/, '')
if (config.publicDirectory === '') {
throw new Error('laravel-vite-plugin: publicDirectory must be a subdirectory. E.g. \'public\'.')
}
}
if (typeof config.buildDirectory === 'string') {
config.buildDirectory = config.buildDirectory.trim().replace(/^\/+/, '').replace(/\/+$/, '')
if (config.buildDirectory === '') {
throw new Error('laravel-vite-plugin: buildDirectory must be a subdirectory. E.g. \'build\'.')
}
}
if (typeof config.ssrOutputDirectory === 'string') {
config.ssrOutputDirectory = config.ssrOutputDirectory.trim().replace(/^\/+/, '').replace(/\/+$/, '')
}
if (config.refresh === true) {
config.refresh = [{ paths: refreshPaths }]
}
return {
input: config.input,
publicDirectory: config.publicDirectory ?? 'public',
buildDirectory: config.buildDirectory ?? 'build',
ssr: config.ssr ?? config.input,
ssrOutputDirectory: config.ssrOutputDirectory ?? 'bootstrap/ssr',
refresh: config.refresh ?? false,
}
}
/**
* Resolve the Vite base option from the configuration.
*/
function resolveBase(config: Required<PluginConfig>, assetUrl: string): string {
return assetUrl + (! assetUrl.endsWith('/') ? '/' : '') + config.buildDirectory + '/'
}
/**
* Resolve the Vite input path from the configuration.
*/
function resolveInput(config: Required<PluginConfig>, ssr: boolean): string|string[]|undefined {
if (ssr) {
return config.ssr
}
return config.input
}
/**
* Resolve the Vite outDir path from the configuration.
*/
function resolveOutDir(config: Required<PluginConfig>, ssr: boolean): string|undefined {
if (ssr) {
return config.ssrOutputDirectory
}
return path.join(config.publicDirectory, config.buildDirectory)
}
function resolveFullReloadConfig({ refresh: config }: Required<PluginConfig>): PluginOption[]{
if (typeof config === 'boolean') {
return [];
}
if (typeof config === 'string') {
config = [{ paths: [config]}]
}
if (! Array.isArray(config)) {
config = [config]
}
if (config.some(c => typeof c === 'string')) {
config = [{ paths: config }] as RefreshConfig[]
}
return (config as RefreshConfig[]).flatMap(c => {
const plugin = fullReload(c.paths, c.config)
/* eslint-disable-next-line @typescript-eslint/ban-ts-comment */
/** @ts-ignore */
plugin.__laravel_plugin_config = c
return plugin
})
}
/**
* Resolve the dev server URL from the server address and configuration.
*/
function resolveDevServerUrl(address: AddressInfo, config: ResolvedConfig): DevServerUrl {
const configHmrProtocol = typeof config.server.hmr === 'object' ? config.server.hmr.protocol : null
const clientProtocol = configHmrProtocol ? (configHmrProtocol === 'wss' ? 'https' : 'http') : null
const serverProtocol = config.server.https ? 'https' : 'http'
const protocol = clientProtocol ?? serverProtocol
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 = address.family === 'IPv6' ? `[${address.address}]` : address.address
const host = configHmrHost ?? configHost ?? serverAddress
return `${protocol}://${host}:${address.port}`
}
/**
* Add the Inertia helpers to the list of SSR dependencies that aren't externalized.
*
* @see https://vitejs.dev/guide/ssr.html#ssr-externals
*/
function noExternalInertiaHelpers(config: UserConfig): true|Array<string|RegExp> {
/* eslint-disable-next-line @typescript-eslint/ban-ts-comment */
/* @ts-ignore */
const userNoExternal = (config.ssr as SSROptions|undefined)?.noExternal
const pluginNoExternal = ['laravel-vite-plugin']
if (userNoExternal === true) {
return true
}
if (typeof userNoExternal === 'undefined') {
return pluginNoExternal
}
return [
...(Array.isArray(userNoExternal) ? userNoExternal : [userNoExternal]),
...pluginNoExternal,
]
}