Skip to content

Conversation

@marklundin
Copy link
Contributor

@marklundin marklundin commented Apr 23, 2025

Adds support for explicit naming of ESM Scripts using a static scriptName field.

class Rotator extends Script {
  static scriptName = 'customRotator'
}

entity.script.create(Rotator)
console.log(entity.script.customRotator);

This addresses #7580 where class names can get mangled whilst adding support for more verbose script names
and custom casing, both of which have been requested by users.

It handles both cases of loading Scripts as Assets, and also direct insantiation via entity.script.create(Class)

Fixes #7580. Tests included

I confirm I have read the contributing guidelines and signed the Contributor License Agreement.

Added warnings for ESM Script classes lacking a static "scriptName" property. Updated the ScriptComponent and ScriptHandler to infer script names and log appropriate debug messages. Enhanced getScriptName function to return the scriptName if defined. Added tests to verify the new behavior.
Updated the ScriptComponent to ensure that a static "scriptName" property is required only for non-ScriptType instances. Modified tests to check for script existence using the `has` method and added a new test to confirm that no warnings are logged when a ScriptType is used. This improves the robustness of script registration and validation.
@marklundin marklundin requested a review from Copilot April 23, 2025 19:09
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds explicit support for naming ESM Script classes via a static "scriptName" field, with appropriate warnings for missing definitions.

  • Updated script name resolution to prefer the static "scriptName" property.
  • Added warning messages when ESM Scripts do not specify a "scriptName".
  • Extended unit tests to validate correct script naming behavior and fallback mechanisms.

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated no comments.

File Description
test/framework/components/script/component.test.mjs Added tests for warning messages and for correct script name registration.
src/framework/script/script.js Updated getScriptName to return a static "scriptName" if available.
src/framework/handlers/script.js Added warning and fallback logic based on the "scriptName" property of script classes.
src/framework/components/script/component.js Modified script name resolution and warning logic to enforce explicit "scriptName" usage.
Comments suppressed due to low confidence (1)

src/framework/components/script/component.js:712

  • [nitpick] Consider caching the result of getScriptName(scriptType) in a variable to avoid calling it twice in this block, which would improve code clarity.
scriptType.__name ??= toLowerCamelCase(getScriptName(scriptType));

@marklundin marklundin requested a review from mvaligursky April 23, 2025 19:10
Copy link
Contributor

@mvaligursky mvaligursky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. It'd be good to update engine supplied scripts https://github.com/playcanvas/engine/tree/main/scripts/esm to this, and also at least one (rotator) in the examples
Separate PR is fine of course.

@marklundin marklundin merged commit 429ece3 into main Apr 24, 2025
7 checks passed
@marklundin marklundin deleted the feat-esm-script-name branch April 24, 2025 10:32
marklundin added a commit to playcanvas/editor-api that referenced this pull request Apr 24, 2025
ESM Scripts now require a `scriptName` field. See playcanvas/engine#7593

This updates the boiler plate to include this new field.
marklundin added a commit to playcanvas/editor-api that referenced this pull request Apr 24, 2025
ESM Scripts now require a `scriptName` field. See playcanvas/engine#7593

This updates the boiler plate to include this new field.
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

Successfully merging this pull request may close these issues.

Terser mangles Script name

3 participants