Skip to content
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

Allow imported scripts to load as modules #1054

Closed
LeXXik opened this issue Aug 27, 2023 · 5 comments
Closed

Allow imported scripts to load as modules #1054

LeXXik opened this issue Aug 27, 2023 · 5 comments
Assignees

Comments

@LeXXik
Copy link

LeXXik commented Aug 27, 2023

This is a feature request to allow third party scripts to be loaded as module.
image

<script>...</script>

would become

<script type="module">...</script>
@willeastcott
Copy link
Contributor

I'm wondering how this would work....I'm guessing we'd need to add a checkbox next to a the URL to specify if the script is a module?
Can you give me some examples of scripts you'd prefer to include as modules?

@LeXXik
Copy link
Author

LeXXik commented Aug 29, 2023

An example:

// index.html
<script type="module" src="./module.js"></script>
// module.js
import { print } from './printer.js';
globalThis.Print = print;
// printer.js
export function print(name) {
    console.log(name);
}
// pc.ScriptType
var Script = pc.createScript('script');
Script.prototype.initialize = function() {
    Print('example');
};

@Maksims
Copy link
Collaborator

Maksims commented Aug 29, 2023

This will definitely break if enabled by default for all scripts. So this should be an option for individual scripts.

@yaustar
Copy link
Collaborator

yaustar commented Aug 31, 2023

A good example that users have run into is the firebase SDK https://forum.playcanvas.com/t/uncaught-syntaxerror-cannot-use-import-statement-outside-a-module/23242/

@marklundin
Copy link
Member

Adding type='module' on these will cause these scripts to be async which may be problematic as there's no guarantee no of execution order between these and classic scripts.

We are gradually moving toward first class support for ES modules in the engine and editor, so the above use case can be solved like this;

// printer.js
export function print(name) {
    console.log(name);
}
import { print } from './printer.js';
// pc.ScriptType
var Script = pc.createScript('script');
Script.prototype.initialize = function() {
    print('example');
};

Closing for now as unplanned

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants