@wells-nelson/build-tools 0.0.1 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -6,12 +6,12 @@
6
6
  * ```ts
7
7
  * // src/service-worker/index.ts
8
8
  * /// <reference types="@sveltejs/kit" />
9
- * import { version } from '$app/env';
9
+ * import { dev, version } from '$app/env';
10
10
  * import { assets, immutable } from '$app/manifest';
11
11
  * import { self } from '$app/service-worker';
12
12
  * import { registerAppShellCache } from '@wells-nelson/build-tools/service-worker';
13
13
  *
14
- * registerAppShellCache({ sw: self, version, files: [...immutable, ...assets], cachePrefix: 'my-app' });
14
+ * registerAppShellCache({ sw: self, version, dev, files: [...immutable, ...assets], cachePrefix: 'my-app' });
15
15
  * ```
16
16
  */
17
17
  /** The parts of ServiceWorkerGlobalScope this uses (so it type-checks without the WebWorker lib). */
@@ -35,8 +35,16 @@ export interface AppShellCacheOptions {
35
35
  }>;
36
36
  /** Cache name prefix (default 'app'). */
37
37
  cachePrefix?: string;
38
+ /**
39
+ * Development: cache nothing and delete what an earlier worker cached, so every code change
40
+ * loads (a cache-first worker would otherwise keep serving stale dev modules).
41
+ */
42
+ dev?: boolean;
38
43
  }
