-
Notifications
You must be signed in to change notification settings - Fork 52
Adds documentation on exporting & bundling for ESM Script #752
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
Open
marklundin
wants to merge
13
commits into
main
Choose a base branch
from
esm-exporting
base: main
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
Changes from 4 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
fe54f62
Add exporting info
marklundin 4861438
Update terminology in exporting documentation: change "Classic Script…
marklundin 8314789
linting
marklundin 8eff68f
linting fix
marklundin 2f17d88
Update exporting.md
marklundin 434d30e
removing tx changes
marklundin c9ac737
Merge branch 'main' into esm-exporting
marklundin b1b924e
Merge branch 'main' into esm-exporting
marklundin 67ee521
Add Japanese localization
marklundin 51665f5
Merge branch 'esm-exporting' of https://github.com/playcanvas/develop…
marklundin 980b7ad
Merge branch 'main' into esm-exporting
marklundin ed2dbb3
Merge branch 'main' into esm-exporting
marklundin 232bbd0
Merge branch 'main' into esm-exporting
marklundin 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 hidden or 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 hidden or 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,73 @@ | ||
--- | ||
title: Exporting and Preloading | ||
marklundin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
sidebar_position: 5 | ||
--- | ||
|
||
## Loading Order | ||
|
||
### ESM Scripts | ||
|
||
ESM Scripts do not have an explicit loading order, and should not be relied upon to load in a specific order. Instead, you should use module import statements to declare dependencies between modules. | ||
|
||
### Classic Scripts | ||
|
||
Generally all scripts are loaded at the beginning of your application. The loading order is determined by a setting in your project which you can access from the main Editor menu or Scene Settings: | ||
|
||
 | ||
|
||
The loading order panel shows all Classic scripts marked as `preload` and the order that they are loaded and executed in. | ||
|
||
 | ||
