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: exclude external dependencies from html rewriting #11053

Closed
wants to merge 1 commit into from
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
16 changes: 15 additions & 1 deletion packages/vite/src/node/plugins/asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@ import {
} from '../build'
import type { Plugin } from '../plugin'
import type { ResolvedConfig } from '../config'
import { cleanUrl, getHash, joinUrlSegments, normalizePath } from '../utils'
import {
cleanUrl,
getHash,
getIdMatcher,
joinUrlSegments,
normalizePath
} from '../utils'
import { FS_PREFIX } from '../constants'

export const assetUrlRE = /__VITE_ASSET__([a-z\d]{8})__(?:\$_(.*?)__)?/g
Expand Down Expand Up @@ -224,6 +230,14 @@ export function checkPublicFile(
}
}

export function checkUserExternal(
url: string,
{ build }: ResolvedConfig
): boolean {
const isUserExternal = getIdMatcher(build?.rollupOptions?.external)
return isUserExternal(url, undefined, false)
}

export async function fileToUrl(
id: string,
config: ResolvedConfig,
Expand Down
4 changes: 3 additions & 1 deletion packages/vite/src/node/plugins/html.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { toOutputFilePathInHtml } from '../build'
import {
assetUrlRE,
checkPublicFile,
checkUserExternal,
getAssetFilename,
getPublicAssetFilename,
publicAssetUrlRE,
Expand Down Expand Up @@ -290,7 +291,8 @@ export function buildHtmlPlugin(config: ResolvedConfig): Plugin {
url.startsWith('#') ||
isExternalUrl(url) ||
isDataUrl(url) ||
checkPublicFile(url, config)
checkPublicFile(url, config) ||
checkUserExternal(url, config)
// Same reason with `htmlInlineProxyPlugin`
isAsyncScriptMap.set(config, new Map())

Expand Down
52 changes: 52 additions & 0 deletions packages/vite/src/node/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1210,3 +1210,55 @@ export function joinUrlSegments(a: string, b: string): string {
}
return a + b
}

/**
* based on rollup
* https://github.com/rollup/rollup/blob/96a5ba4cafc78e20eeb885ab3bdaed1640977753/src/utils/options/normalizeInputOptions.ts#L115
*/
export const getIdMatcher = <T extends Array<any>>(
option:
| undefined
| boolean
| string
| RegExp
| (string | RegExp)[]
| ((id: string, ...parameters: T) => boolean | null | void)
): ((id: string, ...parameters: T) => boolean) => {
if (option === true) {
return () => true
}
if (typeof option === 'function') {
return (id, ...parameters) =>
(!id.startsWith('\0') && option(id, ...parameters)) || false
}
if (option) {
const ids = new Set<string>()
const matchers: RegExp[] = []
for (const value of ensureArray(option)) {
if (value instanceof RegExp) {
matchers.push(value)
} else {
ids.add(value)
}
}
return (id: string, ..._arguments) =>
ids.has(id) || matchers.some((matcher) => matcher.test(id))
}
return () => false
}

/**
* based on rollup
* https://github.com/rollup/rollup/blob/96a5ba4cafc78e20eeb885ab3bdaed1640977753/src/utils/ensureArray.ts#L1
*/
function ensureArray<T>(
items: (T | false | null | undefined)[] | T | false | null | undefined
): T[] {
if (Array.isArray(items)) {
return items.filter(Boolean) as T[]
}
if (items) {
return [items]
}
return []
}