Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,4 @@ e2e/reports/

# auto generated files
src/i18n/types/generated.d.ts
CLAUDE.md
41 changes: 12 additions & 29 deletions src/core/scripting/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,38 +15,25 @@ let executor: Executor | null = null;
class GlobalEnv {
public async record(fn: () => Promise<void>) {
this.clear();
this._queue.push(async () => {
const beforeKeys = Object.keys(globalThis);
await fn();
const afterKeys = Object.keys(globalThis);
for (const afterKey of afterKeys) {
if (!beforeKeys.includes(afterKey)) {
this._incrementalKeys.add(afterKey);
}
const beforeKeys = Object.keys(globalThis);
await fn();
const afterKeys = Object.keys(globalThis);
for (const afterKey of afterKeys) {
if (!beforeKeys.includes(afterKey)) {
this._incrementalKeys.add(afterKey);
}
console.debug(`Incremental keys: ${Array.from(this._incrementalKeys)}`);
});
await this.processQueue(); // 处理队列
}
console.debug(`Incremental keys: ${Array.from(this._incrementalKeys)}`);
}

private clear() {
this._queue.push(async () => {
for (const incrementalKey of this._incrementalKeys) {
delete (globalThis as any)[incrementalKey];
}
this._incrementalKeys.clear();
});
}

private async processQueue() {
while (this._queue.length > 0) {
const next = this._queue.shift();
if (next) await next(); // 执行队列中的下一个任务
for (const incrementalKey of this._incrementalKeys) {
delete (globalThis as any)[incrementalKey];
}
this._incrementalKeys.clear();
}

private _incrementalKeys = new Set<string>();
private _queue: (() => Promise<void>)[] = [];
}

const globalEnv = new GlobalEnv();
Expand Down Expand Up @@ -153,7 +140,7 @@ class ScriptManager {
this._pendingCompileTimer = setTimeout(async () => {
if (this.isCompiling()) {
this.postCompileScripts(delay);
return taskId;
return;
}

this._pendingCompileTimer = null;
Expand Down Expand Up @@ -224,10 +211,6 @@ class ScriptManager {
executor.addPolyfillFile(require.resolve('@cocos/build-polyfills/prebuilt/editor/bundle'));
}

if (!executor) {
console.error('Failed to init executor');
return;
}
executor.setPluginScripts(pluginScripts || []);
await executor.reload();
});
Expand Down
12 changes: 9 additions & 3 deletions src/core/scripting/language-service/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ export class LanguageServiceAdapter {
protected _executingCommandID: Command['id'] = '';
protected readonly _changedFileSet: Set<string> = new Set();
protected readonly _afterOutputTasks: (() => void)[] = [];
private readonly _delegateCallback: (changes: ModifiedAssetChange[]) => Promise<void>;
constructor(
protected readonly _tsconfigPath: FilePath,
protected readonly _currentDirectory: FilePath,
Expand All @@ -153,11 +154,16 @@ export class LanguageServiceAdapter {
this._parseConfigFileHost = new ParseConfigFileHostAdapter(_currentDirectory);
this.host = new LanguageServiceHostAdapter(this._parseConfigFileHost, this._tsconfigPath, this._currentDirectory, this._compilerOptions);
this.languageService = ts.createLanguageService(this.host, undefined, ts.LanguageServiceMode.Semantic);
this._beforeBuildDelegate.add(async (assetChanges) => {
this._delegateCallback = async (assetChanges) => {
assetChanges.forEach(item => item.oldFilePath && item.newFilePath && this.requestRenameFile(item.oldFilePath, item.newFilePath));
await this.finishCommand(assetChanges);
});
};
this._beforeBuildDelegate.add(this._delegateCallback);

}

public dispose() {
this._beforeBuildDelegate.remove(this._delegateCallback);
}

public isExecuting(commandID: string): boolean {
Expand Down Expand Up @@ -203,7 +209,7 @@ export class LanguageServiceAdapter {

/** 请求更新路径 */
public async requestRenameFile(oldFilePath: FilePath, newFilePath: FilePath): Promise<void> {
if (oldFilePath && newFilePath && oldFilePath.endsWith('.ts') && newFilePath.endsWith('.ts') || !extname(oldFilePath)) {
if ((oldFilePath && newFilePath && oldFilePath.endsWith('.ts') && newFilePath.endsWith('.ts')) || (oldFilePath && newFilePath && !extname(oldFilePath))) {
if (oldFilePath === newFilePath) {
return;
}
Expand Down
11 changes: 6 additions & 5 deletions src/core/scripting/packer-driver/asset-db-interop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ export class AssetDbInterop {
onAssetChange(
changeInfo: AssetChangeInfo
) {
const importer = changeInfo.importer;
if (!(importer === 'javascript' || importer === 'typescript')) {
return;
}

const filePath = resolveFileName(changeInfo.filePath);
const uuid = changeInfo.uuid;
const assetChange: AssetChange = {
Expand All @@ -73,11 +78,7 @@ export class AssetDbInterop {
type: changeInfo.type === AssetActionEnum.none ? AssetActionEnum.change : changeInfo.type,
isPluginScript: isPluginScript(changeInfo.userData),
};

const importer = changeInfo.importer;
if (!(importer === 'javascript' || importer === 'typescript')) {
return;
}

let info : TypeScriptAssetInfoCache | null = null;
if (importer === 'typescript') {
info = mapperForTypeScriptAssetInfoCache(changeInfo);
Expand Down
15 changes: 9 additions & 6 deletions src/core/scripting/packer-driver/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import { compressUuid } from '../../builder/worker/builder/utils';
import { TypeScriptConfigBuilder } from '../intelligence';
import { eventEmitter } from '../event-emitter';
import { DBInfo } from '../@types/config-export';
import path from 'path';

const VERSION = '20';

Expand Down Expand Up @@ -294,6 +293,9 @@ export class PackerDriver {
const projectPath = tsBuilder.getProjectPath();
const compilerOptions = await tsBuilder.getCompilerOptions();
const internalDbURLInfos = await tsBuilder.getInternalDbURLInfos();
if (self.languageService) {
self.languageService.dispose();
}
self.languageService = new LanguageServiceAdapter(realTsConfigPath, projectPath, self.beforeEditorBuildDelegate, compilerOptions, internalDbURLInfos);
for (const target of Object.values(this._targets)) {
target.updateDbInfos(this._dbInfos);
Expand All @@ -302,7 +304,7 @@ export class PackerDriver {
};
if (this.busy()) {
this._beforeBuildTasks.push(() => {
update();
return update();
});
} else {
await update();
Expand Down Expand Up @@ -393,15 +395,15 @@ export class PackerDriver {
}

public queryScriptDeps(queryPath: string): string[] {
const scriptPath: string = path.normalize(queryPath).replace(/\\/g, '/');
const scriptPath: string = ps.normalize(queryPath).replace(/\\/g, '/');
this._transformDepsGraph();
if (this._depsGraphCache[scriptPath]) {
return Array.from(this._depsGraphCache[scriptPath]);
}
return [];
}
public queryScriptUsers(queryPath: string): string[] {
const scriptPath: string = path.normalize(queryPath).replace(/\\/g, '/');
const scriptPath: string = ps.normalize(queryPath).replace(/\\/g, '/');
this._transformDepsGraph();
if (this._usedGraphCache[scriptPath]) {
return Array.from(this._usedGraphCache[scriptPath]);
Expand All @@ -423,7 +425,7 @@ export class PackerDriver {
private _assetChangeQueue: AssetChange[] = [];
private _building = false;
private _featureChanged = false;
private _beforeBuildTasks: (() => void)[] = [];
private _beforeBuildTasks: (() => Promise<void> | void)[] = [];
private _depsGraph: Record<string, string[]> = {};
private _needUpdateDepsCache = false;
private _usedGraphCache: Record<string, Set<string>> = {};
Expand Down Expand Up @@ -505,7 +507,7 @@ export class PackerDriver {
const beforeTasks = this._beforeBuildTasks.slice();
this._beforeBuildTasks.length = 0;
for (const beforeTask of beforeTasks) {
beforeTask();
await beforeTask();
}
await this.beforeEditorBuildDelegate.dispatch(assetChanges.filter(item => item.type === AssetActionEnum.change) as ModifiedAssetChange[]);
const nonDTSChanges = assetChanges.filter(item => !item.filePath.endsWith('.d.ts'));
Expand Down Expand Up @@ -1069,6 +1071,7 @@ function matchObject(lhs: unknown, rhs: unknown) {
} else if (typeof lhs === 'object' && lhs !== null) {
return typeof rhs === 'object'
&& rhs !== null
&& Object.keys(lhs).length === Object.keys(rhs as object).length
&& Object.keys(lhs).every((key) => matchLhs((lhs as any)[key], (rhs as any)[key]));
} else if (lhs === null) {
return rhs === null;
Expand Down
6 changes: 5 additions & 1 deletion src/core/scripting/shared/query-shared-settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,11 @@ function verifyImportMapJson(input: unknown): input is ImportMap {
}
}
if ('scopes' in input) {
for (const value of Object.values(input)) {
const scopes = (input as { scopes: unknown }).scopes;
if (typeof scopes !== 'object' || !scopes) {
return false;
}
for (const value of Object.values(scopes)) {
if (!verifySpecifierMap(value)) {
return false;
}
Expand Down
53 changes: 0 additions & 53 deletions src/core/scripting/utils/awaiter.ts

This file was deleted.

3 changes: 1 addition & 2 deletions src/core/scripting/utils/delegate.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
/* eslint-disable prefer-rest-params */
/**
* @zh
* 移除首个指定的数组元素。判定元素相等时相当于于使用了 `Array.prototype.indexOf`。
Expand Down Expand Up @@ -97,6 +96,6 @@ export class AsyncDelegate<T extends (...args: any) => (Promise<void> | void) =
* @returns @en The promise awaiting all async callback resolved. @zh 等待所有异步回调结束的 Promise 对象。
*/
public dispatch(...args: Parameters<T>) {
return Promise.all(this._delegates.map((func) => func(...arguments)).filter(Boolean));
return Promise.all(this._delegates.map((func) => func(...args)).filter(Boolean));
}
}
Loading