Skip to content

Commit 638365e

Browse files
authored
Merge pull request #77 from laravel/vite-3
[0.4.x] Vite 3 support
2 parents d0b8894 + 70db475 commit 638365e

File tree

4 files changed

+25
-78
lines changed

4 files changed

+25
-78
lines changed

UPGRADE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ If you prefer to build your assets on deploy instead of committing them to your
325325
You may start the SSR server using `node`:
326326

327327
```sh
328-
node bootstrap/ssr/ssr.js
328+
node bootstrap/ssr/ssr.mjs
329329
```
330330

331331
### Optional: Expose Vite port when using Laravel Sail

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@
3434
"eslint": "^8.14.0",
3535
"picocolors": "^1.0.0",
3636
"typescript": "^4.6.4",
37-
"vite": "^2.9.6",
37+
"vite": "^3.0.0",
3838
"vitest": "^0.12.4"
3939
},
4040
"peerDependencies": {
41-
"vite": "^2.9.9"
41+
"vite": "^3.0.0"
4242
},
4343
"engines": {
4444
"node": ">=14"

src/index.ts

Lines changed: 17 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import fs from 'fs'
22
import { AddressInfo } from 'net'
33
import path from 'path'
44
import colors from 'picocolors'
5-
import { Plugin, loadEnv, UserConfig, ConfigEnv, Manifest, ResolvedConfig, SSROptions, normalizePath, PluginOption } from 'vite'
5+
import { Plugin, loadEnv, UserConfig, ConfigEnv, ResolvedConfig, SSROptions, PluginOption } from 'vite'
66
import fullReload, { Config as FullReloadConfig } from 'vite-plugin-full-reload'
77

88
interface PluginConfig {
@@ -85,7 +85,6 @@ export default function laravel(config: string|string[]|PluginConfig): [LaravelP
8585
function resolveLaravelPlugin(pluginConfig: Required<PluginConfig>): LaravelPlugin {
8686
let viteDevServerUrl: DevServerUrl
8787
let resolvedConfig: ResolvedConfig
88-
const cssManifest: Manifest = {}
8988

9089
const defaultAliases: Record<string, string> = {
9190
'@': '/resources/js',
@@ -159,9 +158,10 @@ function resolveLaravelPlugin(pluginConfig: Required<PluginConfig>): LaravelPlug
159158
fs.writeFileSync(hotFile, viteDevServerUrl)
160159

161160
setTimeout(() => {
162-
server.config.logger.info(colors.red(`\n Laravel ${laravelVersion()} `))
163-
server.config.logger.info(`\n > APP_URL: ` + colors.cyan(appUrl))
164-
})
161+
server.config.logger.info(`\n ${colors.red(`${colors.bold('LARAVEL')} ${laravelVersion()}`)} ${colors.dim('plugin')} ${colors.bold(`v${pluginVersion()}`)}`)
162+
server.config.logger.info('')
163+
server.config.logger.info(` ${colors.green('➜')} ${colors.bold('APP_URL')}: ${colors.cyan(appUrl.replace(/:(\d+)/, (_, port) => `:${colors.bold(port)}`))}`)
164+
}, 100)
165165
}
166166
})
167167

@@ -199,50 +199,6 @@ function resolveLaravelPlugin(pluginConfig: Required<PluginConfig>): LaravelPlug
199199

200200
next()
201201
})
202-
},
203-
204-
// The following two hooks are a workaround to help solve a "flash of unstyled content" with Blade.
205-
// They add any CSS entry points into the manifest because Vite does not currently do this.
206-
renderChunk(_, chunk) {
207-
const cssLangs = `\\.(css|less|sass|scss|styl|stylus|pcss|postcss)($|\\?)`
208-
const cssLangRE = new RegExp(cssLangs)
209-
210-
if (! chunk.isEntry || chunk.facadeModuleId === null || ! cssLangRE.test(chunk.facadeModuleId)) {
211-
return null
212-
}
213-
214-
const relativeChunkPath = normalizePath(path.relative(resolvedConfig.root, chunk.facadeModuleId))
215-
216-
cssManifest[relativeChunkPath] = {
217-
/* eslint-disable-next-line @typescript-eslint/ban-ts-comment */
218-
/* @ts-ignore */
219-
file: Array.from(chunk.viteMetadata.importedCss)[0] ?? chunk.fileName,
220-
src: relativeChunkPath,
221-
isEntry: true,
222-
}
223-
224-
return null
225-
},
226-
writeBundle() {
227-
const manifestConfig = resolveManifestConfig(resolvedConfig)
228-
229-
if (manifestConfig === false) {
230-
return;
231-
}
232-
233-
const manifestPath = path.resolve(resolvedConfig.root, resolvedConfig.build.outDir, manifestConfig)
234-
235-
if (! fs.existsSync(manifestPath)) {
236-
// The manifest does not exist yet when first writing the legacy asset bundle.
237-
return;
238-
}
239-
240-
const manifest = JSON.parse(fs.readFileSync(manifestPath).toString())
241-
const newManifest = {
242-
...manifest,
243-
...cssManifest,
244-
}
245-
fs.writeFileSync(manifestPath, JSON.stringify(newManifest, null, 2))
246202
}
247203
}
248204
}
@@ -260,6 +216,17 @@ function laravelVersion(): string {
260216
}
261217
}
262218

