From a5d0bf693ed71d13c6ec52e466c10f5d0616c10c Mon Sep 17 00:00:00 2001 From: Jess Archer Date: Thu, 9 Jun 2022 12:03:27 +1000 Subject: [PATCH] Add CSS entrypoints to the manifest --- src/index.ts | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/src/index.ts b/src/index.ts index dda6002..ab5e20e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -2,7 +2,7 @@ import fs from 'fs' import { AddressInfo } from 'net' import path from 'path' import colors from 'picocolors' -import { Plugin, loadEnv, UserConfig, ConfigEnv, ResolvedConfig } from 'vite' +import { Plugin, loadEnv, UserConfig, ConfigEnv, Manifest, ResolvedConfig } from 'vite' interface PluginConfig { /** @@ -54,6 +54,7 @@ export default function laravel(config?: string|string[]|Partial): const pluginConfig = resolvePluginConfig(config) let viteDevServerUrl: string let resolvedConfig: ResolvedConfig + const cssManifest: Manifest = {} const ziggy = 'vendor/tightenco/ziggy/dist/index.es.js'; const defaultAliases: Record = { @@ -144,6 +145,38 @@ export default function laravel(config?: string|string[]|Partial): process.on('SIGHUP', clean) process.on('SIGINT', clean) process.on('SIGTERM', clean) + }, + + // 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 = 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 manifestPath = path.resolve(resolvedConfig.root, resolvedConfig.build.outDir, 'manifest.json') + const manifest = JSON.parse(fs.readFileSync(manifestPath).toString()) + const newManifest = { + ...manifest, + ...cssManifest, + } + fs.writeFileSync(manifestPath, JSON.stringify(newManifest, null, 2)) } } }