spark-html-offline 0.1.1 → 0.1.2

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.
package/README.md CHANGED
@@ -17,11 +17,10 @@ offline();
17
17
  ```
18
18
 
19
19
  ```js
20
- // vite.config.js — emits /spark-sw.js in build, serves it in dev
21
- import spark from 'spark-html/vite';
22
- import offlineSw from 'spark-html-offline/vite';
20
+ // spark.config.js — writes /spark-sw.js in build, serves it in dev
21
+ import offlineSw from 'spark-html-offline/bun';
23
22
 
24
- export default { plugins: [spark(), offlineSw()] };
23
+ export default { pipeline: [offlineSw()] };
25
24
  ```
26
25
 
27
26
  ```html
@@ -54,9 +53,9 @@ own assets behave exactly as before.
54
53
  ## Options
55
54
 
56
55
  ```js
57
- // vite.config.js
56
+ // spark.config.js
58
57
  offlineSw({
59
- file: 'spark-sw.js', // emitted worker file name
58
+ file: 'spark-sw.js', // written worker file name
60
59
  include: ['/components/'], // ALSO cache these same-origin paths
61
60
  exclude: ['/api/'], // never touch these (substring match)
62
61
  cacheName: 'spark-offline-v1',
@@ -77,7 +76,7 @@ just without the safety net.
77
76
 
78
77
  ## No build step?
79
78
 
80
- You don't need Vite. Generate the worker once and host it next to
79
+ You don't need the build step. Generate the worker once and host it next to
81
80
  `index.html`:
82
81
 
83
82
  ```js
@@ -96,7 +95,7 @@ Then call `offline()` from any `<script type="module">`.
96
95
  | `swSource(options?)` | The full worker source as a string. |
97
96
  | `shouldHandle(url, origin, config?)` | The matching rule the worker uses (exported for tests/tooling). |
98
97
  | `CACHE_NAME` | Default cache bucket name (`'spark-offline-v1'`). |
99
- | `spark-html-offline/vite` | Vite pluginemits the worker in build, serves it in dev. |
98
+ | `spark-html-offline/bun` | Build stepwrites the worker in build, serves it in dev. |
100
99
 
101
100
  ## The Spark family
102
101
 