219+
/**
220+
* The version of the Laravel Vite plugin being run.
221+
*/
222+
function pluginVersion(): string {
223+
try {
224+
return JSON.parse(fs.readFileSync(path.join(__dirname, '../package.json')).toString())?.version
225+
} catch {
226+
return ''
227+
}
228+
}
229+
263230
/**
264231
* Convert the users configuration into a standard structure with defaults.
265232
*/
@@ -339,26 +306,6 @@ function resolveOutDir(config: Required<PluginConfig>, ssr: boolean): string|und
339306
return path.join(config.publicDirectory, config.buildDirectory)
340307
}
341308

342-
/**
343-
* Resolve the Vite manifest config from the configuration.
344-
*/
345-
function resolveManifestConfig(config: ResolvedConfig): string|false
346-
{
347-
const manifestConfig = config.build.ssr
348-
? config.build.ssrManifest
349-
: config.build.manifest;
350-
351-
if (manifestConfig === false) {
352-
return false
353-
}
354-
355-
if (manifestConfig === true) {
356-
return config.build.ssr ? 'ssr-manifest.json' : 'manifest.json'
357-
}
358-
359-
return manifestConfig
360-
}
361-
362309
function resolveFullReloadConfig({ refresh: config }: Required<PluginConfig>): PluginOption[]{
363310
if (typeof config === 'boolean') {
364311
return [];
@@ -413,7 +360,7 @@ function noExternalInertiaHelpers(config: UserConfig): true|Array<string|RegExp>
413360
/* eslint-disable-next-line @typescript-eslint/ban-ts-comment */
414361
/* @ts-ignore */
415362
const userNoExternal = (config.ssr as SSROptions|undefined)?.noExternal
416-
const pluginNoExternal = ['laravel-vite-plugin']
363+
const pluginNoExternal = ['laravel-vite-plugin/inertia-helpers']
417364

418365
if (userNoExternal === true) {
419366
return true

tests/index.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ describe('laravel-vite-plugin', () => {
219219

220220
const noSsrConfig = plugin.config({ build: { ssr: true } }, { command: 'build', mode: 'production' })
221221
/* @ts-ignore */
222-
expect(noSsrConfig.ssr.noExternal).toEqual(['laravel-vite-plugin'])
222+
expect(noSsrConfig.ssr.noExternal).toEqual(['laravel-vite-plugin/inertia-helpers'])
223223

224224
/* @ts-ignore */
225225
const nothingExternalConfig = plugin.config({ ssr: { noExternal: true }, build: { ssr: true } }, { command: 'build', mode: 'production' })
@@ -229,12 +229,12 @@ describe('laravel-vite-plugin', () => {
229229
/* @ts-ignore */
230230
const arrayNoExternalConfig = plugin.config({ ssr: { noExternal: ['foo'] }, build: { ssr: true } }, { command: 'build', mode: 'production' })
231231
/* @ts-ignore */
232-
expect(arrayNoExternalConfig.ssr.noExternal).toEqual(['foo', 'laravel-vite-plugin'])
232+
expect(arrayNoExternalConfig.ssr.noExternal).toEqual(['foo', 'laravel-vite-plugin/inertia-helpers'])
233233

234234
/* @ts-ignore */
235235
const stringNoExternalConfig = plugin.config({ ssr: { noExternal: 'foo' }, build: { ssr: true } }, { command: 'build', mode: 'production' })
236236
/* @ts-ignore */
237-
expect(stringNoExternalConfig.ssr.noExternal).toEqual(['foo', 'laravel-vite-plugin'])
237+
expect(stringNoExternalConfig.ssr.noExternal).toEqual(['foo', 'laravel-vite-plugin/inertia-helpers'])
238238
})
239239

240240
it('does not configure full reload when configuration it not an object', () => {
@@ -360,8 +360,8 @@ describe('inertia-helpers', () => {
360360
expect(file.default).toBe('Dummy File')
361361
})
362362

363-
it('pass globEager value to resolvePageComponent', async () => {
364-
const file = await resolvePageComponent<{ default: string }>(path, import.meta.globEager('./__data__/*.ts'))
363+
it('pass eagerly globed value to resolvePageComponent', async () => {
364+
const file = await resolvePageComponent<{ default: string }>(path, import.meta.glob('./__data__/*.ts', { eager: true }))
365365
expect(file.default).toBe('Dummy File')
366366
})
367367
})

0 commit comments

Comments
 (0)