refactor: a bit of cleanup for better readability

This commit is contained in:
Haoqun Jiang 2025-03-16 20:29:54 +08:00
parent 66299535a3
commit da82083df6
No known key found for this signature in database
GPG Key ID: BC1665FD8DE94CD5
1 changed files with 92 additions and 79 deletions

View File

@ -153,22 +153,31 @@ export const transformAssetUrl: NodeTransform = (
} }
} }
function getImportsExpressionExp( /**
path: string | null, * Resolves or registers an import for the given source path
hash: string | null, * @param source - Path to resolve import for
* @param loc - Source location
* @param context - Transform context
* @returns Object containing import name and expression
*/
function resolveOrRegisterImport(
source: string,
loc: SourceLocation, loc: SourceLocation,
context: TransformContext, context: TransformContext,
): ExpressionNode { ): {
if (path) { name: string
let name: string exp: SimpleExpressionNode
let exp: SimpleExpressionNode } {
const existingIndex = context.imports.findIndex(i => i.path === path) const existingIndex = context.imports.findIndex(i => i.path === source)
if (existingIndex > -1) { if (existingIndex > -1) {
name = `_imports_${existingIndex}` return {
exp = context.imports[existingIndex].exp as SimpleExpressionNode name: `_imports_${existingIndex}`,
} else { exp: context.imports[existingIndex].exp as SimpleExpressionNode,
name = `_imports_${context.imports.length}` }
exp = createSimpleExpression( }
const name = `_imports_${context.imports.length}`
const exp = createSimpleExpression(
name, name,
false, false,
loc, loc,
@ -179,14 +188,42 @@ function getImportsExpressionExp(
// so we decode it back in case it is encoded // so we decode it back in case it is encoded
context.imports.push({ context.imports.push({
exp, exp,
path: decodeURIComponent(path), path: decodeURIComponent(source),
}) })
return { name, exp }
} }
if (!hash) { /**
* Transforms asset URLs into import expressions or string literals
*/
function getImportsExpressionExp(
path: string | null,
hash: string | null,
loc: SourceLocation,
context: TransformContext,
): ExpressionNode {
// Neither path nor hash - return empty string
if (!path && !hash) {
return createSimpleExpression(`''`, false, loc, ConstantTypes.CAN_STRINGIFY)
}
// Only hash without path - treat hash as the import source (likely a subpath import)
if (!path && hash) {
const { exp } = resolveOrRegisterImport(hash, loc, context)
return exp return exp
} }
// Only path without hash - straightforward import
if (path && !hash) {
const { exp } = resolveOrRegisterImport(path, loc, context)
return exp
}
// At this point, we know we have both path and hash components
const { name } = resolveOrRegisterImport(path!, loc, context)
// Combine path import with hash
const hashExp = `${name} + '${hash}'` const hashExp = `${name} + '${hash}'`
const finalExp = createSimpleExpression( const finalExp = createSimpleExpression(
hashExp, hashExp,
@ -195,10 +232,12 @@ function getImportsExpressionExp(
ConstantTypes.CAN_STRINGIFY, ConstantTypes.CAN_STRINGIFY,
) )
// No hoisting needed
if (!context.hoistStatic) { if (!context.hoistStatic) {
return finalExp return finalExp
} }
// Check for existing hoisted expression
const existingHoistIndex = context.hoists.findIndex(h => { const existingHoistIndex = context.hoists.findIndex(h => {
return ( return (
h && h &&
@ -207,6 +246,8 @@ function getImportsExpressionExp(
h.content === hashExp h.content === hashExp
) )
}) })
// Return existing hoisted expression if found
if (existingHoistIndex > -1) { if (existingHoistIndex > -1) {
return createSimpleExpression( return createSimpleExpression(
`_hoisted_${existingHoistIndex + 1}`, `_hoisted_${existingHoistIndex + 1}`,
@ -215,35 +256,7 @@ function getImportsExpressionExp(
ConstantTypes.CAN_STRINGIFY, ConstantTypes.CAN_STRINGIFY,
) )
} }
// Hoist the expression and return the hoisted expression
return context.hoist(finalExp) return context.hoist(finalExp)
} else if (hash) {
// Hash without path, e.g. subpath import
let name: string
let exp: SimpleExpressionNode
const existingIndex = context.imports.findIndex(i => i.path === hash)
if (existingIndex > -1) {
name = `_imports_${existingIndex}`
exp = context.imports[existingIndex].exp as SimpleExpressionNode
} else {
name = `_imports_${context.imports.length}`
exp = createSimpleExpression(
name,
false,
loc,
ConstantTypes.CAN_STRINGIFY,
)
// We need to ensure the path is not encoded (to %2F),
// so we decode it back in case it is encoded
context.imports.push({
exp,
path: decodeURIComponent(hash),
})
}
return exp
} else {
return createSimpleExpression(`''`, false, loc, ConstantTypes.CAN_STRINGIFY)
}
} }