kit: [bug] service workers built in wrong format

Describe the bug

In src/service-worker.ts I have the sole line:

import { relayPool } from 'nostr-tools'

but the bundle (build/service-worker.js) contains:

export default Fl();

The same happens if I import nostr from 'nostr-tools' (or probably anything other nostr-tools.

My bundle is free of exports when importing Dexie from 'dexie' or a bunch of other imports.

I tried to figure out in the vite discord if this “transient” export might be a feature of sorts but I’m more and more convinced it is a bug in the bundler.

Reproduction

https://github.com/Giszmo/tmpForBugReport is a blank svelteKit project with the added dependency nostr-tools and src/service-worker.ts` consisting of only an import from that dependency.

Logs

$ npm run build && npm run preview 
... (no errors)

Chromium dev console:


Uncaught SyntaxError: Unexpected token 'export' (at service-worker.js:6207:1)


System Info

$ npx envinfo --system --binaries --browsers --npmPackages "{svelte,@sveltejs/*,vite}"
Need to install the following packages:
  envinfo@7.8.1
Ok to proceed? (y) 

  System:
    OS: Linux 5.10 Debian GNU/Linux 11 (bullseye) 11 (bullseye)
    CPU: (24) x64 AMD Ryzen 9 3900X 12-Core Processor
    Memory: 17.28 GB / 31.33 GB
    Container: Yes
    Shell: 5.1.4 - /bin/bash
  Binaries:
    Node: 18.6.0 - ~/.nvm/versions/node/v18.6.0/bin/node
    npm: 8.13.2 - ~/.nvm/versions/node/v18.6.0/bin/npm
  Browsers:
    Chromium: 103.0.5060.53
    Firefox: 96.0.3
  npmPackages:
    @sveltejs/adapter-auto: next => 1.0.0-next.71 
    @sveltejs/kit: next => 1.0.0-next.476 
    svelte: ^3.46.0 => 3.50.0 
    vite: ^3.1.0 => 3.1.0 

Severity

blocking an upgrade

Additional Information

I need to move nostr stuff into a service-worker and this bug is blocking this necessary upgrade. On vite discord I couldn’t find how to prevent this “transitive” export.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 16 (16 by maintainers)

Most upvoted comments

just FYI, we ended up swapping out your workaround for another one: https://github.com/sveltejs/kit/pull/11400

Summary: @Gizmo is using nostr-tools package in the service worker, I’ve created this repo https://github.com/userquin/nostroid-with-pwa-plugin to try import the package:

  • I enable dev options in vite-plugin-pwa: the plugin will delegate the build to Vite registering the service worker with type module (enabled in devOptions.type option in pwa plugin): the service worker fails when installing, readable-stream with the global error
  • once readable-stream patched, the service worker with type module works in dev
  • next step was build the service worker and test it with vite preview: the service worker on build was generated with export default; applying the patch to pwa plugin switching Rollup build format from es to iife works

SvelteKit just uses Vite for bundling, so you should really file this issue in the Vite issue tracker: https://github.com/sveltejs/kit#bug-reporting