From d58727f2f7b75d2990161d3c6c807a1d4de4e729 Mon Sep 17 00:00:00 2001 From: Simon Holthausen Date: Wed, 24 Jan 2024 10:25:34 +0100 Subject: [PATCH] fix: exclude server worker from tsconfig again Removing this was an accidental breaking change as part of #10994, strictly speaking. The new solution isn't ideal but very few people will use `$env/static/public` so it's probably fine. fixes #11721 --- .changeset/odd-masks-check.md | 5 +++++ documentation/docs/30-advanced/40-service-workers.md | 2 +- packages/kit/src/core/sync/write_tsconfig.js | 11 ++++++++++- 3 files changed, 16 insertions(+), 2 deletions(-) create mode 100644 .changeset/odd-masks-check.md diff --git a/.changeset/odd-masks-check.md b/.changeset/odd-masks-check.md new file mode 100644 index 000000000000..899ecfeb10fc --- /dev/null +++ b/.changeset/odd-masks-check.md @@ -0,0 +1,5 @@ +--- +"@sveltejs/kit": patch +--- + +fix: exclude server worker from tsconfig again diff --git a/documentation/docs/30-advanced/40-service-workers.md b/documentation/docs/30-advanced/40-service-workers.md index a4888a6ac8de..27dd2a07e678 100644 --- a/documentation/docs/30-advanced/40-service-workers.md +++ b/documentation/docs/30-advanced/40-service-workers.md @@ -143,7 +143,7 @@ const sw = /** @type {ServiceWorkerGlobalScope} */ (/** @type {unknown} */ (self const sw = self as unknown as ServiceWorkerGlobalScope; ``` -This disables access to DOM typings like `HTMLElement` which are not available inside a service worker and instantiates the correct globals. The reassignment of `self` to `sw` allows you to type cast it in the process (there are a couple of ways to do this, but the easiest that requires no additional files). Use `sw` instead of `self` in the rest of the file. The reference to the SvelteKit types ensures that the `$service-worker` import has proper type definitions. +This disables access to DOM typings like `HTMLElement` which are not available inside a service worker and instantiates the correct globals. The reassignment of `self` to `sw` allows you to type cast it in the process (there are a couple of ways to do this, but the easiest that requires no additional files). Use `sw` instead of `self` in the rest of the file. The reference to the SvelteKit types ensures that the `$service-worker` import has proper type definitions. If you import `$env/static/public` you either have to `// @ts-ignore` the import or add `/// ` to the reference types. ## Other solutions diff --git a/packages/kit/src/core/sync/write_tsconfig.js b/packages/kit/src/core/sync/write_tsconfig.js index 73bd239159c9..0bf360ab77d1 100644 --- a/packages/kit/src/core/sync/write_tsconfig.js +++ b/packages/kit/src/core/sync/write_tsconfig.js @@ -55,7 +55,7 @@ export function get_tsconfig(kit) { const config_relative = (file) => posixify(path.relative(kit.outDir, file)); const include = new Set([ - 'ambient.d.ts', + 'ambient.d.ts', // careful: changing this name would be a breaking change, because it's referenced in the service-workers documentation 'non-ambient.d.ts', './types/**/$types.d.ts', config_relative('vite.config.js'), @@ -81,6 +81,15 @@ export function get_tsconfig(kit) { include.add(config_relative(`${test_folder}/**/*.svelte`)); const exclude = [config_relative('node_modules/**')]; + // Add service worker to exclude list so that worker types references in it don't spill over into the rest of the app + // (i.e. suddenly ServiceWorkerGlobalScope would be available throughout the app, and some types might even clash) + if (path.extname(kit.files.serviceWorker)) { + exclude.push(config_relative(kit.files.serviceWorker)); + } else { + exclude.push(config_relative(`${kit.files.serviceWorker}.js`)); + exclude.push(config_relative(`${kit.files.serviceWorker}.ts`)); + exclude.push(config_relative(`${kit.files.serviceWorker}.d.ts`)); + } const config = { compilerOptions: {