mirror of
https://github.com/openharmony/developtools_ace-ets2bundle.git
synced 2026-07-19 16:43:34 -04:00
!1031 decrease resolve modules time in watch mode
Merge pull request !1031 from houhaoyu/master23
This commit is contained in:
@@ -73,6 +73,7 @@ interface Info {
|
||||
export interface CacheFileName {
|
||||
mtimeMs: number,
|
||||
children: string[],
|
||||
parent: string[],
|
||||
error: boolean
|
||||
}
|
||||
|
||||
@@ -80,7 +81,8 @@ interface NeedUpdateFlag {
|
||||
flag: boolean;
|
||||
}
|
||||
|
||||
export let cache: Cache;
|
||||
export let cache: Cache = {};
|
||||
export const shouldResolvedFiles: Set<string> = new Set()
|
||||
type Cache = Record<string, CacheFileName>;
|
||||
|
||||
export class ResultStates {
|
||||
@@ -226,27 +228,38 @@ export class ResultStates {
|
||||
});
|
||||
|
||||
compiler.hooks.watchRun.tap('WatchRun', (comp) => {
|
||||
if (comp.modifiedFiles) {
|
||||
const isTsAndEtsFile: boolean = [...comp.modifiedFiles].some((item: string) => {
|
||||
comp.modifiedFiles = comp.modifiedFiles || [];
|
||||
comp.removedFiles = comp.removedFiles || [];
|
||||
const watchModifiedFiles: string[] = [...comp.modifiedFiles];
|
||||
const watchRemovedFiles: string[] = [...comp.removedFiles];
|
||||
if (watchModifiedFiles.length) {
|
||||
const isTsAndEtsFile: boolean = watchModifiedFiles.some((item: string) => {
|
||||
return /.(ts|ets)$/.test(item);
|
||||
});
|
||||
if (!isTsAndEtsFile) {
|
||||
process.env.watchTs = 'end';
|
||||
}
|
||||
}
|
||||
if (this.shouldWriteChangedList(comp)) {
|
||||
if (this.shouldWriteChangedList(watchModifiedFiles, watchRemovedFiles)) {
|
||||
interface filesObj {
|
||||
modifiedFiles: string[],
|
||||
removedFiles: string[]
|
||||
}
|
||||
const filesObj: filesObj = {
|
||||
modifiedFiles: [...comp.modifiedFiles].filter((file) => {
|
||||
modifiedFiles: watchModifiedFiles.filter((file) => {
|
||||
return fs.statSync(file).isFile();
|
||||
}),
|
||||
removedFiles: [...comp.removedFiles]
|
||||
removedFiles: watchRemovedFiles
|
||||
};
|
||||
writeFileSync(projectConfig.outChangedFileList, JSON.stringify(filesObj));
|
||||
}
|
||||
const changedFiles: string[] = [...watchModifiedFiles, ...watchRemovedFiles];
|
||||
if (changedFiles.length) {
|
||||
shouldResolvedFiles.clear();
|
||||
}
|
||||
changedFiles.forEach((file) => {
|
||||
this.judgeFileShouldResolved(file, shouldResolvedFiles)
|
||||
})
|
||||
})
|
||||
|
||||
compiler.hooks.done.tap('Result States', (stats: Stats) => {
|
||||
@@ -267,11 +280,29 @@ export class ResultStates {
|
||||
});
|
||||
}
|
||||
|
||||
private shouldWriteChangedList(comp): boolean {
|
||||
private shouldWriteChangedList(watchModifiedFiles: string[], watchRemovedFiles: string[]): boolean {
|
||||
return projectConfig.compileMode === ESMODULE && process.env.watchMode && !projectConfig.isPreview &&
|
||||
projectConfig.outChangedFileList && (comp.modifiedFiles || comp.removedFiles) &&
|
||||
!(comp.modifiedFiles && [...comp.modifiedFiles].length === 1 &&
|
||||
[...comp.modifiedFiles][0] == projectConfig.projectPath);
|
||||
projectConfig.outChangedFileList && (watchRemovedFiles.length + watchModifiedFiles.length) &&
|
||||
!(watchModifiedFiles.length === 1 && watchModifiedFiles[0] == projectConfig.projectPath);
|
||||
}
|
||||
|
||||
private judgeFileShouldResolved(file: string, shouldResolvedFiles: Set<string>): void {
|
||||
if (shouldResolvedFiles.has(file)) {
|
||||
return;
|
||||
}
|
||||
shouldResolvedFiles.add(file);
|
||||
if (cache && cache[file] && cache[file].parent) {
|
||||
cache[file].parent.forEach((item)=>{
|
||||
this.judgeFileShouldResolved(item, shouldResolvedFiles);
|
||||
})
|
||||
cache[file].parent = [];
|
||||
}
|
||||
if (cache && cache[file] && cache[file].children) {
|
||||
cache[file].children.forEach((item)=>{
|
||||
this.judgeFileShouldResolved(item, shouldResolvedFiles);
|
||||
})
|
||||
cache[file].children = [];
|
||||
}
|
||||
}
|
||||
|
||||
private printDiagnostic(diagnostic: ts.Diagnostic): void {
|
||||
|
||||
+70
-59
@@ -42,7 +42,8 @@ import { props } from './compile_info';
|
||||
import { resolveSourceFile } from './resolve_ohm_url';
|
||||
import {
|
||||
CacheFileName,
|
||||
cache
|
||||
cache,
|
||||
shouldResolvedFiles
|
||||
} from './compile_info';
|
||||
import { hasDecorator } from './utils';
|
||||
import { isExtendFunction, isOriginalExtend } from './process_ui_syntax';
|
||||
@@ -136,68 +137,74 @@ function getOhmUrlFile(moduleName: string): {modulePath: string, suffix: string}
|
||||
return {modulePath, suffix};
|
||||
}
|
||||
|
||||
const resolvedModulesCache: Map<string, ts.ResolvedModuleFull[]> = new Map();
|
||||
|
||||
function resolveModuleNames(moduleNames: string[], containingFile: string): ts.ResolvedModuleFull[] {
|
||||
const resolvedModules: ts.ResolvedModuleFull[] = [];
|
||||
for (const moduleName of moduleNames) {
|
||||
const result = ts.resolveModuleName(moduleName, containingFile, compilerOptions, {
|
||||
fileExists(fileName: string): boolean {
|
||||
return ts.sys.fileExists(fileName);
|
||||
},
|
||||
readFile(fileName: string): string | undefined {
|
||||
return ts.sys.readFile(fileName);
|
||||
}
|
||||
});
|
||||
if (result.resolvedModule) {
|
||||
resolvedModules.push(result.resolvedModule);
|
||||
} else if (/^@bundle:/.test(moduleName.trim())) {
|
||||
const module: {modulePath: string, suffix: string} = getOhmUrlFile(moduleName.trim());
|
||||
if (ts.sys.fileExists(module.modulePath)) {
|
||||
resolvedModules.push(getResolveModule(module.modulePath, module.suffix));
|
||||
if (![...shouldResolvedFiles].length || shouldResolvedFiles.has(path.resolve(containingFile))) {
|
||||
for (const moduleName of moduleNames) {
|
||||
const result = ts.resolveModuleName(moduleName, containingFile, compilerOptions, {
|
||||
fileExists(fileName: string): boolean {
|
||||
return ts.sys.fileExists(fileName);
|
||||
},
|
||||
readFile(fileName: string): string | undefined {
|
||||
return ts.sys.readFile(fileName);
|
||||
}
|
||||
});
|
||||
if (result.resolvedModule) {
|
||||
resolvedModules.push(result.resolvedModule);
|
||||
} else if (/^@bundle:/.test(moduleName.trim())) {
|
||||
const module: {modulePath: string, suffix: string} = getOhmUrlFile(moduleName.trim());
|
||||
if (ts.sys.fileExists(module.modulePath)) {
|
||||
resolvedModules.push(getResolveModule(module.modulePath, module.suffix));
|
||||
} else {
|
||||
resolvedModules.push(null);
|
||||
}
|
||||
} else if (/^@(system|ohos)\./i.test(moduleName.trim())) {
|
||||
const modulePath: string = path.resolve(__dirname, '../../../api', moduleName + '.d.ts');
|
||||
if (systemModules.includes(moduleName + '.d.ts') && ts.sys.fileExists(modulePath)) {
|
||||
resolvedModules.push(getResolveModule(modulePath, '.d.ts'));
|
||||
} else {
|
||||
resolvedModules.push(null);
|
||||
}
|
||||
} else if (/\.ets$/.test(moduleName)) {
|
||||
const modulePath: string = path.resolve(path.dirname(containingFile), moduleName);
|
||||
if (ts.sys.fileExists(modulePath)) {
|
||||
resolvedModules.push(getResolveModule(modulePath, '.ets'));
|
||||
} else {
|
||||
resolvedModules.push(null);
|
||||
}
|
||||
} else if (/\.ts$/.test(moduleName)) {
|
||||
const modulePath: string = path.resolve(path.dirname(containingFile), moduleName);
|
||||
if (ts.sys.fileExists(modulePath)) {
|
||||
resolvedModules.push(getResolveModule(modulePath, '.ts'));
|
||||
} else {
|
||||
resolvedModules.push(null);
|
||||
}
|
||||
} else {
|
||||
resolvedModules.push(null);
|
||||
}
|
||||
} else if (/^@(system|ohos)\./i.test(moduleName.trim())) {
|
||||
const modulePath: string = path.resolve(__dirname, '../../../api', moduleName + '.d.ts');
|
||||
if (systemModules.includes(moduleName + '.d.ts') && ts.sys.fileExists(modulePath)) {
|
||||
resolvedModules.push(getResolveModule(modulePath, '.d.ts'));
|
||||
} else {
|
||||
resolvedModules.push(null);
|
||||
}
|
||||
} else if (/\.ets$/.test(moduleName)) {
|
||||
const modulePath: string = path.resolve(path.dirname(containingFile), moduleName);
|
||||
if (ts.sys.fileExists(modulePath)) {
|
||||
resolvedModules.push(getResolveModule(modulePath, '.ets'));
|
||||
} else {
|
||||
resolvedModules.push(null);
|
||||
}
|
||||
} else if (/\.ts$/.test(moduleName)) {
|
||||
const modulePath: string = path.resolve(path.dirname(containingFile), moduleName);
|
||||
if (ts.sys.fileExists(modulePath)) {
|
||||
resolvedModules.push(getResolveModule(modulePath, '.ts'));
|
||||
} else {
|
||||
resolvedModules.push(null);
|
||||
}
|
||||
} else {
|
||||
const modulePath: string = path.resolve(__dirname, '../../../api', moduleName + '.d.ts');
|
||||
const suffix: string = /\.js$/.test(moduleName) ? '' : '.js';
|
||||
const jsModulePath: string = path.resolve(__dirname, '../node_modules', moduleName + suffix);
|
||||
const fileModulePath: string =
|
||||
path.resolve(__dirname, '../node_modules', moduleName + '/index.js');
|
||||
if (ts.sys.fileExists(modulePath)) {
|
||||
resolvedModules.push(getResolveModule(modulePath, '.d.ts'));
|
||||
} else if (ts.sys.fileExists(jsModulePath)) {
|
||||
resolvedModules.push(getResolveModule(jsModulePath, '.js'));
|
||||
} else if (ts.sys.fileExists(fileModulePath)) {
|
||||
resolvedModules.push(getResolveModule(fileModulePath, '.js'));
|
||||
} else {
|
||||
resolvedModules.push(null);
|
||||
const modulePath: string = path.resolve(__dirname, '../../../api', moduleName + '.d.ts');
|
||||
const suffix: string = /\.js$/.test(moduleName) ? '' : '.js';
|
||||
const jsModulePath: string = path.resolve(__dirname, '../node_modules', moduleName + suffix);
|
||||
const fileModulePath: string =
|
||||
path.resolve(__dirname, '../node_modules', moduleName + '/index.js');
|
||||
if (ts.sys.fileExists(modulePath)) {
|
||||
resolvedModules.push(getResolveModule(modulePath, '.d.ts'));
|
||||
} else if (ts.sys.fileExists(jsModulePath)) {
|
||||
resolvedModules.push(getResolveModule(jsModulePath, '.js'));
|
||||
} else if (ts.sys.fileExists(fileModulePath)) {
|
||||
resolvedModules.push(getResolveModule(fileModulePath, '.js'));
|
||||
} else {
|
||||
resolvedModules.push(null);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!projectConfig.xtsMode) {
|
||||
createOrUpdateCache(resolvedModules, containingFile);
|
||||
}
|
||||
resolvedModulesCache[path.resolve(containingFile)] = resolvedModules
|
||||
return resolvedModules;
|
||||
}
|
||||
if (process.env.watchMode !== 'true' && !projectConfig.xtsMode) {
|
||||
createOrUpdateCache(resolvedModules, containingFile);
|
||||
}
|
||||
return resolvedModules;
|
||||
return resolvedModulesCache[path.resolve(containingFile)];
|
||||
}
|
||||
|
||||
function createOrUpdateCache(resolvedModules: ts.ResolvedModuleFull[], containingFile: string): void {
|
||||
@@ -212,12 +219,16 @@ function createOrUpdateCache(resolvedModules: ts.ResolvedModuleFull[], containin
|
||||
if (value) {
|
||||
value.mtimeMs = mtimeMs;
|
||||
value.error = error;
|
||||
value.parent.push(path.resolve(containingFile));
|
||||
value.parent = [...new Set(value.parent)];
|
||||
} else {
|
||||
cache[file] = { mtimeMs, children: [], error };
|
||||
cache[file] = { mtimeMs, children: [], parent: [containingFile], error };
|
||||
}
|
||||
}
|
||||
});
|
||||
cache[path.resolve(containingFile)] = { mtimeMs: fs.statSync(containingFile).mtimeMs, children, error };
|
||||
cache[path.resolve(containingFile)] = { mtimeMs: fs.statSync(containingFile).mtimeMs, children,
|
||||
parent: cache[path.resolve(containingFile)] && cache[path.resolve(containingFile)].parent ?
|
||||
cache[path.resolve(containingFile)].parent : [], error };
|
||||
}
|
||||
|
||||
export function createWatchCompilerHost(rootFileNames: string[],
|
||||
|
||||
Reference in New Issue
Block a user