perf(compiler-sfc): improve asset url trasnform efficiency
This commit is contained in:
		
							parent
							
								
									40166a8637
								
							
						
					
					
						commit
						c5dcfe16f6
					
				@ -81,15 +81,22 @@ 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 => {
 | 
			
		||||
    const attrs = tags[node.tag]
 | 
			
		||||
    const wildCardAttrs = tags['*']
 | 
			
		||||
    if (!attrs && !wildCardAttrs) {
 | 
			
		||||
      return
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    const assetAttrs = (attrs || []).concat(wildCardAttrs || [])
 | 
			
		||||
    node.props.forEach((attr, index) => {
 | 
			
		||||
      if (
 | 
			
		||||
        attr.type !== NodeTypes.ATTRIBUTE ||
 | 
			
		||||
              attr.name !== name ||
 | 
			
		||||
        !assetAttrs.includes(attr.name) ||
 | 
			
		||||
        !attr.value ||
 | 
			
		||||
        (!options.includeAbsolute && !isRelativeUrl(attr.value.content))
 | 
			
		||||
      ) {
 | 
			
		||||
@ -97,7 +104,6 @@ export const transformAssetUrl: NodeTransform = (
 | 
			
		||||
      }
 | 
			
		||||
 | 
			
		||||
      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 `@`
 | 
			
		||||
@ -119,24 +125,16 @@ export const transformAssetUrl: NodeTransform = (
 | 
			
		||||
      // 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
 | 
			
		||||
            )
 | 
			
		||||
      const exp = getImportsExpressionExp(url.path, url.hash, attr.loc, context)
 | 
			
		||||
      node.props[index] = {
 | 
			
		||||
        type: NodeTypes.DIRECTIVE,
 | 
			
		||||
        name: 'bind',
 | 
			
		||||
              arg: createSimpleExpression(name, true, attr.loc),
 | 
			
		||||
        arg: createSimpleExpression(attr.name, true, attr.loc),
 | 
			
		||||
        exp,
 | 
			
		||||
        modifiers: [],
 | 
			
		||||
        loc: attr.loc
 | 
			
		||||
      }
 | 
			
		||||
    })
 | 
			
		||||
        })
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user