-
Notifications
You must be signed in to change notification settings - Fork 2
ESM Scripts #19
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
12
commits into
main
Choose a base branch
from
esm-scripts
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
ESM Scripts #19
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
13e214a
esm scripts initial
marklundin e97f06d
Merge branch 'main' into esm-scripts
marklundin 8c24d1d
Add new article on ESM Scripts in PlayCanvas
marklundin 53aa59b
Enhance ESM Scripts article with video embed and editor focus
marklundin 9c5d1bf
Refine ESM Scripts article for clarity and consistency
marklundin dcc4805
Update blog/2025-06-10-esm-scripts-are-here.md
marklundin ae382f0
Update blog/2025-06-10-esm-scripts-are-here.md
marklundin da47150
feedback
marklundin be2504a
Merge branch 'esm-scripts' of https://github.com/playcanvas/blog into…
marklundin 117e736
Update blog/2025-06-10-esm-scripts-are-here.md
marklundin 247aab3
Refactor ESM Scripts article for improved clarity
marklundin 133869b
Merge branch 'esm-scripts' of https://github.com/playcanvas/blog into…
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
--- | ||
authors: mark | ||
slug: introducing-esm-scripts-in-playcanvas | ||
title: "Introducing ESM Scripts in PlayCanvas" | ||
tags: | ||
- esm | ||
- modules | ||
- editor | ||
- developer | ||
--- | ||
|
||
import ReactPlayer from 'react-player' | ||
|
||
**We’re excited to announce a major step forward for PlayCanvas: **ESM Scripts are now supported** — and officially the recommended way to write scripts in your PlayCanvas projects.** | ||
|
||
Back in 2016, we introduced what we now call [Classic Scripts](https://blog.playcanvas.com/playcanvas-scripts-2-0/). At the time, this represented the state of the art in JavaScript. But things have evolved rapidly since then, and developers today expect modern tooling, clearer patterns, and better integration with the broader JavaScript ecosystem. | ||
|
||
If you’ve ever struggled with managing classic scripts, wondered why your auto-complete didn’t work, or wished you could reuse code across projects more easily — this is for you. | ||
|
||
ESM (ECMAScript Modules) brings modern JavaScript development to the heart of PlayCanvas. It’s faster to get started, easier to scale your project, and way more fun to work with. No more hidden globals. No more messy script loading order. Just well-structured, maintainable, and modular code — exactly how modern web development should be. | ||
|
||
<ReactPlayer controls url="/img/pc-esm-scripts.mp4" /> | ||
|
||
--- | ||
|
||
## Why use ESM Scripts? | ||
|
||
ESM Scripts offer major improvements across the board: | ||
|
||
💡 **Modern JavaScript** — use `import`, `export` class based module syntax | ||
🧠 **Smarter Editor** — better auto-complete and inline docs | ||
🧱 **Modular Codebase** — structure large projects cleanly with reusable modules | ||
⚙️ **No Global Scope Issues** — each script has its own context | ||
📦 **Import Maps** — define aliases and pull in libraries from CDNs | ||
🚀 **Optimized for Production** — static imports enable bundling and future support for tree-shaking | ||
|
||
## What it looks like | ||
|
||
Instead of attaching scripts to an entity and hoping things load in the right order, you now write self-contained, class-based modules like this: | ||
|
||
```js | ||
import { Script } from 'playcanvas'; | ||
|
||
export class Rotator extends Script { | ||
static scriptName = 'rotator'; | ||
|
||
/** | ||
* @attribute | ||
* @range [0, 10] | ||
*/ | ||
speed = 5; | ||
|
||
update(dt) { | ||
this.entity.rotateLocal(0, this.speed * dt, 0); | ||
} | ||
} | ||
``` | ||
|
||
The Editor picks up your `scriptName`, exposes your attributes automatically, and everything behaves as expected — no magic, no legacy syntax. | ||
|
||
--- | ||
|
||
## Real benefits for real projects | ||
|
||
We didn’t just add ESM support because it was shiny. We added it because it solves real problems PlayCanvas developers have told us about for years. | ||
|
||
- You get **stronger editor feedback** while writing code. | ||
- You can **reuse logic cleanly** across multiple scripts. | ||
- You can **share modules between projects** or publish them as libraries. | ||
- You’ll write **fewer bugs** — and spend less time wrestling with script order or context issues. | ||
|
||
Plus, by adopting a standard that the rest of the JS world already embraces, it’s easier than ever to onboard new developers and bring in existing tools and libraries. | ||
|
||
--- | ||
willeastcott marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
## Seamless integration without disruption | ||
|
||
Don’t worry — classic `.js` scripts aren’t going anywhere. If you have an existing project using them, everything still works. You can even mix classic and ESM scripts in the same project. | ||
|
||
But going forward, **ESM Scripts are the best choice** for all new projects — and we think once you try them, you won’t want to go back. | ||
|
||
--- | ||
|
||
## Try It Today | ||
|
||
You can start using ESM Scripts right now — just create a script with a `.mjs` extension and enjoy everything modern JavaScript has to offer. | ||
|
||
Check out the [user manual](https://developer.playcanvas.com/user-manual/scripting/fundamentals/esm-scripts/) for examples, or drop into the [PlayCanvas Forum](https://forum.playcanvas.com/) to share your thoughts. | ||
|
||
We can’t wait to see what you build. |
Binary file not shown.
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.