|
||
You can click-and-drag to move individual scripts around to edit the order. | ||
|
||
When scripts are first loaded, they are immediately executed. That means that the scripts are first executed in the order that they are loaded. However, the loading order of the script **does not** affect the execution of order of script methods within script component. For example, the `initialize` methods of scripts on the same entity are called in the order that they are listed on the Entity, not the loading order. | ||
|
||
## Preloading | ||
|
||
By default, as with other assets in PlayCanvas, a script asset is marked as `preload`. This means that it will be loaded before the application starts. If you disable preloading on a script, it will not be loaded under normal circumstances. This way, you can include a script in your project but prevent it from loading by unchecking `preload`. You can trigger a non-preloading script to load dynamically by using the regular asset API (see [`AssetRegistry#load`](https://api.playcanvas.com/engine/classes/AssetRegistry.html#load)). | ||
|
||
It is possible to subscribe to dynamic changes to script registry: | ||
|
||
```javascript | ||
this.app.scripts.on('add', (name, scriptType) => { | ||
console.log('script', name, 'has been loaded'); | ||
}); | ||
``` | ||
|
||
## Exporting | ||
|
||
When you publish or export your PlayCanvas project, the way your scripts are processed depends on whether you’re using Classic scripts or ECMAScript Modules (ESM). | ||
|
||
### Classic | ||
|
||
All preloaded Classic scripts are concatenated into a single file by default. This reduces the number of network requests and improves load times. This method ensures compatibility with older projects and is ideal for simpler codebases. | ||
|
||
### ESM | ||
|
||
Projects that use ESM Scripts go through a modern bundling and optimization process. If your project contains any ESM scripts, the export process will automatically generate an ESM build. | ||
|
||
Key features of ESM builds: | ||
|
||
- **Bundled output** Your code is bundled together—optionally including the PlayCanvas Engine—into a set of optimized JavaScript files. | ||
- **Tree-shaking** Unused code is eliminated, reducing bundle size. | ||
- **Code splitting** Your application is split into smaller chunks, which are loaded only when needed. This improves perceived performance by prioritizing critical code. | ||
- **Minification (optional)** If you enable Minify Scripts, your code is also compressed to reduce download size further. | ||
- **Concatenation option** If you enable Concatenate Scripts, the exporter bundles your entire application—including the engine—into the most efficient structure, based on usage patterns. | ||
|
||
#### Code Splitting | ||
|
||
Although you don’t have fine-grained control over where code is split, you can influence it. If you use dynamic `import()` statements instead of static imports, the bundler treats that module as a candidate for on-demand loading. This helps you defer non-critical code until it’s actually needed — ideal for features like menus, optional tools, or cutscenes. | ||
|
||
#### Mixing ESM and Classic Scripts | ||
|
||
You can freely mix ESM and Classic scripts within the same project. However, they are treated differently: | ||
|
||
- ESM scripts are bundled, optimized, and can benefit from tree-shaking and code splitting. | ||
- Classic scripts are included as separate files and are not part of the ESM bundle. | ||
|
||
This ensures backward compatibility while allowing modern development practices. | ||
|
||
:::warning | ||
Avoid using `import()` inside Classic scripts to load ESM modules. The final paths of bundled modules may change during export, and Classic scripts won’t be able to resolve them correctly. | ||
::: | ||
marklundin marked this conversation as resolved.
Show resolved
Hide resolved
|
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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
73 changes: 73 additions & 0 deletions
73
...rus-plugin-content-docs/current/user-manual/scripting/editor-users/exporting.md
This file contains hidden or 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,73 @@ | ||
--- | ||
marklundin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
title: Exporting and Preloading | ||
marklundin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
sidebar_position: 5 | ||
--- | ||
|
||
## Loading Order | ||
|
||
### ESM Scripts | ||
|
||
ESM Scripts do not have an explicit loading order, and should not be relied upon to load in a specific order. Instead, you should use module import statements to declare dependencies between modules. | ||
|
||
### Classic Scripts | ||
|
||
Generally all scripts are loaded at the beginning of your application. The loading order is determined by a setting in your project which you can access from the main Editor menu or Scene Settings: | ||
|
||
 | ||
|
||
The loading order panel shows all Classic scripts marked as `preload` and the order that they are loaded and executed in. | ||
|
||
 | ||
|
||
You can click-and-drag to move individual scripts around to edit the order. | ||
|
||
When scripts are first loaded, they are immediately executed. That means that the scripts are first executed in the order that they are loaded. However, the loading order of the script **does not** affect the execution of order of script methods within script component. For example, the `initialize` methods of scripts on the same entity are called in the order that they are listed on the Entity, not the loading order. | ||
|
||
## Preloading | ||
|
||
By default, as with other assets in PlayCanvas, a script asset is marked as `preload`. This means that it will be loaded before the application starts. If you disable preloading on a script, it will not be loaded under normal circumstances. This way, you can include a script in your project but prevent it from loading by unchecking `preload`. You can trigger a non-preloading script to load dynamically by using the regular asset API (see [`AssetRegistry#load`](https://api.playcanvas.com/engine/classes/AssetRegistry.html#load)). | ||
|
||
It is possible to subscribe to dynamic changes to script registry: | ||
|
||
```javascript | ||
this.app.scripts.on('add', (name, scriptType) => { | ||
console.log('script', name, 'has been loaded'); | ||
}); | ||
``` | ||
|
||
## Exporting | ||
|
||
When you publish or export your PlayCanvas project, the way your scripts are processed depends on whether you’re using Classic scripts or ECMAScript Modules (ESM). | ||
|
||
### Classic Scripts | ||
|
||
All preloaded Classic scripts are concatenated into a single file by default. This reduces the number of network requests and improves load times. This method ensures compatibility with older projects and is ideal for simpler codebases. | ||
|
||
### ESM Scripts | ||
|
||
Projects that use ESM Scripts go through a modern bundling and optimization process. If your project contains any ESM scripts, the export process will automatically generate an ESM build. | ||
|
||
Key features of ESM builds: | ||
- **Bundled output** | ||
Your code is bundled together—optionally including the PlayCanvas Engine—into a set of optimized JavaScript files. | ||
- **Tree-shaking** Unused code is eliminated, reducing bundle size. | ||
- **Code splitting** Your application is split into smaller chunks, which are loaded only when needed. This improves perceived performance by prioritizing critical code. | ||
- **Minification (optional)** If you enable Minify Scripts, your code is also compressed to reduce download size further. | ||
- **Concatenation option** If you enable Concatenate Scripts, the exporter bundles your entire application—including the engine—into the most efficient structure, based on usage patterns. | ||
|
||
#### Code Splitting | ||
|
||
Although you don’t have fine-grained control over where code is split, you can influence it. If you use dynamic `import()` statements instead of static imports, the bundler treats that module as a candidate for on-demand loading. This helps you defer non-critical code until it’s actually needed — ideal for features like menus, optional tools, or cutscenes. | ||
|
||
#### Mixing ESM and Classic Scripts | ||
|
||
You can freely mix ESM and Classic scripts within the same project. However, they are treated differently: | ||
|
||
- ESM scripts are bundled, optimized, and can benefit from tree-shaking and code splitting. | ||
- Classic scripts are included as separate files and are not part of the ESM bundle. | ||
|
||
This ensures backward compatibility while allowing modern development practices. | ||
|
||
:::warning | ||
Avoid using `import()` inside Classic scripts to load ESM modules. The final paths of bundled modules may change during export, and Classic scripts won’t be able to resolve them correctly. | ||
::: |
38 changes: 0 additions & 38 deletions
38
...plugin-content-docs/current/user-manual/scripting/editor-users/loading-order.md
This file was deleted.
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.
Uh oh!
There was an error while loading. Please reload this page.