Skip to content

Commit a5d0bf6

Browse files
committed
Add CSS entrypoints to the manifest
1 parent 3f0be5d commit a5d0bf6

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

src/index.ts

Lines changed: 34 additions & 1 deletion
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, ResolvedConfig } from 'vite'
5+
import { Plugin, loadEnv, UserConfig, ConfigEnv, Manifest, ResolvedConfig } from 'vite'
66

77
interface PluginConfig {
88
/**
@@ -54,6 +54,7 @@ export default function laravel(config?: string|string[]|Partial<PluginConfig>):
5454
const pluginConfig = resolvePluginConfig(config)
5555
let viteDevServerUrl: string
5656
let resolvedConfig: ResolvedConfig
57+
const cssManifest: Manifest = {}
5758

5859
const ziggy = 'vendor/tightenco/ziggy/dist/index.es.js';
5960
const defaultAliases: Record<string, string> = {
@@ -144,6 +145,38 @@ export default function laravel(config?: string|string[]|Partial<PluginConfig>):
144145
process.on('SIGHUP', clean)
145146
process.on('SIGINT', clean)
146147
process.on('SIGTERM', clean)
148+
},
149+
150+
// The following two hooks are a workaround to help solve a "flash of unstyled content" with Blade.
151+
// They add any CSS entry points into the manifest because Vite does not currently do this.
152+
renderChunk(_, chunk) {
153+
const cssLangs = `\\.(css|less|sass|scss|styl|stylus|pcss|postcss)($|\\?)`
154+
const cssLangRE = new RegExp(cssLangs)
155+
156+
if (! chunk.isEntry || chunk.facadeModuleId === null || ! cssLangRE.test(chunk.facadeModuleId)) {
157+
return null
158+
}
159+
160+
const relativeChunkPath = path.relative(resolvedConfig.root, chunk.facadeModuleId)
161+
162+
cssManifest[relativeChunkPath] = {
163+
/* eslint-disable-next-line @typescript-eslint/ban-ts-comment */
164+
/* @ts-ignore */
165+
file: Array.from(chunk.viteMetadata.importedCss)[0],
166+
src: relativeChunkPath,
167+
isEntry: true,
168+
}
169+
170+
return null
171+
},
172+
writeBundle() {
173+
const manifestPath = path.resolve(resolvedConfig.root, resolvedConfig.build.outDir, 'manifest.json')
174+
const manifest = JSON.parse(fs.readFileSync(manifestPath).toString())
175+
const newManifest = {
176+
...manifest,
177+
...cssManifest,
178+
}
179+
fs.writeFileSync(manifestPath, JSON.stringify(newManifest, null, 2))
147180
}
148181
}
149182
}

0 commit comments

Comments
 (0)