@serwist/vite 9.0.0-preview.9 → 9.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.
Files changed (79) hide show
  1. package/dist/chunks/schema.js +56 -0
  2. package/dist/index.d.ts +8 -7
  3. package/dist/index.d.ts.map +1 -1
  4. package/dist/index.js +77 -180
  5. package/dist/index.schema.d.ts +3 -0
  6. package/dist/index.schema.d.ts.map +1 -0
  7. package/dist/index.schema.js +3 -0
  8. package/dist/index.worker.d.ts +8 -10
  9. package/dist/index.worker.d.ts.map +1 -1
  10. package/dist/index.worker.js +18 -11
  11. package/dist/lib/api.d.ts.map +1 -0
  12. package/dist/lib/constants.d.ts +3 -0
  13. package/dist/lib/constants.d.ts.map +1 -0
  14. package/dist/{context.d.ts → lib/context.d.ts} +2 -2
  15. package/dist/lib/context.d.ts.map +1 -0
  16. package/dist/lib/log.d.ts.map +1 -0
  17. package/dist/{modules.d.ts → lib/modules.d.ts} +1 -1
  18. package/dist/lib/modules.d.ts.map +1 -0
  19. package/dist/lib/options.d.ts +4 -0
  20. package/dist/lib/options.d.ts.map +1 -0
  21. package/dist/lib/schema.d.ts +359 -0
  22. package/dist/lib/schema.d.ts.map +1 -0
  23. package/dist/lib/types.d.ts +130 -0
  24. package/dist/lib/types.d.ts.map +1 -0
  25. package/dist/lib/utils.d.ts.map +1 -0
  26. package/dist/lib/validator.d.ts +3 -0
  27. package/dist/lib/validator.d.ts.map +1 -0
  28. package/dist/plugins/build.d.ts +2 -2
  29. package/dist/plugins/build.d.ts.map +1 -1
  30. package/dist/plugins/dev.d.ts +2 -2
  31. package/dist/plugins/dev.d.ts.map +1 -1
  32. package/dist/plugins/main.d.ts +2 -2
  33. package/dist/plugins/main.d.ts.map +1 -1
  34. package/package.json +29 -65
  35. package/src/index.schema.ts +3 -0
  36. package/src/index.ts +8 -7
  37. package/src/index.worker.ts +111 -96
  38. package/src/lib/constants.ts +2 -0
  39. package/src/{context.ts → lib/context.ts} +2 -2
  40. package/src/{log.ts → lib/log.ts} +1 -1
  41. package/src/{modules.ts → lib/modules.ts} +4 -5
  42. package/src/lib/options.ts +76 -0
  43. package/src/lib/schema.ts +35 -0
  44. package/src/lib/types.ts +163 -0
  45. package/src/lib/validator.ts +10 -0
  46. package/src/plugins/build.ts +3 -3
  47. package/src/plugins/dev.ts +3 -3
  48. package/src/plugins/main.ts +15 -9
  49. package/src/rollup.js +4 -5
  50. package/dist/api.d.ts.map +0 -1
  51. package/dist/assets.d.ts +0 -4
  52. package/dist/assets.d.ts.map +0 -1
  53. package/dist/constants.d.ts +0 -3
  54. package/dist/constants.d.ts.map +0 -1
  55. package/dist/context.d.ts.map +0 -1
  56. package/dist/index.browser.d.ts +0 -2
  57. package/dist/index.browser.d.ts.map +0 -1
  58. package/dist/index.browser.js +0 -13
  59. package/dist/log.d.ts.map +0 -1
  60. package/dist/modules.d.ts.map +0 -1
  61. package/dist/options.d.ts +0 -4
  62. package/dist/options.d.ts.map +0 -1
  63. package/dist/types.d.ts +0 -191
  64. package/dist/types.d.ts.map +0 -1
  65. package/dist/utils-types.d.ts +0 -2
  66. package/dist/utils-types.d.ts.map +0 -1
  67. package/dist/utils.d.ts.map +0 -1
  68. package/src/assets.ts +0 -76
  69. package/src/constants.ts +0 -2
  70. package/src/index.browser.ts +0 -8
  71. package/src/options.ts +0 -90
  72. package/src/types.ts +0 -207
  73. package/src/utils-types.ts +0 -1
  74. package/src/virtual.d.ts +0 -5
  75. /package/dist/{api.d.ts → lib/api.d.ts} +0 -0
  76. /package/dist/{log.d.ts → lib/log.d.ts} +0 -0
  77. /package/dist/{utils.d.ts → lib/utils.d.ts} +0 -0
  78. /package/src/{api.ts → lib/api.ts} +0 -0
  79. /package/src/{utils.ts → lib/utils.ts} +0 -0
