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 keepNames esbuild option #148

Closed
fredgig opened this issue Jul 27, 2023 · 4 comments · Fixed by #150
Closed

Add keepNames esbuild option #148

fredgig opened this issue Jul 27, 2023 · 4 comments · Fixed by #150

Comments

@fredgig
Copy link

fredgig commented Jul 27, 2023

When building an application (with node-fetch as an external dependency) for production or local testing via swa start, the runtime error TypeError: Expected signal to be an instanceof AbortSignal comes up. This can be fixed by setting the esbuild option keepNames to true, though the adapter does not seem to take that option into account unless I'm mistaken.

svelte.config.js

import adapter from "svelte-adapter-azure-swa";
import { vitePreprocess } from "@sveltejs/kit/vite";

/** @type {import('@sveltejs/kit').Config} */
const config = {
  preprocess: vitePreprocess(),

  kit: {
    adapter: adapter({
      esbuildOptions: {
        keepNames: true,
      },
    }),
  },
};

export default config;

I have also tried setting the options in vite.config.ts, but to no avail

import { sveltekit } from "@sveltejs/kit/vite";
import { defineConfig } from "vite";

export default defineConfig({
  esbuild: {
    keepNames: true,
  },
  optimizeDeps: {
    esbuildOptions: {
      keepNames: true,
    },
  },
  build: {
    minify: "esbuild"
  },
  plugins: [sveltekit()],
});

This is in reference to an existing issue in this repo: #56

Further documentation on the issue, with the recommendation to set keepNames to true in order to fix the runtime error: node-fetch/node-fetch#784

For the time being, the only way to avoid this error is to comment out the line throwing the error in the generated build/server/sk_render/index.js

Minimum repro: https://github.com/fredgig/sveltekit-azure-repro

  1. npm run build
  2. swa start
  3. See throw new TypeError("Expected signal to be an instanceof AbortSignal"); at line 37296 in build/server/sk_render/index.js
@bananabrann
Copy link
Contributor

I'm having this same issue too with my grandma's TV app (it's secretly a website, don't tell her). Works fine on dev and prod builds locally, but throws this same error regardless of my vite config, both for terser and esbuild, when trying to add a logging feature that fetches a file from Azure Storage

https://black-mushroom-06e3d7310-50.centralus.3.azurestaticapps.net/ask

import { sveltekit } from "@sveltejs/kit/vite";
import { defineConfig } from "vitest/config";

export default defineConfig({
  plugins: [sveltekit()],

  build: {
    minify: "terser",
    terserOptions: {
      mangle: false,
      sourceMap: true,
      compress: false,
      keep_classnames: /AbortSignal/,
      keep_fnames: /AbortSignal/,
      output: {
        comments: false
      }
    }
  },
//...

@geoffrich
Copy link
Owner

PR adding keepNames as an esbuild option welcome! Will probably be similar to #51

I can make the PR myself when I have time, but it might be a few days

@tlaundal
Copy link
Contributor

tlaundal commented Aug 2, 2023

I just remembered that I also ran into this issue when using some azure sdk package for my app.
There are some details about the root cause of this in this comment and the rest of that thread: node-fetch/node-fetch#784 (comment)

The take away from that thread is that the issue is really solved, but some non-ESM packages still have it, because it's only available on the ESM versions of node-fetch. Particularily, some Azure packages are using these old versions.

I think I worked around the issue by avoiding that problematic package and updating some other packages to pre-release versions.

An alternative fix to keepNames might be to mark node-fetch as external. This would then require it to be listed as a dependency in the package.json generated by the adapter. I haven't tested that this actually works.

@geoffrich
Copy link
Owner

You can now set keepNames in svelte-adapter-azure-swa v0.18.0

// svelte.config.js
import azure from 'svelte-adapter-azure-swa';

export default {
	kit: {
		adapter: azure({
			esbuildOptions: {
				keepNames: true
			}
		})
	}
};

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 a pull request may close this issue.

4 participants