refactor: use existing types

This commit is contained in:
Evan You 2022-06-10 17:06:37 +08:00
parent 06594f68b7
commit ed4bbed990
4 changed files with 15 additions and 19 deletions

View File

@ -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
) {

View File

@ -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

View File

@ -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 = {}

View File

@ -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 }`