-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
ESM Modules - Scripts 2.1 #5906
Comments
Based on the initial implementation, using |
One note about extending a ScriptType, which is marked as already supported. At the moment the support is only partial - it works fine in engine-only setups and at runtime, but Editor doesn't support extended scripts (e.g. they cannot be parsed). |
Do you mean
Yep, I would love to use proper ESM with relative import declarations in the editor, but the artificial file hierarchy (every asset receives some unknown ID) is also blocking it (I have no clue if that changed by now). Parsing scripts for the editor could be done via Babel AST (for my sake they can also be in |
This reminds me. The ScriptType 2.0 cannot be used when trying to run engine in headless mode. The current script handler is using window. Perhaps 2.1 could be made so they do not depend on it. |
We've used current script system in node.js.
Yes, indeed. We need to implement a good mjs solution for scripts in order to make it a good option for the Editor. |
Right. I am referring to this engine/src/framework/handlers/script.js Lines 99 to 102 in dfc4512
I don't see how it can be done without some polyfill or shimming. Since this is a new design, could this dependency be removed as well? |
Oh this, that will have to change, depending on context. This is also complex for modules, as currently, dynamic import does not work in modern browsers. |
Modules would need to be treated differently to other scripts and loaded as |
Just thinking about this @Maksims, we can't really guarantee the loading order of ES Modules scripts. Consider the following ScriptType files: // User specified loading order
./script-a.mjs
./script-b.mjs
./script-c.mjs
// script-a
import './script-c'
console.log('a')
// script-b
console.log('b')
// script-c
console.log('c')
// output of loading
c, a, b This may be ok though, as we just exclude ES Module Scripts from the loading order API. In addition any es module should probably be excluded from the loadingOrder, as it'll just be imported from a script directly. So we could just keep the loading order for existing classic ES5 scripts. |
I believe you are right. ES Modules cannot be controlled, and their order of loading should be based on how they are imported in various scripts. Also, because they are executed in isolated namespace, their local variables are not shareable, and for referencing some object from another module, it must be imported - so it will guarantee the loading order. So indeed, they should be excluded from the loading order API, but the leaves of the import-tree should be loaded still. |
One thing that could become more problematic is that attributes with getters/setters would not work using ScriptType class Rotator extends ScriptType {
_offset = 0;
set offset (value) {
_offset = value % 360;
}
}
// Overrides the offset
Rotator.add('offset', { ... })
script.Rotator.offset = 370// By passes the setter This is not inherently specific to ES6 classes, but it feels like a much more common scenario. Not sure what the options are here. |
I thought about this, and there is an option: when accessors are created based on attributes, we can check if such property is already defined on the instance, and so we can skip overriding. It will have a logical impact: if developer defines own properties / accessors, then events and copying of provided values will not work. But I believe this should be a separate PR. |
I think yes. |
As the community and developers using PlayCanvas express more need for a ESM Modules for Script Components, which is well discussed here: #4767.
This Issue is an alternative to #5764 in the way to implement ESM Modules with current ScriptType (Scripts 2.0).
Considerations for the feature:
pc
- is the same.Implementation:
pc.ScriptType improvements:
name
.Support static classattributes
definition.Benefits of this approach:
Concerns:
Proposed API:
Defining a ScriptType:
Using jsm file uploaded to
/scripts/utils.jsm
in Editor:The text was updated successfully, but these errors were encountered: