]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
perf(compiler-sfc): improve asset url trasnform efficiency
authorEvan You <yyx990803@gmail.com>
Tue, 5 May 2020 20:07:15 +0000 (16:07 -0400)
committerEvan You <yyx990803@gmail.com>
Tue, 5 May 2020 20:07:15 +0000 (16:07 -0400)
packages/compiler-sfc/src/templateTransformAssetUrl.ts

index b491de6c930f720e09d3ee2c976ce6e78697837a..0b7c9175f4d7be70ebd03982ec6f0e8200117b2c 100644 (file)
@@ -81,62 +81,60 @@ export const transformAssetUrl: NodeTransform = (
   options: AssetURLOptions = defaultAssetUrlOptions
 ) => {
   if (node.type === NodeTypes.ELEMENT) {
+    if (!node.props.length) {
+      return
+    }
+
     const tags = options.tags || defaultAssetUrlOptions.tags
-    for (const tag in tags) {
-      if ((tag === '*' || node.tag === tag) && node.props.length) {
-        const attributes = tags[tag]
-        attributes.forEach(name => {
-          node.props.forEach((attr, index) => {
-            if (
-              attr.type !== NodeTypes.ATTRIBUTE ||
-              attr.name !== name ||
-              !attr.value ||
-              (!options.includeAbsolute && !isRelativeUrl(attr.value.content))
-            ) {
-              return
-            }
+    const attrs = tags[node.tag]
+    const wildCardAttrs = tags['*']
+    if (!attrs && !wildCardAttrs) {
+      return
+    }
 
-            const url = parseUrl(attr.value.content)
+    const assetAttrs = (attrs || []).concat(wildCardAttrs || [])
+    node.props.forEach((attr, index) => {
+      if (
+        attr.type !== NodeTypes.ATTRIBUTE ||
+        !assetAttrs.includes(attr.name) ||
+        !attr.value ||
+        (!options.includeAbsolute && !isRelativeUrl(attr.value.content))
+      ) {
+        return
+      }
 
-            if (options.base) {
-              // explicit base - directly rewrite the url into absolute url
-              // does not apply to absolute urls or urls that start with `@`
-              // since they are aliases
-              if (
-                attr.value.content[0] !== '@' &&
-                isRelativeUrl(attr.value.content)
-              ) {
-                // when packaged in the browser, path will be using the posix-
-                // only version provided by rollup-plugin-node-builtins.
-                attr.value.content = (path.posix || path).join(
-                  options.base,
-                  url.path + (url.hash || '')
-                )
-              }
-              return
-            }
+      const url = parseUrl(attr.value.content)
+      if (options.base) {
+        // explicit base - directly rewrite the url into absolute url
+        // does not apply to absolute urls or urls that start with `@`
+        // since they are aliases
+        if (
+          attr.value.content[0] !== '@' &&
+          isRelativeUrl(attr.value.content)
+        ) {
+          // when packaged in the browser, path will be using the posix-
+          // only version provided by rollup-plugin-node-builtins.
+          attr.value.content = (path.posix || path).join(
+            options.base,
+            url.path + (url.hash || '')
+          )
+        }
+        return
+      }
 
-            // otherwise, transform the url into an import.
-            // this assumes a bundler will resolve the import into the correct
-            // absolute url (e.g. webpack file-loader)
-            const exp = getImportsExpressionExp(
-              url.path,
-              url.hash,
-              attr.loc,
-              context
-            )
-            node.props[index] = {
-              type: NodeTypes.DIRECTIVE,
-              name: 'bind',
-              arg: createSimpleExpression(name, true, attr.loc),
-              exp,
-              modifiers: [],
-              loc: attr.loc
-            }
-          })
-        })
+      // otherwise, transform the url into an import.
+      // this assumes a bundler will resolve the import into the correct
+      // absolute url (e.g. webpack file-loader)
+      const exp = getImportsExpressionExp(url.path, url.hash, attr.loc, context)
+      node.props[index] = {
+        type: NodeTypes.DIRECTIVE,
+        name: 'bind',
+        arg: createSimpleExpression(attr.name, true, attr.loc),
+        exp,
+        modifiers: [],
+        loc: attr.loc
       }
-    }
+    })
   }
 }