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
feat(adapter-cloudflare): allow override esbuild option #10214
Conversation
🦋 Changeset detectedLatest commit: 4c42bb8 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This has come up several times in the past. Rather than exposing the esuild options we've tried to address whatever usecase directly. It's hard to know how to do that in this case as I'm not sure what issue you're trying to solve
Hi @benmccann, thanks for you insight. I was a bit rush when creating this PR so didn't have time to properly explain the issue, sorry. The issueMy use case is simple: I want to use some 3rd party packges, that require NodeJS runtime. In the official doc, you can use the combination of import { Buffer } from 'node:buffer'; But when working with some external library, they sometime still assume that those object exist in global scope, and using it directly with out import them from
I tried to polyfill the Buffer object, but it doesn't work, due to hoisting when bundling // buffer-shim.ts
import { Buffer as _NodeBuffer } from "node:buffer";
globalThis.Buffer = _NodeBuffer; // Does NOT work. The global assignment will come AFTER the import.
import './buffer-shim'
import { SiweMessage } from "siwe";
// ... Proposed solutionWith this PR, we can easily polyfill those kind of packages, by using // src/lib/buffer-shim.ts
import { Buffer as _NodeBuffer } from "node:buffer";
globalThis.Buffer = _NodeBuffer;
+export { _NodeBuffer as Buffer } // svelte.config.js
kit: {
adapter: adapter({
// other config ...
+ esbuildOptions: {
+ external: ["node:buffer"],
+ inject: ["./src/lib/buffer-shim.ts"],
+ },
}), ExampleCheck out this example repo: git checkout https://github.com/Toanzzz/cloudflare-adapter-issue Install deps, build the worker, and invoke wrangler on the final
Repeat the same steps above on the |
Thanks for the explanation @Toanzzz. While I'm sympathetic to your predicament, I'd have to discuss your PR with the other maintainers some of whom are away right now, and I'm not optimistic this PR would be accepted based on past discussions around this issue. I took a look at your issue and it seems to be caused by ldthomas/apg-js#16. I think the easiest path forward for you is probably to fix that issue, which is probably the better fix anyway rather than trying to work around it with a bundler |
I'm going to close this in favor of #10424, which seems closer to being ready to merge |
This PR is similar to #10110, but allow you to freely override any esbuild options, by passing specific option(s) or a callback.
Example:
Callback style:
Similarly, this will help the following issues:
Please don't delete this checklist! Before submitting the PR, please make sure you do the following:
Tests
pnpm test
and lint the project withpnpm lint
andpnpm check
Changesets
pnpm changeset
and following the prompts. Changesets that add features should beminor
and those that fix bugs should bepatch
. Please prefix changeset messages withfeat:
,fix:
, orchore:
.