From 2c3c3b252b0f1c403652c3f558080348f26b32f2 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Thu, 15 Aug 2024 11:54:06 +0900 Subject: [PATCH 1/2] refactor: remove redundant prepend/strip base --- packages/vite/src/node/plugins/asset.ts | 14 ++++++++++++-- packages/vite/src/node/plugins/css.ts | 8 ++------ 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/packages/vite/src/node/plugins/asset.ts b/packages/vite/src/node/plugins/asset.ts index 1415211d5faedf..e608a8c7f7737d 100644 --- a/packages/vite/src/node/plugins/asset.ts +++ b/packages/vite/src/node/plugins/asset.ts @@ -268,7 +268,10 @@ export async function fileToUrl( } } -function fileToDevUrl(id: string, config: ResolvedConfig) { +export function fileToDevUrlWithoutBase( + id: string, + config: ResolvedConfig, +): string { let rtn: string if (checkPublicFile(id, config)) { // in public dir during dev, keep the url as-is @@ -281,8 +284,15 @@ function fileToDevUrl(id: string, config: ResolvedConfig) { // (this is special handled by the serve static middleware rtn = path.posix.join(FS_PREFIX, id) } + return rtn +} + +function fileToDevUrl(id: string, config: ResolvedConfig) { const base = joinUrlSegments(config.server?.origin ?? '', config.decodedBase) - return joinUrlSegments(base, removeLeadingSlash(rtn)) + return joinUrlSegments( + base, + removeLeadingSlash(fileToDevUrlWithoutBase(id, config)), + ) } export function getPublicAssetFilename( diff --git a/packages/vite/src/node/plugins/css.ts b/packages/vite/src/node/plugins/css.ts index 2426e66685a41e..a5f2cdee85a3f7 100644 --- a/packages/vite/src/node/plugins/css.ts +++ b/packages/vite/src/node/plugins/css.ts @@ -65,7 +65,6 @@ import { removeDirectQuery, removeUrlQuery, requireResolveFromRootWithFallback, - stripBase, stripBomTag, urlRE, } from '../utils' @@ -75,6 +74,7 @@ import type { TransformPluginContext } from '../server/pluginContainer' import { addToHTMLProxyTransformResult } from './html' import { assetUrlRE, + fileToDevUrlWithoutBase, fileToUrl, generatedAssets, publicAssetUrlCache, @@ -995,16 +995,12 @@ export function cssAnalysisPlugin(config: ResolvedConfig): Plugin { // record deps in the module graph so edits to @import css can trigger // main import to hot update const depModules = new Set() - const devBase = config.base for (const file of pluginImports) { depModules.add( isCSSRequest(file) ? moduleGraph.createFileOnlyEntry(file) : await moduleGraph.ensureEntryFromUrl( - stripBase( - await fileToUrl(file, config, this), - (config.server?.origin ?? '') + devBase, - ), + fileToDevUrlWithoutBase(file, config), ssr, ), ) From a16f22ecb6f38916829425272d089104948b07d2 Mon Sep 17 00:00:00 2001 From: Hiroshi Ogawa Date: Fri, 16 Aug 2024 08:25:51 +0900 Subject: [PATCH 2/2] refactor: tweak fileToDevUrl --- packages/vite/src/node/plugins/asset.ts | 15 ++++++--------- packages/vite/src/node/plugins/css.ts | 4 ++-- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/packages/vite/src/node/plugins/asset.ts b/packages/vite/src/node/plugins/asset.ts index e608a8c7f7737d..c0f7d635751ddc 100644 --- a/packages/vite/src/node/plugins/asset.ts +++ b/packages/vite/src/node/plugins/asset.ts @@ -268,9 +268,10 @@ export async function fileToUrl( } } -export function fileToDevUrlWithoutBase( +export function fileToDevUrl( id: string, config: ResolvedConfig, + skipBase = false, ): string { let rtn: string if (checkPublicFile(id, config)) { @@ -284,15 +285,11 @@ export function fileToDevUrlWithoutBase( // (this is special handled by the serve static middleware rtn = path.posix.join(FS_PREFIX, id) } - return rtn -} - -function fileToDevUrl(id: string, config: ResolvedConfig) { + if (skipBase) { + return rtn + } const base = joinUrlSegments(config.server?.origin ?? '', config.decodedBase) - return joinUrlSegments( - base, - removeLeadingSlash(fileToDevUrlWithoutBase(id, config)), - ) + return joinUrlSegments(base, removeLeadingSlash(rtn)) } export function getPublicAssetFilename( diff --git a/packages/vite/src/node/plugins/css.ts b/packages/vite/src/node/plugins/css.ts index a5f2cdee85a3f7..2b4f5f9faf307e 100644 --- a/packages/vite/src/node/plugins/css.ts +++ b/packages/vite/src/node/plugins/css.ts @@ -74,7 +74,7 @@ import type { TransformPluginContext } from '../server/pluginContainer' import { addToHTMLProxyTransformResult } from './html' import { assetUrlRE, - fileToDevUrlWithoutBase, + fileToDevUrl, fileToUrl, generatedAssets, publicAssetUrlCache, @@ -1000,7 +1000,7 @@ export function cssAnalysisPlugin(config: ResolvedConfig): Plugin { isCSSRequest(file) ? moduleGraph.createFileOnlyEntry(file) : await moduleGraph.ensureEntryFromUrl( - fileToDevUrlWithoutBase(file, config), + fileToDevUrl(file, config, /* skipBase */ true), ssr, ), )