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

refactor: replace parse with splitFileAndPostfix #18185

Merged
merged 5 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
refactor: extract splitFileAndPostfix
  • Loading branch information
btea committed Sep 25, 2024
commit f91b5d1f83bb05592c8b974d5ddbff3f500dbaca
18 changes: 6 additions & 12 deletions packages/vite/src/node/plugins/asset.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import path from 'node:path'
import { URL } from 'node:url'
import fsp from 'node:fs/promises'
import { Buffer } from 'node:buffer'
import * as mrmime from 'mrmime'
Expand All @@ -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 $
Expand Down Expand Up @@ -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
Expand All @@ -371,15 +374,6 @@ async function fileToBuiltUrl(
}
} else {
// emit as asset
// On Mac or Linux platforms, the path resolution is considered an invalid URL by the new URL and an error will be thrown.
// So it will be converted into a valid URL.
let { search, hash } = new URL(id, 'http://vitejs.dev')
if (!search && id.includes('?')) {
// When the string structure is like `woff2?#iefix`, the search value obtained by parsing the new URL is an empty string
search = '?'
}
const postfix = search + hash

const originalFileName = normalizePath(
path.relative(environment.config.root, file),
)
Expand Down
6 changes: 1 addition & 5 deletions packages/vite/src/node/plugins/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import {
cleanUrl,
isWindows,
slash,
splitFileAndPostfix,
withTrailingSlash,
} from '../../shared/utils'

Expand Down Expand Up @@ -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,
Expand Down
8 changes: 8 additions & 0 deletions packages/vite/src/shared/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
}
Expand Down