Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: warn for unresolved css in html #7911

Merged
merged 7 commits into from May 4, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/playground/html/index.html
Expand Up @@ -5,3 +5,6 @@ <h1>Hello</h1>
<!-- comment two -->
<script type="module"></script>
<script type="module" src="/main.js"></script>
<link rel="icon" href="{{cdn_host}}/cdn/images/favicon.ico" />
<link rel="stylesheet" href="{{cdn_host}}/css.css" type="text/css" />
<script src="{{cdn_host}}/js.js"></script>
16 changes: 15 additions & 1 deletion packages/vite/src/node/plugins/html.ts
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -469,6 +470,19 @@ 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)}`
}
})
)
poyoho marked this conversation as resolved.
Show resolved Hide resolved

processedHtml.set(id, s.toString())

// inject module preload polyfill only when configured and needed
Expand Down