@@ -16,14 +16,25 @@ import {
1616import type { ResolvedConfig , ViteDevServer } from '../..'
1717import { send } from '../send'
1818import { CLIENT_PUBLIC_PATH , FS_PREFIX } from '../../constants'
19- import { cleanUrl , fsPathFromId , normalizePath , injectQuery } from '../../utils'
19+ import {
20+ cleanUrl ,
21+ fsPathFromId ,
22+ normalizePath ,
23+ injectQuery ,
24+ ensureWatchedFile
25+ } from '../../utils'
2026import type { ModuleGraph } from '../moduleGraph'
2127
28+ interface AssetNode {
29+ start : number
30+ end : number
31+ code : string
32+ }
33+
2234export function createDevHtmlTransformFn (
2335 server : ViteDevServer
2436) : ( url : string , html : string , originalUrl : string ) => Promise < string > {
2537 const [ preHooks , postHooks ] = resolveHtmlTransforms ( server . config . plugins )
26-
2738 return ( url : string , html : string , originalUrl : string ) : Promise < string > => {
2839 return applyHtmlTransforms ( html , [ ...preHooks , devHtmlHook , ...postHooks ] , {
2940 path : url ,
@@ -94,14 +105,15 @@ const devHtmlHook: IndexHtmlTransformHook = async (
94105 html ,
95106 { path : htmlPath , filename, server, originalUrl }
96107) => {
97- const { config, moduleGraph } = server !
108+ const { config, moduleGraph, watcher } = server !
98109 const base = config . base || '/'
99110
100111 const s = new MagicString ( html )
101112 let inlineModuleIndex = - 1
102113 const filePath = cleanUrl ( htmlPath )
114+ const styleUrl : AssetNode [ ] = [ ]
103115
104- const addInlineModule = ( node : ElementNode , ext : 'js' | 'css' ) => {
116+ const addInlineModule = ( node : ElementNode , ext : 'js' ) => {
105117 inlineModuleIndex ++
106118
107119 const url = filePath . replace ( normalizePath ( config . root ) , '' )
@@ -128,7 +140,6 @@ const devHtmlHook: IndexHtmlTransformHook = async (
128140 if ( module ) {
129141 server ?. moduleGraph . invalidateModule ( module )
130142 }
131-
132143 s . overwrite (
133144 node . loc . start . offset ,
134145 node . loc . end . offset ,
@@ -154,7 +165,12 @@ const devHtmlHook: IndexHtmlTransformHook = async (
154165 }
155166
156167 if ( node . tag === 'style' && node . children . length ) {
157- addInlineModule ( node , 'css' )
168+ const children = node . children [ 0 ] as TextNode
169+ styleUrl . push ( {
170+ start : children . loc . start . offset ,
171+ end : children . loc . end . offset ,
172+ code : children . content
173+ } )
158174 }
159175
160176 // elements with [href/src] attrs
@@ -172,6 +188,19 @@ const devHtmlHook: IndexHtmlTransformHook = async (
172188 }
173189 } )
174190
191+ await Promise . all (
192+ styleUrl . map ( async ( { start, end, code } , index ) => {
193+ const url = filename + `?html-proxy&${ index } .css`
194+
195+ // ensure module in graph after successful load
196+ const mod = await moduleGraph . ensureEntryFromUrl ( url , false )
197+ ensureWatchedFile ( watcher , mod . file , config . root )
198+
199+ const result = await server ! . pluginContainer . transform ( code , url )
200+ s . overwrite ( start , end , result ?. code || '' )
201+ } )
202+ )
203+
175204 html = s . toString ( )
176205
177206 return {
0 commit comments