@@ -120,7 +119,7 @@ virtual DOM, no build step required. Add only what you use.
120
119
  | [`spark-html-manifest`](https://www.npmjs.com/package/spark-html-manifest) | PWA manifest + icons + head tags (and optional service worker) from one config. |
121
120
  | [`spark-html-offline`](https://www.npmjs.com/package/spark-html-offline) | Offline URL imports — a service worker that caches CDN components. |
122
121
  | [`spark-html-sri`](https://www.npmjs.com/package/spark-html-sri) | Subresource Integrity — hash + verify assets and remote components. |
123
- | [`create-spark-html-app`](https://www.npmjs.com/package/create-spark-html-app) | Scaffold a Vite + spark-html app in one command. |
122
+ | [`create-spark-html-app`](https://www.npmjs.com/package/create-spark-html-app) | Scaffold a spark-html app in one command. |
124
123
  | [`prettier-plugin-spark`](https://www.npmjs.com/package/prettier-plugin-spark) | Prettier for components — formats `<script>`/`<style>`, markup stays byte-for-byte. |
125
124
  | [`spark-html-language-server`](https://www.npmjs.com/package/spark-html-language-server) | LSP — diagnostics, go-to-definition, prop autocomplete, hover docs. |
126
125
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "spark-html-offline",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "description": "Offline-capable URL imports for spark-html — a tiny service worker that caches CDN-imported components on first fetch and serves them when the network is gone. Zero dependencies.",
5
5
  "type": "module",
6
6
  "main": "./src/index.js",
@@ -11,9 +11,8 @@
11
11
  "types": "./src/index.d.ts",
12
12
  "default": "./src/index.js"
13
13
  },
14
- "./vite": {
15
- "types": "./src/vite.d.ts",
16
- "default": "./src/vite.js"
14
+ "./bun": {
15
+ "default": "./src/bun.js"
17
16
  }
18
17
  },
19
18
  "scripts": {
package/src/bun.js ADDED
@@ -0,0 +1,33 @@
1
+ /**
2
+ * spark-html-offline/bun — emit + serve the worker file, as a spark-html-bun
3
+ * pipeline step.
4
+ *
5
+ * • run({ outDir }) — write the generated service worker into the output dir
6
+ * (default /spark-sw.js) so offline() finds it in production.
7
+ * • devRoutes() — serve the same source in dev, so the worker can be
8
+ * exercised locally too (it only touches cross-origin URLs by default, so
9
+ * HMR and local files are unaffected).
10
+ *
11
+ * import offline from 'spark-html-offline/bun';
12
+ * export default { pipeline: [offline({ include: ['/components/'] })] };
13
+ */
14
+ import { join, resolve } from 'node:path';
15
+ import { writeFile } from 'node:fs/promises';
16
+ import { existsSync } from 'node:fs';
17
+ import { swSource } from './index.js';
18
+
19
+ export default function sparkOffline(options = {}) {
20
+ const file = (options.file || 'spark-sw.js').replace(/^\//, '');
21
+ const source = swSource(options);
22
+ return {
23
+ name: 'spark-html-offline',
24
+ async run({ outDir }) {
25
+ const root = resolve(outDir);
26
+ if (!existsSync(root)) return;
27
+ await writeFile(join(root, file), source, 'utf8');
28
+ },
29
+ devRoutes() {
30
+ return { [`/${file}`]: { type: 'text/javascript', body: () => source } };
31
+ },
32
+ };
33
+ }
package/src/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- /** Options shared by the worker generator and the vite plugin. */
1
+ /** Options shared by the worker generator and the build step. */
2
2
  export interface OfflineSwOptions {
3
3
  /** Same-origin URL substrings to cache too (e.g. ['/components/']). */
4
4
  include?: string[];
@@ -39,8 +39,8 @@ declare const _default: {
39
39
  };
40
40
  export default _default;
41
41
 
42
- /** Vite plugin: emits the worker in build, serves it in dev. */
43
- export interface OfflineVitePluginOptions extends OfflineSwOptions {
44
- /** Emitted file name (default 'spark-sw.js'). */
42
+ /** spark-html-bun build step: writes the worker in build, serves it in dev. */
43
+ export interface OfflineBuildOptions extends OfflineSwOptions {
44
+ /** Written file name (default 'spark-sw.js'). */
45
45
  file?: string;
46
46
  }
package/src/index.js CHANGED
@@ -12,8 +12,8 @@
12
12
  * import { offline } from 'spark-html-offline';
13
13
  * offline();
14
14
  *
15
- * // vite.config.js — emits + serves the worker file for you
16
- * import offlineSw from 'spark-html-offline/vite';
15
+ * // spark.config.js — writes + serves the worker file for you
16
+ * import offlineSw from 'spark-html-offline/bun';
17
17
  * plugins: [spark(), offlineSw()]
18
18
  *
19
19
  * Works with any CDN (esm.sh, unpkg, jsdelivr, your own). By default it
@@ -53,7 +53,7 @@ export function shouldHandle(url, origin, config) {
53
53
 
54
54
  /**
55
55
  * The service-worker source, as a string — write it to a file served from
56
- * your origin (the vite plugin does this for you as /spark-sw.js).
56
+ * your origin (the bun build step does this for you as /spark-sw.js).
57
57
  *
58
58
  * Strategy: cache-first with background revalidation (stale-while-
59
59
  * revalidate). First visit fetches + caches; every visit after serves
package/src/vite.d.ts DELETED
@@ -1,8 +0,0 @@
1
- import type { OfflineVitePluginOptions } from './index.js';
2
-
3
- /** Vite plugin: emits the service worker in build, serves it in dev. */
4
- export default function sparkOffline(options?: OfflineVitePluginOptions): {
5
- name: string;
6
- configureServer(server: unknown): void;
7
- generateBundle(): void;
8
- };
package/src/vite.js DELETED
@@ -1,35 +0,0 @@
1
- /**
2
- * spark-html-offline/vite — emit + serve the worker file.
3
- *
4
- * Build: writes the generated service worker into the output dir (default
5
- * name /spark-sw.js) so `offline()` finds it in production.
6
- * Dev: serves the same source from the dev server, so the worker can be
7
- * exercised locally too (it only touches cross-origin URLs by default, so
8
- * HMR and local files are unaffected).
9
- *
10
- * import offlineSw from 'spark-html-offline/vite';
11
- * plugins: [spark(), offlineSw({ include: ['/components/'] })]
12
- */
13
- import { swSource } from './index.js';
14
-
15
- export default function sparkOffline(options = {}) {
16
- const file = (options.file || 'spark-sw.js').replace(/^\//, '');
17
- const source = swSource(options);
18
- return {
19
- name: 'spark-html-offline',
20
- configureServer(server) {
21
- server.middlewares.use((req, res, next) => {
22
- if ((req.url || '').split('?')[0] === `/${file}`) {
23
- res.setHeader('Content-Type', 'text/javascript');
24
- res.setHeader('Cache-Control', 'no-cache');
25
- res.end(source);
26
- return;
27
- }
28
- next();
29
- });
30
- },
31
- generateBundle() {
32
- this.emitFile({ type: 'asset', fileName: file, source });
33
- },
34
- };
35
- }