diff --git a/packages/taroize/src/cache.ts b/packages/taroize/src/cache.ts new file mode 100644 index 000000000000..232165e3ab2a --- /dev/null +++ b/packages/taroize/src/cache.ts @@ -0,0 +1,11 @@ +import { Wxml } from './wxml' +import { cloneDeep } from 'lodash' +const cacheMap = new Map() + +export function getCacheWxml(dirpath: string): Wxml | undefined { + return cloneDeep(cacheMap.get(dirpath)) +} + +export function saveCacheWxml(dirpath: string, wxml: Wxml) { + cacheMap.set(dirpath, cloneDeep(wxml)) +} \ No newline at end of file diff --git a/packages/taroize/src/wxml.ts b/packages/taroize/src/wxml.ts index 5e0fc6f5f610..15b7cd3c310d 100644 --- a/packages/taroize/src/wxml.ts +++ b/packages/taroize/src/wxml.ts @@ -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) => @@ -70,6 +71,13 @@ export interface Imports { wxs?: boolean } +export interface Wxml { + wxses: WXS[] + wxml?: t.Node + imports: Imports[] + refIds: Set +} + const WX_IF = 'wx:if' const WX_ELSE_IF = 'wx:elif' const WX_FOR = 'wx:for' @@ -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 -} { +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, @@ -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, imports: Imports[]): WXS {