@zephyr3d/scene 0.6.1 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/animation/animation.js +149 -27
- package/dist/animation/animation.js.map +1 -1
- package/dist/animation/animationset.js +173 -73
- package/dist/animation/animationset.js.map +1 -1
- package/dist/animation/animationtrack.js +65 -10
- package/dist/animation/animationtrack.js.map +1 -1
- package/dist/animation/eulerrotationtrack.js +41 -20
- package/dist/animation/eulerrotationtrack.js.map +1 -1
- package/dist/animation/morphtarget.js +14 -38
- package/dist/animation/morphtarget.js.map +1 -1
- package/dist/animation/morphtrack.js +59 -22
- package/dist/animation/morphtrack.js.map +1 -1
- package/dist/animation/proptrack.js +190 -0
- package/dist/animation/proptrack.js.map +1 -0
- package/dist/animation/rotationtrack.js +29 -11
- package/dist/animation/rotationtrack.js.map +1 -1
- package/dist/animation/scaletrack.js +29 -11
- package/dist/animation/scaletrack.js.map +1 -1
- package/dist/animation/skeleton.js +241 -78
- package/dist/animation/skeleton.js.map +1 -1
- package/dist/animation/translationtrack.js +30 -11
- package/dist/animation/translationtrack.js.map +1 -1
- package/dist/app/api.js +43 -0
- package/dist/app/api.js.map +1 -0
- package/dist/app/app.js +153 -0
- package/dist/app/app.js.map +1 -0
- package/dist/app/engine.js +312 -0
- package/dist/app/engine.js.map +1 -0
- package/dist/app/inputmgr.js +351 -0
- package/dist/app/inputmgr.js.map +1 -0
- package/dist/app/runtimescript.js +62 -0
- package/dist/app/runtimescript.js.map +1 -0
- package/dist/app/scriptingsystem.js +220 -0
- package/dist/app/scriptingsystem.js.map +1 -0
- package/dist/app/scriptregistry.js +432 -0
- package/dist/app/scriptregistry.js.map +1 -0
- package/dist/asset/assetmanager.js +772 -291
- package/dist/asset/assetmanager.js.map +1 -1
- package/dist/asset/builtin.js +7 -43
- package/dist/asset/builtin.js.map +1 -1
- package/dist/asset/loaders/dds/dds.js +17 -93
- package/dist/asset/loaders/dds/dds.js.map +1 -1
- package/dist/asset/loaders/dds/dds_loader.js +8 -18
- package/dist/asset/loaders/dds/dds_loader.js.map +1 -1
- package/dist/asset/loaders/gltf/gltf_loader.js +36 -42
- package/dist/asset/loaders/gltf/gltf_loader.js.map +1 -1
- package/dist/asset/loaders/gltf/helpers.js +27 -14
- package/dist/asset/loaders/gltf/helpers.js.map +1 -1
- package/dist/asset/loaders/hdr/hdr.js +17 -12
- package/dist/asset/loaders/hdr/hdr.js.map +1 -1
- package/dist/asset/loaders/image/tga_Loader.js +13 -16
- package/dist/asset/loaders/image/tga_Loader.js.map +1 -1
- package/dist/asset/loaders/image/webimage_loader.js +51 -50
- package/dist/asset/loaders/image/webimage_loader.js.map +1 -1
- package/dist/asset/loaders/loader.js.map +1 -1
- package/dist/asset/model.js +158 -4
- package/dist/asset/model.js.map +1 -1
- package/dist/blitter/bilateralblur.js +0 -1
- package/dist/blitter/bilateralblur.js.map +1 -1
- package/dist/blitter/blitter.js +22 -23
- package/dist/blitter/blitter.js.map +1 -1
- package/dist/blitter/box.js.map +1 -1
- package/dist/blitter/copy.js.map +1 -1
- package/dist/blitter/gaussianblur.js +23 -23
- package/dist/blitter/gaussianblur.js.map +1 -1
- package/dist/camera/base.js +141 -34
- package/dist/camera/base.js.map +1 -1
- package/dist/camera/camera.js +653 -149
- package/dist/camera/camera.js.map +1 -1
- package/dist/camera/fps.js +2 -10
- package/dist/camera/fps.js.map +1 -1
- package/dist/camera/orbit.js +132 -59
- package/dist/camera/orbit.js.map +1 -1
- package/dist/camera/orthocamera.js +37 -12
- package/dist/camera/orthocamera.js.map +1 -1
- package/dist/camera/perspectivecamera.js +37 -20
- package/dist/camera/perspectivecamera.js.map +1 -1
- package/dist/index.d.ts +18268 -6982
- package/dist/index.js +49 -16
- package/dist/index.js.map +1 -1
- package/dist/material/blinn.js +5 -0
- package/dist/material/blinn.js.map +1 -1
- package/dist/material/grassmaterial.js +6 -1
- package/dist/material/grassmaterial.js.map +1 -1
- package/dist/material/lambert.js +6 -1
- package/dist/material/lambert.js.map +1 -1
- package/dist/material/material.js +346 -73
- package/dist/material/material.js.map +1 -1
- package/dist/material/meshmaterial.js +498 -156
- package/dist/material/meshmaterial.js.map +1 -1
- package/dist/material/mixins/albedocolor.js +5 -1
- package/dist/material/mixins/albedocolor.js.map +1 -1
- package/dist/material/mixins/foliage.js +3 -3
- package/dist/material/mixins/foliage.js.map +1 -1
- package/dist/material/mixins/lightmodel/blinnphong.js +18 -5
- package/dist/material/mixins/lightmodel/blinnphong.js.map +1 -1
- package/dist/material/mixins/lightmodel/lambert.js +6 -6
- package/dist/material/mixins/lightmodel/lambert.js.map +1 -1
- package/dist/material/mixins/lightmodel/pbrblueprintmixin.js +235 -0
- package/dist/material/mixins/lightmodel/pbrblueprintmixin.js.map +1 -0
- package/dist/material/mixins/lightmodel/pbrmetallicroughness.js +52 -15
- package/dist/material/mixins/lightmodel/pbrmetallicroughness.js.map +1 -1
- package/dist/material/mixins/lightmodel/pbrspecularglossness.js +27 -9
- package/dist/material/mixins/lightmodel/pbrspecularglossness.js.map +1 -1
- package/dist/material/mixins/lit.js +16 -4
- package/dist/material/mixins/lit.js.map +1 -1
- package/dist/material/mixins/pbr/brdf.js +134 -0
- package/dist/material/mixins/pbr/brdf.js.map +1 -0
- package/dist/material/mixins/pbr/common.js +68 -85
- package/dist/material/mixins/pbr/common.js.map +1 -1
- package/dist/material/mixins/texture.js +98 -83
- package/dist/material/mixins/texture.js.map +1 -1
- package/dist/material/mixins/vertexcolor.js +6 -2
- package/dist/material/mixins/vertexcolor.js.map +1 -1
- package/dist/material/particle.js +272 -0
- package/dist/material/particle.js.map +1 -0
- package/dist/material/pbrblueprint.js +186 -0
- package/dist/material/pbrblueprint.js.map +1 -0
- package/dist/material/pbrmr.js +10 -0
- package/dist/material/pbrmr.js.map +1 -1
- package/dist/material/pbrsg.js +10 -0
- package/dist/material/pbrsg.js.map +1 -1
- package/dist/material/shader/helper.js +521 -287
- package/dist/material/shader/helper.js.map +1 -1
- package/dist/material/terrain-cm.js +607 -0
- package/dist/material/terrain-cm.js.map +1 -0
- package/dist/material/terrainmaterial.js +59 -54
- package/dist/material/terrainmaterial.js.map +1 -1
- package/dist/material/unlit.js +5 -0
- package/dist/material/unlit.js.map +1 -1
- package/dist/material/water.js +415 -0
- package/dist/material/water.js.map +1 -0
- package/dist/node_modules/@zephyr3d/runtime/dist/runtime/runtimemgr.js +38 -0
- package/dist/node_modules/@zephyr3d/runtime/dist/runtime/runtimemgr.js.map +1 -0
- package/dist/node_modules/@zephyr3d/runtime/dist/runtime/runtimescript.js +10 -0
- package/dist/node_modules/@zephyr3d/runtime/dist/runtime/runtimescript.js.map +1 -0
- package/dist/node_modules/@zephyr3d/runtime/dist/runtime/scriptingsystem.js +127 -0
- package/dist/node_modules/@zephyr3d/runtime/dist/runtime/scriptingsystem.js.map +1 -0
- package/dist/node_modules/@zephyr3d/runtime/dist/runtime/scriptregistry.js +263 -0
- package/dist/node_modules/@zephyr3d/runtime/dist/runtime/scriptregistry.js.map +1 -0
- package/dist/node_modules/es-module-lexer/dist/lexer.js +5 -0
- package/dist/node_modules/es-module-lexer/dist/lexer.js.map +1 -0
- package/dist/posteffect/bloom.js +39 -54
- package/dist/posteffect/bloom.js.map +1 -1
- package/dist/posteffect/compositor.js +95 -128
- package/dist/posteffect/compositor.js.map +1 -1
- package/dist/posteffect/fxaa.js +10 -18
- package/dist/posteffect/fxaa.js.map +1 -1
- package/dist/posteffect/grayscale.js +9 -17
- package/dist/posteffect/grayscale.js.map +1 -1
- package/dist/posteffect/motionblur.js +105 -0
- package/dist/posteffect/motionblur.js.map +1 -0
- package/dist/posteffect/posteffect.js +66 -35
- package/dist/posteffect/posteffect.js.map +1 -1
- package/dist/posteffect/sao.js +13 -21
- package/dist/posteffect/sao.js.map +1 -1
- package/dist/posteffect/ssr.js +60 -100
- package/dist/posteffect/ssr.js.map +1 -1
- package/dist/posteffect/taa.js +175 -0
- package/dist/posteffect/taa.js.map +1 -0
- package/dist/posteffect/tonemap.js +12 -20
- package/dist/posteffect/tonemap.js.map +1 -1
- package/dist/render/abuffer_oit.js +30 -19
- package/dist/render/abuffer_oit.js.map +1 -1
- package/dist/render/clipmap.js +429 -101
- package/dist/render/clipmap.js.map +1 -1
- package/dist/render/cluster_light.js +7 -5
- package/dist/render/cluster_light.js.map +1 -1
- package/dist/render/cull_visitor.js +43 -3
- package/dist/render/cull_visitor.js.map +1 -1
- package/dist/render/depthpass.js +14 -5
- package/dist/render/depthpass.js.map +1 -1
- package/dist/render/drawable_mixin.js +113 -40
- package/dist/render/drawable_mixin.js.map +1 -1
- package/dist/render/envlight.js +86 -141
- package/dist/render/envlight.js.map +1 -1
- package/dist/render/fbm_wavegenerator.js +234 -0
- package/dist/render/fbm_wavegenerator.js.map +1 -0
- package/dist/render/fft_wavegenerator.js +64 -50
- package/dist/render/fft_wavegenerator.js.map +1 -1
- package/dist/render/fullscreenquad.js +2 -2
- package/dist/render/fullscreenquad.js.map +1 -1
- package/dist/render/gerstner_wavegenerator.js +98 -48
- package/dist/render/gerstner_wavegenerator.js.map +1 -1
- package/dist/render/globalbindgroup_allocator.js +4 -2
- package/dist/render/globalbindgroup_allocator.js.map +1 -1
- package/dist/render/hzb.js +6 -3
- package/dist/render/hzb.js.map +1 -1
- package/dist/render/lightpass.js +19 -18
- package/dist/render/lightpass.js.map +1 -1
- package/dist/render/objectcolorpass.js +4 -4
- package/dist/render/objectcolorpass.js.map +1 -1
- package/dist/render/primitive.js +213 -104
- package/dist/render/primitive.js.map +1 -1
- package/dist/render/render_queue.js +40 -20
- package/dist/render/render_queue.js.map +1 -1
- package/dist/render/renderbundle_wrapper.js +65 -15
- package/dist/render/renderbundle_wrapper.js.map +1 -1
- package/dist/render/renderer.js +326 -185
- package/dist/render/renderer.js.map +1 -1
- package/dist/render/renderpass.js +20 -36
- package/dist/render/renderpass.js.map +1 -1
- package/dist/render/shadowmap_pass.js +3 -3
- package/dist/render/shadowmap_pass.js.map +1 -1
- package/dist/render/sky.js +864 -541
- package/dist/render/sky.js.map +1 -1
- package/dist/render/weightedblended_oit.js +13 -15
- package/dist/render/weightedblended_oit.js.map +1 -1
- package/dist/scene/batchgroup.js +18 -5
- package/dist/scene/batchgroup.js.map +1 -1
- package/dist/scene/environment.js +78 -48
- package/dist/scene/environment.js.map +1 -1
- package/dist/scene/graph_node.js +2 -3
- package/dist/scene/graph_node.js.map +1 -1
- package/dist/scene/light.js +28 -89
- package/dist/scene/light.js.map +1 -1
- package/dist/scene/mesh.js +218 -87
- package/dist/scene/mesh.js.map +1 -1
- package/dist/scene/octree.js +371 -162
- package/dist/scene/octree.js.map +1 -1
- package/dist/scene/particlesys.js +684 -0
- package/dist/scene/particlesys.js.map +1 -0
- package/dist/scene/raycast_visitor.js +34 -5
- package/dist/scene/raycast_visitor.js.map +1 -1
- package/dist/scene/scene.js +309 -85
- package/dist/scene/scene.js.map +1 -1
- package/dist/scene/scene_node.js +675 -102
- package/dist/scene/scene_node.js.map +1 -1
- package/dist/scene/terrain/grass.js +48 -49
- package/dist/scene/terrain/grass.js.map +1 -1
- package/dist/scene/terrain/heightfield.js +46 -44
- package/dist/scene/terrain/heightfield.js.map +1 -1
- package/dist/scene/terrain/patch.js +20 -29
- package/dist/scene/terrain/patch.js.map +1 -1
- package/dist/scene/terrain/quadtree.js +58 -27
- package/dist/scene/terrain/quadtree.js.map +1 -1
- package/dist/scene/terrain/terrain.js +31 -45
- package/dist/scene/terrain/terrain.js.map +1 -1
- package/dist/scene/terrain-cm/grass.js +603 -0
- package/dist/scene/terrain-cm/grass.js.map +1 -0
- package/dist/scene/terrain-cm/grassmaterial.js +160 -0
- package/dist/scene/terrain-cm/grassmaterial.js.map +1 -0
- package/dist/scene/terrain-cm/terrain-cm.js +533 -0
- package/dist/scene/terrain-cm/terrain-cm.js.map +1 -0
- package/dist/scene/water.js +378 -0
- package/dist/scene/water.js.map +1 -0
- package/dist/shaders/atmosphere.js +957 -0
- package/dist/shaders/atmosphere.js.map +1 -0
- package/dist/shaders/fog.js +116 -0
- package/dist/shaders/fog.js.map +1 -0
- package/dist/shaders/misc.js.map +1 -1
- package/dist/shaders/noise.js +217 -8
- package/dist/shaders/noise.js.map +1 -1
- package/dist/shaders/pbr.js.map +1 -1
- package/dist/shaders/shadow.js +8 -8
- package/dist/shaders/shadow.js.map +1 -1
- package/dist/shaders/ssr.js +87 -39
- package/dist/shaders/ssr.js.map +1 -1
- package/dist/shaders/temporal.js +216 -0
- package/dist/shaders/temporal.js.map +1 -0
- package/dist/shaders/water.js +42 -20
- package/dist/shaders/water.js.map +1 -1
- package/dist/shadow/esm.js +31 -13
- package/dist/shadow/esm.js.map +1 -1
- package/dist/shadow/pcf_opt.js +12 -13
- package/dist/shadow/pcf_opt.js.map +1 -1
- package/dist/shadow/pcf_pd.js +12 -13
- package/dist/shadow/pcf_pd.js.map +1 -1
- package/dist/shadow/shader.js +38 -0
- package/dist/shadow/shader.js.map +1 -0
- package/dist/shadow/shadow_impl.js.map +1 -1
- package/dist/shadow/shadowmapper.js +67 -26
- package/dist/shadow/shadowmapper.js.map +1 -1
- package/dist/shadow/ssm.js +15 -16
- package/dist/shadow/ssm.js.map +1 -1
- package/dist/shadow/vsm.js +33 -16
- package/dist/shadow/vsm.js.map +1 -1
- package/dist/shapes/box.js +181 -83
- package/dist/shapes/box.js.map +1 -1
- package/dist/shapes/cylinder.js +101 -43
- package/dist/shapes/cylinder.js.map +1 -1
- package/dist/shapes/plane.js +70 -29
- package/dist/shapes/plane.js.map +1 -1
- package/dist/shapes/shape.js +120 -17
- package/dist/shapes/shape.js.map +1 -1
- package/dist/shapes/sphere.js +78 -44
- package/dist/shapes/sphere.js.map +1 -1
- package/dist/shapes/tetrahedron.js +256 -0
- package/dist/shapes/tetrahedron.js.map +1 -0
- package/dist/shapes/torus.js +76 -55
- package/dist/shapes/torus.js.map +1 -1
- package/dist/src/animation/animation.js +127 -0
- package/dist/src/animation/animation.js.map +1 -0
- package/dist/src/animation/animationset.js +255 -0
- package/dist/src/animation/animationset.js.map +1 -0
- package/dist/src/animation/animationtrack.js +34 -0
- package/dist/src/animation/animationtrack.js.map +1 -0
- package/dist/src/animation/eulerrotationtrack.js +52 -0
- package/dist/src/animation/eulerrotationtrack.js.map +1 -0
- package/dist/src/animation/morphtarget.js +93 -0
- package/dist/src/animation/morphtarget.js.map +1 -0
- package/dist/src/animation/morphtrack.js +70 -0
- package/dist/src/animation/morphtrack.js.map +1 -0
- package/dist/src/animation/proptrack.js +161 -0
- package/dist/src/animation/proptrack.js.map +1 -0
- package/dist/src/animation/rotationtrack.js +51 -0
- package/dist/src/animation/rotationtrack.js.map +1 -0
- package/dist/src/animation/scaletrack.js +50 -0
- package/dist/src/animation/scaletrack.js.map +1 -0
- package/dist/src/animation/skeleton.js +204 -0
- package/dist/src/animation/skeleton.js.map +1 -0
- package/dist/src/animation/translationtrack.js +50 -0
- package/dist/src/animation/translationtrack.js.map +1 -0
- package/dist/{app.js → src/app/app.js} +18 -40
- package/dist/src/app/app.js.map +1 -0
- package/dist/{input → src/app}/inputmgr.js +41 -16
- package/dist/src/app/inputmgr.js.map +1 -0
- package/dist/src/asset/assetmanager.js +404 -0
- package/dist/src/asset/assetmanager.js.map +1 -0
- package/dist/src/asset/builtin.js +337 -0
- package/dist/src/asset/builtin.js.map +1 -0
- package/dist/src/asset/loaders/dds/dds.js +470 -0
- package/dist/src/asset/loaders/dds/dds.js.map +1 -0
- package/dist/src/asset/loaders/dds/dds_loader.js +28 -0
- package/dist/src/asset/loaders/dds/dds_loader.js.map +1 -0
- package/dist/src/asset/loaders/gltf/gltf_loader.js +1265 -0
- package/dist/src/asset/loaders/gltf/gltf_loader.js.map +1 -0
- package/dist/src/asset/loaders/gltf/helpers.js +327 -0
- package/dist/src/asset/loaders/gltf/helpers.js.map +1 -0
- package/dist/src/asset/loaders/hdr/hdr.js +180 -0
- package/dist/src/asset/loaders/hdr/hdr.js.map +1 -0
- package/dist/src/asset/loaders/image/tga_Loader.js +116 -0
- package/dist/src/asset/loaders/image/tga_Loader.js.map +1 -0
- package/dist/src/asset/loaders/image/webimage_loader.js +63 -0
- package/dist/src/asset/loaders/image/webimage_loader.js.map +1 -0
- package/dist/src/asset/loaders/loader.js +45 -0
- package/dist/src/asset/loaders/loader.js.map +1 -0
- package/dist/src/asset/model.js +414 -0
- package/dist/src/asset/model.js.map +1 -0
- package/dist/{blitter/depthlimitedgaussion.js → src/blitter/bilateralblur.js} +1 -2
- package/dist/src/blitter/bilateralblur.js.map +1 -0
- package/dist/src/blitter/blitter.js +390 -0
- package/dist/src/blitter/blitter.js.map +1 -0
- package/dist/src/blitter/box.js +118 -0
- package/dist/src/blitter/box.js.map +1 -0
- package/dist/src/blitter/copy.js +22 -0
- package/dist/src/blitter/copy.js.map +1 -0
- package/dist/src/blitter/gaussianblur.js +228 -0
- package/dist/src/blitter/gaussianblur.js.map +1 -0
- package/dist/src/camera/base.js +92 -0
- package/dist/src/camera/base.js.map +1 -0
- package/dist/src/camera/camera.js +1005 -0
- package/dist/src/camera/camera.js.map +1 -0
- package/dist/src/camera/fps.js +238 -0
- package/dist/src/camera/fps.js.map +1 -0
- package/dist/src/camera/orbit.js +245 -0
- package/dist/src/camera/orbit.js.map +1 -0
- package/dist/src/camera/orthocamera.js +167 -0
- package/dist/src/camera/orthocamera.js.map +1 -0
- package/dist/src/camera/perspectivecamera.js +141 -0
- package/dist/src/camera/perspectivecamera.js.map +1 -0
- package/dist/src/index.js +120 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/material/blinn.js +81 -0
- package/dist/src/material/blinn.js.map +1 -0
- package/dist/src/material/grassmaterial.js +113 -0
- package/dist/src/material/grassmaterial.js.map +1 -0
- package/dist/src/material/lambert.js +92 -0
- package/dist/src/material/lambert.js.map +1 -0
- package/dist/src/material/material.js +301 -0
- package/dist/src/material/material.js.map +1 -0
- package/dist/src/material/meshmaterial.js +704 -0
- package/dist/src/material/meshmaterial.js.map +1 -0
- package/dist/src/material/mixins/albedocolor.js +76 -0
- package/dist/src/material/mixins/albedocolor.js.map +1 -0
- package/dist/src/material/mixins/foliage.js +47 -0
- package/dist/src/material/mixins/foliage.js.map +1 -0
- package/dist/src/material/mixins/lightmodel/blinnphong.js +112 -0
- package/dist/src/material/mixins/lightmodel/blinnphong.js.map +1 -0
- package/dist/src/material/mixins/lightmodel/lambert.js +58 -0
- package/dist/src/material/mixins/lightmodel/lambert.js.map +1 -0
- package/dist/src/material/mixins/lightmodel/pbrmetallicroughness.js +178 -0
- package/dist/src/material/mixins/lightmodel/pbrmetallicroughness.js.map +1 -0
- package/dist/src/material/mixins/lightmodel/pbrspecularglossness.js +139 -0
- package/dist/src/material/mixins/lightmodel/pbrspecularglossness.js.map +1 -0
- package/dist/src/material/mixins/lit.js +476 -0
- package/dist/src/material/mixins/lit.js.map +1 -0
- package/dist/src/material/mixins/pbr/common.js +918 -0
- package/dist/src/material/mixins/pbr/common.js.map +1 -0
- package/dist/src/material/mixins/texture.js +172 -0
- package/dist/src/material/mixins/texture.js.map +1 -0
- package/dist/src/material/mixins/vertexcolor.js +56 -0
- package/dist/src/material/mixins/vertexcolor.js.map +1 -0
- package/dist/src/material/particle.js +178 -0
- package/dist/src/material/particle.js.map +1 -0
- package/dist/src/material/pbrmr.js +97 -0
- package/dist/src/material/pbrmr.js.map +1 -0
- package/dist/src/material/pbrsg.js +97 -0
- package/dist/src/material/pbrsg.js.map +1 -0
- package/dist/src/material/shader/helper.js +1209 -0
- package/dist/src/material/shader/helper.js.map +1 -0
- package/dist/src/material/terrain-cm.js +606 -0
- package/dist/src/material/terrain-cm.js.map +1 -0
- package/dist/src/material/terrainmaterial.js +375 -0
- package/dist/src/material/terrainmaterial.js.map +1 -0
- package/dist/src/material/unlit.js +41 -0
- package/dist/src/material/unlit.js.map +1 -0
- package/dist/src/material/water.js +417 -0
- package/dist/src/material/water.js.map +1 -0
- package/dist/src/posteffect/bloom.js +361 -0
- package/dist/src/posteffect/bloom.js.map +1 -0
- package/dist/src/posteffect/compositor.js +226 -0
- package/dist/src/posteffect/compositor.js.map +1 -0
- package/dist/src/posteffect/fxaa.js +273 -0
- package/dist/src/posteffect/fxaa.js.map +1 -0
- package/dist/src/posteffect/grayscale.js +69 -0
- package/dist/src/posteffect/grayscale.js.map +1 -0
- package/dist/src/posteffect/motionblur.js +96 -0
- package/dist/src/posteffect/motionblur.js.map +1 -0
- package/dist/src/posteffect/posteffect.js +126 -0
- package/dist/src/posteffect/posteffect.js.map +1 -0
- package/dist/src/posteffect/sao.js +324 -0
- package/dist/src/posteffect/sao.js.map +1 -0
- package/dist/src/posteffect/ssr.js +489 -0
- package/dist/src/posteffect/ssr.js.map +1 -0
- package/dist/src/posteffect/taa.js +172 -0
- package/dist/src/posteffect/taa.js.map +1 -0
- package/dist/src/posteffect/tonemap.js +94 -0
- package/dist/src/posteffect/tonemap.js.map +1 -0
- package/dist/src/render/abuffer_oit.js +361 -0
- package/dist/src/render/abuffer_oit.js.map +1 -0
- package/dist/src/render/clipmap.js +851 -0
- package/dist/src/render/clipmap.js.map +1 -0
- package/dist/src/render/cluster_light.js +333 -0
- package/dist/src/render/cluster_light.js.map +1 -0
- package/dist/src/render/cull_visitor.js +187 -0
- package/dist/src/render/cull_visitor.js.map +1 -0
- package/dist/src/render/depthpass.js +68 -0
- package/dist/src/render/depthpass.js.map +1 -0
- package/dist/src/render/drawable_mixin.js +227 -0
- package/dist/src/render/drawable_mixin.js.map +1 -0
- package/dist/src/render/envlight.js +463 -0
- package/dist/src/render/envlight.js.map +1 -0
- package/dist/src/render/fbm_wavegenerator.js +251 -0
- package/dist/src/render/fbm_wavegenerator.js.map +1 -0
- package/dist/src/render/fft_wavegenerator.js +1006 -0
- package/dist/src/render/fft_wavegenerator.js.map +1 -0
- package/dist/src/render/fullscreenquad.js +38 -0
- package/dist/src/render/fullscreenquad.js.map +1 -0
- package/dist/src/render/gerstner_wavegenerator.js +314 -0
- package/dist/src/render/gerstner_wavegenerator.js.map +1 -0
- package/dist/src/render/globalbindgroup_allocator.js +60 -0
- package/dist/src/render/globalbindgroup_allocator.js.map +1 -0
- package/dist/src/render/hzb.js +273 -0
- package/dist/src/render/hzb.js.map +1 -0
- package/dist/src/render/lightpass.js +172 -0
- package/dist/src/render/lightpass.js.map +1 -0
- package/dist/src/render/objectcolorpass.js +51 -0
- package/dist/src/render/objectcolorpass.js.map +1 -0
- package/dist/src/render/primitive.js +364 -0
- package/dist/src/render/primitive.js.map +1 -0
- package/dist/src/render/render_queue.js +467 -0
- package/dist/src/render/render_queue.js.map +1 -0
- package/dist/src/render/renderbundle_wrapper.js +152 -0
- package/dist/src/render/renderbundle_wrapper.js.map +1 -0
- package/dist/src/render/renderer.js +455 -0
- package/dist/src/render/renderer.js.map +1 -0
- package/dist/src/render/renderpass.js +200 -0
- package/dist/src/render/renderpass.js.map +1 -0
- package/dist/src/render/shadowmap_pass.js +56 -0
- package/dist/src/render/shadowmap_pass.js.map +1 -0
- package/dist/src/render/sky.js +1103 -0
- package/dist/src/render/sky.js.map +1 -0
- package/dist/src/render/weightedblended_oit.js +168 -0
- package/dist/src/render/weightedblended_oit.js.map +1 -0
- package/dist/src/scene/batchgroup.js +162 -0
- package/dist/src/scene/batchgroup.js.map +1 -0
- package/dist/src/scene/environment.js +209 -0
- package/dist/src/scene/environment.js.map +1 -0
- package/dist/src/scene/graph_node.js +72 -0
- package/dist/src/scene/graph_node.js.map +1 -0
- package/dist/src/scene/light.js +416 -0
- package/dist/src/scene/light.js.map +1 -0
- package/dist/src/scene/mesh.js +341 -0
- package/dist/src/scene/mesh.js.map +1 -0
- package/dist/src/scene/octree.js +649 -0
- package/dist/src/scene/octree.js.map +1 -0
- package/dist/src/scene/particlesys.js +738 -0
- package/dist/src/scene/particlesys.js.map +1 -0
- package/dist/src/scene/raycast_visitor.js +103 -0
- package/dist/src/scene/raycast_visitor.js.map +1 -0
- package/dist/src/scene/scene.js +284 -0
- package/dist/src/scene/scene.js.map +1 -0
- package/dist/src/scene/scene_node.js +732 -0
- package/dist/src/scene/scene_node.js.map +1 -0
- package/dist/src/scene/terrain/grass.js +278 -0
- package/dist/src/scene/terrain/grass.js.map +1 -0
- package/dist/src/scene/terrain/heightfield.js +475 -0
- package/dist/src/scene/terrain/heightfield.js.map +1 -0
- package/dist/src/scene/terrain/patch.js +530 -0
- package/dist/src/scene/terrain/patch.js.map +1 -0
- package/dist/src/scene/terrain/quadtree.js +461 -0
- package/dist/src/scene/terrain/quadtree.js.map +1 -0
- package/dist/src/scene/terrain/terrain.js +246 -0
- package/dist/src/scene/terrain/terrain.js.map +1 -0
- package/dist/src/scene/terrain-cm/grass.js +594 -0
- package/dist/src/scene/terrain-cm/grass.js.map +1 -0
- package/dist/src/scene/terrain-cm/grassmaterial.js +159 -0
- package/dist/src/scene/terrain-cm/grassmaterial.js.map +1 -0
- package/dist/src/scene/terrain-cm/terrain-cm.js +538 -0
- package/dist/src/scene/terrain-cm/terrain-cm.js.map +1 -0
- package/dist/src/scene/water.js +374 -0
- package/dist/src/scene/water.js.map +1 -0
- package/dist/src/shaders/atmosphere.js +957 -0
- package/dist/src/shaders/atmosphere.js.map +1 -0
- package/dist/src/shaders/fog.js +112 -0
- package/dist/src/shaders/fog.js.map +1 -0
- package/dist/src/shaders/misc.js +266 -0
- package/dist/src/shaders/misc.js.map +1 -0
- package/dist/src/shaders/noise.js +222 -0
- package/dist/src/shaders/noise.js.map +1 -0
- package/dist/src/shaders/pbr.js +51 -0
- package/dist/src/shaders/pbr.js.map +1 -0
- package/dist/src/shaders/shadow.js +636 -0
- package/dist/src/shaders/shadow.js.map +1 -0
- package/dist/src/shaders/ssr.js +490 -0
- package/dist/src/shaders/ssr.js.map +1 -0
- package/dist/src/shaders/temporal.js +215 -0
- package/dist/src/shaders/temporal.js.map +1 -0
- package/dist/src/shaders/water.js +756 -0
- package/dist/src/shaders/water.js.map +1 -0
- package/dist/src/shadow/esm.js +237 -0
- package/dist/src/shadow/esm.js.map +1 -0
- package/dist/src/shadow/pcf_opt.js +181 -0
- package/dist/src/shadow/pcf_opt.js.map +1 -0
- package/dist/src/shadow/pcf_pd.js +189 -0
- package/dist/src/shadow/pcf_pd.js.map +1 -0
- package/dist/src/shadow/shader.js +37 -0
- package/dist/src/shadow/shader.js.map +1 -0
- package/dist/src/shadow/shadow_impl.js +15 -0
- package/dist/src/shadow/shadow_impl.js.map +1 -0
- package/dist/src/shadow/shadowmapper.js +790 -0
- package/dist/src/shadow/shadowmapper.js.map +1 -0
- package/dist/src/shadow/ssm.js +159 -0
- package/dist/src/shadow/ssm.js.map +1 -0
- package/dist/src/shadow/vsm.js +297 -0
- package/dist/src/shadow/vsm.js.map +1 -0
- package/dist/src/shapes/box.js +386 -0
- package/dist/src/shapes/box.js.map +1 -0
- package/dist/src/shapes/cylinder.js +125 -0
- package/dist/src/shapes/cylinder.js.map +1 -0
- package/dist/src/shapes/plane.js +88 -0
- package/dist/src/shapes/plane.js.map +1 -0
- package/dist/src/shapes/shape.js +87 -0
- package/dist/src/shapes/shape.js.map +1 -0
- package/dist/src/shapes/sphere.js +114 -0
- package/dist/src/shapes/sphere.js.map +1 -0
- package/dist/src/shapes/tetrahedron.js +188 -0
- package/dist/src/shapes/tetrahedron.js.map +1 -0
- package/dist/src/shapes/torus.js +111 -0
- package/dist/src/shapes/torus.js.map +1 -0
- package/dist/src/utility/aabbtree.js +400 -0
- package/dist/src/utility/aabbtree.js.map +1 -0
- package/dist/src/utility/bounding_volume.js +29 -0
- package/dist/src/utility/bounding_volume.js.map +1 -0
- package/dist/src/utility/debug.js +28 -0
- package/dist/src/utility/debug.js.map +1 -0
- package/dist/src/utility/draco/decoder.js +116 -0
- package/dist/src/utility/draco/decoder.js.map +1 -0
- package/dist/src/utility/misc.js +105 -0
- package/dist/src/utility/misc.js.map +1 -0
- package/dist/src/utility/panorama.js +163 -0
- package/dist/src/utility/panorama.js.map +1 -0
- package/dist/src/utility/pmrem.js +354 -0
- package/dist/src/utility/pmrem.js.map +1 -0
- package/dist/src/utility/rendermipmap.js +115 -0
- package/dist/src/utility/rendermipmap.js.map +1 -0
- package/dist/src/utility/serialization/json.js +402 -0
- package/dist/src/utility/serialization/json.js.map +1 -0
- package/dist/src/utility/serialization/manager.js +623 -0
- package/dist/src/utility/serialization/manager.js.map +1 -0
- package/dist/src/utility/serialization/scene/animation.js +248 -0
- package/dist/src/utility/serialization/scene/animation.js.map +1 -0
- package/dist/src/utility/serialization/scene/batch.js +59 -0
- package/dist/src/utility/serialization/scene/batch.js.map +1 -0
- package/dist/src/utility/serialization/scene/camera.js +790 -0
- package/dist/src/utility/serialization/scene/camera.js.map +1 -0
- package/dist/src/utility/serialization/scene/common.js +222 -0
- package/dist/src/utility/serialization/scene/common.js.map +1 -0
- package/dist/src/utility/serialization/scene/light.js +575 -0
- package/dist/src/utility/serialization/scene/light.js.map +1 -0
- package/dist/src/utility/serialization/scene/material.js +1111 -0
- package/dist/src/utility/serialization/scene/material.js.map +1 -0
- package/dist/src/utility/serialization/scene/mesh.js +148 -0
- package/dist/src/utility/serialization/scene/mesh.js.map +1 -0
- package/dist/src/utility/serialization/scene/misc.js +39 -0
- package/dist/src/utility/serialization/scene/misc.js.map +1 -0
- package/dist/src/utility/serialization/scene/node.js +451 -0
- package/dist/src/utility/serialization/scene/node.js.map +1 -0
- package/dist/src/utility/serialization/scene/particle.js +425 -0
- package/dist/src/utility/serialization/scene/particle.js.map +1 -0
- package/dist/src/utility/serialization/scene/primitive.js +692 -0
- package/dist/src/utility/serialization/scene/primitive.js.map +1 -0
- package/dist/src/utility/serialization/scene/scene.js +704 -0
- package/dist/src/utility/serialization/scene/scene.js.map +1 -0
- package/dist/src/utility/serialization/scene/terrain.js +488 -0
- package/dist/src/utility/serialization/scene/terrain.js.map +1 -0
- package/dist/src/utility/serialization/scene/water.js +465 -0
- package/dist/src/utility/serialization/scene/water.js.map +1 -0
- package/dist/src/utility/shprojector.js +297 -0
- package/dist/src/utility/shprojector.js.map +1 -0
- package/dist/{material/mixins → src/utility/textures}/ggxlut.js +1 -1
- package/dist/src/utility/textures/ggxlut.js.map +1 -0
- package/dist/src/utility/textures/gradientnoise.js +62 -0
- package/dist/src/utility/textures/gradientnoise.js.map +1 -0
- package/dist/src/utility/textures/randomnoise.js +41 -0
- package/dist/src/utility/textures/randomnoise.js.map +1 -0
- package/dist/src/values.js +162 -0
- package/dist/src/values.js.map +1 -0
- package/dist/utility/aabbtree.js +15 -5
- package/dist/utility/aabbtree.js.map +1 -1
- package/dist/utility/blueprint/common/constants.js +851 -0
- package/dist/utility/blueprint/common/constants.js.map +1 -0
- package/dist/utility/blueprint/common/math.js +2045 -0
- package/dist/utility/blueprint/common/math.js.map +1 -0
- package/dist/utility/blueprint/common.js +7 -0
- package/dist/utility/blueprint/common.js.map +1 -0
- package/dist/utility/blueprint/material/common.js +7 -0
- package/dist/utility/blueprint/material/common.js.map +1 -0
- package/dist/utility/blueprint/material/func.js +521 -0
- package/dist/utility/blueprint/material/func.js.map +1 -0
- package/dist/utility/blueprint/material/inputs.js +1659 -0
- package/dist/utility/blueprint/material/inputs.js.map +1 -0
- package/dist/utility/blueprint/material/ir.js +1311 -0
- package/dist/utility/blueprint/material/ir.js.map +1 -0
- package/dist/utility/blueprint/material/pbr.js +362 -0
- package/dist/utility/blueprint/material/pbr.js.map +1 -0
- package/dist/utility/blueprint/material/texture.js +1099 -0
- package/dist/utility/blueprint/material/texture.js.map +1 -0
- package/dist/utility/blueprint/node.js +187 -0
- package/dist/utility/blueprint/node.js.map +1 -0
- package/dist/utility/bounding_volume.js +0 -1
- package/dist/utility/bounding_volume.js.map +1 -1
- package/dist/utility/debug.js +26 -0
- package/dist/utility/debug.js.map +1 -0
- package/dist/utility/draco/decoder.js +1 -1
- package/dist/utility/draco/decoder.js.map +1 -1
- package/dist/utility/misc.js +18 -6
- package/dist/utility/misc.js.map +1 -1
- package/dist/utility/panorama.js +8 -6
- package/dist/utility/panorama.js.map +1 -1
- package/dist/utility/pmrem.js +30 -18
- package/dist/utility/pmrem.js.map +1 -1
- package/dist/utility/rendermipmap.js +116 -0
- package/dist/utility/rendermipmap.js.map +1 -0
- package/dist/utility/serialization/blueprint/constants.js +255 -0
- package/dist/utility/serialization/blueprint/constants.js.map +1 -0
- package/dist/utility/serialization/blueprint/material/constants.js +203 -0
- package/dist/utility/serialization/blueprint/material/constants.js.map +1 -0
- package/dist/utility/serialization/blueprint/material/texture.js +165 -0
- package/dist/utility/serialization/blueprint/material/texture.js.map +1 -0
- package/dist/utility/serialization/json.js +402 -0
- package/dist/utility/serialization/json.js.map +1 -0
- package/dist/utility/serialization/manager.js +1176 -0
- package/dist/utility/serialization/manager.js.map +1 -0
- package/dist/utility/serialization/scene/animation.js +699 -0
- package/dist/utility/serialization/scene/animation.js.map +1 -0
- package/dist/utility/serialization/scene/batch.js +53 -0
- package/dist/utility/serialization/scene/batch.js.map +1 -0
- package/dist/utility/serialization/scene/camera.js +808 -0
- package/dist/utility/serialization/scene/camera.js.map +1 -0
- package/dist/utility/serialization/scene/common.js +224 -0
- package/dist/utility/serialization/scene/common.js.map +1 -0
- package/dist/utility/serialization/scene/light.js +589 -0
- package/dist/utility/serialization/scene/light.js.map +1 -0
- package/dist/utility/serialization/scene/material.js +942 -0
- package/dist/utility/serialization/scene/material.js.map +1 -0
- package/dist/utility/serialization/scene/mesh.js +374 -0
- package/dist/utility/serialization/scene/mesh.js.map +1 -0
- package/dist/utility/serialization/scene/misc.js +39 -0
- package/dist/utility/serialization/scene/misc.js.map +1 -0
- package/dist/utility/serialization/scene/node.js +391 -0
- package/dist/utility/serialization/scene/node.js.map +1 -0
- package/dist/utility/serialization/scene/particle.js +425 -0
- package/dist/utility/serialization/scene/particle.js.map +1 -0
- package/dist/utility/serialization/scene/primitive.js +582 -0
- package/dist/utility/serialization/scene/primitive.js.map +1 -0
- package/dist/utility/serialization/scene/scene.js +763 -0
- package/dist/utility/serialization/scene/scene.js.map +1 -0
- package/dist/utility/serialization/scene/terrain.js +495 -0
- package/dist/utility/serialization/scene/terrain.js.map +1 -0
- package/dist/utility/serialization/scene/water.js +465 -0
- package/dist/utility/serialization/scene/water.js.map +1 -0
- package/dist/utility/shprojector.js +313 -0
- package/dist/utility/shprojector.js.map +1 -0
- package/dist/utility/textures/ggxlut.js +4 -5
- package/dist/utility/textures/ggxlut.js.map +1 -1
- package/dist/utility/textures/gradientnoise.js +2 -1
- package/dist/utility/textures/gradientnoise.js.map +1 -1
- package/dist/utility/textures/randomnoise.js +1 -0
- package/dist/utility/textures/randomnoise.js.map +1 -1
- package/dist/values.js +91 -4
- package/dist/values.js.map +1 -1
- package/package.json +29 -15
- package/dist/app.js.map +0 -1
- package/dist/blitter/depthlimitedgaussion.js.map +0 -1
- package/dist/input/inputmgr.js.map +0 -1
- package/dist/material/mixins/ggxlut.js.map +0 -1
- package/dist/posteffect/water.js +0 -508
- package/dist/posteffect/water.js.map +0 -1
- package/dist/render/oit.js +0 -16
- package/dist/render/oit.js.map +0 -1
- package/dist/render/scatteringlut.js +0 -634
- package/dist/render/scatteringlut.js.map +0 -1
- package/dist/render/watermesh.js +0 -193
- package/dist/render/watermesh.js.map +0 -1
- package/dist/render/wavegenerator.js +0 -8
- package/dist/render/wavegenerator.js.map +0 -1
- package/dist/scene/xform.js +0 -247
- package/dist/scene/xform.js.map +0 -1
- package/dist/utility/shprojection.js +0 -442
- package/dist/utility/shprojection.js.map +0 -1
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
import { HttpFS } from '@zephyr3d/base';
|
|
2
|
+
import { ScriptRegistry } from './scriptregistry.js';
|
|
3
|
+
import { RuntimeScript } from './runtimescript.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Script system that resolves, loads, and manages lifecycle of runtime scripts.
|
|
7
|
+
*
|
|
8
|
+
* Responsibilities:
|
|
9
|
+
* - Resolves module IDs to URLs via {@link ScriptRegistry}
|
|
10
|
+
* - Dynamically imports modules and instantiates a `RuntimeScript` (default export)
|
|
11
|
+
* - Tracks attachments between hosts and script instances
|
|
12
|
+
* - Bridges script lifecycle hooks: `onCreated`, `onAttached`, `onDetached`, `onDestroy`, `onUpdate`
|
|
13
|
+
* - Auto-detaches scripts when a host is disposed
|
|
14
|
+
*
|
|
15
|
+
* Notes:
|
|
16
|
+
* - Multiple hosts can reference the same `RuntimeScript` instance; destruction
|
|
17
|
+
* occurs when the last host detaches.
|
|
18
|
+
* - Errors during load/attach/update are caught and logged; an optional
|
|
19
|
+
* `onLoadError` callback can be provided.
|
|
20
|
+
*
|
|
21
|
+
* @public
|
|
22
|
+
*/ class ScriptingSystem {
|
|
23
|
+
_registry;
|
|
24
|
+
_hostScripts;
|
|
25
|
+
_scriptHosts;
|
|
26
|
+
_onLoadError;
|
|
27
|
+
_importComment;
|
|
28
|
+
/**
|
|
29
|
+
* Constructs a new scripting system.
|
|
30
|
+
*
|
|
31
|
+
* @param opts - Optional configuration.
|
|
32
|
+
*/ constructor(opts = {}){
|
|
33
|
+
this._registry = new ScriptRegistry(opts.VFS ?? new HttpFS('./'), opts.scriptsRoot ?? '/', opts.editorMode ?? false);
|
|
34
|
+
this._hostScripts = new Map();
|
|
35
|
+
this._scriptHosts = new Map();
|
|
36
|
+
this._importComment = opts.importComment;
|
|
37
|
+
this._onLoadError = opts.onLoadError;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Accessor for the underlying script registry used for module resolution.
|
|
41
|
+
*/ get registry() {
|
|
42
|
+
return this._registry;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Attaches a script to a host and returns the `RuntimeScript` instance.
|
|
46
|
+
*
|
|
47
|
+
* Process:
|
|
48
|
+
* 1. Resolve module ID to a runtime URL via the registry.
|
|
49
|
+
* 2. Dynamically import the module.
|
|
50
|
+
* 3. Instantiate the default export if it is a constructor.
|
|
51
|
+
* 4. If this is the first time the instance is seen, call `onCreated()`.
|
|
52
|
+
* 5. Link the instance to the host, and call `onAttached(host)`.
|
|
53
|
+
* 6. Subscribe to the host's `'dispose'` event to auto-detach.
|
|
54
|
+
*
|
|
55
|
+
* If the module cannot be resolved or does not export a default `RuntimeScript`
|
|
56
|
+
* subclass, a warning is logged and `null` is returned.
|
|
57
|
+
*
|
|
58
|
+
* @typeParam T - Host type.
|
|
59
|
+
* @param host - The host object to attach the script to.
|
|
60
|
+
* @param module - Module identifier used by the registry (logical ID or path).
|
|
61
|
+
* @returns The instantiated `RuntimeScript<T>` or `null` on failure.
|
|
62
|
+
*/ async attachScript(host, module) {
|
|
63
|
+
try {
|
|
64
|
+
const url = await this._registry.resolveRuntimeUrl(module);
|
|
65
|
+
if (!url) {
|
|
66
|
+
return null;
|
|
67
|
+
}
|
|
68
|
+
const mod = await import(url + (this._importComment ?? ''));
|
|
69
|
+
let instance = null;
|
|
70
|
+
if (typeof mod?.default === 'function') {
|
|
71
|
+
// default export
|
|
72
|
+
instance = new mod.default();
|
|
73
|
+
if (instance instanceof RuntimeScript) {
|
|
74
|
+
if (!this._scriptHosts.has(instance)) {
|
|
75
|
+
const P = instance.onCreated();
|
|
76
|
+
if (P instanceof Promise) {
|
|
77
|
+
await P;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
} else {
|
|
81
|
+
instance = null;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
if (!instance) {
|
|
85
|
+
console.warn(`Script '${module}' does not have RuntimeScript class exported as default`);
|
|
86
|
+
return null;
|
|
87
|
+
}
|
|
88
|
+
let hostList = this._scriptHosts.get(instance);
|
|
89
|
+
if (!hostList) {
|
|
90
|
+
hostList = [];
|
|
91
|
+
this._scriptHosts.set(instance, hostList);
|
|
92
|
+
}
|
|
93
|
+
if (hostList.includes(host)) {
|
|
94
|
+
console.warn(`Script '${module}' already attached`);
|
|
95
|
+
return instance;
|
|
96
|
+
}
|
|
97
|
+
hostList.push(host);
|
|
98
|
+
const P = instance.onAttached(host);
|
|
99
|
+
if (P instanceof Promise) {
|
|
100
|
+
await P;
|
|
101
|
+
}
|
|
102
|
+
const attached = {
|
|
103
|
+
id: module,
|
|
104
|
+
url,
|
|
105
|
+
instance
|
|
106
|
+
};
|
|
107
|
+
let list = this._hostScripts.get(host);
|
|
108
|
+
if (!list) {
|
|
109
|
+
list = [];
|
|
110
|
+
this._hostScripts.set(host, list);
|
|
111
|
+
if (host) {
|
|
112
|
+
host.on('dispose', ()=>{
|
|
113
|
+
this.detachScript(host);
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
list.push(attached);
|
|
118
|
+
return attached.instance;
|
|
119
|
+
} catch (e) {
|
|
120
|
+
const moduleName = module ? String(module).slice(0, 64) : '';
|
|
121
|
+
console.error(`Load module '${moduleName}' failed: ${e}`);
|
|
122
|
+
this._onLoadError?.(e, module);
|
|
123
|
+
return null;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Detaches script(s) from a host.
|
|
128
|
+
*
|
|
129
|
+
* Behavior:
|
|
130
|
+
* - If `idOrInstance` is omitted, detaches all scripts from the host.
|
|
131
|
+
* - If a module ID is provided, detaches only the matching script.
|
|
132
|
+
* - If a `RuntimeScript` instance is provided, detaches that instance.
|
|
133
|
+
* - Invokes `onDetached(host)` on each detached instance.
|
|
134
|
+
* - If the instance has no remaining hosts, invokes `onDestroy()` and disposes tracking.
|
|
135
|
+
*
|
|
136
|
+
* @typeParam T - Host type.
|
|
137
|
+
* @param host - The host to detach from.
|
|
138
|
+
* @param idOrInstance - Optional module ID or script instance to target.
|
|
139
|
+
*/ detachScript(host, idOrInstance) {
|
|
140
|
+
const list = this._hostScripts.get(host);
|
|
141
|
+
if (!list || list.length === 0) {
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
for(let i = list.length - 1; i >= 0; i--){
|
|
145
|
+
const it = list[i];
|
|
146
|
+
const hit = !idOrInstance || typeof idOrInstance === 'string' ? it.id === idOrInstance : it.instance === idOrInstance;
|
|
147
|
+
if (hit) {
|
|
148
|
+
list.splice(i, 1);
|
|
149
|
+
try {
|
|
150
|
+
it.instance.onDetached(host);
|
|
151
|
+
} catch (err) {
|
|
152
|
+
console.error(`Error occured at onDetach() of module '${it.id}': ${err}`);
|
|
153
|
+
}
|
|
154
|
+
const hostList = this._scriptHosts.get(it.instance);
|
|
155
|
+
const index = hostList.indexOf(host);
|
|
156
|
+
if (index >= 0) {
|
|
157
|
+
hostList.splice(index, 1);
|
|
158
|
+
}
|
|
159
|
+
if (hostList.length === 0) {
|
|
160
|
+
try {
|
|
161
|
+
it.instance.onDestroy();
|
|
162
|
+
} catch (err) {
|
|
163
|
+
console.error(`Error occured at onDestroy() of module '${it.id}': ${err}`);
|
|
164
|
+
} finally{
|
|
165
|
+
this._scriptHosts.delete(it.instance);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
if (list.length === 0) {
|
|
171
|
+
this._hostScripts.delete(host);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Get all script instances attached to a host.
|
|
176
|
+
*
|
|
177
|
+
* @typeParam T - Expected script type.
|
|
178
|
+
* @param host - The host whose scripts to retrieve.
|
|
179
|
+
* @returns Script instances attached to the host, or an empty array if none.
|
|
180
|
+
*/ getScriptObjects(host) {
|
|
181
|
+
return this._hostScripts.get(host) || [];
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Ticks all attached script instances.
|
|
185
|
+
*
|
|
186
|
+
* Calls `onUpdate(deltaTime, elapsedTime)` on every attached script instance
|
|
187
|
+
* across all hosts. Exceptions thrown by a script are caught and logged,
|
|
188
|
+
* allowing other scripts to continue updating.
|
|
189
|
+
*
|
|
190
|
+
* @param deltaTime - Time in seconds since last update.
|
|
191
|
+
* @param elapsedTime - Total time in seconds since start.
|
|
192
|
+
*/ update(deltaTime, elapsedTime) {
|
|
193
|
+
if (this._hostScripts.size === 0) {
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
for (const list of this._hostScripts.values()){
|
|
197
|
+
for (const s of list){
|
|
198
|
+
try {
|
|
199
|
+
s.instance.onUpdate(deltaTime, elapsedTime);
|
|
200
|
+
} catch (err) {
|
|
201
|
+
console.error(`Error occured at onUpdate() of module '${s.id}': ${err}`);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Detaches all scripts from all hosts.
|
|
208
|
+
*
|
|
209
|
+
* Iteratively calls {@link ScriptingSystem.detachScript} on each host until no attachments remain.
|
|
210
|
+
*/ detachAllScripts() {
|
|
211
|
+
while(this._hostScripts.size > 0){
|
|
212
|
+
for (const entry of this._hostScripts){
|
|
213
|
+
this.detachScript(entry[0]);
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
export { ScriptingSystem };
|
|
220
|
+
//# sourceMappingURL=scriptingsystem.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"scriptingsystem.js","sources":["../../src/app/scriptingsystem.ts"],"sourcesContent":["import type { IDisposable, VFS } from '@zephyr3d/base';\r\nimport { HttpFS } from '@zephyr3d/base';\r\nimport { ScriptRegistry } from './scriptregistry';\r\nimport { RuntimeScript } from './runtimescript';\r\n\r\n/**\r\n * A host object that supports disposal.\r\n *\r\n * Hosts are objects to which scripts can be attached. They are expected to\r\n * emit a `'dispose'` event compatible with `IDisposable` so that scripts\r\n * can be automatically detached when the host is destroyed.\r\n *\r\n * @public\r\n */\r\nexport type Host = IDisposable;\r\n\r\n/**\r\n * Options for configuring a `ScriptingSystem`.\r\n *\r\n * @public\r\n */\r\nexport type ScriptingSystemOptions = {\r\n /** Virtual file system used by the script registry. Defaults to `new HttpFS('./')`. */\r\n VFS?: VFS;\r\n /** Root path for scripts within the VFS. Defaults to `/`. */\r\n scriptsRoot?: string;\r\n /** If true, enables editor-oriented behaviors in the registry. Defaults to `false`. */\r\n editorMode?: boolean;\r\n /**\r\n * Optional string appended to dynamic import URLs (e.g., for cache busting).\r\n * Example: `'?v=' + Date.now()`\r\n */\r\n importComment?: string;\r\n /**\r\n * Optional callback invoked when a module fails to load.\r\n * @param e - The error that occurred.\r\n * @param id - The module ID that failed to load.\r\n */\r\n onLoadError?: (e: unknown, id: string) => void;\r\n};\r\n\r\n/**\r\n * Information about a script attached to a host.\r\n *\r\n * @public\r\n */\r\nexport interface IAttachedScript {\r\n /** The logical module identifier used to resolve the script. */\r\n id: string;\r\n /** The resolved runtime URL used for dynamic import. */\r\n url: string;\r\n /** The instantiated runtime script. */\r\n instance: RuntimeScript<any>;\r\n}\r\n\r\n/**\r\n * Script system that resolves, loads, and manages lifecycle of runtime scripts.\r\n *\r\n * Responsibilities:\r\n * - Resolves module IDs to URLs via {@link ScriptRegistry}\r\n * - Dynamically imports modules and instantiates a `RuntimeScript` (default export)\r\n * - Tracks attachments between hosts and script instances\r\n * - Bridges script lifecycle hooks: `onCreated`, `onAttached`, `onDetached`, `onDestroy`, `onUpdate`\r\n * - Auto-detaches scripts when a host is disposed\r\n *\r\n * Notes:\r\n * - Multiple hosts can reference the same `RuntimeScript` instance; destruction\r\n * occurs when the last host detaches.\r\n * - Errors during load/attach/update are caught and logged; an optional\r\n * `onLoadError` callback can be provided.\r\n *\r\n * @public\r\n */\r\nexport class ScriptingSystem {\r\n private _registry: ScriptRegistry;\r\n private _hostScripts: Map<Host, IAttachedScript[]>;\r\n private _scriptHosts: Map<RuntimeScript<any>, Host[]>;\r\n private _onLoadError?: (e: unknown, id: string) => void;\r\n private _importComment?: string;\r\n\r\n /**\r\n * Constructs a new scripting system.\r\n *\r\n * @param opts - Optional configuration.\r\n */\r\n constructor(opts: ScriptingSystemOptions = {}) {\r\n this._registry = new ScriptRegistry(\r\n opts.VFS ?? new HttpFS('./'),\r\n opts.scriptsRoot ?? '/',\r\n opts.editorMode ?? false\r\n );\r\n this._hostScripts = new Map();\r\n this._scriptHosts = new Map();\r\n this._importComment = opts.importComment;\r\n this._onLoadError = opts.onLoadError;\r\n }\r\n\r\n /**\r\n * Accessor for the underlying script registry used for module resolution.\r\n */\r\n get registry() {\r\n return this._registry;\r\n }\r\n\r\n /**\r\n * Attaches a script to a host and returns the `RuntimeScript` instance.\r\n *\r\n * Process:\r\n * 1. Resolve module ID to a runtime URL via the registry.\r\n * 2. Dynamically import the module.\r\n * 3. Instantiate the default export if it is a constructor.\r\n * 4. If this is the first time the instance is seen, call `onCreated()`.\r\n * 5. Link the instance to the host, and call `onAttached(host)`.\r\n * 6. Subscribe to the host's `'dispose'` event to auto-detach.\r\n *\r\n * If the module cannot be resolved or does not export a default `RuntimeScript`\r\n * subclass, a warning is logged and `null` is returned.\r\n *\r\n * @typeParam T - Host type.\r\n * @param host - The host object to attach the script to.\r\n * @param module - Module identifier used by the registry (logical ID or path).\r\n * @returns The instantiated `RuntimeScript<T>` or `null` on failure.\r\n */\r\n async attachScript<T extends Host>(host: T, module: string): Promise<RuntimeScript<T>> {\r\n try {\r\n const url = await this._registry.resolveRuntimeUrl(module);\r\n if (!url) {\r\n return null;\r\n }\r\n const mod = await import(url + (this._importComment ?? ''));\r\n let instance: RuntimeScript<T> = null;\r\n if (typeof mod?.default === 'function') {\r\n // default export\r\n instance = new mod.default();\r\n if (instance instanceof RuntimeScript) {\r\n if (!this._scriptHosts.has(instance)) {\r\n const P = instance.onCreated();\r\n if (P instanceof Promise) {\r\n await P;\r\n }\r\n }\r\n } else {\r\n instance = null;\r\n }\r\n }\r\n if (!instance) {\r\n console.warn(`Script '${module}' does not have RuntimeScript class exported as default`);\r\n return null;\r\n }\r\n\r\n let hostList = this._scriptHosts.get(instance);\r\n if (!hostList) {\r\n hostList = [];\r\n this._scriptHosts.set(instance, hostList);\r\n }\r\n if (hostList.includes(host)) {\r\n console.warn(`Script '${module}' already attached`);\r\n return instance;\r\n }\r\n hostList.push(host);\r\n\r\n const P = instance.onAttached(host);\r\n if (P instanceof Promise) {\r\n await P;\r\n }\r\n\r\n const attached: IAttachedScript = {\r\n id: module,\r\n url,\r\n instance\r\n };\r\n\r\n let list = this._hostScripts.get(host);\r\n if (!list) {\r\n list = [];\r\n this._hostScripts.set(host, list);\r\n if (host) {\r\n host.on('dispose', () => {\r\n this.detachScript(host);\r\n });\r\n }\r\n }\r\n list.push(attached);\r\n\r\n return attached.instance;\r\n } catch (e) {\r\n const moduleName = module ? String(module).slice(0, 64) : '';\r\n console.error(`Load module '${moduleName}' failed: ${e}`);\r\n this._onLoadError?.(e, module);\r\n return null;\r\n }\r\n }\r\n\r\n /**\r\n * Detaches script(s) from a host.\r\n *\r\n * Behavior:\r\n * - If `idOrInstance` is omitted, detaches all scripts from the host.\r\n * - If a module ID is provided, detaches only the matching script.\r\n * - If a `RuntimeScript` instance is provided, detaches that instance.\r\n * - Invokes `onDetached(host)` on each detached instance.\r\n * - If the instance has no remaining hosts, invokes `onDestroy()` and disposes tracking.\r\n *\r\n * @typeParam T - Host type.\r\n * @param host - The host to detach from.\r\n * @param idOrInstance - Optional module ID or script instance to target.\r\n */\r\n detachScript<T extends Host>(host: T, idOrInstance?: string | RuntimeScript<T>) {\r\n const list = this._hostScripts.get(host);\r\n if (!list || list.length === 0) {\r\n return;\r\n }\r\n for (let i = list.length - 1; i >= 0; i--) {\r\n const it = list[i];\r\n const hit =\r\n !idOrInstance || typeof idOrInstance === 'string'\r\n ? it.id === idOrInstance\r\n : it.instance === idOrInstance;\r\n if (hit) {\r\n list.splice(i, 1);\r\n try {\r\n it.instance.onDetached(host);\r\n } catch (err) {\r\n console.error(`Error occured at onDetach() of module '${it.id}': ${err}`);\r\n }\r\n\r\n const hostList = this._scriptHosts.get(it.instance);\r\n const index = hostList.indexOf(host);\r\n if (index >= 0) {\r\n hostList.splice(index, 1);\r\n }\r\n\r\n if (hostList.length === 0) {\r\n try {\r\n it.instance.onDestroy();\r\n } catch (err) {\r\n console.error(`Error occured at onDestroy() of module '${it.id}': ${err}`);\r\n } finally {\r\n this._scriptHosts.delete(it.instance);\r\n }\r\n }\r\n }\r\n }\r\n if (list.length === 0) {\r\n this._hostScripts.delete(host);\r\n }\r\n }\r\n\r\n /**\r\n * Get all script instances attached to a host.\r\n *\r\n * @typeParam T - Expected script type.\r\n * @param host - The host whose scripts to retrieve.\r\n * @returns Script instances attached to the host, or an empty array if none.\r\n */\r\n getScriptObjects<T extends RuntimeScript<any>>(host: unknown): T[] {\r\n return (this._hostScripts.get(host as Host) as unknown as T[]) || [];\r\n }\r\n\r\n /**\r\n * Ticks all attached script instances.\r\n *\r\n * Calls `onUpdate(deltaTime, elapsedTime)` on every attached script instance\r\n * across all hosts. Exceptions thrown by a script are caught and logged,\r\n * allowing other scripts to continue updating.\r\n *\r\n * @param deltaTime - Time in seconds since last update.\r\n * @param elapsedTime - Total time in seconds since start.\r\n */\r\n update(deltaTime: number, elapsedTime: number): void {\r\n if (this._hostScripts.size === 0) {\r\n return;\r\n }\r\n for (const list of this._hostScripts.values()) {\r\n for (const s of list) {\r\n try {\r\n s.instance.onUpdate(deltaTime, elapsedTime);\r\n } catch (err) {\r\n console.error(`Error occured at onUpdate() of module '${s.id}': ${err}`);\r\n }\r\n }\r\n }\r\n }\r\n\r\n /**\r\n * Detaches all scripts from all hosts.\r\n *\r\n * Iteratively calls {@link ScriptingSystem.detachScript} on each host until no attachments remain.\r\n */\r\n detachAllScripts() {\r\n while (this._hostScripts.size > 0) {\r\n for (const entry of this._hostScripts) {\r\n this.detachScript(entry[0]);\r\n }\r\n }\r\n }\r\n}\r\n"],"names":["ScriptingSystem","_registry","_hostScripts","_scriptHosts","_onLoadError","_importComment","opts","ScriptRegistry","VFS","HttpFS","scriptsRoot","editorMode","Map","importComment","onLoadError","registry","attachScript","host","module","url","resolveRuntimeUrl","mod","instance","default","RuntimeScript","has","P","onCreated","Promise","console","warn","hostList","get","set","includes","push","onAttached","attached","id","list","on","detachScript","e","moduleName","String","slice","error","idOrInstance","length","i","it","hit","splice","onDetached","err","index","indexOf","onDestroy","delete","getScriptObjects","update","deltaTime","elapsedTime","size","values","s","onUpdate","detachAllScripts","entry"],"mappings":";;;;AAuDA;;;;;;;;;;;;;;;;;AAiBC,IACM,MAAMA,eAAAA,CAAAA;IACHC,SAA0B;IAC1BC,YAA2C;IAC3CC,YAA8C;IAC9CC,YAAgD;IAChDC,cAAwB;AAEhC;;;;AAIC,MACD,WAAYC,CAAAA,IAAAA,GAA+B,EAAE,CAAE;AAC7C,QAAA,IAAI,CAACL,SAAS,GAAG,IAAIM,cAAAA,CACnBD,KAAKE,GAAG,IAAI,IAAIC,MAAAA,CAAO,OACvBH,IAAKI,CAAAA,WAAW,IAAI,GACpBJ,EAAAA,IAAAA,CAAKK,UAAU,IAAI,KAAA,CAAA;QAErB,IAAI,CAACT,YAAY,GAAG,IAAIU,GAAAA,EAAAA;QACxB,IAAI,CAACT,YAAY,GAAG,IAAIS,GAAAA,EAAAA;AACxB,QAAA,IAAI,CAACP,cAAc,GAAGC,IAAAA,CAAKO,aAAa;AACxC,QAAA,IAAI,CAACT,YAAY,GAAGE,IAAAA,CAAKQ,WAAW;AACtC;AAEA;;AAEC,MACD,IAAIC,QAAW,GAAA;QACb,OAAO,IAAI,CAACd,SAAS;AACvB;AAEA;;;;;;;;;;;;;;;;;;AAkBC,MACD,MAAMe,YAAAA,CAA6BC,IAAO,EAAEC,MAAc,EAA6B;QACrF,IAAI;AACF,YAAA,MAAMC,MAAM,MAAM,IAAI,CAAClB,SAAS,CAACmB,iBAAiB,CAACF,MAAAA,CAAAA;AACnD,YAAA,IAAI,CAACC,GAAK,EAAA;gBACR,OAAO,IAAA;AACT;YACA,MAAME,GAAAA,GAAM,MAAM,OAAOF,GAAAA,IAAO,IAAI,CAACd,cAAc,IAAI,EAAC,CAAA,CAAA;AACxD,YAAA,IAAIiB,QAA6B,GAAA,IAAA;YACjC,IAAI,OAAOD,GAAKE,EAAAA,OAAAA,KAAY,UAAY,EAAA;;gBAEtCD,QAAW,GAAA,IAAID,IAAIE,OAAO,EAAA;AAC1B,gBAAA,IAAID,oBAAoBE,aAAe,EAAA;AACrC,oBAAA,IAAI,CAAC,IAAI,CAACrB,YAAY,CAACsB,GAAG,CAACH,QAAW,CAAA,EAAA;wBACpC,MAAMI,CAAAA,GAAIJ,SAASK,SAAS,EAAA;AAC5B,wBAAA,IAAID,aAAaE,OAAS,EAAA;4BACxB,MAAMF,CAAAA;AACR;AACF;iBACK,MAAA;oBACLJ,QAAW,GAAA,IAAA;AACb;AACF;AACA,YAAA,IAAI,CAACA,QAAU,EAAA;AACbO,gBAAAA,OAAAA,CAAQC,IAAI,CAAC,CAAC,QAAQ,EAAEZ,MAAAA,CAAO,uDAAuD,CAAC,CAAA;gBACvF,OAAO,IAAA;AACT;AAEA,YAAA,IAAIa,WAAW,IAAI,CAAC5B,YAAY,CAAC6B,GAAG,CAACV,QAAAA,CAAAA;AACrC,YAAA,IAAI,CAACS,QAAU,EAAA;AACbA,gBAAAA,QAAAA,GAAW,EAAE;AACb,gBAAA,IAAI,CAAC5B,YAAY,CAAC8B,GAAG,CAACX,QAAUS,EAAAA,QAAAA,CAAAA;AAClC;YACA,IAAIA,QAAAA,CAASG,QAAQ,CAACjB,IAAO,CAAA,EAAA;AAC3BY,gBAAAA,OAAAA,CAAQC,IAAI,CAAC,CAAC,QAAQ,EAAEZ,MAAAA,CAAO,kBAAkB,CAAC,CAAA;gBAClD,OAAOI,QAAAA;AACT;AACAS,YAAAA,QAAAA,CAASI,IAAI,CAAClB,IAAAA,CAAAA;YAEd,MAAMS,CAAAA,GAAIJ,QAASc,CAAAA,UAAU,CAACnB,IAAAA,CAAAA;AAC9B,YAAA,IAAIS,aAAaE,OAAS,EAAA;gBACxB,MAAMF,CAAAA;AACR;AAEA,YAAA,MAAMW,QAA4B,GAAA;gBAChCC,EAAIpB,EAAAA,MAAAA;AACJC,gBAAAA,GAAAA;AACAG,gBAAAA;AACF,aAAA;AAEA,YAAA,IAAIiB,OAAO,IAAI,CAACrC,YAAY,CAAC8B,GAAG,CAACf,IAAAA,CAAAA;AACjC,YAAA,IAAI,CAACsB,IAAM,EAAA;AACTA,gBAAAA,IAAAA,GAAO,EAAE;AACT,gBAAA,IAAI,CAACrC,YAAY,CAAC+B,GAAG,CAAChB,IAAMsB,EAAAA,IAAAA,CAAAA;AAC5B,gBAAA,IAAItB,IAAM,EAAA;oBACRA,IAAKuB,CAAAA,EAAE,CAAC,SAAW,EAAA,IAAA;wBACjB,IAAI,CAACC,YAAY,CAACxB,IAAAA,CAAAA;AACpB,qBAAA,CAAA;AACF;AACF;AACAsB,YAAAA,IAAAA,CAAKJ,IAAI,CAACE,QAAAA,CAAAA;AAEV,YAAA,OAAOA,SAASf,QAAQ;AAC1B,SAAA,CAAE,OAAOoB,CAAG,EAAA;AACV,YAAA,MAAMC,aAAazB,MAAS0B,GAAAA,MAAAA,CAAO1B,QAAQ2B,KAAK,CAAC,GAAG,EAAM,CAAA,GAAA,EAAA;YAC1DhB,OAAQiB,CAAAA,KAAK,CAAC,CAAC,aAAa,EAAEH,UAAW,CAAA,UAAU,EAAED,CAAG,CAAA,CAAA,CAAA;YACxD,IAAI,CAACtC,YAAY,GAAGsC,CAAGxB,EAAAA,MAAAA,CAAAA;YACvB,OAAO,IAAA;AACT;AACF;AAEA;;;;;;;;;;;;;AAaC,MACDuB,YAA6BxB,CAAAA,IAAO,EAAE8B,YAAwC,EAAE;AAC9E,QAAA,MAAMR,OAAO,IAAI,CAACrC,YAAY,CAAC8B,GAAG,CAACf,IAAAA,CAAAA;AACnC,QAAA,IAAI,CAACsB,IAAAA,IAAQA,IAAKS,CAAAA,MAAM,KAAK,CAAG,EAAA;AAC9B,YAAA;AACF;QACA,IAAK,IAAIC,IAAIV,IAAKS,CAAAA,MAAM,GAAG,CAAGC,EAAAA,CAAAA,IAAK,GAAGA,CAAK,EAAA,CAAA;YACzC,MAAMC,EAAAA,GAAKX,IAAI,CAACU,CAAE,CAAA;AAClB,YAAA,MAAME,GACJ,GAAA,CAACJ,YAAgB,IAAA,OAAOA,YAAiB,KAAA,QAAA,GACrCG,EAAGZ,CAAAA,EAAE,KAAKS,YAAAA,GACVG,EAAG5B,CAAAA,QAAQ,KAAKyB,YAAAA;AACtB,YAAA,IAAII,GAAK,EAAA;gBACPZ,IAAKa,CAAAA,MAAM,CAACH,CAAG,EAAA,CAAA,CAAA;gBACf,IAAI;oBACFC,EAAG5B,CAAAA,QAAQ,CAAC+B,UAAU,CAACpC,IAAAA,CAAAA;AACzB,iBAAA,CAAE,OAAOqC,GAAK,EAAA;oBACZzB,OAAQiB,CAAAA,KAAK,CAAC,CAAC,uCAAuC,EAAEI,GAAGZ,EAAE,CAAC,GAAG,EAAEgB,GAAK,CAAA,CAAA,CAAA;AAC1E;gBAEA,MAAMvB,QAAAA,GAAW,IAAI,CAAC5B,YAAY,CAAC6B,GAAG,CAACkB,GAAG5B,QAAQ,CAAA;gBAClD,MAAMiC,KAAAA,GAAQxB,QAASyB,CAAAA,OAAO,CAACvC,IAAAA,CAAAA;AAC/B,gBAAA,IAAIsC,SAAS,CAAG,EAAA;oBACdxB,QAASqB,CAAAA,MAAM,CAACG,KAAO,EAAA,CAAA,CAAA;AACzB;gBAEA,IAAIxB,QAAAA,CAASiB,MAAM,KAAK,CAAG,EAAA;oBACzB,IAAI;wBACFE,EAAG5B,CAAAA,QAAQ,CAACmC,SAAS,EAAA;AACvB,qBAAA,CAAE,OAAOH,GAAK,EAAA;wBACZzB,OAAQiB,CAAAA,KAAK,CAAC,CAAC,wCAAwC,EAAEI,GAAGZ,EAAE,CAAC,GAAG,EAAEgB,GAAK,CAAA,CAAA,CAAA;qBACjE,QAAA;AACR,wBAAA,IAAI,CAACnD,YAAY,CAACuD,MAAM,CAACR,GAAG5B,QAAQ,CAAA;AACtC;AACF;AACF;AACF;QACA,IAAIiB,IAAAA,CAAKS,MAAM,KAAK,CAAG,EAAA;AACrB,YAAA,IAAI,CAAC9C,YAAY,CAACwD,MAAM,CAACzC,IAAAA,CAAAA;AAC3B;AACF;AAEA;;;;;;MAOA0C,gBAAAA,CAA+C1C,IAAa,EAAO;QACjE,OAAQ,IAAI,CAACf,YAAY,CAAC8B,GAAG,CAACf,SAAoC,EAAE;AACtE;AAEA;;;;;;;;;AASC,MACD2C,MAAOC,CAAAA,SAAiB,EAAEC,WAAmB,EAAQ;AACnD,QAAA,IAAI,IAAI,CAAC5D,YAAY,CAAC6D,IAAI,KAAK,CAAG,EAAA;AAChC,YAAA;AACF;AACA,QAAA,KAAK,MAAMxB,IAAQ,IAAA,IAAI,CAACrC,YAAY,CAAC8D,MAAM,EAAI,CAAA;YAC7C,KAAK,MAAMC,KAAK1B,IAAM,CAAA;gBACpB,IAAI;AACF0B,oBAAAA,CAAAA,CAAE3C,QAAQ,CAAC4C,QAAQ,CAACL,SAAWC,EAAAA,WAAAA,CAAAA;AACjC,iBAAA,CAAE,OAAOR,GAAK,EAAA;oBACZzB,OAAQiB,CAAAA,KAAK,CAAC,CAAC,uCAAuC,EAAEmB,EAAE3B,EAAE,CAAC,GAAG,EAAEgB,GAAK,CAAA,CAAA,CAAA;AACzE;AACF;AACF;AACF;AAEA;;;;AAIC,MACDa,gBAAmB,GAAA;AACjB,QAAA,MAAO,IAAI,CAACjE,YAAY,CAAC6D,IAAI,GAAG,CAAG,CAAA;AACjC,YAAA,KAAK,MAAMK,KAAAA,IAAS,IAAI,CAAClE,YAAY,CAAE;AACrC,gBAAA,IAAI,CAACuC,YAAY,CAAC2B,KAAK,CAAC,CAAE,CAAA,CAAA;AAC5B;AACF;AACF;AACF;;;;"}
|
|
@@ -0,0 +1,432 @@
|
|
|
1
|
+
import { textToBase64 } from '@zephyr3d/base';
|
|
2
|
+
import { init, parse } from '../node_modules/es-module-lexer/dist/lexer.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Converts JavaScript source to a data URL tied to a logical module id.
|
|
6
|
+
*
|
|
7
|
+
* @param js - The JavaScript source code to embed.
|
|
8
|
+
* @param id - Logical module identifier (used only for sourceURL tagging).
|
|
9
|
+
* @returns A `data:text/javascript;base64,...` URL with an encoded `#id` suffix.
|
|
10
|
+
* @internal
|
|
11
|
+
*/ function toDataUrl(js, id) {
|
|
12
|
+
const b64 = textToBase64(js);
|
|
13
|
+
return `data:text/javascript;base64,${b64}#${encodeURIComponent(String(id))}`;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Checks whether a specifier is an absolute HTTP(S) URL.
|
|
17
|
+
* @internal
|
|
18
|
+
*/ function isAbsoluteUrl(spec) {
|
|
19
|
+
return /^https?:\/\//i.test(spec);
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Checks whether a specifier is a special URL (data: or blob:).
|
|
23
|
+
* @internal
|
|
24
|
+
*/ function isSpecialUrl(spec) {
|
|
25
|
+
return /^(data|blob):/i.test(spec);
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Checks whether a specifier is a bare module (not starting with ./, ../, /, or #/).
|
|
29
|
+
* @internal
|
|
30
|
+
*/ function isBareModule(spec) {
|
|
31
|
+
return !spec.startsWith('./') && !spec.startsWith('../') && !spec.startsWith('/') && !spec.startsWith('#/');
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Resolves, builds, and serves runtime modules using a VFS.
|
|
35
|
+
*
|
|
36
|
+
* Responsibilities:
|
|
37
|
+
* - Resolve logical module IDs to physical paths or URLs.
|
|
38
|
+
* - In editor mode, rewrite import specifiers and serve modules as data URLs after transpile.
|
|
39
|
+
* - Transpile TypeScript to JavaScript on the fly (requires `window.ts` TypeScript runtime).
|
|
40
|
+
* - Gather static and dynamic import dependencies for tooling.
|
|
41
|
+
*
|
|
42
|
+
* Modes:
|
|
43
|
+
* - Editor mode (`editorMode === true`): modules are rewritten to data URLs after transpile/build.
|
|
44
|
+
* - Runtime mode (`editorMode === false`): returns .js URLs directly (with .ts -\> .js mapping).
|
|
45
|
+
*
|
|
46
|
+
* Caching:
|
|
47
|
+
* - Built modules are memoized in `_built` map keyed by logical ID.
|
|
48
|
+
*
|
|
49
|
+
* @public
|
|
50
|
+
*/ class ScriptRegistry {
|
|
51
|
+
_vfs;
|
|
52
|
+
_scriptsRoot;
|
|
53
|
+
_built = new Map();
|
|
54
|
+
_editorMode = false;
|
|
55
|
+
/**
|
|
56
|
+
* @param vfs - The virtual file system for existence checks, reads, and path ops.
|
|
57
|
+
* @param scriptsRoot - Root directory for script resolution (used with `#/` specifiers).
|
|
58
|
+
* @param editorMode - Whether to build modules to data URLs and rewrite imports.
|
|
59
|
+
*/ constructor(vfs, scriptsRoot, editorMode){
|
|
60
|
+
this._vfs = vfs;
|
|
61
|
+
this._scriptsRoot = scriptsRoot;
|
|
62
|
+
this._editorMode = editorMode;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* The active virtual file system.
|
|
66
|
+
*
|
|
67
|
+
* Assigning a new VFS clears the build cache.
|
|
68
|
+
*/ get VFS() {
|
|
69
|
+
return this._vfs;
|
|
70
|
+
}
|
|
71
|
+
set VFS(vfs) {
|
|
72
|
+
if (vfs !== this._vfs) {
|
|
73
|
+
this._vfs = vfs;
|
|
74
|
+
this._built.clear();
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Whether the registry operates in editor mode (rewrite/build to data URLs).
|
|
79
|
+
*/ get editorMode() {
|
|
80
|
+
return this._editorMode;
|
|
81
|
+
}
|
|
82
|
+
set editorMode(val) {
|
|
83
|
+
this._editorMode = val;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* The root path used by `#/` specifiers.
|
|
87
|
+
*/ get scriptsRoot() {
|
|
88
|
+
return this._scriptsRoot;
|
|
89
|
+
}
|
|
90
|
+
set scriptsRoot(path) {
|
|
91
|
+
this._scriptsRoot = path;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Fetches raw source for a logical module id by probing known extensions.
|
|
95
|
+
*
|
|
96
|
+
* Search order:
|
|
97
|
+
* - If `id` already ends with `.ts` or `.js` and is a file -\> return it.
|
|
98
|
+
* - Else try `.id.ts`, then `.id.js`.
|
|
99
|
+
*
|
|
100
|
+
* @param id - Logical module identifier (absolute or logical path-like).
|
|
101
|
+
* @returns Source code, resolved path, and type (`'js' | 'ts'`), or `undefined` if not found.
|
|
102
|
+
*/ async fetchSource(id) {
|
|
103
|
+
let type = null;
|
|
104
|
+
let pathWithExt = '';
|
|
105
|
+
if (id.endsWith('.ts')) {
|
|
106
|
+
pathWithExt = id;
|
|
107
|
+
type = 'ts';
|
|
108
|
+
} else if (id.endsWith('.js')) {
|
|
109
|
+
pathWithExt = id;
|
|
110
|
+
type = 'js';
|
|
111
|
+
}
|
|
112
|
+
if (type) {
|
|
113
|
+
const exists = await this._vfs.exists(pathWithExt);
|
|
114
|
+
if (!exists) {
|
|
115
|
+
type = null;
|
|
116
|
+
}
|
|
117
|
+
const stat = await this._vfs.stat(pathWithExt);
|
|
118
|
+
if (stat.isDirectory) {
|
|
119
|
+
type = null;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
const types = [
|
|
123
|
+
'ts',
|
|
124
|
+
'js'
|
|
125
|
+
];
|
|
126
|
+
if (!type) {
|
|
127
|
+
for (const t of types){
|
|
128
|
+
pathWithExt = `${id}.${t}`;
|
|
129
|
+
const exists = await this._vfs.exists(pathWithExt);
|
|
130
|
+
if (exists) {
|
|
131
|
+
const stats = await this._vfs.stat(pathWithExt);
|
|
132
|
+
if (stats.isFile) {
|
|
133
|
+
type = t;
|
|
134
|
+
break;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
if (type) {
|
|
140
|
+
const code = await this._vfs.readFile(pathWithExt, {
|
|
141
|
+
encoding: 'utf8'
|
|
142
|
+
});
|
|
143
|
+
return {
|
|
144
|
+
code,
|
|
145
|
+
type,
|
|
146
|
+
path: pathWithExt
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Resolves a module entry to a URL suitable for dynamic import.
|
|
152
|
+
*
|
|
153
|
+
* Behavior:
|
|
154
|
+
* - In editor mode, builds the module to a data URL.
|
|
155
|
+
* - Otherwise, returns `.js` URL directly:
|
|
156
|
+
* - If `id` ends with `.js`: return as-is.
|
|
157
|
+
* - If `id` ends with `.ts`: map to `.js` (assumes pre-built file exists).
|
|
158
|
+
* - Else: append `.js`.
|
|
159
|
+
*
|
|
160
|
+
* @param entryId - Entry module identifier (logical or path-like).
|
|
161
|
+
* @returns A URL string that can be used in `import(...)`.
|
|
162
|
+
*/ async resolveRuntimeUrl(entryId) {
|
|
163
|
+
const id = await this.resolveLogicalId(entryId);
|
|
164
|
+
return this._editorMode ? await this.build(String(id)) : id.endsWith('.js') ? id : id.endsWith('.ts') ? `${id.slice(0, -3)}.js` : `${id}.js`;
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Recursively gathers direct static and dynamic import dependencies for a module.
|
|
168
|
+
*
|
|
169
|
+
* Only relative specifiers (`./` or `../`) are followed. Absolute, special, and bare
|
|
170
|
+
* module specifiers are ignored here.
|
|
171
|
+
*
|
|
172
|
+
* @param entryId - The starting (possibly relative) specifier from `fromId`.
|
|
173
|
+
* @param fromId - The logical id of the module containing `entryId`.
|
|
174
|
+
* @param dependencies - Output map of `resolvedSourcePath -\> file contents`.
|
|
175
|
+
*/ async getDependencies(entryId, fromId, dependencies) {
|
|
176
|
+
const reStatic = /\b(?:import|export)\s+[^"']*?from\s+(['"])([^'"]+)\1/g;
|
|
177
|
+
const reDynamic = /\bimport\s*\(\s*(['"])([^'"]+)\1\s*\)/g;
|
|
178
|
+
const normalizedId = await this.resolveLogicalId(entryId, fromId);
|
|
179
|
+
const srcPath = await this.resolveSourcePath(normalizedId);
|
|
180
|
+
if (!srcPath || dependencies[srcPath.path] !== undefined) {
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
const code = await this._vfs.readFile(srcPath.path, {
|
|
184
|
+
encoding: 'utf8'
|
|
185
|
+
});
|
|
186
|
+
dependencies[srcPath.path] = code;
|
|
187
|
+
const gather = async (input, re)=>{
|
|
188
|
+
for(;;){
|
|
189
|
+
const m = re.exec(input);
|
|
190
|
+
if (!m) {
|
|
191
|
+
break;
|
|
192
|
+
}
|
|
193
|
+
const spec = m[2];
|
|
194
|
+
if (spec.startsWith('./') || spec.startsWith('../')) {
|
|
195
|
+
await this.getDependencies(spec, normalizedId, dependencies);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
};
|
|
199
|
+
await gather(code, reStatic);
|
|
200
|
+
await gather(code, reDynamic);
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* Builds a logical module id into a data URL (editor mode pipeline).
|
|
204
|
+
*
|
|
205
|
+
* Steps:
|
|
206
|
+
* - Resolve source path (.ts/.js) via {@link ScriptRegistry.resolveSourcePath}.
|
|
207
|
+
* - Read source code.
|
|
208
|
+
* - Rewrite import specifiers via {@link ScriptRegistry.rewriteImports}.
|
|
209
|
+
* - Transpile TypeScript if needed via {@link ScriptRegistry.transpile}.
|
|
210
|
+
* - Convert to `data:` URL and memoize in `_built`.
|
|
211
|
+
*
|
|
212
|
+
* @param id - Logical module id to build.
|
|
213
|
+
* @returns Data URL string for dynamic import, or empty string if not found.
|
|
214
|
+
*/ async build(id) {
|
|
215
|
+
const key = String(id);
|
|
216
|
+
const cached = this._built.get(key);
|
|
217
|
+
if (cached) {
|
|
218
|
+
return cached;
|
|
219
|
+
}
|
|
220
|
+
const srcPath = await this.resolveSourcePath(key);
|
|
221
|
+
if (!srcPath) {
|
|
222
|
+
return '';
|
|
223
|
+
}
|
|
224
|
+
const code = await this._vfs.readFile(srcPath.path, {
|
|
225
|
+
encoding: 'utf8'
|
|
226
|
+
});
|
|
227
|
+
const rewritten = await this.rewriteImports(code, key);
|
|
228
|
+
const js = await this.transpile(rewritten, key, srcPath.type);
|
|
229
|
+
const url = toDataUrl(js, key);
|
|
230
|
+
this._built.set(key, url);
|
|
231
|
+
return url;
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* Transpiles code to JavaScript and appends sourceURL/sourceMap hints.
|
|
235
|
+
*
|
|
236
|
+
* Behavior:
|
|
237
|
+
* - For `'js'`, returns code with `//# sourceURL=logicalId`.
|
|
238
|
+
* - For `'ts'`, requires `window.ts` (TypeScript compiler) to be present and
|
|
239
|
+
* transpiles to ES2015/ESNext module with inline source maps.
|
|
240
|
+
*
|
|
241
|
+
* @param code - Source code to transpile.
|
|
242
|
+
* @param _id - Logical module id (used for fileName/sourceURL).
|
|
243
|
+
* @param type - Source type (`'js' | 'ts'`).
|
|
244
|
+
* @returns Transpiled JavaScript source.
|
|
245
|
+
* @throws If TypeScript runtime is not found for TS input.
|
|
246
|
+
*/ async transpile(code, _id, type) {
|
|
247
|
+
const logicalId = String(_id);
|
|
248
|
+
if (type === 'js') {
|
|
249
|
+
return `${code}\n//# sourceURL=${logicalId}`;
|
|
250
|
+
}
|
|
251
|
+
const ts = window.ts;
|
|
252
|
+
if (!ts) {
|
|
253
|
+
throw new Error('TypeScript runtime (window.ts) not found. Load typescript.js first.');
|
|
254
|
+
}
|
|
255
|
+
const res = ts.transpileModule(code, {
|
|
256
|
+
compilerOptions: {
|
|
257
|
+
target: ts.ScriptTarget.ES2015,
|
|
258
|
+
module: ts.ModuleKind.ESNext,
|
|
259
|
+
sourceMap: true,
|
|
260
|
+
inlineSources: true,
|
|
261
|
+
experimentalDecorators: true,
|
|
262
|
+
useDefineForClassFields: false
|
|
263
|
+
},
|
|
264
|
+
fileName: logicalId
|
|
265
|
+
});
|
|
266
|
+
let out = res.outputText || '';
|
|
267
|
+
if (res.sourceMapText) {
|
|
268
|
+
const mapBase64 = btoa(unescape(encodeURIComponent(res.sourceMapText)));
|
|
269
|
+
out += `\n//# sourceMappingURL=data:application/json;base64,${mapBase64}`;
|
|
270
|
+
}
|
|
271
|
+
out += `\n//# sourceURL=${logicalId}`;
|
|
272
|
+
return out;
|
|
273
|
+
}
|
|
274
|
+
/**
|
|
275
|
+
* Rewrites ESM import specifiers in `code` into runtime-loadable URLs.
|
|
276
|
+
*
|
|
277
|
+
* Parsing:
|
|
278
|
+
* - Uses `es-module-lexer` to find import spans; sorts them ascending by start.
|
|
279
|
+
*
|
|
280
|
+
* Replacement rules:
|
|
281
|
+
* - Skip invalid spans or ones without quoted specifiers.
|
|
282
|
+
* - If spec is absolute URL, special URL (data:, blob:), or bare module:
|
|
283
|
+
* - If it starts with `@zephyr3d/`, keep as-is (external).
|
|
284
|
+
* - Otherwise resolve to a logical id and attempt to `build` it (if available).
|
|
285
|
+
* - Else (relative spec), resolve from `fromId` and `build` recursively.
|
|
286
|
+
*
|
|
287
|
+
* Output:
|
|
288
|
+
* - Directly writes the replacement specifier without re-adding quotes,
|
|
289
|
+
* so replacements must themselves be quoted or be valid URLs/data URLs.
|
|
290
|
+
*
|
|
291
|
+
* @param code - Module source code to transform.
|
|
292
|
+
* @param fromId - The logical id of the current module (resolution base for relatives).
|
|
293
|
+
* @returns Transformed source with rewritten import specifiers.
|
|
294
|
+
*/ async rewriteImports(code, fromId) {
|
|
295
|
+
await init;
|
|
296
|
+
const [imports] = parse(code);
|
|
297
|
+
const list = [
|
|
298
|
+
...imports
|
|
299
|
+
].sort((a, b)=>(a.s || 0) - (b.s || 0));
|
|
300
|
+
let out = '';
|
|
301
|
+
let last = 0;
|
|
302
|
+
for (const im of list){
|
|
303
|
+
// must have quotes
|
|
304
|
+
const hasQuote = im.ss != null && im.se != null;
|
|
305
|
+
if (!hasQuote || im.se <= im.ss) {
|
|
306
|
+
continue;
|
|
307
|
+
}
|
|
308
|
+
// must have contents
|
|
309
|
+
if (im.e <= im.s) {
|
|
310
|
+
continue;
|
|
311
|
+
}
|
|
312
|
+
// append [last, s)
|
|
313
|
+
out += code.slice(last, im.s);
|
|
314
|
+
const spec = code.slice(im.s, im.e); // original spec
|
|
315
|
+
let replacement = spec;
|
|
316
|
+
if (isAbsoluteUrl(spec) || isSpecialUrl(spec) || isBareModule(spec)) {
|
|
317
|
+
if (spec.startsWith('@zephyr3d/')) {
|
|
318
|
+
replacement = spec;
|
|
319
|
+
} else {
|
|
320
|
+
const depId = await this.resolveLogicalId(spec);
|
|
321
|
+
replacement = await this.build(depId); // try build as dependence
|
|
322
|
+
}
|
|
323
|
+
} else {
|
|
324
|
+
const depId = await this.resolveLogicalId(spec, String(fromId));
|
|
325
|
+
replacement = await this.build(depId); // recursively build as dataURL
|
|
326
|
+
}
|
|
327
|
+
out += replacement; // 不加引号
|
|
328
|
+
last = im.e;
|
|
329
|
+
}
|
|
330
|
+
out += code.slice(last);
|
|
331
|
+
return out;
|
|
332
|
+
}
|
|
333
|
+
/**
|
|
334
|
+
* Resolves a specifier to a logical id suitable for further processing.
|
|
335
|
+
*
|
|
336
|
+
* Resolution rules:
|
|
337
|
+
* - `#/path`: resolved against `scriptsRoot` via VFS join/normalize.
|
|
338
|
+
* - `./` or `../`: resolved relative to `fromId` directory (requires `fromId`).
|
|
339
|
+
* - `/absolute`: treated as absolute from root (normalized).
|
|
340
|
+
* - Bare module in editor mode: if `/deps.lock.json` exists and contains an entry,
|
|
341
|
+
* map to the dependency's `entry` path; otherwise return as-is.
|
|
342
|
+
* - Else (non-editor bare module): return `spec` unchanged (external).
|
|
343
|
+
*
|
|
344
|
+
* @param spec - Import specifier string.
|
|
345
|
+
* @param fromId - Optional base logical id used for relative resolution.
|
|
346
|
+
* @returns A normalized logical id or an external specifier string.
|
|
347
|
+
* @throws If a relative import is provided without `fromId`.
|
|
348
|
+
*/ async resolveLogicalId(spec, fromId) {
|
|
349
|
+
let path;
|
|
350
|
+
if (spec.startsWith('#/')) {
|
|
351
|
+
path = this._vfs.normalizePath(this._vfs.join(this._scriptsRoot, spec.slice(2)));
|
|
352
|
+
} else if (spec.startsWith('./') || spec.startsWith('../')) {
|
|
353
|
+
if (!fromId) {
|
|
354
|
+
throw new Error(`Relative import "${spec}" requires fromId`);
|
|
355
|
+
}
|
|
356
|
+
path = this._vfs.normalizePath(this._vfs.join(this._vfs.dirname(this._vfs.normalizePath(fromId)), spec));
|
|
357
|
+
} else if (spec.startsWith('/')) {
|
|
358
|
+
path = spec.replace(/^\/+/, '/');
|
|
359
|
+
} else if (this._editorMode) {
|
|
360
|
+
// naked module, checking if it is a installed module in editor mode
|
|
361
|
+
const depsExists = await this._vfs.exists('/libs/deps.lock.json');
|
|
362
|
+
if (depsExists) {
|
|
363
|
+
const content = await this._vfs.readFile('/libs/deps.lock.json', {
|
|
364
|
+
encoding: 'utf8'
|
|
365
|
+
});
|
|
366
|
+
const depsInfo = JSON.parse(content);
|
|
367
|
+
if (depsInfo?.dependencies[spec]) {
|
|
368
|
+
path = this._vfs.normalizePath(depsInfo.dependencies[spec].entry);
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
} else {
|
|
372
|
+
return spec;
|
|
373
|
+
}
|
|
374
|
+
return path;
|
|
375
|
+
}
|
|
376
|
+
/**
|
|
377
|
+
* Resolves a logical id to a concrete source path and type by probing extensions.
|
|
378
|
+
*
|
|
379
|
+
* Rules:
|
|
380
|
+
* - If `logicalId` ends with `.ts` or `.js`/`.mjs` and is a file, return it.
|
|
381
|
+
* - Else probe `logicalId.ts`, `logicalId.js`, `logicalId.mjs` in that order.
|
|
382
|
+
* - Maps `.mjs` to type `'js'`.
|
|
383
|
+
*
|
|
384
|
+
* @param logicalId - The normalized logical module id (path-like).
|
|
385
|
+
* @returns `{ type, path }` or `null` if not found.
|
|
386
|
+
*/ async resolveSourcePath(logicalId) {
|
|
387
|
+
let type = null;
|
|
388
|
+
let pathWithExt = '';
|
|
389
|
+
if (logicalId.endsWith('.ts')) {
|
|
390
|
+
pathWithExt = logicalId;
|
|
391
|
+
type = 'ts';
|
|
392
|
+
} else if (logicalId.endsWith('.js') || logicalId.endsWith('.mjs')) {
|
|
393
|
+
pathWithExt = logicalId;
|
|
394
|
+
type = 'js';
|
|
395
|
+
}
|
|
396
|
+
if (type) {
|
|
397
|
+
const exists = await this._vfs.exists(pathWithExt);
|
|
398
|
+
if (!exists) {
|
|
399
|
+
type = null;
|
|
400
|
+
}
|
|
401
|
+
const stat = await this._vfs.stat(pathWithExt);
|
|
402
|
+
if (stat.isDirectory) {
|
|
403
|
+
type = null;
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
const types = [
|
|
407
|
+
'ts',
|
|
408
|
+
'js',
|
|
409
|
+
'mjs'
|
|
410
|
+
];
|
|
411
|
+
if (!type) {
|
|
412
|
+
for (const t of types){
|
|
413
|
+
pathWithExt = `${logicalId}.${t}`;
|
|
414
|
+
const exists = await this._vfs.exists(pathWithExt);
|
|
415
|
+
if (exists) {
|
|
416
|
+
const stats = await this._vfs.stat(pathWithExt);
|
|
417
|
+
if (stats.isFile) {
|
|
418
|
+
type = t === 'ts' ? 'ts' : 'js';
|
|
419
|
+
break;
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
return type ? {
|
|
425
|
+
type,
|
|
426
|
+
path: pathWithExt
|
|
427
|
+
} : null;
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
export { ScriptRegistry };
|
|
432
|
+
//# sourceMappingURL=scriptregistry.js.map
|