Skip to content

Commit c5dcfe1

Browse files
committed
perf(compiler-sfc): improve asset url trasnform efficiency
1 parent 40166a8 commit c5dcfe1

File tree

1 file changed

+49
-51
lines changed

1 file changed

+49
-51
lines changed

packages/compiler-sfc/src/templateTransformAssetUrl.ts

+49-51
Original file line numberDiff line numberDiff line change
@@ -81,62 +81,60 @@ export const transformAssetUrl: NodeTransform = (
8181
options: AssetURLOptions = defaultAssetUrlOptions
8282
) => {
8383
if (node.type === NodeTypes.ELEMENT) {
84+
if (!node.props.length) {
85+
return
86+
}
87+
8488
const tags = options.tags || defaultAssetUrlOptions.tags
85-
for (const tag in tags) {
86-
if ((tag === '*' || node.tag === tag) && node.props.length) {
87-
const attributes = tags[tag]
88-
attributes.forEach(name => {
89-
node.props.forEach((attr, index) => {
90-
if (
91-
attr.type !== NodeTypes.ATTRIBUTE ||
92-
attr.name !== name ||
93-
!attr.value ||
94-
(!options.includeAbsolute && !isRelativeUrl(attr.value.content))
95-
) {
96-
return
97-
}
89+
const attrs = tags[node.tag]
90+
const wildCardAttrs = tags['*']
91+
if (!attrs && !wildCardAttrs) {
92+
return
93+
}
9894

99-
const url = parseUrl(attr.value.content)
95+
const assetAttrs = (attrs || []).concat(wildCardAttrs || [])
96+
node.props.forEach((attr, index) => {
97+
if (
98+
attr.type !== NodeTypes.ATTRIBUTE ||
99+
!assetAttrs.includes(attr.name) ||
100+
!attr.value ||
101+
(!options.includeAbsolute && !isRelativeUrl(attr.value.content))
102+
) {
103+
return
104+
}
100105

101-
if (options.base) {
102-
// explicit base - directly rewrite the url into absolute url
103-
// does not apply to absolute urls or urls that start with `@`
104-
// since they are aliases
105-
if (
106-
attr.value.content[0] !== '@' &&
107-
isRelativeUrl(attr.value.content)
108-
) {
109-
// when packaged in the browser, path will be using the posix-
110-
// only version provided by rollup-plugin-node-builtins.
111-
attr.value.content = (path.posix || path).join(
112-
options.base,
113-
url.path + (url.hash || '')
114-
)
115-
}
116-
return
117-
}
106+
const url = parseUrl(attr.value.content)
107+
if (options.base) {
108+
// explicit base - directly rewrite the url into absolute url
109+
// does not apply to absolute urls or urls that start with `@`
110+
// since they are aliases
111+
if (
112+
attr.value.content[0] !== '@' &&
113+
isRelativeUrl(attr.value.content)
114+
) {
115+
// when packaged in the browser, path will be using the posix-
116+
// only version provided by rollup-plugin-node-builtins.
117+
attr.value.content = (path.posix || path).join(
118+
options.base,
119+
url.path + (url.hash || '')
120+
)
121+
}
122+
return
123+
}
118124

119-
// otherwise, transform the url into an import.
120-
// this assumes a bundler will resolve the import into the correct
121-
// absolute url (e.g. webpack file-loader)
122-
const exp = getImportsExpressionExp(
123-
url.path,
124-
url.hash,
125-
attr.loc,
126-
context
127-
)
128-
node.props[index] = {
129-
type: NodeTypes.DIRECTIVE,
130-
name: 'bind',
131-
arg: createSimpleExpression(name, true, attr.loc),
132-
exp,
133-
modifiers: [],
134-
loc: attr.loc
135-
}
136-
})
137-
})
125+
// otherwise, transform the url into an import.
126+
// this assumes a bundler will resolve the import into the correct
127+
// absolute url (e.g. webpack file-loader)
128+
const exp = getImportsExpressionExp(url.path, url.hash, attr.loc, context)
129+
node.props[index] = {
130+
type: NodeTypes.DIRECTIVE,
131+
name: 'bind',
132+
arg: createSimpleExpression(attr.name, true, attr.loc),
133+
exp,
134+
modifiers: [],
135+
loc: attr.loc
138136
}
139-
}
137+
})
140138
}
141139
}
142140

0 commit comments

Comments
 (0)