Skip to content

Commit

Permalink
[Bug] 支持在场景脚本中导入项目模块 (cocos#38)
Browse files Browse the repository at this point in the history
* 支持在场景脚本中导入项目模块

* 修复 setTimeout 应该从 node 里面导入的问题
  • Loading branch information
shrinktofit committed Mar 30, 2022
1 parent 1d72d08 commit e0d6e16
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
ModLoOptions,
ImportMap,
} from '@cocos/creator-programming-mod-lo/lib/mod-lo';
import { setTimeout } from 'timers';

const VERSION = '18';

Expand Down
9 changes: 8 additions & 1 deletion app/builtin/scene/source/script/3d/manager/scripts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import ps from 'path';
import { SourceMapSupport } from '@editor/lib-programming/dist/source-map-support';
import { QuickPackLoaderContext } from '@cocos/creator-programming-quick-pack/lib/loader';
import { sortPluginScripts, getPluginScriptDependencies } from '@editor/lib-programming/dist/executor/sort-plugin-scripts';
import { pathToFileURL } from 'url';

/**
* 异步迭代。有以下特点:
Expand Down Expand Up @@ -129,7 +130,13 @@ class ScriptManager extends EventEmitter {
await this._asyncIteration.nextIteration();

Editor.Module.setImportProjectModuleDelegate(async (url: string) => {
throw new Error(`Not supported`);
const filePath = await Editor.Message.request('asset-db', 'query-path', url) as string;
if (!filePath) {
throw new Error(`${url} is not a valid database URL.`);
}

const fileURL = pathToFileURL(filePath);
return await this._executor.import(fileURL.href);
});
}

Expand Down

0 comments on commit e0d6e16

Please sign in to comment.