davidmyersdev / vite-plugin-node-polyfills

A Vite plugin to polyfill Node's Core Modules for browser environments.
MIT License
263 stars 17 forks source link

Sveltekit builds? #13

Closed Drewsapple closed 1 year ago

Drewsapple commented 1 year ago

I was using this plugin with a svelte vite project, which uses some commonJs modules that import Buffer and process.

This plugin works fine in dev mode, but fails in build. To make this plugin work, I forced dependency optimization on and and made vite skip the commonJs transforms.

optimizeDeps: {
  disabled: false,
},
build: {
  commonjsOptions: {
    include: [],
  }
}

I'm not really sure why this worked, but sveltekit doesn't support forcing dependency optimization on. Is there a better/proper way that I can make the polyfills available to non-optimized commonJs dependencies?

davidmyersdev commented 1 year ago

Hi @Drewsapple. If the CommonJS dependencies you are talking about are not actually a part of the application you are building and are instead third-party dependencies, then this behavior makes sense. By default, Vite does not attempt to optimize dependencies on build even though it does do this by default on dev.

I'm not that familiar with SvelteKit. When you say it's not possible to force optimization on, do you mean that SvelteKit's configuration does not allow you to pass optimizeDeps: { disabled: false } like you are doing in the example above?

Drewsapple commented 1 year ago

I saw a warning that said it would override the setting, but I can't get the CLI to reproduce that. After build with optimizeDeps: { disabled: false }, SvelteKit chokes while taking the bundle output, saying "Buffer" is not exported by "node_modules/.pnpm/buffer@5.7.1/node_modules/buffer/index.js", imported by "node_modules/.pnpm/node-stdlib-browser@1.2.0/node_modules/node-stdlib-browser/helpers/esbuild/shim.js".

I guess I am confused about which items must be optimized, which must be in ssr.noExternal, and which belong in build.commonjsOptions.include. With buffer externalized or not, the above happens, when /buffer/ is in commonjsOptions.include, ssr fails at exports.byteLength = byteLength; saying ReferenceError: exports is not defined in ES module scope. Allowing deps to not be optimized just makes things fail at runtime when the polyfills aren't there.

Drewsapple commented 1 year ago

Interestingly, with ssr.noExternal: ['@sveltejs/kit', 'svelte/internal', 'svelte', /buffer/, /process/], I can get a successful build, but only if I first run pnpm build with build.commonjsOptions.include: ['@sveltejs/kit', 'svelte/internal', 'svelte', /buffer/, /process/] and then run another build with build.commonjsOptions.include: ["node_modules/**"].

If I jump straight to just node_modules/**, transforming immediately panics with Unexpected early exit. This happens when Promises returned by plugins cannot resolve. Unfinished hook action(s) on exit: ...

davidmyersdev commented 1 year ago

If you can share a reproducible example, I'd be happy to take a look. It's hard to know what could be happening without seeing the entire build configuration.

Drewsapple commented 1 year ago

https://github.com/Drewsapple/sveltekit-siwe

Sorry about the complexity of this repo, but it should illustrate the problem. This one is not able to be built successfully by broadening the scope of build.commonjsOptions.include: ["node_modules/**"] as the other one, but I really don't know how to simplify that one enough to make it useful to you. Hopefully this lets you gain some insight about what is going wrong.

Thanks for looking into this.

davidmyersdev commented 1 year ago

Thanks so much for getting this together! I should have time to dig in tomorrow.

davidmyersdev commented 1 year ago

I'm a little behind schedule with other stuff queued up, but I just wanted to reach out to let you know I haven't forgotten about this. Will be taking a look soon.

davidmyersdev commented 1 year ago

I tried to build/preview that project, but I'm running into some auth errors. I think I might be missing some environment variables/secrets. Any chance you can clone a fresh copy of that project and give me an idea of what I need to do to run it?

davidmyersdev commented 1 year ago

Without a reproduction, I am unable to test or come up with a fix for this issue. I think it could be related to #19, but I am unsure. I am closing for now, but I am happy to reopen if someone can provide a working reproduction on StackBlitz or CodeSandbox.