refactor: replace parse with splitFileAndPostfix (#18185)

This commit is contained in:
btea 2024-09-25 22:39:44 +08:00 committed by GitHub
parent c8aea5ae0a
commit 6f030ec15f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 15 additions and 11 deletions

View File

@ -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),
)

View File

@ -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,

View File

@ -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')
}