Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[xstate/svelte] bundling of @xstate/svelte/lib/fsm with sveltekit & vite is broken #2458

Closed
benbender opened this issue Jul 23, 2021 · 14 comments · Fixed by #2899
Closed

[xstate/svelte] bundling of @xstate/svelte/lib/fsm with sveltekit & vite is broken #2458

benbender opened this issue Jul 23, 2021 · 14 comments · Fixed by #2899

Comments

@benbender
Copy link

benbender commented Jul 23, 2021

Description
If I create a new sveltekit-app:

  1. npm init svelte@next xstate-sveltekit-repro
  2. install its dependencies via npm i && npm add @xstate/svelte @xstate/fsm
  3. add the example for the usage of @xstate/svelte/lib/fsm from the docs @ https://xstate.js.org/docs/packages/xstate-svelte/#usemachine-machine-with-xstate-fsm
  4. build the app via npm run build
  5. start it via npm preview

I get the following error:

> svelte-kit preview

> Cannot find module '[...]/xstate-sveltekit-repro/node_modules/@xstate/svelte/lib/fsm' imported from [...]/xstate-sveltekit-repro/.svelte-kit/output/server/app.js
Did you mean to import @xstate/svelte/lib/fsm.js?
Error [ERR_MODULE_NOT_FOUND]: Cannot find module '[...]/xstate-sveltekit-repro/node_modules/@xstate/svelte/lib/fsm' imported from [...]/xstate-sveltekit-repro/.svelte-kit/output/server/app.js
Did you mean to import @xstate/svelte/lib/fsm.js?
    at finalizeResolution (internal/modules/esm/resolve.js:271:11)
    at moduleResolve (internal/modules/esm/resolve.js:694:10)
    at Loader.defaultResolve [as _resolve] (internal/modules/esm/resolve.js:805:11)
    at Loader.resolve (internal/modules/esm/loader.js:88:40)
    at Loader.getModuleJob (internal/modules/esm/loader.js:241:28)
    at ModuleWrap.<anonymous> (internal/modules/esm/module_job.js:72:40)
    at link (internal/modules/esm/module_job.js:71:36)

Expected Result
The app should run with the production build.

Actual Result
@xstate/svelte/lib/fsm can't be imported

Reproduction
see above.

Additional context

  • The app works in dev-mode. The problem seems to be rooted in the vite-build.
  • Reproduced with NPM & PNPM
  • Strangely the @xstate/svelte package doesn't seem to use rollup for bundling like the rest of the packages in this repo. Any reason for that?
@floratmin
Copy link

I just made it work with the description from:
https://xstate.js.org/docs/recipes/svelte.html

But contrary to the description the @rollup/plugin-replace breaks the build. Without this step everything seems to work.

@benbender
Copy link
Author

@floratmin svelte@next is sveltekit, which uses vite as its bundler. Additionally I did no extra steps - as stated above. So I don't think both problems are related.

@floratmin
Copy link

No that's right I use it with SvelteKit.

@benbender

This comment has been minimized.

@floratmin
Copy link

No I use it with the full xstate package, I tried it also with @sveltejs/adapter-static where it also worked.

@benbender

This comment has been minimized.

@floratmin
Copy link

I just replaced xstate with @xstate/fsm and it does not work. I get the error :
'default' is not exported by node_modules/@xstate/fsm/es/index.js, imported by src/lib/state/login.ts

@benbender

This comment has been minimized.

@floratmin
Copy link

@benbender This could related sveltejs/kit#928

@benbender

This comment has been minimized.

@annaghi
Copy link
Contributor

annaghi commented Jul 31, 2021

4. build the app via npm run build
5. start it via npm preview

I get the following error:

@benbender What happens when you run npm run preview?

@benbender
Copy link
Author

benbender commented Jul 31, 2021

Oh sh*t. You're right... Its still broken - my memory betrayed me as I thought that it broke when building. sorry for the noise!

Btw, my old workaround:

    import { useMachine } from '@xstate/svelte/lib/fsm.js';
    import * as pkg from '@xstate/fsm';

    const { createMachine, assign } = pkg.default ? pkg.default : pkg;

Works for now even if its not pretty...

@benbender benbender reopened this Jul 31, 2021
@annaghi
Copy link
Contributor

annaghi commented Jul 31, 2021

No problem :) Thanks for confirming!

With your workaround we have two possible solutions now! Good. 👍

@benmccann
Copy link

It's best to used named imports instead of default imports for this reason. Then it will work the same with both ESM and CJS

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
5 participants