Skip to content

Commit

Permalink
refactor: remove redundant prepend/strip base (#17887)
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa committed Aug 16, 2024
1 parent fac3a8e commit 3b8f03d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
9 changes: 8 additions & 1 deletion packages/vite/src/node/plugins/asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,11 @@ export async function fileToUrl(
}
}

function fileToDevUrl(id: string, config: ResolvedConfig) {
export function fileToDevUrl(
id: string,
config: ResolvedConfig,
skipBase = false,
): string {
let rtn: string
if (checkPublicFile(id, config)) {
// in public dir during dev, keep the url as-is
Expand All @@ -281,6 +285,9 @@ function fileToDevUrl(id: string, config: ResolvedConfig) {
// (this is special handled by the serve static middleware
rtn = path.posix.join(FS_PREFIX, id)
}
if (skipBase) {
return rtn
}
const base = joinUrlSegments(config.server?.origin ?? '', config.decodedBase)
return joinUrlSegments(base, removeLeadingSlash(rtn))
}
Expand Down
8 changes: 2 additions & 6 deletions packages/vite/src/node/plugins/css.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ import {
removeDirectQuery,
removeUrlQuery,
requireResolveFromRootWithFallback,
stripBase,
stripBomTag,
urlRE,
} from '../utils'
Expand All @@ -75,6 +74,7 @@ import type { TransformPluginContext } from '../server/pluginContainer'
import { addToHTMLProxyTransformResult } from './html'
import {
assetUrlRE,
fileToDevUrl,
fileToUrl,
generatedAssets,
publicAssetUrlCache,
Expand Down Expand Up @@ -996,16 +996,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<string | ModuleNode>()
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,
),
fileToDevUrl(file, config, /* skipBase */ true),
ssr,
),
)
Expand Down

0 comments on commit 3b8f03d

Please sign in to comment.