From 197e01f95652f511160f38b37b9da73a124ecd48 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Tue, 19 Dec 2023 11:47:36 -0500 Subject: [PATCH] Revert "fix: build service workers in IIFE format" (#11400) * Revert "fix: build service workers in IIFE format (#11129)" This reverts commit 5f052a34572f439894ade2c6ef37bae573a96b16. * better fix * changeset * remove format --------- Co-authored-by: Rich Harris --- .changeset/green-cycles-shake.md | 5 +++++ .changeset/tricky-meals-perform.md | 5 ----- .../kit/src/exports/vite/build/build_service_worker.js | 9 +++++---- 3 files changed, 10 insertions(+), 9 deletions(-) create mode 100644 .changeset/green-cycles-shake.md delete mode 100644 .changeset/tricky-meals-perform.md diff --git a/.changeset/green-cycles-shake.md b/.changeset/green-cycles-shake.md new file mode 100644 index 000000000000..5b0d39476f01 --- /dev/null +++ b/.changeset/green-cycles-shake.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/kit': patch +--- + +fix: prevent esbuild adding phantom exports to service worker diff --git a/.changeset/tricky-meals-perform.md b/.changeset/tricky-meals-perform.md deleted file mode 100644 index 029af8321852..000000000000 --- a/.changeset/tricky-meals-perform.md +++ /dev/null @@ -1,5 +0,0 @@ ---- -'@sveltejs/kit': patch ---- - -fix: build service workers in IIFE format diff --git a/packages/kit/src/exports/vite/build/build_service_worker.js b/packages/kit/src/exports/vite/build/build_service_worker.js index 8bb292dc68c6..3d3849c6d727 100644 --- a/packages/kit/src/exports/vite/build/build_service_worker.js +++ b/packages/kit/src/exports/vite/build/build_service_worker.js @@ -70,10 +70,8 @@ export async function build_service_worker( 'service-worker': service_worker_entry_file }, output: { - // default 'es' format would be nicer - // iife is workaround for https://github.com/vitejs/vite/issues/15379 - format: 'iife', - entryFileNames: '[name].js', + // .mjs so that esbuild doesn't incorrectly inject `export` https://github.com/vitejs/vite/issues/15379 + entryFileNames: 'service-worker.mjs', assetFileNames: `${kit.appDir}/immutable/assets/[name].[hash][extname]`, inlineDynamicImports: true } @@ -95,4 +93,7 @@ export async function build_service_worker( } } }); + + // rename .mjs to .js to avoid incorrect MIME types with ancient webservers + fs.renameSync(`${out}/client/service-worker.mjs`, `${out}/client/service-worker.js`); }