-
Notifications
You must be signed in to change notification settings - Fork 148
/
index.ts
327 lines (277 loc) · 10.6 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
import fs from 'fs'
import { AddressInfo } from 'net'
import path from 'path'
import colors from 'picocolors'
import { Plugin, loadEnv, UserConfig, ConfigEnv, Manifest, ResolvedConfig, SSROptions, normalizePath } from 'vite'
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 'storage/ssr'
*/
ssrOutputDirectory?: string
}
interface LaravelPlugin extends Plugin {
config: (config: UserConfig, env: ConfigEnv) => UserConfig
}
let exitHandlersBound = false
/**
* 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 {
const pluginConfig = resolvePluginConfig(config)
let viteDevServerUrl: string
let resolvedConfig: ResolvedConfig
const cssManifest: Manifest = {}
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, 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: '0.0.0.0',
port: env.VITE_PORT ? parseInt(env.VITE_PORT) : 5173,
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')
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)) {
const protocol = server.config.server.https ? 'https' : 'http'
const configHost = typeof server.config.server.host === 'string' ? server.config.server.host : null
const serverAddress = address.family === 'IPv6' ? `[${address.address}]` : address.address
const host = configHost ?? serverAddress
viteDevServerUrl = `${protocol}://${host}:${address.port}`
fs.writeFileSync(hotFile, viteDevServerUrl)
const envDir = resolvedConfig.envDir || process.cwd()
const appUrl = loadEnv('', envDir, 'APP_URL').APP_URL
setTimeout(() => {
server.config.logger.info(colors.red(`\n Laravel ${laravelVersion()} `))
server.config.logger.info(`\n > APP_URL: ` + colors.cyan(appUrl))
})
}
})
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
},
// The following two hooks are a workaround to help solve a "flash of unstyled content" with Blade.
// They add any CSS entry points into the manifest because Vite does not currently do this.
renderChunk(_, chunk) {
const cssLangs = `\\.(css|less|sass|scss|styl|stylus|pcss|postcss)($|\\?)`
const cssLangRE = new RegExp(cssLangs)
if (! chunk.isEntry || chunk.facadeModuleId === null || ! cssLangRE.test(chunk.facadeModuleId)) {
return null
}
const relativeChunkPath = normalizePath(path.relative(resolvedConfig.root, chunk.facadeModuleId))
cssManifest[relativeChunkPath] = {
/* eslint-disable-next-line @typescript-eslint/ban-ts-comment */
/* @ts-ignore */
file: Array.from(chunk.viteMetadata.importedCss)[0],
src: relativeChunkPath,
isEntry: true,
}
return null
},
writeBundle() {
const manifestConfig = resolveManifestConfig(resolvedConfig)
if (manifestConfig === false) {
return;
}
const manifestPath = path.resolve(resolvedConfig.root, resolvedConfig.build.outDir, manifestConfig)
const manifest = JSON.parse(fs.readFileSync(manifestPath).toString())
const newManifest = {
...manifest,
...cssManifest,
}
fs.writeFileSync(manifestPath, JSON.stringify(newManifest, null, 2))
}
}
}
/**
* 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 ''
}
}
/**
* 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(/\/+$/, '')
}
return {
input: config.input,
publicDirectory: config.publicDirectory ?? 'public',
buildDirectory: config.buildDirectory ?? 'build',
ssr: config.ssr ?? config.input,
ssrOutputDirectory: config.ssrOutputDirectory ?? 'storage/ssr',
}
}
/**
* 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)
}
/**
* Resolve the Vite manifest config from the configuration.
*/
function resolveManifestConfig(config: ResolvedConfig): string|false
{
const manifestConfig = config.build.ssr
? config.build.ssrManifest
: config.build.manifest;
if (manifestConfig === false) {
return false
}
if (manifestConfig === true) {
return config.build.ssr ? 'ssr-manifest.json' : 'manifest.json'
}
return manifestConfig
}
/**
* Add the Interia 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,
]
}