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

refactor(vite-node-miniflare): use ssr.optimizeDeps for vite-node #152

Merged
merged 28 commits into from
Feb 9, 2024

Conversation

hi-ogawa
Copy link
Owner

@hi-ogawa hi-ogawa commented Jan 9, 2024

(To be merged after Vite 5.1 release with vitejs/vite#15561)

summary

  • call ssrLoadModule to trigger ssr deps optimization
  • set ssr.target: "webworker" so that esbuild pre-bundling target is "browser"
  • set ViteNodeServerOptions.cacheDir to random directory to disable vite-node's externalization
old comment

Vite/esbuild doesn't look happy with bundling react-dom/server:

 [ERROR] Cannot bundle Node.js built-in "stream" imported from "../../../../node_modules/.pnpm/[email protected][email protected]/node_modules/react-dom/cjs/react-dom-server-legacy.node.development.js". Consider disabling ssr.noExternal or remove the built-in dependency. [plugin vite:dep-pre-bundle]

Probably tweaking ssr.optimizeDeps.esbuildOptions would make it work?
(Either force resolve.conditions to browser or just importing react-dom/server.browser is fine)

@hi-ogawa hi-ogawa changed the title fix(vite-node-miniflare): use ssr.optimizeDeps for vite-node refactor(vite-node-miniflare): use ssr.optimizeDeps for vite-node Jan 9, 2024
@hi-ogawa
Copy link
Owner Author

hi-ogawa commented Jan 9, 2024

Looks like ViteNodeServer.fetchModule returns externalize for pre-bundled deps, which tells ViteNodeRunner to directly import which, of course, is not possible on Workerd.

Error: No such module "home/hiroshi/code/personal/vite-plugins/packages/vite-node-miniflare/examples/react/node_modules/.vite/deps_ssr/react_jsx-dev-runtime.js".
    at ViteNodeRunner.importExternalModule (__vite_node_miniflare_entry.js:1677:5)
    at ViteNodeRunner.interopedImport (__vite_node_miniflare_entry.js:1683:39)
    at ViteNodeRunner.directRequest (__vite_node_miniflare_entry.js:1563:35)
    at ViteNodeRunner.cachedRequest (__vite_node_miniflare_entry.js:1499:14)
    at ViteNodeRunner.dependencyRequest (__vite_node_miniflare_entry.js:1543:12)
    at /home/hiroshi/code/personal/vite-plugins/packages/vite-node-miniflare/examples/react/src/worker-entry.tsx:1:154
    at ViteNodeRunner.runModule (__vite_node_miniflare_entry.js:1662:5)
    at ViteNodeRunner.directRequest (__vite_node_miniflare_entry.js:1646:5)
    at ViteNodeRunner.cachedRequest (__vite_node_miniflare_entry.js:1499:14)
    at ViteNodeRunner.executeId (__vite_node_miniflare_entry.js:1475:12)

https://github.com/vitest-dev/vitest/blob/043b78f3257b266302cdd68849a76b8ed343bba1/packages/vite-node/src/client.ts#L439-L441

This decision is coming from here:

  // always externalize Vite deps, they are too big to inline
  if (options?.cacheDir && id.includes(options.cacheDir))
    return id

https://github.com/vitest-dev/vitest/blob/043b78f3257b266302cdd68849a76b8ed343bba1/packages/vite-node/src/externalize.ts#L104-L106

@hi-ogawa hi-ogawa marked this pull request as ready for review January 9, 2024 09:35
@hi-ogawa hi-ogawa merged commit e91a10f into main Feb 9, 2024
3 checks passed
@hi-ogawa hi-ogawa deleted the fix-vite-node-ssr-optimizeDeps branch February 9, 2024 01:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

figure out ssr.optimizeDeps for vite-node-miniflare
1 participant