39
- export declare function registerAppShellCache({ sw, version, files, cachePrefix }: AppShellCacheOptions): {
44
+ export declare function registerAppShellCache({ sw, version, files, cachePrefix, dev }: AppShellCacheOptions): {
45
+ cacheName: null;
46
+ precache: never[];
47
+ } | {
40
48
  cacheName: string;
41
49
  precache: string[];
42
50
  };
@@ -6,16 +6,28 @@
6
6
  * ```ts
7
7
  * // src/service-worker/index.ts
8
8
  * /// <reference types="@sveltejs/kit" />
9
- * import { version } from '$app/env';
9
+ * import { dev, version } from '$app/env';
10
10
  * import { assets, immutable } from '$app/manifest';
11
11
  * import { self } from '$app/service-worker';
12
12
  * import { registerAppShellCache } from '@wells-nelson/build-tools/service-worker';
13
13
  *
14
- * registerAppShellCache({ sw: self, version, files: [...immutable, ...assets], cachePrefix: 'my-app' });
14
+ * registerAppShellCache({ sw: self, version, dev, files: [...immutable, ...assets], cachePrefix: 'my-app' });
15
15
  * ```
16
16
  */
17
- export function registerAppShellCache({ sw, version, files, cachePrefix = 'app' }) {
17
+ export function registerAppShellCache({ sw, version, files, cachePrefix = 'app', dev = false }) {
18
18
  const CACHE = `${cachePrefix}-cache-${version}`;
19
+ if (dev) {
20
+ sw.addEventListener('install', (event) => event.waitUntil(sw.skipWaiting()));
21
+ sw.addEventListener('activate', (event) => {
22
+ event.waitUntil((async () => {
23
+ for (const key of await caches.keys())
24
+ if (key.startsWith(`${cachePrefix}-cache-`))
25
+ await caches.delete(key);
26
+ await sw.clients.claim();
27
+ })());
28
+ });
29
+ return { cacheName: null, precache: [] };
30
+ }
19
31
  const scope = new URL(sw.registration.scope);
20
32
  // The app root serves the SPA shell (index.html on GitHub Pages, the fallback page elsewhere).
21
33
  const SHELL = scope.href;
package/dist/vite.d.ts CHANGED
@@ -16,8 +16,19 @@ export declare function basePath({ mode, env, fallback }: {
16
16
  * break ("Cannot read properties of undefined (reading 'call')" / a 500 page) until a full reload.
17
17
  */
18
18
  export declare const SHARED_OPTIMIZE_DEPS: string[];
19
- /** `optimizeDeps` with the shared list plus the app's own lazily loaded deps (e.g. a search library). */
20
- export declare function optimizeDeps(extra?: string[]): {
19
+ /**
20
+ * Entry points of the Wells Nelson packages. They must be pre-bundled at startup too: one
21
+ * imported only by a lazily loaded page (e.g. `interactions/account`) or by the service worker
22
+ * otherwise triggers the same mid-session re-bundle.
23
+ */
24
+ export declare const WELLS_NELSON_ENTRIES: string[];
25
+ /** The entries that resolve from `root` (an app installs only the packages it uses). */
26
+ export declare function installedEntries(entries: string[], root?: string): string[];
27
+ /**
28
+ * `optimizeDeps` with the shared runtime dependencies, every installed Wells Nelson entry point,
29
+ * and the app's own lazily loaded deps (e.g. a search library).
30
+ */
31
+ export declare function optimizeDeps(extra?: string[], root?: string): {
21
32
  include: string[];
22
33
  };
23
34
  /**
package/dist/vite.js CHANGED
@@ -1,5 +1,6 @@
1
1
  import { existsSync, readFileSync } from 'node:fs';
2
- import { resolve } from 'node:path';
2
+ import { createRequire } from 'node:module';
3
+ import { join, resolve } from 'node:path';
3
4
  /**
4
5
  * SvelteKit `paths.base` from the BASE_PATH env var: the one place the deploy path is configured.
5
6
  * GitHub project pages are served from /<repo-name> (the `fallback`); set BASE_PATH to "/" for
@@ -23,9 +24,37 @@ export const SHARED_OPTIMIZE_DEPS = [
23
24
  'rxdb/plugins/replication-supabase',
24
25
  'rxdb/plugins/storage-dexie'
25
26
  ];
26
- /** `optimizeDeps` with the shared list plus the app's own lazily loaded deps (e.g. a search library). */
27
- export function optimizeDeps(extra = []) {
28
- return { include: [...extra, ...SHARED_OPTIMIZE_DEPS] };
27
+ /**
28
+ * Entry points of the Wells Nelson packages. They must be pre-bundled at startup too: one
29
+ * imported only by a lazily loaded page (e.g. `interactions/account`) or by the service worker
30
+ * otherwise triggers the same mid-session re-bundle.
31
+ */
32
+ export const WELLS_NELSON_ENTRIES = [
33
+ '@wells-nelson/displays',
34
+ '@wells-nelson/interactions',
35
+ '@wells-nelson/interactions/account',
36
+ '@wells-nelson/data-storage',
37
+ '@wells-nelson/build-tools/service-worker'
38
+ ];
39
+ /** The entries that resolve from `root` (an app installs only the packages it uses). */
40
+ export function installedEntries(entries, root = process.cwd()) {
41
+ const require = createRequire(join(root, 'package.json'));
42
+ return entries.filter((id) => {
43
+ try {
44
+ require.resolve(id);
45
+ return true;
46
+ }
47
+ catch {
48
+ return false;
49
+ }
50
+ });
51
+ }
52
+ /**
53
+ * `optimizeDeps` with the shared runtime dependencies, every installed Wells Nelson entry point,
54
+ * and the app's own lazily loaded deps (e.g. a search library).
55
+ */
56
+ export function optimizeDeps(extra = [], root = process.cwd()) {
57
+ return { include: [...extra, ...installedEntries(WELLS_NELSON_ENTRIES, root), ...SHARED_OPTIMIZE_DEPS] };
29
58
  }
30
59
  /**
31
60
  * Serve a git-ignored JSON file (e.g. seed data with real names) as a virtual module to the dev
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wells-nelson/build-tools",
3
- "version": "0.0.1",
3
+ "version": "1.0.1",
4
4
  "description": "Build and deployment tooling for Wells Nelson SvelteKit PWAs: postbuild, PWA icon generator, Vite config pieces, service worker, and templates.",
5
5
  "license": "MIT",
6
6
  "repository": {