@@ -0,0 +1,163 @@
1
+ import type {
2
+ BasePartial,
3
+ BaseResolved,
4
+ GlobPartial,
5
+ GlobResolved,
6
+ InjectPartial as BaseInjectPartial,
7
+ InjectResolved as BaseInjectResolved,
8
+ ManifestEntry,
9
+ RequiredGlobDirectoryPartial,
10
+ RequiredGlobDirectoryResolved,
11
+ RequiredSwDestPartial,
12
+ RequiredSwDestResolved,
13
+ } from "@serwist/build";
14
+ import type { Require } from "@serwist/utils";
15
+ import type { RollupOptions } from "rollup";
16
+ import type { BuildOptions, Plugin, ResolvedConfig } from "vite";
17
+
18
+ export interface InjectPartial {
19
+ /**
20
+ * The mode in which your service worker should be built.
21
+ *
22
+ * @default
23
+ * process.env.NODE_ENV // or "production" if undefined
24
+ */
25
+ mode?: "development" | "production";
26
+ /**
27
+ * The module type with which the service worker should be registered. Usually used alongside
28
+ * `rollupFormat`.
29
+ *
30
+ * @default "classic"
31
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerContainer/register#type
32
+ */
33
+ type?: WorkerType;
34
+ /**
35
+ * The service worker's URL scope. Set to `"/foo/"` so that paths under "/foo/"
36
+ * are under the service worker's control while others are not.
37
+ *
38
+ * @default viteOptions.base
39
+ * @see https://vitejs.dev/config/shared-options.html#base
40
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerContainer/register#scope
41
+ */
42
+ scope?: string;
43
+ /**
44
+ * The base from which Serwist resolves URLs.
45
+ *
46
+ * @default viteOptions.base
47
+ * @see https://vitejs.dev/config/shared-options.html#base
48
+ */
49
+ base?: string;
50
+ /**
51
+ * Whether Serwist should be disabled.
52
+ *
53
+ * @default false
54
+ */
55
+ disable?: boolean;
56
+ /**
57
+ * Hooks of the build lifecycle.
58
+ */
59
+ integration?: Hooks;
60
+ /**
61
+ * The URL to the service worker.
62
+ *
63
+ * @default "/sw.js"
64
+ */
65
+ swUrl?: string;
66
+ /**
67
+ * Rollup/Vite plugins used to build the service worker.
68
+ */
69
+ plugins?: Plugin[];
70
+ /**
71
+ * The format used to build the service worker.
72
+ *
73
+ * @default "es"
74
+ */
75
+ rollupFormat?: "es" | "iife";
76
+ /**
77
+ * Custom Rollup options used to build the service worker.
78
+ */
79
+ rollupOptions?: Omit<RollupOptions, "plugins" | "output">;
80
+ /**
81
+ * Development-specific options.
82
+ */
83
+ devOptions?: DevOptions;
84
+ }
85
+
86
+ export interface InjectResolved extends Require<InjectPartial, "mode" | "type" | "scope" | "base" | "disable" | "swUrl" | "rollupFormat"> {
87
+ devOptions: Required<DevOptions>;
88
+ }
89
+
90
+ export interface InjectManifestOptions
91
+ extends Omit<BasePartial, "disablePrecacheManifest">,
92
+ GlobPartial,
93
+ BaseInjectPartial,
94
+ RequiredSwDestPartial,
95
+ RequiredGlobDirectoryPartial,
96
+ InjectPartial {}
97
+
98
+ export interface InjectManifestOptionsComplete
99
+ extends BaseResolved,
100
+ GlobResolved,
101
+ BaseInjectResolved,
102
+ RequiredSwDestResolved,
103
+ RequiredGlobDirectoryResolved,
104
+ InjectResolved {}
105
+
106
+ export interface Hooks {
107
+ /**
108
+ * Allows you to run some logic before the service worker is built.
109
+ * @param options
110
+ * @returns
111
+ */
112
+ beforeBuildServiceWorker?: (options: PluginOptionsComplete) => void | Promise<void>;
113
+ /**
114
+ * Adjusts the application order of `@serwist/vite`'s `closeBundle` hook.
115
+ */
116
+ closeBundleOrder?: "pre" | "post" | null;
117
+ /**
118
+ * Allows you to configure the options of Serwist and Vite. Useful when there is a dependency between the two.
119
+ * @param viteOptions
120
+ * @param options
121
+ * @returns
122
+ */
123
+ configureOptions?: (viteOptions: ResolvedConfig, options: PluginOptions) => void | Promise<void>;
124
+ }
125
+
126
+ export interface DevOptions {
127
+ /**
128
+ * Whether the service worker should be bundled in development mode.
129
+ *
130
+ * @default true
131
+ */
132
+ bundle?: boolean;
133
+ /**
134
+ * Whether the service worker should be minified in development mode.
135
+ *
136
+ * @default false
137
+ */
138
+ minify?: BuildOptions["minify"];
139
+ }
140
+
141
+ export interface PluginOptions extends InjectManifestOptions {}
142
+
143
+ export interface PluginOptionsComplete extends InjectResolved {
144
+ injectManifest: Omit<InjectManifestOptionsComplete, keyof InjectResolved>;
145
+ }
146
+
147
+ export interface SerwistViteApi {
148
+ /**
149
+ * Whether the plugin is disabled.
150
+ */
151
+ disabled: boolean;
152
+ /**
153
+ * Extends the precache manifest.
154
+ * @param fn
155
+ */
156
+ extendManifestEntries(fn: ExtendManifestEntriesHook): void;
157
+ /*
158
+ * Generates the service worker.
159
+ */
160
+ generateSW(): Promise<void>;
161
+ }
162
+
163
+ export type ExtendManifestEntriesHook = (manifestEntries: (string | ManifestEntry)[]) => (string | ManifestEntry)[] | undefined;
@@ -0,0 +1,10 @@
1
+ import { SerwistConfigError, validationErrorMap } from "@serwist/build/schema";
2
+ import type { InjectManifestOptionsComplete } from "./types.js";
3
+
4
+ export const validateInjectManifestOptions = async (input: unknown): Promise<InjectManifestOptionsComplete> => {
5
+ const result = await (await import("./schema.js")).injectManifestOptions.spa(input, { errorMap: validationErrorMap });
6
+ if (!result.success) {
7
+ throw new SerwistConfigError({ moduleName: "@serwist/vite", message: JSON.stringify(result.error.format(), null, 2) });
8
+ }
9
+ return result.data;
10
+ };
@@ -1,7 +1,7 @@
1
1
  import type { Plugin } from "vite";
