Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix issue 4203,加入缓存机制,转换时间从半个小时优化至8分钟内 #4216

Merged
merged 3 commits into from
Aug 26, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions packages/taroize/src/cache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Wxml } from './wxml'
import { cloneDeep } from 'lodash'
const cacheMap = new Map()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里最好把 Map 的泛型签名写一下。

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已指定类型


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

export function saveCacheWxml(dirpath: string, wxml: Wxml) {
if (!dirpath) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里 dirpath 应该一直都有才对?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

嗯,dirpath是不会为空的

return
}
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: Wxml = getCacheWxml(dirPath)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getCacheWxml 可能会返回 undefined,那这个签名应该就是不对的。如果不会,下一个判断也就没必要了。

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getCacheWxml是可能返回undefined

if (parseResult) {
return parseResult
}
try {
wxml = prettyPrint(wxml, {
max_char: 0,
Expand Down Expand Up @@ -343,13 +350,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