playcanvas / engine

JavaScript game engine built on WebGL, WebGPU, WebXR and glTF
https://playcanvas.com
MIT License
9.52k stars 1.33k forks source link

Stopped working with SvelteKit / Vite SSR #6105

Closed michaelfranzl closed 6 months ago

michaelfranzl commented 6 months ago

Description

I'm using Playcanvas in SvelteKit. This works well up until Playcanvas v1.65.5 but stops working when upgrading to the following release of Playcanvas, which is v1.66.0 (and newer).

Steps to Reproduce

The issue is reproducible with minimal effort.

Using Node.js v20.11.1 LTS, create a new SvelteKit application:

mkdir test
cd test
npm create svelte@latest # interactively accept all defaults
npm i

In the file src/routes/+page.svelte import Playcanvas and instantiate a Vec3:

import * as pc from 'playcanvas';
const vec3 = new pc.Vec3();

Save the file and start the server:

npm run dev

Access the URL printed in the terminal (either using browser or using curl/wget).

Vite immediately crashes with the following messages:

  VITE v5.1.4  ready in 615 ms

  ➜  Local:   http://localhost:5173/
  ➜  Network:  use --host to expose
  ➜  press h + enter to show help
TypeError: __vite_ssr_import_4__.Vec3 is not a constructor
    at /path/to/test/src/routes/+page.svelte:8:12
    at Object.$$render (/path/to/test/node_modules/svelte/src/runtime/internal/ssr.js:156:16)
    at Object.default (/path/to/test/.svelte-kit/generated/root.svelte:45:41)
    at eval (/path/to/test/src/routes/+layout.svelte:20:218)
    at Object.$$render (/path/to/test/node_modules/svelte/src/runtime/internal/ssr.js:156:16)
    at /path/to/test/.svelte-kit/generated/root.svelte:44:40
    at $$render (/path/to/test/node_modules/svelte/src/runtime/internal/ssr.js:156:16)
    at Object.render (/path/toS/test/node_modules/svelte/src/runtime/internal/ssr.js:164:17)
    at Module.render_response (/path/to/test/node_modules/@sveltejs/kit/src/runtime/server/page/render.js:171:29)
    at async Module.render_page (/path/to/test/node_modules/@sveltejs/kit/src/runtime/server/page/index.js:286:10

I tested with Vite v4.5 and current (v5.0) and @sveltejs/kit v1.27 and current (v2.0), with the same results.

mvaligursky commented 6 months ago

@marklundin - any idea here?

marklundin commented 6 months ago

Hey @michaelfranzl, thanks for flagging this and the detailed repro.

Great catch. It looks like the engine was updated in 1.66.0 to use type module in the package.json https://github.com/playcanvas/engine/blob/d75dba58a5ca60178f31d4a9c7e5bcb094e75eb3/package.json#L24

My understanding is that if the type is a module, then Node expects `main' to be a ES module, which it actually is not.

For the time being, you should be able to directly import the ES6 build using import { Vec3 } from 'playcanvas/build/playcanvas.mjs' which will fix your issue. But we'll need to update our side to correctly handle this.

marklundin commented 6 months ago

It looks like we can fix this by including an exports field

"exports": {
  ".": {
    "import": "./build/playcanvas.mjs",
    "require": "./build/playcanvas.js"
  }
},

This looks like the recommended approach for Node > 10 https://nodejs.org/api/packages.html#package-entry-points. Any thoughts @epreston @mvaligursky @kungfooman @kpal81xd

kpal81xd commented 6 months ago

Yep that looks fine to me had a similar issue with exports for react on PCUI. Ill make a PR to add that change

marklundin commented 6 months ago

Yep, I think we still need the existing main and modules fields for older versions of Node, but this should at least "feature detect" if import is available