From 219dda2a4f8337fcad0af6e81f53a3d98580353e Mon Sep 17 00:00:00 2001 From: yoho Date: Tue, 26 Apr 2022 19:46:53 +0800 Subject: [PATCH 1/7] fix: warn for unresolved css in html --- packages/playground/html/index.html | 3 +++ packages/vite/src/node/plugins/html.ts | 13 ++++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/packages/playground/html/index.html b/packages/playground/html/index.html index 7320ff2b097db0..783cad93172f7a 100644 --- a/packages/playground/html/index.html +++ b/packages/playground/html/index.html @@ -5,3 +5,6 @@

Hello

+ + + diff --git a/packages/vite/src/node/plugins/html.ts b/packages/vite/src/node/plugins/html.ts index ed4250f1965869..eb5b283ad38237 100644 --- a/packages/vite/src/node/plugins/html.ts +++ b/packages/vite/src/node/plugins/html.ts @@ -246,6 +246,7 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin { const s = new MagicString(html) const assetUrls: AttributeNode[] = [] const scriptUrls: ScriptAssetsUrl[] = [] + const styleUrls: string[] = [] let inlineModuleIndex = -1 let everyScriptIsAsync = true @@ -338,7 +339,7 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin { if (!isExcludedUrl(url)) { if (node.tag === 'link' && isCSSRequest(url)) { // CSS references, convert to import - js += `\nimport ${JSON.stringify(url)}` + styleUrls.push(url) shouldRemove = true } else { assetUrls.push(p) @@ -468,6 +469,16 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin { }) } } + for (const styleUrl of styleUrls) { + const resolvedUrl = await this.resolve(styleUrl) + if (!resolvedUrl) { + config.logger.warnOnce( + `\n${styleUrl} doesn't exist at build time, it will remain unchanged to be resolved at runtime` + ) + } else { + js += `\nimport ${JSON.stringify(styleUrl)}` + } + } processedHtml.set(id, s.toString()) From 77bcec0bbf0284ba3665d96164d0d32558172e4b Mon Sep 17 00:00:00 2001 From: yoho Date: Tue, 26 Apr 2022 23:39:26 +0800 Subject: [PATCH 2/7] feat: promise all --- packages/vite/src/node/plugins/html.ts | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/packages/vite/src/node/plugins/html.ts b/packages/vite/src/node/plugins/html.ts index eb5b283ad38237..4fbcbec36e2adf 100644 --- a/packages/vite/src/node/plugins/html.ts +++ b/packages/vite/src/node/plugins/html.ts @@ -469,16 +469,19 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin { }) } } - for (const styleUrl of styleUrls) { - const resolvedUrl = await this.resolve(styleUrl) - if (!resolvedUrl) { - config.logger.warnOnce( - `\n${styleUrl} doesn't exist at build time, it will remain unchanged to be resolved at runtime` - ) - } else { - js += `\nimport ${JSON.stringify(styleUrl)}` - } - } + + await Promise.all( + styleUrls.map(async (styleUrl) => { + const resolvedUrl = await this.resolve(styleUrl, id) + if (resolvedUrl == null) { + config.logger.warnOnce( + `\n${styleUrl} doesn't exist at build time, it will remain unchanged to be resolved at runtime` + ) + } else { + js += `\nimport ${JSON.stringify(styleUrl)}` + } + }) + ) processedHtml.set(id, s.toString()) From 1a84a447692a09725dc53c5393c09f2a3cc1a7c1 Mon Sep 17 00:00:00 2001 From: yoho Date: Wed, 27 Apr 2022 00:43:17 +0800 Subject: [PATCH 3/7] fix: remove node --- packages/vite/src/node/plugins/html.ts | 33 ++++++++++++++------------ 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/packages/vite/src/node/plugins/html.ts b/packages/vite/src/node/plugins/html.ts index 4fbcbec36e2adf..2de4a729b5715b 100644 --- a/packages/vite/src/node/plugins/html.ts +++ b/packages/vite/src/node/plugins/html.ts @@ -246,7 +246,7 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin { const s = new MagicString(html) const assetUrls: AttributeNode[] = [] const scriptUrls: ScriptAssetsUrl[] = [] - const styleUrls: string[] = [] + const styleUrls: ScriptAssetsUrl[] = [] let inlineModuleIndex = -1 let everyScriptIsAsync = true @@ -339,8 +339,11 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin { if (!isExcludedUrl(url)) { if (node.tag === 'link' && isCSSRequest(url)) { // CSS references, convert to import - styleUrls.push(url) - shouldRemove = true + styleUrls.push({ + url, + start: node.loc.start.offset, + end: node.loc.end.offset + }) } else { assetUrls.push(p) } @@ -470,18 +473,18 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin { } } - await Promise.all( - styleUrls.map(async (styleUrl) => { - const resolvedUrl = await this.resolve(styleUrl, id) - if (resolvedUrl == null) { - config.logger.warnOnce( - `\n${styleUrl} doesn't exist at build time, it will remain unchanged to be resolved at runtime` - ) - } else { - js += `\nimport ${JSON.stringify(styleUrl)}` - } - }) - ) + // ignore can't resolve src + for (const { start, end, url } of styleUrls) { + const resolvedUrl = await this.resolve(url, id) + if (resolvedUrl == null) { + config.logger.warnOnce( + `\n${url} doesn't exist at build time, it will remain unchanged to be resolved at runtime` + ) + } else { + js += `\nimport ${JSON.stringify(url)}` + s.remove(start, end) + } + } processedHtml.set(id, s.toString()) From 2560d4b4ccc946f637e1ac36af98d3333d2f43a3 Mon Sep 17 00:00:00 2001 From: yoho <907415276@qq.com> Date: Wed, 27 Apr 2022 09:22:09 +0800 Subject: [PATCH 4/7] chore: comment Co-authored-by: patak --- packages/vite/src/node/plugins/html.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/vite/src/node/plugins/html.ts b/packages/vite/src/node/plugins/html.ts index 2de4a729b5715b..ffff2f5a36e351 100644 --- a/packages/vite/src/node/plugins/html.ts +++ b/packages/vite/src/node/plugins/html.ts @@ -473,7 +473,7 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin { } } - // ignore can't resolve src + // ignore if its url can't be resolved for (const { start, end, url } of styleUrls) { const resolvedUrl = await this.resolve(url, id) if (resolvedUrl == null) { From e1b92fbd21555fe227dd7122cceb9bcd39e7515a Mon Sep 17 00:00:00 2001 From: yoho Date: Wed, 27 Apr 2022 22:07:30 +0800 Subject: [PATCH 5/7] feat: kept import order --- packages/vite/src/node/plugins/html.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/vite/src/node/plugins/html.ts b/packages/vite/src/node/plugins/html.ts index ffff2f5a36e351..61e438337b1d9c 100644 --- a/packages/vite/src/node/plugins/html.ts +++ b/packages/vite/src/node/plugins/html.ts @@ -339,11 +339,13 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin { if (!isExcludedUrl(url)) { if (node.tag === 'link' && isCSSRequest(url)) { // CSS references, convert to import + const importExpression = `\nimport ${JSON.stringify(url)}` styleUrls.push({ - url, + url: importExpression, start: node.loc.start.offset, end: node.loc.end.offset }) + js += importExpression } else { assetUrls.push(p) } @@ -480,8 +482,8 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin { config.logger.warnOnce( `\n${url} doesn't exist at build time, it will remain unchanged to be resolved at runtime` ) + js = js.replace(url, '') } else { - js += `\nimport ${JSON.stringify(url)}` s.remove(start, end) } } From 829ebcacc04e47e4761f2810c77101093f42f2f4 Mon Sep 17 00:00:00 2001 From: yoho Date: Fri, 29 Apr 2022 19:21:09 +0800 Subject: [PATCH 6/7] feat: promise.all --- packages/vite/src/node/plugins/html.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/packages/vite/src/node/plugins/html.ts b/packages/vite/src/node/plugins/html.ts index 61e438337b1d9c..0ace8123f43801 100644 --- a/packages/vite/src/node/plugins/html.ts +++ b/packages/vite/src/node/plugins/html.ts @@ -476,9 +476,14 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin { } // ignore if its url can't be resolved - for (const { start, end, url } of styleUrls) { - const resolvedUrl = await this.resolve(url, id) - if (resolvedUrl == null) { + const resolvedStyleUrls = await Promise.all( + styleUrls.map(async (styleUrl) => ({ + ...styleUrl, + resolved: await this.resolve(styleUrl.url, id) + })) + ) + for (const { start, end, url, resolved } of resolvedStyleUrls) { + if (resolved == null) { config.logger.warnOnce( `\n${url} doesn't exist at build time, it will remain unchanged to be resolved at runtime` ) From 9afaa75c1aaddde927485b57e537fa2779501ce8 Mon Sep 17 00:00:00 2001 From: yoho Date: Thu, 5 May 2022 00:38:44 +0800 Subject: [PATCH 7/7] fix: errors --- packages/vite/src/node/plugins/html.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/vite/src/node/plugins/html.ts b/packages/vite/src/node/plugins/html.ts index 0ace8123f43801..ac06e5d5954cde 100644 --- a/packages/vite/src/node/plugins/html.ts +++ b/packages/vite/src/node/plugins/html.ts @@ -341,7 +341,7 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin { // CSS references, convert to import const importExpression = `\nimport ${JSON.stringify(url)}` styleUrls.push({ - url: importExpression, + url, start: node.loc.start.offset, end: node.loc.end.offset }) @@ -487,7 +487,8 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin { config.logger.warnOnce( `\n${url} doesn't exist at build time, it will remain unchanged to be resolved at runtime` ) - js = js.replace(url, '') + const importExpression = `\nimport ${JSON.stringify(url)}` + js = js.replace(importExpression, '') } else { s.remove(start, end) }