From 6f030ec15f25a2a1d7d912f1b84d83ebb28a3515 Mon Sep 17 00:00:00 2001 From: btea <2356281422@qq.com> Date: Wed, 25 Sep 2024 22:39:44 +0800 Subject: [PATCH] refactor: replace `parse` with `splitFileAndPostfix` (#18185) --- packages/vite/src/node/plugins/asset.ts | 12 ++++++------ packages/vite/src/node/plugins/resolve.ts | 6 +----- packages/vite/src/shared/utils.ts | 8 ++++++++ 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/packages/vite/src/node/plugins/asset.ts b/packages/vite/src/node/plugins/asset.ts index 4da87390c..2b1330864 100644 --- a/packages/vite/src/node/plugins/asset.ts +++ b/packages/vite/src/node/plugins/asset.ts @@ -1,5 +1,4 @@ import path from 'node:path' -import { parse as parseUrl } from 'node:url' import fsp from 'node:fs/promises' import { Buffer } from 'node:buffer' import * as mrmime from 'mrmime' @@ -25,7 +24,11 @@ import { urlRE, } from '../utils' import { DEFAULT_ASSETS_INLINE_LIMIT, FS_PREFIX } from '../constants' -import { cleanUrl, withTrailingSlash } from '../../shared/utils' +import { + cleanUrl, + splitFileAndPostfix, + withTrailingSlash, +} from '../../shared/utils' import type { Environment } from '../environment' // referenceId is base64url but replaces - with $ @@ -351,7 +354,7 @@ async function fileToBuiltUrl( return cached } - const file = cleanUrl(id) + const { file, postfix } = splitFileAndPostfix(id) const content = await fsp.readFile(file) let url: string @@ -371,9 +374,6 @@ async function fileToBuiltUrl( } } else { // emit as asset - const { search, hash } = parseUrl(id) - const postfix = (search || '') + (hash || '') - const originalFileName = normalizePath( path.relative(environment.config.root, file), ) diff --git a/packages/vite/src/node/plugins/resolve.ts b/packages/vite/src/node/plugins/resolve.ts index c0b5314d8..f740981ba 100644 --- a/packages/vite/src/node/plugins/resolve.ts +++ b/packages/vite/src/node/plugins/resolve.ts @@ -53,6 +53,7 @@ import { cleanUrl, isWindows, slash, + splitFileAndPostfix, withTrailingSlash, } from '../../shared/utils' @@ -589,11 +590,6 @@ function ensureVersionQuery( return resolved } -function splitFileAndPostfix(path: string) { - const file = cleanUrl(path) - return { file, postfix: path.slice(file.length) } -} - export function tryFsResolve( fsPath: string, options: InternalResolveOptions, diff --git a/packages/vite/src/shared/utils.ts b/packages/vite/src/shared/utils.ts index 3b3507a82..666ed737a 100644 --- a/packages/vite/src/shared/utils.ts +++ b/packages/vite/src/shared/utils.ts @@ -33,6 +33,14 @@ export function cleanUrl(url: string): string { return url.replace(postfixRE, '') } +export function splitFileAndPostfix(path: string): { + file: string + postfix: string +} { + const file = cleanUrl(path) + return { file, postfix: path.slice(file.length) } +} + export function isPrimitive(value: unknown): boolean { return !value || (typeof value !== 'object' && typeof value !== 'function') }