From ed4bbed990364feafed79967e2e0cf5336ff67ce Mon Sep 17 00:00:00 2001 From: Evan You Date: Fri, 10 Jun 2022 17:06:37 +0800 Subject: [PATCH] refactor: use existing types --- .../src/templateCompilerModules/assetUrl.ts | 14 +++++++++----- .../src/templateCompilerModules/srcset.ts | 9 ++++++--- .../src/templateCompilerModules/utils.ts | 10 ---------- packages/compiler-sfc/test/compileTemplate.spec.ts | 1 - 4 files changed, 15 insertions(+), 19 deletions(-) diff --git a/packages/compiler-sfc/src/templateCompilerModules/assetUrl.ts b/packages/compiler-sfc/src/templateCompilerModules/assetUrl.ts index 3bc490af3..07f8eb9ff 100644 --- a/packages/compiler-sfc/src/templateCompilerModules/assetUrl.ts +++ b/packages/compiler-sfc/src/templateCompilerModules/assetUrl.ts @@ -1,6 +1,7 @@ // vue compiler module for transforming `:` 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 ) { diff --git a/packages/compiler-sfc/src/templateCompilerModules/srcset.ts b/packages/compiler-sfc/src/templateCompilerModules/srcset.ts index 7f66ab347..f0df692b8 100644 --- a/packages/compiler-sfc/src/templateCompilerModules/srcset.ts +++ b/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 diff --git a/packages/compiler-sfc/src/templateCompilerModules/utils.ts b/packages/compiler-sfc/src/templateCompilerModules/utils.ts index f81862cf8..859c5f456 100644 --- a/packages/compiler-sfc/src/templateCompilerModules/utils.ts +++ b/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 = {} diff --git a/packages/compiler-sfc/test/compileTemplate.spec.ts b/packages/compiler-sfc/test/compileTemplate.spec.ts index aa0e226c9..1f30c68e2 100644 --- a/packages/compiler-sfc/test/compileTemplate.spec.ts +++ b/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 = {}) { - console.log(code) const fn = new Function( `require`, `${code}; return { render, staticRenderFns }`