From 1e18187450b722592d4839b60a4c4a4076b7c2c2 Mon Sep 17 00:00:00 2001 From: Github Actions Date: Mon, 7 Jun 2021 12:05:57 +0530 Subject: [PATCH 01/10] Add SW files exclusion support --- packages/kit/src/core/config/index.spec.js | 6 +++ packages/kit/src/core/config/options.js | 6 +++ packages/kit/src/core/config/test/index.js | 3 ++ .../src/core/create_manifest_data/index.js | 43 +++++++++++++++++-- .../core/create_manifest_data/index.spec.js | 5 ++- packages/kit/types/config.d.ts | 4 ++ 6 files changed, 62 insertions(+), 5 deletions(-) diff --git a/packages/kit/src/core/config/index.spec.js b/packages/kit/src/core/config/index.spec.js index bd53d4f6e6b2..c224bcfd09e2 100644 --- a/packages/kit/src/core/config/index.spec.js +++ b/packages/kit/src/core/config/index.spec.js @@ -38,6 +38,9 @@ test('fills in defaults', () => { exclude: [] } }, + serviceWorker: { + filesExclusions: [] + }, paths: { base: '', assets: '/.' @@ -133,6 +136,9 @@ test('fills in partial blanks', () => { exclude: [] } }, + serviceWorker: { + filesExclusions: [] + }, paths: { base: '', assets: '/.' diff --git a/packages/kit/src/core/config/options.js b/packages/kit/src/core/config/options.js index c763465039e9..243ff173ea15 100644 --- a/packages/kit/src/core/config/options.js +++ b/packages/kit/src/core/config/options.js @@ -79,6 +79,12 @@ const options = { hostHeader: expect_string(null), hydrate: expect_boolean(true), + serviceWorker: { + type: 'branch', + children: { + filesExclusions: expect_array_of_strings([]) + } + }, package: { type: 'branch', diff --git a/packages/kit/src/core/config/test/index.js b/packages/kit/src/core/config/test/index.js index eb41d36ad697..1b80ff274aa5 100644 --- a/packages/kit/src/core/config/test/index.js +++ b/packages/kit/src/core/config/test/index.js @@ -48,6 +48,9 @@ async function testLoadDefaultConfig(path) { exclude: [] } }, + serviceWorker: { + filesExclusions: [] + }, paths: { base: '', assets: '/.' }, prerender: { crawl: true, enabled: true, force: false, pages: ['*'] }, router: true, diff --git a/packages/kit/src/core/create_manifest_data/index.js b/packages/kit/src/core/create_manifest_data/index.js index e03d2223e58a..a3d301d4c18a 100644 --- a/packages/kit/src/core/create_manifest_data/index.js +++ b/packages/kit/src/core/create_manifest_data/index.js @@ -2,6 +2,7 @@ import fs from 'fs'; import path from 'path'; import mime from 'mime'; import { posixify } from '../utils.js'; +import glob from 'tiny-glob/sync.js'; /** @typedef {{ * content: string; @@ -238,9 +239,36 @@ export default function create_manifest_data({ config, output, cwd = process.cwd walk(config.kit.files.routes, [], [], [layout], [error]); const assets_dir = config.kit.files.assets; + /** + * @type {import('types/internal').Asset[]} + */ + let assets = []; + if (fs.existsSync(assets_dir)) { + /** + * @type {string[]} + */ + let exclusions = config.kit.serviceWorker.filesExclusions || []; + exclusions = [...exclusions, '**/.DS_STORE']; + + /** + * @type {string[]} + */ + let excludedPaths = []; + + exclusions.forEach((exclusion) => { + excludedPaths = [ + ...excludedPaths, + ...glob(exclusion, { + cwd: assets_dir, + dot: true + }) + ]; + }); + assets = list_files(assets_dir, '', [], excludedPaths); + } return { - assets: fs.existsSync(assets_dir) ? list_files(assets_dir, '') : [], + assets, layout, error, components, @@ -375,8 +403,9 @@ function get_pattern(segments, add_trailing_slash) { * @param {string} dir * @param {string} path * @param {import('types/internal').Asset[]} files + * @param {string[]} excludedPaths */ -function list_files(dir, path, files = []) { +function list_files(dir, path, files = [], excludedPaths = []) { fs.readdirSync(dir).forEach((file) => { const full = `${dir}/${file}`; @@ -384,9 +413,15 @@ function list_files(dir, path, files = []) { const joined = path ? `${path}/${file}` : file; if (stats.isDirectory()) { - list_files(full, joined, files); + list_files(full, joined, files, excludedPaths); } else { - if (file === '.DS_Store') return; + if ( + excludedPaths.some((exclusion) => { + return exclusion === joined; + }) + ) { + return; + } files.push({ file: joined, size: stats.size, diff --git a/packages/kit/src/core/create_manifest_data/index.spec.js b/packages/kit/src/core/create_manifest_data/index.spec.js index 2b543a4613e8..aa0d55d1ca23 100644 --- a/packages/kit/src/core/create_manifest_data/index.spec.js +++ b/packages/kit/src/core/create_manifest_data/index.spec.js @@ -21,7 +21,10 @@ const create = (dir, extensions = ['.svelte']) => { assets: path.resolve(cwd, 'static'), routes: path.resolve(cwd, dir) }, - appDir: '_app' + appDir: '_app', + serviceWorker: { + filesExclusions: [] + } } }, cwd, diff --git a/packages/kit/types/config.d.ts b/packages/kit/types/config.d.ts index 5471b5dbdc89..eac43f573b61 100644 --- a/packages/kit/types/config.d.ts +++ b/packages/kit/types/config.d.ts @@ -94,6 +94,10 @@ export type ValidatedConfig = { host: string; hostHeader: string; hydrate: boolean; + serviceWorker: { + // Globs relative to assets folder. + filesExclusions: string[]; + }; package: { dir: string; exports: { From 3f0160f37bb80965045df1ffc7594ed3e4dec55b Mon Sep 17 00:00:00 2001 From: Github Actions Date: Mon, 7 Jun 2021 17:05:56 +0530 Subject: [PATCH 02/10] Add documentation and simplify code --- packages/kit/src/core/create_manifest_data/index.js | 10 ++++------ packages/kit/types/config.d.ts | 4 +++- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/packages/kit/src/core/create_manifest_data/index.js b/packages/kit/src/core/create_manifest_data/index.js index a3d301d4c18a..86bd9d7e7e44 100644 --- a/packages/kit/src/core/create_manifest_data/index.js +++ b/packages/kit/src/core/create_manifest_data/index.js @@ -248,6 +248,8 @@ export default function create_manifest_data({ config, output, cwd = process.cwd * @type {string[]} */ let exclusions = config.kit.serviceWorker.filesExclusions || []; + + // .DS_STORE files are automatically removed to keep the compatiblity exclusions = [...exclusions, '**/.DS_STORE']; /** @@ -403,7 +405,7 @@ function get_pattern(segments, add_trailing_slash) { * @param {string} dir * @param {string} path * @param {import('types/internal').Asset[]} files - * @param {string[]} excludedPaths + * @param {string[]} excludedPaths Paths relative to dir which should be excluded from files list. */ function list_files(dir, path, files = [], excludedPaths = []) { fs.readdirSync(dir).forEach((file) => { @@ -415,11 +417,7 @@ function list_files(dir, path, files = [], excludedPaths = []) { if (stats.isDirectory()) { list_files(full, joined, files, excludedPaths); } else { - if ( - excludedPaths.some((exclusion) => { - return exclusion === joined; - }) - ) { + if (excludedPaths.includes(joined)) { return; } files.push({ diff --git a/packages/kit/types/config.d.ts b/packages/kit/types/config.d.ts index eac43f573b61..4a3e7d68f054 100644 --- a/packages/kit/types/config.d.ts +++ b/packages/kit/types/config.d.ts @@ -95,7 +95,9 @@ export type ValidatedConfig = { hostHeader: string; hydrate: boolean; serviceWorker: { - // Globs relative to assets folder. + // Glob patterns relative to `files.assets` dir. Files matching this would not be available in $service-worker.files + // e.g. if `files.assets` has value `static` then ['og-tags-images/**/*'] would match all files under `static/og-tags-images` dir. + // As og-tags-images are never loaded by a normal client(browser), they can be comfortably skipped from here. filesExclusions: string[]; }; package: { From c9f86e01300e67dfa3ddbcd1f343eefc75b06b95 Mon Sep 17 00:00:00 2001 From: Github Actions Date: Mon, 7 Jun 2021 20:54:55 +0530 Subject: [PATCH 03/10] Update public facing docs --- documentation/docs/05-modules.md | 2 +- documentation/docs/14-configuration.md | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/documentation/docs/05-modules.md b/documentation/docs/05-modules.md index 25d8608c6b28..2ddec83f7084 100644 --- a/documentation/docs/05-modules.md +++ b/documentation/docs/05-modules.md @@ -66,5 +66,5 @@ import { build, files, timestamp } from '$service-worker'; ``` - `build` is an array of URL strings representing the files generated by Vite, suitable for caching with `cache.addAll(build)` -- `files` is an array of URL strings representing the files in your `static` directory, or whatever directory is specified by [`config.kit.files.assets`](#configuration) +- `files` is an array of URL strings representing the files in your `static` directory, or whatever directory is specified by [`config.kit.files.assets`](#configuration). You can exclude certain files from `static` directory using [`config.kit.serviceWorker.filesExclusions`](#configuration) - `timestamp` is the result of calling `Date.now()` at build time. It's useful for generating unique cache names inside your service worker, so that a later deployment of your app can invalidate old caches diff --git a/documentation/docs/14-configuration.md b/documentation/docs/14-configuration.md index 598704a5d02b..b3722f59c3b1 100644 --- a/documentation/docs/14-configuration.md +++ b/documentation/docs/14-configuration.md @@ -25,6 +25,9 @@ const config = { serviceWorker: 'src/service-worker', template: 'src/app.html' }, + serviceWorker: { + filesExclusions: [] + }, floc: false, host: null, hostHeader: null, @@ -76,6 +79,12 @@ An object containing zero or more of the following `string` values: - `hooks` — the location of your hooks module (see [Hooks](#hooks)) - `template` — the location of the template for HTML responses +### serviceWorker + +An object containing zero or more of the following values: + +- `filesExclusions` - an array of glob patterns relative to `files.assets` dir. Files matching any of these would not be available in `$service-worker.files` e.g. if `files.assets` has value `static` then ['og-tags-images/**/*'] would match all files under `static/og-tags-images` dir. + ### floc Google's [FLoC](https://github.com/WICG/floc) is a technology for targeted advertising that the [Electronic Frontier Foundation](https://www.eff.org/) has deemed [harmful](https://www.eff.org/deeplinks/2021/03/googles-floc-terrible-idea) to user privacy. [Browsers other than Chrome](https://www.theverge.com/2021/4/16/22387492/google-floc-ad-tech-privacy-browsers-brave-vivaldi-edge-mozilla-chrome-safari) have declined to implement it. From 2a30ac4b4af5cacbe6775bad5108640356ef83a0 Mon Sep 17 00:00:00 2001 From: Github Actions Date: Wed, 16 Jun 2021 12:02:54 +0530 Subject: [PATCH 04/10] Define `serviceWorker` in Config type as well --- packages/kit/types/config.d.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/packages/kit/types/config.d.ts b/packages/kit/types/config.d.ts index 4a3e7d68f054..a3f37d180e74 100644 --- a/packages/kit/types/config.d.ts +++ b/packages/kit/types/config.d.ts @@ -44,6 +44,12 @@ export type Config = { host?: string; hostHeader?: string; hydrate?: boolean; + serviceWorker?: { + // Glob patterns relative to `files.assets` dir. Files matching this would not be available in $service-worker.files + // e.g. if `files.assets` has value `static` then ['og-tags-images/**/*'] would match all files under `static/og-tags-images` dir. + // As og-tags-images are never loaded by a normal client(browser), they can be comfortably skipped from here. + filesExclusions?: string[]; + }; package?: { dir?: string; exports?: { @@ -95,9 +101,6 @@ export type ValidatedConfig = { hostHeader: string; hydrate: boolean; serviceWorker: { - // Glob patterns relative to `files.assets` dir. Files matching this would not be available in $service-worker.files - // e.g. if `files.assets` has value `static` then ['og-tags-images/**/*'] would match all files under `static/og-tags-images` dir. - // As og-tags-images are never loaded by a normal client(browser), they can be comfortably skipped from here. filesExclusions: string[]; }; package: { From 86c109e3f0c71e5eb2ca94ed2e2c42b777b9b66d Mon Sep 17 00:00:00 2001 From: Github Actions Date: Sun, 20 Jun 2021 17:45:35 +0530 Subject: [PATCH 05/10] Better name for the config --- packages/kit/src/core/config/index.spec.js | 4 ++-- packages/kit/src/core/config/options.js | 2 +- packages/kit/src/core/config/test/index.js | 2 +- packages/kit/src/core/create_manifest_data/index.js | 2 +- packages/kit/src/core/create_manifest_data/index.spec.js | 2 +- packages/kit/types/config.d.ts | 4 ++-- 6 files changed, 8 insertions(+), 8 deletions(-) diff --git a/packages/kit/src/core/config/index.spec.js b/packages/kit/src/core/config/index.spec.js index c224bcfd09e2..73e71ff21e12 100644 --- a/packages/kit/src/core/config/index.spec.js +++ b/packages/kit/src/core/config/index.spec.js @@ -39,7 +39,7 @@ test('fills in defaults', () => { } }, serviceWorker: { - filesExclusions: [] + exclude: [] }, paths: { base: '', @@ -137,7 +137,7 @@ test('fills in partial blanks', () => { } }, serviceWorker: { - filesExclusions: [] + exclude: [] }, paths: { base: '', diff --git a/packages/kit/src/core/config/options.js b/packages/kit/src/core/config/options.js index 243ff173ea15..c0fed8614f7a 100644 --- a/packages/kit/src/core/config/options.js +++ b/packages/kit/src/core/config/options.js @@ -82,7 +82,7 @@ const options = { serviceWorker: { type: 'branch', children: { - filesExclusions: expect_array_of_strings([]) + exclude: expect_array_of_strings([]) } }, diff --git a/packages/kit/src/core/config/test/index.js b/packages/kit/src/core/config/test/index.js index 1b80ff274aa5..8a0d8e0779fd 100644 --- a/packages/kit/src/core/config/test/index.js +++ b/packages/kit/src/core/config/test/index.js @@ -49,7 +49,7 @@ async function testLoadDefaultConfig(path) { } }, serviceWorker: { - filesExclusions: [] + exclude: [] }, paths: { base: '', assets: '/.' }, prerender: { crawl: true, enabled: true, force: false, pages: ['*'] }, diff --git a/packages/kit/src/core/create_manifest_data/index.js b/packages/kit/src/core/create_manifest_data/index.js index 86bd9d7e7e44..ca7256b4fc73 100644 --- a/packages/kit/src/core/create_manifest_data/index.js +++ b/packages/kit/src/core/create_manifest_data/index.js @@ -247,7 +247,7 @@ export default function create_manifest_data({ config, output, cwd = process.cwd /** * @type {string[]} */ - let exclusions = config.kit.serviceWorker.filesExclusions || []; + let exclusions = config.kit.serviceWorker.exclude || []; // .DS_STORE files are automatically removed to keep the compatiblity exclusions = [...exclusions, '**/.DS_STORE']; diff --git a/packages/kit/src/core/create_manifest_data/index.spec.js b/packages/kit/src/core/create_manifest_data/index.spec.js index aa0d55d1ca23..bd07cbc93826 100644 --- a/packages/kit/src/core/create_manifest_data/index.spec.js +++ b/packages/kit/src/core/create_manifest_data/index.spec.js @@ -23,7 +23,7 @@ const create = (dir, extensions = ['.svelte']) => { }, appDir: '_app', serviceWorker: { - filesExclusions: [] + exclude: [] } } }, diff --git a/packages/kit/types/config.d.ts b/packages/kit/types/config.d.ts index a3f37d180e74..ba50f030aafd 100644 --- a/packages/kit/types/config.d.ts +++ b/packages/kit/types/config.d.ts @@ -48,7 +48,7 @@ export type Config = { // Glob patterns relative to `files.assets` dir. Files matching this would not be available in $service-worker.files // e.g. if `files.assets` has value `static` then ['og-tags-images/**/*'] would match all files under `static/og-tags-images` dir. // As og-tags-images are never loaded by a normal client(browser), they can be comfortably skipped from here. - filesExclusions?: string[]; + exclude?: string[]; }; package?: { dir?: string; @@ -101,7 +101,7 @@ export type ValidatedConfig = { hostHeader: string; hydrate: boolean; serviceWorker: { - filesExclusions: string[]; + exclude: string[]; }; package: { dir: string; From c28a824c5502f2e96b25376529c13c0cc1e9ffc3 Mon Sep 17 00:00:00 2001 From: Github Actions Date: Sun, 20 Jun 2021 18:13:33 +0530 Subject: [PATCH 06/10] Remove comments from undesired location and change option name --- documentation/docs/05-modules.md | 2 +- documentation/docs/14-configuration.md | 4 ++-- packages/kit/src/core/create_manifest_data/index.js | 4 ++-- packages/kit/types/config.d.ts | 3 --- 4 files changed, 5 insertions(+), 8 deletions(-) diff --git a/documentation/docs/05-modules.md b/documentation/docs/05-modules.md index 2ddec83f7084..292182ea2218 100644 --- a/documentation/docs/05-modules.md +++ b/documentation/docs/05-modules.md @@ -66,5 +66,5 @@ import { build, files, timestamp } from '$service-worker'; ``` - `build` is an array of URL strings representing the files generated by Vite, suitable for caching with `cache.addAll(build)` -- `files` is an array of URL strings representing the files in your `static` directory, or whatever directory is specified by [`config.kit.files.assets`](#configuration). You can exclude certain files from `static` directory using [`config.kit.serviceWorker.filesExclusions`](#configuration) +- `files` is an array of URL strings representing the files in your `static` directory, or whatever directory is specified by [`config.kit.files.assets`](#configuration). You can exclude certain files from `static` directory using [`config.kit.serviceWorker.exclude`](#configuration) - `timestamp` is the result of calling `Date.now()` at build time. It's useful for generating unique cache names inside your service worker, so that a later deployment of your app can invalidate old caches diff --git a/documentation/docs/14-configuration.md b/documentation/docs/14-configuration.md index b3722f59c3b1..830c8c180642 100644 --- a/documentation/docs/14-configuration.md +++ b/documentation/docs/14-configuration.md @@ -26,7 +26,7 @@ const config = { template: 'src/app.html' }, serviceWorker: { - filesExclusions: [] + exclude: [] }, floc: false, host: null, @@ -83,7 +83,7 @@ An object containing zero or more of the following `string` values: An object containing zero or more of the following values: -- `filesExclusions` - an array of glob patterns relative to `files.assets` dir. Files matching any of these would not be available in `$service-worker.files` e.g. if `files.assets` has value `static` then ['og-tags-images/**/*'] would match all files under `static/og-tags-images` dir. +- `exclude` - an array of glob patterns relative to `files.assets` dir. Files matching any of these would not be available in `$service-worker.files` e.g. if `files.assets` has value `static` then ['og-tags-images/**/*'] would match all files under `static/og-tags-images` dir. ### floc diff --git a/packages/kit/src/core/create_manifest_data/index.js b/packages/kit/src/core/create_manifest_data/index.js index ca7256b4fc73..c24f474336af 100644 --- a/packages/kit/src/core/create_manifest_data/index.js +++ b/packages/kit/src/core/create_manifest_data/index.js @@ -247,10 +247,10 @@ export default function create_manifest_data({ config, output, cwd = process.cwd /** * @type {string[]} */ - let exclusions = config.kit.serviceWorker.exclude || []; + const exclusions = config.kit.serviceWorker.exclude || []; // .DS_STORE files are automatically removed to keep the compatiblity - exclusions = [...exclusions, '**/.DS_STORE']; + exclusions.push('**/.DS_STORE'); /** * @type {string[]} diff --git a/packages/kit/types/config.d.ts b/packages/kit/types/config.d.ts index ba50f030aafd..48c95bf93425 100644 --- a/packages/kit/types/config.d.ts +++ b/packages/kit/types/config.d.ts @@ -45,9 +45,6 @@ export type Config = { hostHeader?: string; hydrate?: boolean; serviceWorker?: { - // Glob patterns relative to `files.assets` dir. Files matching this would not be available in $service-worker.files - // e.g. if `files.assets` has value `static` then ['og-tags-images/**/*'] would match all files under `static/og-tags-images` dir. - // As og-tags-images are never loaded by a normal client(browser), they can be comfortably skipped from here. exclude?: string[]; }; package?: { From f6c8c196ad1163e262caeea9c8fc63905e5e764f Mon Sep 17 00:00:00 2001 From: hariombalhara Date: Wed, 7 Jul 2021 08:55:02 +0530 Subject: [PATCH 07/10] Remove unnecessary comment Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com> --- packages/kit/src/core/create_manifest_data/index.js | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/kit/src/core/create_manifest_data/index.js b/packages/kit/src/core/create_manifest_data/index.js index c24f474336af..959a3f706acf 100644 --- a/packages/kit/src/core/create_manifest_data/index.js +++ b/packages/kit/src/core/create_manifest_data/index.js @@ -249,7 +249,6 @@ export default function create_manifest_data({ config, output, cwd = process.cwd */ const exclusions = config.kit.serviceWorker.exclude || []; - // .DS_STORE files are automatically removed to keep the compatiblity exclusions.push('**/.DS_STORE'); /** From 5ea3a14897a3c8138ad34cdf7c76ca0540ca8a56 Mon Sep 17 00:00:00 2001 From: hariombalhara Date: Wed, 7 Jul 2021 08:55:35 +0530 Subject: [PATCH 08/10] Use underscored variable names for internal variables Co-authored-by: Ben McCann <322311+benmccann@users.noreply.github.com> --- packages/kit/src/core/create_manifest_data/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/kit/src/core/create_manifest_data/index.js b/packages/kit/src/core/create_manifest_data/index.js index 959a3f706acf..46e477d248c0 100644 --- a/packages/kit/src/core/create_manifest_data/index.js +++ b/packages/kit/src/core/create_manifest_data/index.js @@ -406,7 +406,7 @@ function get_pattern(segments, add_trailing_slash) { * @param {import('types/internal').Asset[]} files * @param {string[]} excludedPaths Paths relative to dir which should be excluded from files list. */ -function list_files(dir, path, files = [], excludedPaths = []) { +function list_files(dir, path, files = [], excluded_paths = []) { fs.readdirSync(dir).forEach((file) => { const full = `${dir}/${file}`; From 8e3b1817a7a3fbbd8e70b09bd8496ba9c664e91f Mon Sep 17 00:00:00 2001 From: Github Actions Date: Wed, 7 Jul 2021 09:09:48 +0530 Subject: [PATCH 09/10] Rename variable elsewhere as well --- .../kit/src/core/create_manifest_data/index.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/packages/kit/src/core/create_manifest_data/index.js b/packages/kit/src/core/create_manifest_data/index.js index 46e477d248c0..2a166e69ab72 100644 --- a/packages/kit/src/core/create_manifest_data/index.js +++ b/packages/kit/src/core/create_manifest_data/index.js @@ -254,18 +254,18 @@ export default function create_manifest_data({ config, output, cwd = process.cwd /** * @type {string[]} */ - let excludedPaths = []; + let excluded_paths = []; exclusions.forEach((exclusion) => { - excludedPaths = [ - ...excludedPaths, + excluded_paths = [ + ...excluded_paths, ...glob(exclusion, { cwd: assets_dir, dot: true }) ]; }); - assets = list_files(assets_dir, '', [], excludedPaths); + assets = list_files(assets_dir, '', [], excluded_paths); } return { @@ -404,7 +404,7 @@ function get_pattern(segments, add_trailing_slash) { * @param {string} dir * @param {string} path * @param {import('types/internal').Asset[]} files - * @param {string[]} excludedPaths Paths relative to dir which should be excluded from files list. + * @param {string[]} excluded_paths Paths relative to dir which should be excluded from files list. */ function list_files(dir, path, files = [], excluded_paths = []) { fs.readdirSync(dir).forEach((file) => { @@ -414,9 +414,9 @@ function list_files(dir, path, files = [], excluded_paths = []) { const joined = path ? `${path}/${file}` : file; if (stats.isDirectory()) { - list_files(full, joined, files, excludedPaths); + list_files(full, joined, files, excluded_paths); } else { - if (excludedPaths.includes(joined)) { + if (excluded_paths.includes(joined)) { return; } files.push({ From d8850660d91a22a0d113eec5a3766eefb57b7c7d Mon Sep 17 00:00:00 2001 From: Github Actions Date: Wed, 7 Jul 2021 09:28:13 +0530 Subject: [PATCH 10/10] Add changeset --- .changeset/pink-poets-begin.md | 5 ++ .../src/core/create_manifest_data/index.js | 70 +++++++++++-------- 2 files changed, 44 insertions(+), 31 deletions(-) create mode 100644 .changeset/pink-poets-begin.md diff --git a/.changeset/pink-poets-begin.md b/.changeset/pink-poets-begin.md new file mode 100644 index 000000000000..78fad02b937b --- /dev/null +++ b/.changeset/pink-poets-begin.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/kit': patch +--- + +ServiceWorker files exclusion support available through svelte.config.js diff --git a/packages/kit/src/core/create_manifest_data/index.js b/packages/kit/src/core/create_manifest_data/index.js index 2a166e69ab72..6c1c8e580d04 100644 --- a/packages/kit/src/core/create_manifest_data/index.js +++ b/packages/kit/src/core/create_manifest_data/index.js @@ -23,6 +23,44 @@ import glob from 'tiny-glob/sync.js'; const specials = new Set(['__layout', '__layout.reset', '__error']); +/** + * + * @param {import('types/config').ValidatedConfig} config + * @returns {import('types/internal').ManifestData['assets']} + */ +function get_assets_list(config) { + const assets_dir = config.kit.files.assets; + /** + * @type {import('types/internal').Asset[]} + */ + let assets = []; + if (fs.existsSync(assets_dir)) { + /** + * @type {string[]} + */ + const exclusions = config.kit.serviceWorker.exclude || []; + + exclusions.push('**/.DS_STORE'); + + /** + * @type {string[]} + */ + let excluded_paths = []; + + exclusions.forEach((exclusion) => { + excluded_paths = [ + ...excluded_paths, + ...glob(exclusion, { + cwd: assets_dir, + dot: true + }) + ]; + }); + assets = list_files(assets_dir, '', [], excluded_paths); + } + return assets; +} + /** * @param {{ * config: import('types/config').ValidatedConfig; @@ -238,38 +276,8 @@ export default function create_manifest_data({ config, output, cwd = process.cwd walk(config.kit.files.routes, [], [], [layout], [error]); - const assets_dir = config.kit.files.assets; - /** - * @type {import('types/internal').Asset[]} - */ - let assets = []; - if (fs.existsSync(assets_dir)) { - /** - * @type {string[]} - */ - const exclusions = config.kit.serviceWorker.exclude || []; - - exclusions.push('**/.DS_STORE'); - - /** - * @type {string[]} - */ - let excluded_paths = []; - - exclusions.forEach((exclusion) => { - excluded_paths = [ - ...excluded_paths, - ...glob(exclusion, { - cwd: assets_dir, - dot: true - }) - ]; - }); - assets = list_files(assets_dir, '', [], excluded_paths); - } - return { - assets, + assets: get_assets_list(config), layout, error, components,