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 2a719d4392d4..11d880698745 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 this is 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 dc12107b4435..fdbb99973a23 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('svelte.config.js'), @@ -82,6 +82,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: {