Skip to content

Commit 7b1b863

Browse files
authored
fix(sdk-commands): stop bundling a second copy of @dcl/ecs into every scene (#1450)
* fix(sdk-commands): stop bundling a second copy of @dcl/ecs into every scene The runtime script embedded into scenes as ~sdk/script-utils imported @dcl/ecs/dist-cjs. EntityState is a runtime enum, so the import survives into the bundle and drags the whole CommonJS build of ecs (~319 KB minified) in next to the ESM build the rest of the scene already uses. Importing @dcl/ecs resolves both to the same ESM copy. A standard production scene drops from 650 KB to 355 KB (-45%); a scene on the published SDK 7.24.4 drops from 668 KB to 361 KB. The CLI still uses dist-cjs where it runs under Node - that was the convention this file followed (#584), correct there but costly inside bundles (#1245). The size-tracked bundle snapshots could not catch this because they build with --customEntryPoint, which skips script-utils; with-main-function is the one snapshot that exercises the real path, and its expectation is regenerated here. * fix(sdk-commands): keep @dcl/ecs tree-shakeable in scene bundles Requiring the @dcl/ecs barrel from the embedded runtime script makes esbuild materialize its full CommonJS namespace, keeping every core component initializer alive: the engine registers 65 component definitions instead of the 20 the scene uses, and the per-frame CRDT dirty sweep doubles. Import the types as type-only and the one runtime value, EntityState, from the dist-cjs leaf (engine/entity.js plus its single dependency, ~2 KB minified), so the barrel stays import-only and shakeable and the compiled file stays requirable under Node. with-main-function vs the previous commit: production bundle 318.6k to 292.2k, eval opcodes 145k to 102k, onUpdate back to the pre-change 5k, VM memory 1606k to 1301k.
1 parent 77493c3 commit 7b1b863

3 files changed

Lines changed: 8 additions & 7 deletions

File tree

packages/@dcl/sdk-commands/src/logic/runtime-script.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* eslint-disable no-console */
2-
import { IEngine, Entity, EntityState } from '@dcl/ecs/dist-cjs'
2+
import type { IEngine, Entity } from '@dcl/ecs'
3+
import { EntityState } from '@dcl/ecs/dist-cjs/engine/entity'
34
import { type ActionRef, getActionEvents } from '@dcl/inspector/node_modules/@dcl/asset-packs'
45

56
declare global {

test/sdk-commands/logic/runtime-script.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ jest.mock(
99
)
1010

1111
import { runScripts } from '../../../packages/@dcl/sdk-commands/src/logic/runtime-script'
12-
import { IEngine, Entity, EntityState } from '../../../packages/@dcl/ecs/dist-cjs'
12+
import { IEngine, Entity, EntityState } from '../../../packages/@dcl/ecs/dist'
1313

1414
describe('runtime-script', () => {
1515
let mockEngine: IEngine

test/snapshots/production-bundles/with-main-function.ts.crdt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
SCENE_COMPILED_JS_SIZE_PROD=608.6k bytes
1+
SCENE_COMPILED_JS_SIZE_PROD=292.2k bytes
22
(start empty vm 0.21.0-3680274614.commit-1808aa1)
33
OPCODES ~= 0k
44
MALLOC_COUNT = 1005
@@ -9,9 +9,9 @@ EVAL test/snapshots/production-bundles/with-main-function.js
99
REQUIRE: ~system/EngineApi
1010
REQUIRE: ~system/EngineApi
1111
REQUIRE: ~system/Runtime
12-
OPCODES ~= 222k
13-
MALLOC_COUNT = 43890
14-
ALIVE_OBJS_DELTA ~= 9.89k
12+
OPCODES ~= 102k
13+
MALLOC_COUNT = 21120
14+
ALIVE_OBJS_DELTA ~= 4.95k
1515
CALL onStart()
1616
OPCODES ~= 0k
1717
MALLOC_COUNT = 6
@@ -38,4 +38,4 @@ CALL onUpdate(0.1)
3838
OPCODES ~= 5k
3939
MALLOC_COUNT = 13
4040
ALIVE_OBJS_DELTA ~= 0.00k
41-
MEMORY_USAGE_COUNT ~= 2968.93k bytes
41+
MEMORY_USAGE_COUNT ~= 1300.70k bytes

0 commit comments

Comments
 (0)