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: external URLs normalized to absolute path #14744

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 5 additions & 10 deletions packages/vite/src/node/plugins/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
cleanUrl,
generateCodeFrame,
getHash,
isDataUrl,
isExcludedUrl,
isExternalUrl,
normalizePath,
processSrcSet,
Expand Down Expand Up @@ -292,11 +292,6 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
preHooks.push(htmlEnvHook(config))
postHooks.push(postImportMapHook())
const processedHtml = new Map<string, string>()
const isExcludedUrl = (url: string) =>
url[0] === '#' ||
isExternalUrl(url) ||
isDataUrl(url) ||
checkPublicFile(url, config)
// Same reason with `htmlInlineProxyPlugin`
isAsyncScriptMap.set(config, new Map())

Expand Down Expand Up @@ -402,7 +397,7 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {

if (isModule) {
inlineModuleIndex++
if (url && !isExcludedUrl(url)) {
if (url && !isExcludedUrl(url, config)) {
// <script type="module" src="..."/>
// add it as an import
js += `\nimport ${JSON.stringify(url)}`
Expand All @@ -424,7 +419,7 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
someScriptsAreAsync ||= isAsync
someScriptsAreDefer ||= !isAsync
} else if (url && !isPublicFile) {
if (!isExcludedUrl(url)) {
if (!isExcludedUrl(url, config)) {
config.logger.warn(
`<script src="${url}"> in "${publicPath}" can't be bundled without type="module" attribute`,
)
Expand All @@ -449,7 +444,7 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
node.sourceCodeLocation!.attrs![attrKey]
// assetsUrl may be encodeURI
const url = decodeURI(p.value)
if (!isExcludedUrl(url)) {
if (!isExcludedUrl(url, config)) {
if (
node.nodeName === 'link' &&
isCSSRequest(url) &&
Expand Down Expand Up @@ -573,7 +568,7 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
}
// emit <script>import("./aaa")</script> asset
for (const { start, end, url } of scriptUrls) {
if (!isExcludedUrl(url)) {
if (!isExcludedUrl(url, config)) {
s.update(start, end, await urlToBuiltUrl(url, id, config, this))
} else if (checkPublicFile(url, config)) {
s.update(start, end, toOutputPublicFilePath(url))
Expand Down
5 changes: 4 additions & 1 deletion packages/vite/src/node/server/middlewares/indexHtml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
fsPathFromId,
getHash,
injectQuery,
isExcludedUrl,
isJSRequest,
joinUrlSegments,
normalizePath,
Expand Down Expand Up @@ -135,7 +136,9 @@ const processNodeUrl = (
// prefix with base (dev only, base is never relative)
const replacer = (url: string) => {
const devBase = config.base
const fullUrl = path.posix.join(devBase, url)
const fullUrl = isExcludedUrl(url, config)
? url
: path.posix.join(devBase, url)
if (server && shouldPreTransform(url, config)) {
preTransformRequest(server, fullUrl, devBase)
}
Expand Down
10 changes: 10 additions & 0 deletions packages/vite/src/node/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import {
findNearestPackageData,
resolvePackageData,
} from './packages'
import { checkPublicFile } from './plugins/asset'
import type { CommonServerOptions } from '.'

/**
Expand Down Expand Up @@ -1320,3 +1321,12 @@ export function getPackageManagerCommand(
throw new TypeError(`Unknown command type: ${type}`)
}
}

export function isExcludedUrl(url: string, config: ResolvedConfig): boolean {
return (
url[0] === '#' ||
isExternalUrl(url) ||
isDataUrl(url) ||
!!checkPublicFile(url, config)
)
}
Loading