2
2
 
3
- import type { SerwistViteContext } from "../context.js";
4
- import type { SerwistViteApi } from "../types.js";
3
+ import type { SerwistViteContext } from "../lib/context.js";
4
+ import type { SerwistViteApi } from "../lib/types.js";
5
5
 
6
6
  /**
7
7
  * Internal build plugin used by `@serwist/vite`.
@@ -24,7 +24,7 @@ export const buildPlugin = (ctx: SerwistViteContext, api: SerwistViteApi) => {
24
24
  }
25
25
  },
26
26
  },
27
- async buildEnd(error) {
27
+ buildEnd(error) {
28
28
  if (error) throw error;
29
29
  },
30
30
  };
@@ -3,9 +3,9 @@ import path from "node:path";
3
3
 
4
4
  import { type Plugin, normalizePath } from "vite";
5
5
 
6
- import type { SerwistViteContext } from "../context.js";
7
- import type { SerwistViteApi } from "../types.js";
8
- import { toFs } from "../utils.js";
6
+ import type { SerwistViteContext } from "../lib/context.js";
7
+ import type { SerwistViteApi } from "../lib/types.js";
8
+ import { toFs } from "../lib/utils.js";
9
9
 
10
10
  // This plugin handles the service worker in two ways:
11
11
  // - If `devOptions.bundle` is enabled, hook a middleware that bundles the service worker
@@ -2,10 +2,10 @@ import path from "node:path";
2
2
 
3
3
  import type { Plugin, UserConfig } from "vite";
4
4
 
5
- import { INTERNAL_SERWIST_VIRTUAL, RESOLVED_INTERNAL_SERWIST_VIRTUAL } from "../constants.js";
6
- import type { SerwistViteContext } from "../context.js";
7
- import { resolveOptions } from "../options.js";
8
- import type { SerwistViteApi } from "../types.js";
5
+ import { RESOLVED_SERWIST_VIRTUAL, SERWIST_VIRTUAL } from "../lib/constants.js";
6
+ import type { SerwistViteContext } from "../lib/context.js";
7
+ import { resolveOptions } from "../lib/options.js";
8
+ import type { SerwistViteApi } from "../lib/types.js";
9
9
 
10
10
  /**
11
11
  * Internal plugin used by `@serwist/vite`.
@@ -31,16 +31,22 @@ export const mainPlugin = (ctx: SerwistViteContext, api: SerwistViteApi) => {
31
31
  ctx.options = await resolveOptions(ctx.userOptions, config);
32
32
  },
33
33
  resolveId(id) {
34
- if (id === INTERNAL_SERWIST_VIRTUAL) {
35
- return RESOLVED_INTERNAL_SERWIST_VIRTUAL;
34
+ if (id === SERWIST_VIRTUAL) {
35
+ return RESOLVED_SERWIST_VIRTUAL;
36
36
  }
37
37
  return undefined;
38
38
  },
39
39
  load(id) {
40
- if (id === RESOLVED_INTERNAL_SERWIST_VIRTUAL) {
41
- return `export const swUrl = "${path.posix.join(ctx.options.buildBase, ctx.options.swUrl)}";
40
+ if (id === RESOLVED_SERWIST_VIRTUAL) {
41
+ return `export const swUrl = "${path.posix.join(ctx.options.base, ctx.options.swUrl)}";
42
42
  export const swScope = "${ctx.options.scope}";
43
- export const swType = "${ctx.devEnvironment ? "module" : ctx.options.type}";`;
43
+ export const swType = "${ctx.devEnvironment ? "module" : ctx.options.type}";
44
+ export const getSerwist = async () => {
45
+ if ("serviceWorker" in navigator) {
46
+ return new (await import("@serwist/window")).Serwist(swUrl, { scope: swScope, type: swType });
47
+ }
48
+ return undefined;
49
+ }`;
44
50
  }
45
51
  return undefined;
46
52
  },
package/src/rollup.js CHANGED
@@ -1,5 +1,5 @@
1
1
  // @ts-check
2
- import { getRollupOptions } from "@serwist/constants/rollup";
2
+ import { getRollupOptions } from "@serwist/configs/rollup";
3
3
 
4
4
  import packageJson from "../package.json" assert { type: "json" };
5
5
 
@@ -9,16 +9,15 @@ export default getRollupOptions({
9
9
  {
10
10
  input: {
11
11
  index: "src/index.ts",
12
- "index.browser": "src/index.browser.ts",
13
12
  "index.worker": "src/index.worker.ts",
13
+ "index.schema": "src/index.schema.ts",
14
14
  },
15
15
  output: {
16
16
  dir: "dist",
17
- format: "esm",
18
17
  entryFileNames: "[name].js",
19
- chunkFileNames: "[name].js",
18
+ chunkFileNames: "chunks/[name].js",
19
+ format: "esm",
20
20
  },
21
- external: ["virtual:internal-serwist"],
22
21
  },
23
22
  ],
24
23
  });
package/dist/api.d.ts.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"api.d.ts","sourceRoot":"","sources":["../src/api.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAC;AAGvD,OAAO,KAAK,EAA6B,cAAc,EAAE,MAAM,YAAY,CAAC;AAE5E,eAAO,MAAM,SAAS,QAAS,kBAAkB,KAAG,cA8BnD,CAAC"}
package/dist/assets.d.ts DELETED
@@ -1,4 +0,0 @@
1
- import type { ResolvedConfig } from "vite";
2
- import type { ResolvedPluginOptions } from "./types.js";
3
- export declare const configureStaticAssets: (resolvedPluginOptions: ResolvedPluginOptions, viteConfig: ResolvedConfig) => Promise<void>;
4
- //# sourceMappingURL=assets.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"assets.d.ts","sourceRoot":"","sources":["../src/assets.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,MAAM,CAAC;AAE3C,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,YAAY,CAAC;AA+BxD,eAAO,MAAM,qBAAqB,0BAAiC,qBAAqB,cAAc,cAAc,kBAoCnH,CAAC"}
@@ -1,3 +0,0 @@
1
- export declare const INTERNAL_SERWIST_VIRTUAL = "virtual:internal-serwist";
2
- export declare const RESOLVED_INTERNAL_SERWIST_VIRTUAL = "\0virtual:internal-serwist";
3
- //# sourceMappingURL=constants.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,wBAAwB,6BAA6B,CAAC;AACnE,eAAO,MAAM,iCAAiC,+BAAkC,CAAC"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../src/context.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,MAAM,CAAC;AAE3C,OAAO,KAAK,EAAE,aAAa,EAAE,qBAAqB,EAAE,MAAM,YAAY,CAAC;AAEvE,MAAM,MAAM,qBAAqB,GAAG,MAAM,CAAC;AAE3C,MAAM,WAAW,kBAAkB;IACjC;;;;OAIG;IACH,UAAU,EAAE,cAAc,CAAC;IAC3B;;OAEG;IACH,WAAW,EAAE,aAAa,CAAC;IAC3B;;;;;OAKG;IACH,OAAO,EAAE,qBAAqB,CAAC;IAC/B,iBAAiB,EAAE,OAAO,CAAC;IAC3B;;;;OAIG;IACH,cAAc,EAAE,OAAO,CAAC;IACxB,8CAA8C;IAC9C,SAAS,EAAE,qBAAqB,GAAG,SAAS,CAAC;CAC9C;AAED,eAAO,MAAM,aAAa,gBAAiB,aAAa,aAAa,qBAAqB,GAAG,SAAS,KAAG,kBASxG,CAAC"}
@@ -1,2 +0,0 @@
1
- export declare const getSerwist: () => Promise<import("@serwist/window").Serwist | undefined>;
2
- //# sourceMappingURL=index.browser.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.browser.d.ts","sourceRoot":"","sources":["../src/index.browser.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,UAAU,8DAKtB,CAAC"}
@@ -1,13 +0,0 @@
1
- import { swUrl, swScope, swType } from 'virtual:internal-serwist';
2
-
3
- const getSerwist = async ()=>{
4
- if ("serviceWorker" in navigator) {
5
- return new (await import('@serwist/window')).Serwist(swUrl, {
6
- scope: swScope,
7
- type: swType
8
- });
9
- }
10
- return undefined;
11
- };
12
-
13
- export { getSerwist };
package/dist/log.d.ts.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"log.d.ts","sourceRoot":"","sources":["../src/log.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,gBAAgB,CAAC;AAElD,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,MAAM,CAAC;AAI3C,eAAO,MAAM,gBAAgB,gBAAiB,KAAK,WAAW,EAAE,OAAO,GAAG,MAAM,GAAG,UAAU,CAAC,eAAe,cAAc,SAkB1H,CAAC"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"modules.d.ts","sourceRoot":"","sources":["../src/modules.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,KAAK,YAAY,MAAM,gBAAgB,CAAC;AAEpD,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAC;AAEvD,eAAO,MAAM,gBAAgB,QAAa,QAAQ,mBAAmB,CASpE,CAAC;AAEF,UAAU,WAAY,SAAQ,YAAY,CAAC,iBAAiB;IAC1D,cAAc,EAAE,MAAM,CAAC;CACxB;AAED,eAAO,MAAM,cAAc,WAAkB,aAAa,yBAAyB,KAAG,QAAQ,WAAW,CAYxG,CAAC;AAEF,eAAO,MAAM,qBAAqB,QAAe,kBAAkB,qCAwIlE,CAAC"}
package/dist/options.d.ts DELETED
@@ -1,4 +0,0 @@
1
- import type { ResolvedConfig } from "vite";
2
- import type { PluginOptions, ResolvedPluginOptions } from "./types.js";
3
- export declare const resolveOptions: (options: PluginOptions, viteConfig: ResolvedConfig) => Promise<ResolvedPluginOptions>;
4
- //# sourceMappingURL=options.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"options.d.ts","sourceRoot":"","sources":["../src/options.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,MAAM,CAAC;AAI3C,OAAO,KAAK,EAAE,aAAa,EAAE,qBAAqB,EAAE,MAAM,YAAY,CAAC;AAGvE,eAAO,MAAM,cAAc,YAAmB,aAAa,cAAc,cAAc,KAAG,QAAQ,qBAAqB,CA+EtH,CAAC"}
package/dist/types.d.ts DELETED
@@ -1,191 +0,0 @@
1
- import type { ManifestEntry, ViteInjectManifestOptions } from "@serwist/build";
2
- import type { RollupOptions } from "rollup";
3
- import type { BuildOptions, Plugin, ResolvedConfig } from "vite";
4
- export type InjectManifestVitePlugins = string[] | ((vitePluginIds: string[]) => string[]);
5
- export interface CustomInjectManifestOptions extends Omit<ViteInjectManifestOptions, "disablePrecacheManifest"> {
6
- /**
7
- * The URL to the service worker.
8
- * @default "/sw.js"
9
- */
10
- swUrl?: string;
11
- /**
12
- * Configure the format to use in the Rollup build.
13
- *
14
- * @default 'es'
15
- */
16
- rollupFormat?: "es" | "iife";
17
- /**
18
- * Since `v0.15.0` you can add plugins to build your service worker.
19
- *
20
- * When using `injectManifest` there are 2 builds, your application and the service worker.
21
- * If you're using custom configuration for your service worker (for example custom plugins) you can use this option to configure the service worker build.
22
- * Both configurations cannot be shared, and so you'll need to duplicate the configuration, with the exception of `define`.
23
- *
24
- * **WARN**: this option is for advanced usage, be aware that you may break your application build.
25
- */
26
- plugins?: Plugin[];
27
- /**
28
- * Since `v0.15.0` you can add custom Rollup options to build your service worker: we expose the same configuration to build a worker using Vite.
29
- */
30
- rollupOptions?: Omit<RollupOptions, "plugins" | "output">;
31
- }
32
- export interface SerwistViteHooks {
33
- beforeBuildServiceWorker?: (options: ResolvedPluginOptions) => void | Promise<void>;
34
- closeBundleOrder?: "pre" | "post" | null;
35
- configureOptions?: (viteOptions: ResolvedConfig, options: PluginOptions) => void | Promise<void>;
36
- }
37
- export interface DevOptions {
38
- /**
39
- * Whether the service worker should be bundled in development mode.
40
- *
41
- * [Many browsers still do not support ES Modules in service workers.](https://caniuse.com/mdn-api_serviceworker_ecmascript_modules) However, in development
42
- * mode, certain frameworks, such as SvelteKit, do not bundle the service worker. As a result, trying to register that service worker on browsers lacking
43
- * support, such as Firefox or Safari, will fail, but doing so on browsers not lacking support will not fail. This option is provided to prevent that from
44
- * happening. What the plugin does is intercepting any request to the service worker (requests for `swUrl`) and returning a bundled one.
45
- */
46
- bundle?: boolean;
47
- minify?: BuildOptions["minify"];
48
- }
49
- /**
50
- * Plugin options.
51
- */
52
- export interface BasePluginOptions {
53
- /**
54
- * Build mode
55
- *
56
- * @default
57
- * process.env.NODE_ENV // or "production" if undefined
58
- */
59
- mode?: "development" | "production";
60
- /**
61
- * The service worker type.
62
- *
63
- * @default "classic"
64
- */
65
- type?: WorkerType;
66
- /**
67
- * The scope to register the Service Worker
68
- *
69
- * @default `viteOptions.base`
70
- */
71
- scope?: string;
72
- /**
73
- * Inject the service worker register inlined in the index.html
74
- *
75
- * If set to "auto", depends on whether you used the `import { registerSW } from 'virtual:pwa-register'`
76
- * it will do nothing or use the `script` mode
77
- *
78
- * `"inline"` - inject a simple register, inlined with the generated html
79
- *
80
- * `"script"` - inject `<script/>` in `<head>` with `src` attribute to a generated script to register the service worker
81
- *
82
- * `"script-defer"` - inject `<script defer />` in `<head>`, with `src` attribute to a generated script to register the service worker
83
- *
84
- * `null` - do nothing. You will need to register the service worker yourself or import `registerSW` from `virtual:pwa-register`.
85
- *
86
- * @default "auto"
87
- */
88
- injectRegister: "inline" | "script" | "script-defer" | "auto" | null | false;
89
- /**
90
- * Mode for the virtual register.
91
- * This is NOT available if `injectRegister` is set to `"inline"` or `"script"`
92
- *
93
- * `"prompt"` - you will need to show a popup/dialog to the user to confirm the reload.
94
- *
95
- * `"autoUpdate"` - when new content is available, the new service worker will update caches and reload all browser
96
- * windows/tabs with the application open automatically, it must take the control for the application to work
97
- * properly.
98
- *
99
- * @default "prompt"
100
- */
101
- registerType?: "prompt" | "autoUpdate";
102
- /**
103
- * Minify the generated manifest
104
- *
105
- * @default true
106
- */
107
- minify: boolean;
108
- /**
109
- * Whether to add the `crossorigin="use-credentials"` attribute to `<link rel="manifest">`
110
- * @default false
111
- */
112
- useCredentials?: boolean;
113
- /**
114
- * Override Vite's base options for `@serwist/vite`.
115
- *
116
- * @default viteOptions.base
117
- */
118
- base?: string;
119
- /**
120
- * `public` resources to be added to the PWA manifest.
121
- *
122
- * You don't need to add `manifest` icons here, it will be auto included.
123
- *
124
- * The `public` directory will be resolved from Vite's `publicDir` option directory.
125
- */
126
- includeAssets: string | string[] | undefined;
127
- /**
128
- * Whether Serwist should be disabled.
129
- *
130
- * @default false
131
- */
132
- disable: boolean;
133
- /**
134
- * `@serwist/vite` integration.
135
- */
136
- integration?: SerwistViteHooks;
137
- /**
138
- * When Vite's build folder is not the same as your base root folder, configure it here.
139
- *
140
- * This option will be useful for integrations like `vite-plugin-laravel` where Vite's build folder is `public/build` but Laravel's base path is `public`.
141
- *
142
- * This option will be used to configure the path for the service worker, "registerSW.js" and the web manifest assets.
143
- *
144
- * For example, if your base path is `/`, then, in your Laravel PWA configuration use `buildPath: '/build/'`.
145
- *
146
- * By default: `vite.base`.
147
- */
148
- buildBase?: string;
149
- }
150
- export interface PluginOptions extends Partial<BasePluginOptions>, CustomInjectManifestOptions {
151
- devOptions?: DevOptions;
152
- }
153
- export interface InjectManifestRollupOptions {
154
- format: "es" | "iife";
155
- plugins: Plugin[];
156
- rollupOptions: RollupOptions;
157
- }
158
- export interface ResolvedPluginOptions extends Required<BasePluginOptions>, Required<Pick<CustomInjectManifestOptions, "swUrl">> {
159
- injectManifest: ViteInjectManifestOptions;
160
- injectManifestRollupOptions: InjectManifestRollupOptions;
161
- devOptions: Required<DevOptions>;
162
- }
163
- export interface ShareTargetFiles {
164
- name: string;
165
- accept: string | string[];
166
- }
167
- /**
168
- * @see https://developer.mozilla.org/en-US/docs/Web/Manifest/launch_handler#launch_handler_item_values
169
- */
170
- export type LaunchHandlerClientMode = "auto" | "focus-existing" | "navigate-existing" | "navigate-new";
171
- export type Display = "fullscreen" | "standalone" | "minimal-ui" | "browser";
172
- export type DisplayOverride = Display | "window-controls-overlay";
173
- export type IconPurpose = "monochrome" | "maskable" | "any";
174
- type Nothing = {};
175
- /**
176
- * type StringLiteralUnion<'maskable'> = 'maskable' | string
177
- * This has auto completion whereas `'maskable' | string` doesn't
178
- * Adapted from https://github.com/microsoft/TypeScript/issues/29729
179
- */
180
- export type StringLiteralUnion<T extends U, U = string> = T | (U & Nothing);
181
- export interface SerwistViteApi {
182
- /**
183
- * Is the plugin disabled?
184
- */
185
- disabled: boolean;
186
- extendManifestEntries(fn: ExtendManifestEntriesHook): void;
187
- generateSW(): Promise<void>;
188
- }
189
- export type ExtendManifestEntriesHook = (manifestEntries: (string | ManifestEntry)[]) => (string | ManifestEntry)[] | undefined;
190
- export {};
191
- //# sourceMappingURL=types.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,yBAAyB,EAAE,MAAM,gBAAgB,CAAC;AAC/E,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,QAAQ,CAAC;AAC5C,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,EAAE,cAAc,EAAE,MAAM,MAAM,CAAC;AAEjE,MAAM,MAAM,yBAAyB,GAAG,MAAM,EAAE,GAAG,CAAC,CAAC,aAAa,EAAE,MAAM,EAAE,KAAK,MAAM,EAAE,CAAC,CAAC;AAC3F,MAAM,WAAW,2BAA4B,SAAQ,IAAI,CAAC,yBAAyB,EAAE,yBAAyB,CAAC;IAC7G;;;OAGG;IACH,KAAK,CAAC,EAAE,MAAM,CAAC;IACf;;;;OAIG;IACH,YAAY,CAAC,E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,IAAI,CAAC,GAAG,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC;AAE5E,MAAM,WAAW,cAAc;IAC7B;;OAEG;IACH,QAAQ,EAAE,OAAO,CAAC;IAClB,qBAAqB,CAAC,EAAE,EAAE,yBAAyB,GAAG,IAAI,CAAC;IAI3D,UAAU,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CAC7B;AAED,MAAM,MAAM,yBAAyB,GAAG,CAAC,eAAe,EAAE,CAAC,MAAM,GAAG,aAAa,CAAC,EAAE,KAAK,CAAC,MAAM,GAAG,aAAa,CAAC,EAAE,GAAG,SAAS,CAAC"}
@@ -1,2 +0,0 @@
1
- export type Optional<T, U extends keyof T> = Omit<T, U> & Partial<Pick<T, U>>;
2
- //# sourceMappingURL=utils-types.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"utils-types.d.ts","sourceRoot":"","sources":["../src/utils-types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,QAAQ,CAAC,CAAC,EAAE,CAAC,SAAS,MAAM,CAAC,IAAI,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAGA,eAAO,MAAM,KAAK,QAAS,MAAM,WAEhC,CAAC;AAEF,eAAO,MAAM,eAAe,SAAU,MAAM,WAG3C,CAAC;AAEF,eAAO,MAAM,UAAU,QAAS,MAAM,4BAErC,CAAC;AAEF,eAAO,MAAM,aAAa,SAAU,MAAM,WAEzC,CAAC;AAIF;;;;;;;;GAQG;AACH,eAAO,MAAM,YAAY,UAAW,MAAM,KAAG,MAAM,GAAG,IAqBrD,CAAC;AAIF;;;;;;;GAOG;AACH,eAAO,MAAM,IAAI,QAAS,MAAM,WAI/B,CAAC"}
package/src/assets.ts DELETED
@@ -1,76 +0,0 @@
1
- import crypto from "node:crypto";
2
- import fs from "node:fs";
3
- import { resolve as resolveFs } from "node:path";
4
-
5
- import type { InjectManifestOptions, ManifestEntry } from "@serwist/build";
6
- import fg from "fast-glob";
7
- import type { ResolvedConfig } from "vite";
8
-
9
- import type { ResolvedPluginOptions } from "./types.js";
10
-
11
- const buildManifestEntry = (publicDir: string, url: string): Promise<ManifestEntry> => {
12
- return new Promise((resolve, reject) => {
13
- const cHash = crypto.createHash("MD5");
14
- const stream = fs.createReadStream(resolveFs(publicDir, url));
15
- stream.on("error", (err) => {
16
- reject(err);
17
- });
18
- stream.on("data", (chunk) => {
19
- cHash.update(chunk);
20
- });
21
- stream.on("end", () => {
22
- return resolve({
23
- url,
24
- revision: `${cHash.digest("hex")}`,
25
- });
26
- });
27
- });
28
- };
29
-
30
- const lookupAdditionalPrecacheEntries = (serwistOptions: Partial<InjectManifestOptions>): (string | ManifestEntry)[] => {
31
- return serwistOptions.additionalPrecacheEntries || [];
32
- };
33
-
34
- // we need to make icons relative, we can have for example icon entries with: /pwa.png
35
- // fast-glob will not resolve absolute paths
36
- const normalizeIconPath = (path: string) => {
37
- return path.startsWith("/") ? path.substring(1) : path;
38
- };
39
-
40
- export const configureStaticAssets = async (resolvedPluginOptions: ResolvedPluginOptions, viteConfig: ResolvedConfig) => {
41
- const { injectManifest, includeAssets } = resolvedPluginOptions;
42
-
43
- const { publicDir } = viteConfig;
44
- const globs: string[] = [];
45
- const manifestEntries: (string | ManifestEntry)[] = lookupAdditionalPrecacheEntries(injectManifest);
46
- if (includeAssets) {
47
- // we need to make icons relative, we can have for example icon entries with: /pwa.png
48
- // fast-glob will not resolve absolute paths
49
- if (Array.isArray(includeAssets)) globs.push(...includeAssets.map(normalizeIconPath));
50
- else globs.push(normalizeIconPath(includeAssets));
51
- }
52
- if (globs.length > 0) {
53
- let assets = await fg(globs, {
54
- cwd: publicDir,
55
- onlyFiles: true,
56
- unique: true,
57
- });
58
- // we also need to remove from the list existing included by the user
59
- if (manifestEntries.length > 0) {
60
- const included = manifestEntries.map((me) => {
61
- if (typeof me === "string") return me;
62
- return me.url;
63
- });
64
- assets = assets.filter((a) => !included.includes(a));
65
- }
66
- const assetsEntries = await Promise.all(
67
- assets.map((a) => {
68
- return buildManifestEntry(publicDir, a);
69
- }),
70
- );
71
- manifestEntries.push(...assetsEntries);
72
- }
73
- if (manifestEntries.length > 0) {
74
- injectManifest.additionalPrecacheEntries = manifestEntries;
75
- }
76
- };
package/src/constants.ts DELETED
@@ -1,2 +0,0 @@
1
- export const INTERNAL_SERWIST_VIRTUAL = "virtual:internal-serwist";
2
- export const RESOLVED_INTERNAL_SERWIST_VIRTUAL = `\0${INTERNAL_SERWIST_VIRTUAL}`;
@@ -1,8 +0,0 @@
1
- import { swScope, swType, swUrl } from "virtual:internal-serwist";
2
-
3
- export const getSerwist = async () => {
4
- if ("serviceWorker" in navigator) {
5
- return new (await import("@serwist/window")).Serwist(swUrl, { scope: swScope, type: swType });
6
- }
7
- return undefined;
8
- };