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

Add back esbuild configuration options adapters #3733

Open
pzuraq opened this issue Feb 4, 2022 · 6 comments
Open

Add back esbuild configuration options adapters #3733

pzuraq opened this issue Feb 4, 2022 · 6 comments

Comments

@pzuraq
Copy link
Contributor

pzuraq commented Feb 4, 2022

Describe the problem

For local and testing builds we use MockServiceWorker for our mocks. There's a Node component to the library so we can mock requests in SSR. However, it uses Node libraries, and esbuild throws an error if the platform is not set to node when we build for production. We want to test our app with a production build so that it's as close to what will actually ship as possible.

Without the esbuild configuration option, there's no way for us to change the platform specifically for test builds.

Describe the proposed solution

An option for configuring the build within adapters.

Alternatives considered

Another possibility would be changing the target/platform based on whether or not it is a "preview" build. Since previewing first requires us to build the app with a separate command, I'm not sure how this could be signaled to the build. Maybe a build flag?

Importance

i cannot use SvelteKit without it

Additional Information

Currently operating off a fork so we can use the latest SvelteKit.

@babichjacob
Copy link
Member

babichjacob commented Feb 4, 2022

Another possibility would be changing the target/platform based on whether or not it is a "preview" build. Since previewing first requires us to build the app with a separate command, I'm not sure how this could be signaled to the build.

svelte-kit preview doesn't use the adapted output, so even if the adapter fails, the .svelte-kit/output folder that is used by preview still exists.

If you don't want to waste resources from the adapter attempting to adapt the build and failing, you can set an environment variable and use it through process.env.WHATEVER in svelte.config.js to not set an adapter in kit config.

@pzuraq
Copy link
Contributor Author

pzuraq commented Feb 9, 2022

That works for solving this particular issue, but there's another issue because the targets passed to ESbuild are incorrect:

Module parse failed: Unexpected token (13066:47)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
|       if (typeof window !== "undefined") {
|         const windowAny = window;
>         const dataLayer = windowAny.dataLayer ?? [];
|         windowAny.dataLayer = dataLayer;
|         gtag = function() {
Error: webpack returned an error. You may be able to resolve this issue by running npm install

I'll open up a separate issue for this

@pzuraq pzuraq closed this as completed Feb 9, 2022
@pzuraq
Copy link
Contributor Author

pzuraq commented Mar 1, 2022

Reopening this issue for a different reason, we need to pass build plugins to esbuild so we can polyfill Node build-ins (annoying, but we currently still need to for a few legacy packages)

@pzuraq pzuraq reopened this Mar 1, 2022
@benmccann benmccann changed the title Add back esbuild configuration options to Cloudflare adapter Add back esbuild configuration options adapters Jul 30, 2022
@benmccann
Copy link
Member

Another request for this in #5756, which I closed as a duplicate of this issue

@Rich-Harris Rich-Harris added this to the whenever milestone Sep 6, 2022
@MilesWellsSVT
Copy link

MilesWellsSVT commented Oct 25, 2022

I am using Azure AD B2C for authentication. Microsoft provides extensions for account caching for refreshing access tokens. These extensions are provided as a C++ addon (and one of their dependencies is another C++ addon). Using the Node adapter results in a No loader is configured for ".node" files error from esbuild (image below).

The author of esbuild has offered a plugin to solve this problem. I could not figure out how to apply this plugin and from this issue I'm assuming it is not possible.

Are there any known workarounds?

image

Realized through some help on the discord that my adapter version was out of date. Was still using a version of adapter-node that used esbuild. Using the current next version I get Unexpected character '�' (Note that you need plugins to import files that are not JavaScript).

Reproduction repo

@pzuraq
Copy link
Contributor Author

pzuraq commented Jan 12, 2024

I dug into this a bit more and figured out why node modules aren't being included in the build despite adding the polyfills to the rollup configuration. It seems that the polyfills are added to the main index.js at /.svelte-kit/output/server/index.js (for our version, though it seems to be pretty much the same in the most recent version). However, this doesn't get assigned to the global and is scoped to the module itself because the output of the rollup build is in esm, and by the time esbuild runs there isn't really a way to ensure that the polyfills are globalized.

Trying to figure out a workaround but it's really difficult without access to the esbuild options in the adapter.

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

No branches or pull requests

5 participants