-
-
Notifications
You must be signed in to change notification settings - Fork 309
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
Add ScriptLoader
#2417
Open
MrKou47
wants to merge
18
commits into
galacean:dev/1.4
Choose a base branch
from
MrKou47:feat/script-loader
base: dev/1.4
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add ScriptLoader
#2417
Changes from 2 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
ed78593
feat: add script loader
MrKou47 2a1e40d
fix: lint error
MrKou47 0a214c7
chore: add test
MrKou47 9081f74
Update AssetType.ts
GuoLei1990 2ef9c8e
Update index.ts
GuoLei1990 e7bf294
chore: add ScriptLoader test
MrKou47 12357bb
chore: update Loader with clear context
MrKou47 f67960e
feat: let import() as it is
MrKou47 44025d7
chore: lint
MrKou47 89b957b
Update ScriptLoader.ts
GuoLei1990 fde6eca
chore: export ESModule type
MrKou47 e68253f
Update ScriptLoader.ts
GuoLei1990 80e4eed
Update ScriptLoader.ts
GuoLei1990 76d32a5
Update ScriptLoader.ts
GuoLei1990 fe5e35c
Update ScriptLoader.test.ts
GuoLei1990 ac334dc
Update ScriptLoader.ts
GuoLei1990 cd9d4bb
Update ScriptLoader.ts
GuoLei1990 a443c04
chore: update test
MrKou47 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { AssetPromise, AssetType, Loader, LoadItem, resourceLoader, Script } from "@galacean/engine-core"; | ||
|
||
@resourceLoader(AssetType.Script, ["js", "mjs"], false) | ||
class ScriptLoader extends Loader<Script> { | ||
load(item: LoadItem): AssetPromise<Script> { | ||
return new AssetPromise((resolve, reject) => { | ||
const { url } = item; | ||
(import(url) as Promise<Script>) | ||
.then((esModule) => { | ||
resolve(esModule); | ||
}) | ||
.catch(reject); | ||
}); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Enhance browser compatibility and error handling The current implementation has several areas for improvement:
Consider implementing these improvements: class ScriptLoader extends Loader<Script> {
+ private loadingScripts: Map<string, Promise<Script>> = new Map();
+
load(item: LoadItem): AssetPromise<Script> {
return new AssetPromise((resolve, reject) => {
const { url } = item;
- (import(url) as Promise<Script>)
- .then((esModule) => {
- resolve(esModule);
- })
- .catch(reject);
+
+ // Reuse existing load promise if available
+ const existing = this.loadingScripts.get(url);
+ if (existing) {
+ return existing.then(resolve).catch(reject);
+ }
+
+ const loadPromise = (async () => {
+ try {
+ // Use polyfill for dynamic import
+ const moduleLoader = await import('dynamic-import-polyfill');
+ const esModule = await moduleLoader.load(url);
+ return esModule;
+ } catch (error) {
+ throw new Error(`Failed to load script ${url}: ${error.message}`);
+ } finally {
+ this.loadingScripts.delete(url);
+ }
+ })();
+
+ this.loadingScripts.set(url, loadPromise);
+ loadPromise.then(resolve).catch(reject);
});
}
+
+ // Cleanup method for resource management
+ destroy(): void {
+ this.loadingScripts.clear();
+ super.destroy?.();
+ }
}
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider adding input validation for script URLs
The loader handles both .js and .mjs files, but there's no URL validation before dynamic importing. This could pose security risks if untrusted URLs are processed.
Consider adding URL validation before processing:
+ import { isValidUrl } from "@galacean/engine-core"; import { AssetPromise, AssetType, Loader, LoadItem, resourceLoader, Script } from "@galacean/engine-core"; @resourceLoader(AssetType.Script, ["js", "mjs"], false)