Parcourir la source

refactor: use existing types

Evan You il y a 4 ans
Parent
commit
ed4bbed990

+ 9 - 5
packages/compiler-sfc/src/templateCompilerModules/assetUrl.ts

@@ -1,6 +1,7 @@
 // vue compiler module for transforming `<tag>:<attribute>` to `require`
 
-import { urlToRequire, ASTNode, Attr } from './utils'
+import { urlToRequire } from './utils'
+import { ASTNode, ASTAttr } from 'types/compiler'
 
 export interface AssetURLOptions {
   [name: string]: string | string[]
@@ -43,16 +44,19 @@ function transform(
   options: AssetURLOptions,
   transformAssetUrlsOption?: TransformAssetUrlsOptions
 ) {
+  if (node.type !== 1 || !node.attrs) return
   for (const tag in options) {
-    if ((tag === '*' || node.tag === tag) && node.attrs) {
+    if (tag === '*' || node.tag === tag) {
       const attributes = options[tag]
       if (typeof attributes === 'string') {
-        node.attrs.some(attr =>
+        node.attrs!.some(attr =>
           rewrite(attr, attributes, transformAssetUrlsOption)
         )
       } else if (Array.isArray(attributes)) {
         attributes.forEach(item =>
-          node.attrs.some(attr => rewrite(attr, item, transformAssetUrlsOption))
+          node.attrs!.some(attr =>
+            rewrite(attr, item, transformAssetUrlsOption)
+          )
         )
       }
     }
@@ -60,7 +64,7 @@ function transform(
 }
 
 function rewrite(
-  attr: Attr,
+  attr: ASTAttr,
   name: string,
   transformAssetUrlsOption?: TransformAssetUrlsOptions
 ) {

+ 6 - 3
packages/compiler-sfc/src/templateCompilerModules/srcset.ts

@@ -1,7 +1,8 @@
 // vue compiler module for transforming `img:srcset` to a number of `require`s
 
-import { urlToRequire, ASTNode } from './utils'
+import { urlToRequire } from './utils'
 import { TransformAssetUrlsOptions } from './assetUrl'
+import { ASTNode } from 'types/compiler'
 
 interface ImageCandidate {
   require: string
@@ -21,9 +22,11 @@ function transform(
   node: ASTNode,
   transformAssetUrlsOptions?: TransformAssetUrlsOptions
 ) {
-  const tags = ['img', 'source']
+  if (node.type !== 1 || !node.attrs) {
+    return
+  }
 
-  if (tags.indexOf(node.tag) !== -1 && node.attrs) {
+  if (node.tag === 'img' || node.tag === 'source') {
     node.attrs.forEach(attr => {
       if (attr.name === 'srcset') {
         // same logic as in transform-require.js

+ 0 - 10
packages/compiler-sfc/src/templateCompilerModules/utils.ts

@@ -2,16 +2,6 @@ import { TransformAssetUrlsOptions } from './assetUrl'
 import { UrlWithStringQuery, parse as uriParse } from 'url'
 import path from 'path'
 
-export interface Attr {
-  name: string
-  value: string
-}
-
-export interface ASTNode {
-  tag: string
-  attrs: Attr[]
-}
-
 export function urlToRequire(
   url: string,
   transformAssetUrlsOption: TransformAssetUrlsOptions = {}

+ 0 - 1
packages/compiler-sfc/test/compileTemplate.spec.ts

@@ -4,7 +4,6 @@ import { compileTemplate } from '../src/compileTemplate'
 import Vue from 'vue'
 
 function mockRender(code: string, mocks: Record<string, any> = {}) {
-  console.log(code)
   const fn = new Function(
     `require`,
     `${code}; return { render, staticRenderFns }`