Skip to content

Commit

Permalink
feat: 🚀 优化类型定义文件生成逻辑,添加缓存机制以防止热更新丢失
Browse files Browse the repository at this point in the history
  • Loading branch information
Vanisper committed Dec 21, 2024
1 parent e236278 commit 408e9a3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
2 changes: 0 additions & 2 deletions src/plugin/async-component-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ export function AsyncComponentProcessor(): Plugin {

/** 生成类型定义文件 */
function generateTypeFile(parseResult?: ReturnType<typeof lexDefaultImportWithQuery>) {
// #region 提取引入路径数组,生成类型定义文件
const typesFilePath = path.resolve(ROOT_DIR, 'async-component.d.ts')
ensureDirectoryExists(typesFilePath)
let cache: string[] = [] // 缓存已经生成的类型定义,防止开发阶段热更新时部分类型定义生成丢失
Expand All @@ -33,7 +32,6 @@ export function AsyncComponentProcessor(): Plugin {
}
const typeDefinition = generateModuleDeclaration(parseResult, cache)
fs.writeFileSync(typesFilePath, typeDefinition)
// #endregion
}
generateTypeFile() // 初始化类型定义文件

Expand Down
15 changes: 10 additions & 5 deletions src/plugin/async-import-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import process from 'node:process'
import MagicString from 'magic-string'
import { AsyncImports } from '../common/AsyncImports'
import { JS_TYPES_RE, ROOT_DIR, SRC_DIR_RE } from '../constants'
import { ensureDirectoryExists, moduleIdProcessor, parseAsyncImports, resolveAliasPath, resolveAssetsPath } from '../utils'
import { ensureDirectoryExists, lexFunctionCalls, moduleIdProcessor, parseAsyncImports, resolveAliasPath, resolveAssetsPath } from '../utils'

/**
* 负责处理`AsyncImport`函数调用的传参路径
Expand All @@ -27,11 +27,16 @@ export function AsyncImportProcessor(): Plugin {
const isApp = platform === 'app'
const AsyncImportsInstance = new AsyncImports()

/** 提取引入路径数组,生产类型定义文件 */
/** 生成类型定义文件 */
function generateTypeFile(paths?: string[]) {
const typeDefinition = generateTypeDefinition(paths)
const typesFilePath = path.resolve(ROOT_DIR, 'async-import.d.ts')
ensureDirectoryExists(typesFilePath)
let cache: string[] = [] // 缓存已经生成的类型定义,防止开发阶段热更新时部分类型定义生成丢失
if (fs.existsSync(typesFilePath)) {
const list = lexFunctionCalls(fs.readFileSync(typesFilePath, 'utf-8'), 'import').flatMap(({ args }) => args.map(({ value }) => value.toString()))
list && list.length && (cache = Array.from(new Set(list)))
}
const typeDefinition = generateModuleDeclaration(paths, cache)
fs.writeFileSync(typesFilePath, typeDefinition)
}
generateTypeFile() // 初始化类型定义文件
Expand Down Expand Up @@ -172,9 +177,9 @@ export function AsyncImportProcessor(): Plugin {
/**
* 生成类型定义
*/
function generateTypeDefinition(paths?: string[]): string {
function generateModuleDeclaration(paths?: string[], cache?: string[]): string {
// 将路径组合成 ModuleMap 中的键
const moduleMapEntries = paths
const moduleMapEntries = Array.from(new Set([...(cache || []), ...(paths || [])]))
?.map((p) => {
return ` '${p}': typeof import('${p}')`
})
Expand Down

0 comments on commit 408e9a3

Please sign in to comment.