@@ -247,6 +247,7 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
247
247
const s = new MagicString ( html )
248
248
const assetUrls : AttributeNode [ ] = [ ]
249
249
const scriptUrls : ScriptAssetsUrl [ ] = [ ]
250
+ const styleUrls : ScriptAssetsUrl [ ] = [ ]
250
251
let inlineModuleIndex = - 1
251
252
252
253
let everyScriptIsAsync = true
@@ -339,8 +340,13 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
339
340
if ( ! isExcludedUrl ( url ) ) {
340
341
if ( node . tag === 'link' && isCSSRequest ( url ) ) {
341
342
// CSS references, convert to import
342
- js += `\nimport ${ JSON . stringify ( url ) } `
343
- shouldRemove = true
343
+ const importExpression = `\nimport ${ JSON . stringify ( url ) } `
344
+ styleUrls . push ( {
345
+ url,
346
+ start : node . loc . start . offset ,
347
+ end : node . loc . end . offset
348
+ } )
349
+ js += importExpression
344
350
} else {
345
351
assetUrls . push ( p )
346
352
}
@@ -470,6 +476,25 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
470
476
}
471
477
}
472
478
479
+ // ignore <link rel="stylesheet"> if its url can't be resolved
480
+ const resolvedStyleUrls = await Promise . all (
481
+ styleUrls . map ( async ( styleUrl ) => ( {
482
+ ...styleUrl ,
483
+ resolved : await this . resolve ( styleUrl . url , id )
484
+ } ) )
485
+ )
486
+ for ( const { start, end, url, resolved } of resolvedStyleUrls ) {
487
+ if ( resolved == null ) {
488
+ config . logger . warnOnce (
489
+ `\n${ url } doesn't exist at build time, it will remain unchanged to be resolved at runtime`
490
+ )
491
+ const importExpression = `\nimport ${ JSON . stringify ( url ) } `
492
+ js = js . replace ( importExpression , '' )
493
+ } else {
494
+ s . remove ( start , end )
495
+ }
496
+ }
497
+
473
498
processedHtml . set ( id , s . toString ( ) )
474
499
475
500
// inject module preload polyfill only when configured and needed
0 commit comments