Skip to content

Commit

Permalink
feat(taroize): 解析 wxml 加入缓存机制
Browse files Browse the repository at this point in the history
* fix issue 4203,加入缓存机制,转换时间从半个小时优化至8分钟内

* fix4203 1.Map增加类型定义 2.去除dirPath的判断

close #4203
  • Loading branch information
Qiyu8 authored and yuche committed Aug 26, 2019
1 parent 9b4882c commit af71480
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
11 changes: 11 additions & 0 deletions packages/taroize/src/cache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Wxml } from './wxml'
import { cloneDeep } from 'lodash'
const cacheMap = new Map<string, Wxml>()

export function getCacheWxml(dirpath: string): Wxml | undefined {
return cloneDeep(cacheMap.get(dirpath))
}

export function saveCacheWxml(dirpath: string, wxml: Wxml) {
cacheMap.set(dirpath, cloneDeep(wxml))
}
24 changes: 16 additions & 8 deletions packages/taroize/src/wxml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { parseTemplate, parseModule } from './template'
import { usedComponents, errors, globals, THIRD_PARTY_COMPONENTS } from './global'
import { reserveKeyWords } from './constant'
import { parse as parseFile } from 'babylon'
import { getCacheWxml, saveCacheWxml } from './cache'
const { prettyPrint } = require('html')

const allCamelCase = (str: string) =>
Expand Down Expand Up @@ -70,6 +71,13 @@ export interface Imports {
wxs?: boolean
}

export interface Wxml {
wxses: WXS[]
wxml?: t.Node
imports: Imports[]
refIds: Set<string>
}

const WX_IF = 'wx:if'
const WX_ELSE_IF = 'wx:elif'
const WX_FOR = 'wx:for'
Expand Down Expand Up @@ -293,12 +301,11 @@ export const createWxmlVistor = (
} as Visitor
}

export function parseWXML (dirPath: string, wxml?: string, parseImport?: boolean): {
wxses: WXS[]
wxml?: t.Node
imports: Imports[]
refIds: Set<string>
} {
export function parseWXML (dirPath: string, wxml?: string, parseImport?: boolean): Wxml {
let parseResult = getCacheWxml(dirPath)
if (parseResult) {
return parseResult
}
try {
wxml = prettyPrint(wxml, {
max_char: 0,
Expand Down Expand Up @@ -344,13 +351,14 @@ export function parseWXML (dirPath: string, wxml?: string, parseImport?: boolean
refIds.delete(id)
}
})

return {
parseResult = {
wxses,
imports,
wxml: hydrate(ast),
refIds
}
saveCacheWxml(dirPath, parseResult)
return parseResult
}

function getWXS (attrs: t.JSXAttribute[], path: NodePath<t.JSXElement>, imports: Imports[]): WXS {
Expand Down

0 comments on commit af71480

Please sign in to comment.