fand / react-vfx

WebGL effects for React elements
https://amagi.dev/react-vfx
454 stars 16 forks source link

Use with sveltekit #72

Closed gabrielatwell1987 closed 1 day ago

gabrielatwell1987 commented 5 days ago

Every time I try to use this with the sveltekit framework, it always says that it can't find the @vfx-js/core module.. I'm guessing it has to do with SSR. How do you use this with SSR?

fand commented 5 days ago

Hi @gabrielatwell1987 ! Thx for report.

I created a sandbox with SvelteKit myself and I confirmed the issue. I haven't figured out the correct way to solve this, but I found a workaround; add ssr.noExternal to your vite.config.ts.

// vite.config.ts
import { sveltekit } from '@sveltejs/kit/vite';
import { defineConfig } from 'vite';

export default defineConfig({
    plugins: [sveltekit()],
    ssr: {
        noExternal: ['@vfx-js/core']
    }
});

Hope this will work on your side as well 🙏 I'll post an update once I fix the problem fundamentally.


(memo for myself)

The problem is that Node.js can't resolve relative imports without extensions from ESM modules.

import { foo } from "./foo.mjs";  // OK
import { foo } from "./foo.js";  // OK
import { foo } from "./foo";  // NG

VFX-JS builds the output file using tsc, but tsc can't convert import statements to include the extension. So we have 2 possible solutions:

I didn't encounter this issue in docs-vfx-js probably because I use monorepo, but I'm not sure.

Nagitch commented 5 days ago

i got same but the workaround is fine so far. here is my changes: https://github.com/Nagitch/mujina-prod-site/commit/6f71b1169243591b84c3bbbd26d62735bdc76f47

as an additional info, i faced error like this.

[vite] Error when evaluating SSR module /src/routes/+page.svelte: failed to import "@vfx-js/core" |- Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/home/default/mujina-prod-site/node_modules/.pnpm/@vfx-js+core@0.2.0/node_modules/@vfx-js/core/lib/esm/vfx' imported from /home/default/mujina-prod-site/node_modules/.pnpm/@vfx-js+core@0.2.0/node_modules/@vfx-js/core/lib/esm/index.mjs at finalizeResolution (node:internal/modules/esm/resolve:264:11) at moduleResolve (node:internal/modules/esm/resolve:917:10) at defaultResolve (node:internal/modules/esm/resolve:1130:11) at ModuleLoader.defaultResolve (node:internal/modules/esm/loader:396:12) at ModuleLoader.resolve (node:internal/modules/esm/loader:365:25) at ModuleLoader.getModuleJob (node:internal/modules/esm/loader:240:38) at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:85:39) at link (node:internal/modules/esm/module_job:84:36)

i'm working with SvelteKit with pnpm + adapter-static (SSG), actually not SSR in production. but parhaps in development, these works with SSR (maybe. i'm not sure).

gabrielatwell1987 commented 4 days ago

Thank you so much for your quick reply. The workaround completely worked! https://gabrielatwell.com Check out the main logo above the hero section

fand commented 1 day ago

I released @vfx-js/core@0.3.0 today, which fixes this issue. Now you can use @vfx-js/core in Svelte / SvelteKit without any workarounds.

FYI @gabrielatwell1987 @Nagitch