@serwist/vite 9.0.0-preview.9 → 9.0.0

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,56 @@
1
+ import { basePartial, globPartial, injectPartial, requiredSwDestPartial, requiredGlobDirectoryPartial } from '@serwist/build/schema';
2
+ import { z } from 'zod';
3
+
4
+ const hooks = z.object({
5
+ beforeBuildServiceWorker: z.function(z.tuple([
6
+ z.any()
7
+ ]), z.union([
8
+ z.promise(z.void()),
9
+ z.void()
10
+ ])).optional(),
11
+ closeBundleOrder: z.union([
12
+ z.literal("pre"),
13
+ z.literal("post"),
14
+ z.null()
15
+ ]).optional(),
16
+ configureOptions: z.function(z.tuple([
17
+ z.any(),
18
+ z.any()
19
+ ]), z.union([
20
+ z.promise(z.void()),
21
+ z.void()
22
+ ])).optional()
23
+ });
24
+ const devOptions = z.object({
25
+ bundle: z.boolean().default(true),
26
+ minify: z.union([
27
+ z.boolean(),
28
+ z.literal("terser"),
29
+ z.literal("esbuild")
30
+ ]).default(false)
31
+ });
32
+ const injectManifestPartial = z.object({
33
+ mode: z.union([
34
+ z.literal("development"),
35
+ z.literal("production")
36
+ ]),
37
+ type: z.union([
38
+ z.literal("classic"),
39
+ z.literal("module")
40
+ ]).default("classic"),
41
+ scope: z.string(),
42
+ base: z.string(),
43
+ disable: z.boolean().default(false),
44
+ integration: hooks.default({}),
45
+ swUrl: z.string().default("/sw.js"),
46
+ plugins: z.array(z.any()).default([]),
47
+ rollupFormat: z.union([
48
+ z.literal("es"),
49
+ z.literal("iife")
50
+ ]).default("es"),
51
+ rollupOptions: z.record(z.string(), z.any()).default({}),
52
+ devOptions: devOptions.default({})
53
+ });
54
+ const injectManifestOptions = basePartial.merge(globPartial).merge(injectPartial).merge(requiredSwDestPartial).merge(requiredGlobDirectoryPartial).merge(injectManifestPartial).strict("Do not pass invalid properties to InjectManifestOptions!");
55
+
56
+ export { devOptions, hooks, injectManifestOptions, injectManifestPartial };
package/dist/index.d.ts CHANGED
@@ -1,18 +1,19 @@
1
1
  import type { Plugin } from "vite";
2
- import { createApi } from "./api.js";
3
- import { createContext } from "./context.js";
2
+ import { createApi } from "./lib/api.js";
3
+ import { createContext } from "./lib/context.js";
4
+ import type { DevOptions, ExtendManifestEntriesHook, Hooks, PluginOptions, PluginOptionsComplete, SerwistViteApi } from "./lib/types.js";
5
+ import { resolveEntry, toFs } from "./lib/utils.js";
6
+ import { validateInjectManifestOptions } from "./lib/validator.js";
4
7
  import { buildPlugin } from "./plugins/build.js";
5
8
  import { devPlugin } from "./plugins/dev.js";
6
9
  import { mainPlugin } from "./plugins/main.js";
7
- import type { PluginOptions } from "./types.js";
8
- import { resolveEntry, toFs } from "./utils.js";
9
10
  /**
10
11
  * Integrates Serwist into your Vite app.
11
12
  * @param userOptions
12
13
  * @returns
13
14
  */
14
15
  export declare const serwist: (userOptions: PluginOptions) => Plugin[];
15
- export { buildPlugin as build, createApi, createContext, devPlugin as dev, mainPlugin as main, resolveEntry, toFs };
16
- export type { SerwistViteContext } from "./context.js";
17
- export * from "./types.js";
16
+ export { buildPlugin as build, createApi, createContext, devPlugin as dev, mainPlugin as main, resolveEntry, toFs, validateInjectManifestOptions };
17
+ export type { SerwistViteContext } from "./lib/context.js";
18
+ export type { PluginOptions, PluginOptionsComplete, Hooks, ExtendManifestEntriesHook, DevOptions, SerwistViteApi };
18
19
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,MAAM,CAAC;AAEnC,OAAO,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AACrC,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AAC7C,OAAO,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AACjD,OAAO,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAC7C,OAAO,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAC/C,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAChD,OAAO,EAAE,YAAY,EAAE,IAAI,EAAE,MAAM,YAAY,CAAC;AAEhD;;;;GAIG;AACH,eAAO,MAAM,OAAO,gBAAiB,aAAa,KAAG,MAAM,EAI1D,CAAC;AAGF,OAAO,EAAE,WAAW,IAAI,KAAK,EAAE,SAAS,EAAE,aAAa,EAAE,SAAS,IAAI,GAAG,EAAE,UAAU,IAAI,IAAI,EAAE,YAAY,EAAE,IAAI,EAAE,CAAC;AACpH,YAAY,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAC;AACvD,cAAc,YAAY,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,MAAM,CAAC;AAEnC,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AACzC,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AACjD,OAAO,KAAK,EAAE,UAAU,EAAE,yBAAyB,EAAE,KAAK,EAAE,aAAa,EAAE,qBAAqB,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AACzI,OAAO,EAAE,YAAY,EAAE,IAAI,EAAE,MAAM,gBAAgB,CAAC;AACpD,OAAO,EAAE,6BAA6B,EAAE,MAAM,oBAAoB,CAAC;AACnE,OAAO,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AACjD,OAAO,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAC7C,OAAO,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAE/C;;;;GAIG;AACH,eAAO,MAAM,OAAO,gBAAiB,aAAa,KAAG,MAAM,EAI1D,CAAC;AAGF,OAAO,EAAE,WAAW,IAAI,KAAK,EAAE,SAAS,EAAE,aAAa,EAAE,SAAS,IAAI,GAAG,EAAE,UAAU,IAAI,IAAI,EAAE,YAAY,EAAE,IAAI,EAAE,6BAA6B,EAAE,CAAC;AACnJ,YAAY,EAAE,kBAAkB,EAAE,MAAM,kBAAkB,CAAC;AAC3D,YAAY,EAAE,aAAa,EAAE,qBAAqB,EAAE,KAAK,EAAE,yBAAyB,EAAE,UAAU,EAAE,cAAc,EAAE,CAAC"}
package/dist/index.js CHANGED
@@ -1,68 +1,13 @@
1
+ import { cyan, green, dim, yellow } from 'kolorist';
1
2
  import assert from 'node:assert';
2
3
  import fs from 'node:fs/promises';
3
- import path, { resolve } from 'node:path';
4
- import { normalizePath } from 'vite';
4
+ import path from 'node:path';
5
5
  import fs$1 from 'node:fs';
6
- import process$1 from 'node:process';
7
- import crypto from 'node:crypto';
8
- import fg from 'fast-glob';
9
-
10
- let enabled = true;
11
- const globalVar = typeof self !== 'undefined' ? self : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : {};
12
- let supportLevel = 0;
13
- if (globalVar.process && globalVar.process.env && globalVar.process.stdout) {
14
- const { FORCE_COLOR, NODE_DISABLE_COLORS, NO_COLOR, TERM, COLORTERM } = globalVar.process.env;
15
- if (NODE_DISABLE_COLORS || NO_COLOR || FORCE_COLOR === '0') {
16
- enabled = false;
17
- } else if (FORCE_COLOR === '1' || FORCE_COLOR === '2' || FORCE_COLOR === '3') {
18
- enabled = true;
19
- } else if (TERM === 'dumb') {
20
- enabled = false;
21
- } else if ('CI' in globalVar.process.env && [
22
- 'TRAVIS',
23
- 'CIRCLECI',
24
- 'APPVEYOR',
25
- 'GITLAB_CI',
26
- 'GITHUB_ACTIONS',
27
- 'BUILDKITE',
28
- 'DRONE'
29
- ].some((vendor)=>vendor in globalVar.process.env)) {
30
- enabled = true;
31
- } else {
32
- enabled = process.stdout.isTTY;
33
- }
34
- if (enabled) {
35
- if (process.platform === 'win32') {
36
- supportLevel = 3;
37
- } else {
38
- if (COLORTERM && (COLORTERM === 'truecolor' || COLORTERM === '24bit')) {
39
- supportLevel = 3;
40
- } else if (TERM && (TERM.endsWith('-256color') || TERM.endsWith('256'))) {
41
- supportLevel = 2;
42
- } else {
43
- supportLevel = 1;
44
- }
45
- }
46
- }
47
- }
48
- let options = {
49
- enabled,
50
- supportLevel
51
- };
52
- function kolorist(start, end, level = 1) {
53
- const open = `\x1b[${start}m`;
54
- const close = `\x1b[${end}m`;
55
- const regex = new RegExp(`\\x1b\\[${end}m`, 'g');
56
- return (str)=>{
57
- return options.enabled && options.supportLevel >= level ? open + ('' + str).replace(regex, open) + close : '' + str;
58
- };
59
- }
60
- const dim = kolorist(2, 22);
61
- const green = kolorist(32, 39);
62
- const yellow = kolorist(33, 39);
63
- const cyan = kolorist(36, 39);
6
+ import { validationErrorMap, SerwistConfigError } from '@serwist/build/schema';
7
+ import { normalizePath } from 'vite';
8
+ import process from 'node:process';
64
9
 
65
- var version = "9.0.0-preview.9";
10
+ var version = "9.0.0";
66
11
 
67
12
  const logSerwistResult = (buildResult, viteOptions)=>{
68
13
  const { logLevel = "info" } = viteOptions;
@@ -89,9 +34,8 @@ const loadSerwistBuild = async ()=>{
89
34
  return require("@serwist/build");
90
35
  }
91
36
  };
92
- const injectManifest = async (config)=>{
93
- const { validateViteInjectManifestOptions, getFileManifestEntries, stringify } = await loadSerwistBuild();
94
- const options = await validateViteInjectManifestOptions(config);
37
+ const injectManifest = async (options)=>{
38
+ const { getFileManifestEntries, stringify } = await loadSerwistBuild();
95
39
  const { count, size, manifestEntries, warnings } = await getFileManifestEntries(options);
96
40
  const manifestString = manifestEntries === undefined ? "undefined" : stringify(manifestEntries);
97
41
  return {
@@ -103,7 +47,7 @@ const injectManifest = async (config)=>{
103
47
  };
104
48
  };
105
49
  const generateServiceWorker = async (ctx)=>{
106
- const { format, plugins, rollupOptions } = ctx.options.injectManifestRollupOptions;
50
+ const { plugins, rollupOptions, rollupFormat } = ctx.options;
107
51
  const parsedSwDest = path.parse(ctx.options.injectManifest.swDest);
108
52
  let injectManifestResult = undefined;
109
53
  if (ctx.options.injectManifest.injectionPoint) {
@@ -189,7 +133,7 @@ const generateServiceWorker = async (ctx)=>{
189
133
  entry: ctx.options.injectManifest.swSrc,
190
134
  name: "app",
191
135
  formats: [
192
- format
136
+ rollupFormat
193
137
  ]
194
138
  },
195
139
  rollupOptions: {
@@ -254,26 +198,6 @@ const createContext = (userOptions, framework)=>{
254
198
  };
255
199
  };
256
200
 
257
- const buildPlugin = (ctx, api)=>{
258
- return {
259
- name: "@serwist/vite:build",
260
- enforce: "post",
261
- apply: "build",
262
- closeBundle: {
263
- sequential: true,
264
- order: ctx.userOptions?.integration?.closeBundleOrder,
265
- async handler () {
266
- if (!ctx.viteConfig.build.ssr && !ctx.options.disable) {
267
- await api.generateSW();
268
- }
269
- }
270
- },
271
- async buildEnd (error) {
272
- if (error) throw error;
273
- }
274
- };
275
- };
276
-
277
201
  const slash = (str)=>{
278
202
  return str.replace(/\\/g, "/");
279
203
  };
@@ -306,6 +230,39 @@ const toFs = (str)=>{
306
230
  return `/@fs${str.startsWith("/") ? "" : "/"}${str}`;
307
231
  };
308
232
 
233
+ const validateInjectManifestOptions = async (input)=>{
234
+ const result = await (await import('./chunks/schema.js')).injectManifestOptions.spa(input, {
235
+ errorMap: validationErrorMap
236
+ });
237
+ if (!result.success) {
238
+ throw new SerwistConfigError({
239
+ moduleName: "@serwist/vite",
240
+ message: JSON.stringify(result.error.format(), null, 2)
241
+ });
242
+ }
243
+ return result.data;
244
+ };
245
+
246
+ const buildPlugin = (ctx, api)=>{
247
+ return {
248
+ name: "@serwist/vite:build",
249
+ enforce: "post",
250
+ apply: "build",
251
+ closeBundle: {
252
+ sequential: true,
253
+ order: ctx.userOptions?.integration?.closeBundleOrder,
254
+ async handler () {
255
+ if (!ctx.viteConfig.build.ssr && !ctx.options.disable) {
256
+ await api.generateSW();
257
+ }
258
+ }
259
+ },
260
+ buildEnd (error) {
261
+ if (error) throw error;
262
+ }
263
+ };
264
+ };
265
+
309
266
  const devPlugin = (ctx, api)=>{
310
267
  return {
311
268
  name: "@serwist/vite:dev",
@@ -346,110 +303,44 @@ const devPlugin = (ctx, api)=>{
346
303
  };
347
304
  };
348
305
 
349
- const INTERNAL_SERWIST_VIRTUAL = "virtual:internal-serwist";
350
- const RESOLVED_INTERNAL_SERWIST_VIRTUAL = `\0${INTERNAL_SERWIST_VIRTUAL}`;
306
+ const SERWIST_VIRTUAL = "virtual:serwist";
307
+ const RESOLVED_SERWIST_VIRTUAL = `\0${SERWIST_VIRTUAL}`;
351
308
 
352
- const buildManifestEntry = (publicDir, url)=>{
353
- return new Promise((resolve$1, reject)=>{
354
- const cHash = crypto.createHash("MD5");
355
- const stream = fs$1.createReadStream(resolve(publicDir, url));
356
- stream.on("error", (err)=>{
357
- reject(err);
358
- });
359
- stream.on("data", (chunk)=>{
360
- cHash.update(chunk);
361
- });
362
- stream.on("end", ()=>{
363
- return resolve$1({
364
- url,
365
- revision: `${cHash.digest("hex")}`
366
- });
367
- });
368
- });
369
- };
370
- const lookupAdditionalPrecacheEntries = (serwistOptions)=>{
371
- return serwistOptions.additionalPrecacheEntries || [];
372
- };
373
- const normalizeIconPath = (path)=>{
374
- return path.startsWith("/") ? path.substring(1) : path;
375
- };
376
- const configureStaticAssets = async (resolvedPluginOptions, viteConfig)=>{
377
- const { injectManifest, includeAssets } = resolvedPluginOptions;
378
- const { publicDir } = viteConfig;
379
- const globs = [];
380
- const manifestEntries = lookupAdditionalPrecacheEntries(injectManifest);
381
- if (includeAssets) {
382
- if (Array.isArray(includeAssets)) globs.push(...includeAssets.map(normalizeIconPath));
383
- else globs.push(normalizeIconPath(includeAssets));
384
- }
385
- if (globs.length > 0) {
386
- let assets = await fg(globs, {
387
- cwd: publicDir,
388
- onlyFiles: true,
389
- unique: true
390
- });
391
- if (manifestEntries.length > 0) {
392
- const included = manifestEntries.map((me)=>{
393
- if (typeof me === "string") return me;
394
- return me.url;
395
- });
396
- assets = assets.filter((a)=>!included.includes(a));
397
- }
398
- const assetsEntries = await Promise.all(assets.map((a)=>{
399
- return buildManifestEntry(publicDir, a);
400
- }));
401
- manifestEntries.push(...assetsEntries);
402
- }
403
- if (manifestEntries.length > 0) {
404
- injectManifest.additionalPrecacheEntries = manifestEntries;
405
- }
309
+ const prepareConfigForValidation = (viteConfig, { mode = process.env.NODE_ENV === "production" || process.env.NODE_ENV === "development" ? process.env.NODE_ENV : "production", base = viteConfig.base, scope: _scope, devOptions, ...injectManifest })=>{
310
+ const basePath = resolveBasePath(base);
311
+ return {
312
+ mode,
313
+ base: basePath,
314
+ scope: _scope || basePath,
315
+ devOptions,
316
+ ...injectManifest
317
+ };
406
318
  };
407
-
408
319
  const resolveOptions = async (options, viteConfig)=>{
409
- const { type = "classic", mode = process$1.env.NODE_ENV === "production" || process$1.env.NODE_ENV === "development" ? process$1.env.NODE_ENV : "production", injectRegister = "auto", registerType = "prompt", minify = true, base = viteConfig.base, scope: _scope, swUrl = "/sw.js", includeAssets = undefined, useCredentials = false, disable = false, integration = {}, buildBase, devOptions, plugins = [], rollupOptions = {}, rollupFormat = "es", ...injectManifest } = options;
410
- const basePath = resolveBasePath(base);
411
- const scope = _scope || basePath;
320
+ const { mode, type, scope, base, disable, integration, swUrl, swSrc, swDest, plugins, rollupFormat, rollupOptions, devOptions, ...userInjectManifest } = await validateInjectManifestOptions(prepareConfigForValidation(viteConfig, options));
412
321
  let assetsDir = slash(viteConfig.build.assetsDir ?? "assets");
413
322
  if (assetsDir[assetsDir.length - 1] !== "/") assetsDir += "/";
414
- const resolvedDevOptions = {
415
- bundle: true,
416
- minify: false,
417
- ...devOptions
418
- };
419
323
  const dontCacheBustURLsMatching = new RegExp(`^${assetsDir.replace(/^\.*?\//, "")}`);
420
- const { validateViteInjectManifestOptions } = await loadSerwistBuild();
421
- const validatedInjectManifest = await validateViteInjectManifestOptions(injectManifest);
422
- const { swSrc, swDest, ...userInjectManifest } = validatedInjectManifest || {};
423
324
  const resolvedPluginOptions = {
424
- base: basePath,
425
- type,
426
325
  mode,
427
- injectRegister,
428
- registerType,
429
- useCredentials,
326
+ type,
327
+ scope,
328
+ base,
329
+ disable,
330
+ integration,
430
331
  swUrl,
332
+ plugins,
333
+ rollupFormat,
334
+ rollupOptions,
335
+ devOptions,
431
336
  injectManifest: {
432
337
  dontCacheBustURLsMatching,
433
338
  ...userInjectManifest,
434
339
  swSrc: path.resolve(viteConfig.root, swSrc),
435
340
  swDest: path.resolve(viteConfig.root, viteConfig.build.outDir, swDest),
436
341
  disablePrecacheManifest: !viteConfig.isProduction
437
- },
438
- scope,
439
- minify,
440
- includeAssets,
441
- disable,
442
- integration,
443
- buildBase: buildBase ?? basePath,
444
- injectManifestRollupOptions: {
445
- plugins,
446
- rollupOptions,
447
- format: rollupFormat
448
- },
449
- devOptions: resolvedDevOptions
342
+ }
450
343
  };
451
- const calculateHash = !resolvedPluginOptions.disable && resolvedPluginOptions.includeAssets && viteConfig.command === "build";
452
- if (calculateHash) await configureStaticAssets(resolvedPluginOptions, viteConfig);
453
344
  return resolvedPluginOptions;
454
345
  };
455
346
 
@@ -470,16 +361,22 @@ const mainPlugin = (ctx, api)=>{
470
361
  ctx.options = await resolveOptions(ctx.userOptions, config);
471
362
  },
472
363
  resolveId (id) {
473
- if (id === INTERNAL_SERWIST_VIRTUAL) {
474
- return RESOLVED_INTERNAL_SERWIST_VIRTUAL;
364
+ if (id === SERWIST_VIRTUAL) {
365
+ return RESOLVED_SERWIST_VIRTUAL;
475
366
  }
476
367
  return undefined;
477
368
  },
478
369
  load (id) {
479
- if (id === RESOLVED_INTERNAL_SERWIST_VIRTUAL) {
480
- return `export const swUrl = "${path.posix.join(ctx.options.buildBase, ctx.options.swUrl)}";
370
+ if (id === RESOLVED_SERWIST_VIRTUAL) {
371
+ return `export const swUrl = "${path.posix.join(ctx.options.base, ctx.options.swUrl)}";
481
372
  export const swScope = "${ctx.options.scope}";
482
- export const swType = "${ctx.devEnvironment ? "module" : ctx.options.type}";`;
373
+ export const swType = "${ctx.devEnvironment ? "module" : ctx.options.type}";
374
+ export const getSerwist = async () => {
375
+ if ("serviceWorker" in navigator) {
376
+ return new (await import("@serwist/window")).Serwist(swUrl, { scope: swScope, type: swType });
377
+ }
378
+ return undefined;
379
+ }`;
483
380
  }
484
381
  return undefined;
485
382
  },
@@ -497,4 +394,4 @@ const serwist = (userOptions)=>{
497
394
  ];
498
395
  };
499
396
 
500
- export { buildPlugin as build, createApi, createContext, devPlugin as dev, mainPlugin as main, resolveEntry, serwist, toFs };
397
+ export { buildPlugin as build, createApi, createContext, devPlugin as dev, mainPlugin as main, resolveEntry, serwist, toFs, validateInjectManifestOptions };
@@ -0,0 +1,3 @@
1
+ import { devOptions, hooks, injectManifestOptions, injectManifestPartial } from "./lib/schema.js";
2
+ export { hooks, devOptions, injectManifestPartial, injectManifestOptions };
3
+ //# sourceMappingURL=index.schema.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.schema.d.ts","sourceRoot":"","sources":["../src/index.schema.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,KAAK,EAAE,qBAAqB,EAAE,qBAAqB,EAAE,MAAM,iBAAiB,CAAC;AAElG,OAAO,EAAE,KAAK,EAAE,UAAU,EAAE,qBAAqB,EAAE,qBAAqB,EAAE,CAAC"}
@@ -0,0 +1,3 @@
1
+ export { devOptions, hooks, injectManifestOptions, injectManifestPartial } from './chunks/schema.js';
2
+ import '@serwist/build/schema';
3
+ import 'zod';
@@ -1,11 +1,9 @@
1
- import { CacheFirst, NetworkFirst } from "@serwist/strategies";
2
- export declare const defaultCache: ({
3
- matcher: RegExp;
4
- handler: CacheFirst;
5
- method?: undefined;
6
- } | {
7
- matcher: RegExp;
8
- method: "GET";
9
- handler: NetworkFirst;
10
- })[];
1
+ import type { RuntimeCaching } from "serwist";
2
+ /**
3
+ * The default, recommended list of caching strategies for applications
4
+ * built with Vite.
5
+ *
6
+ * @see https://serwist.pages.dev/docs/vite/worker-exports#default-cache
7
+ */
8
+ export declare const defaultCache: RuntimeCaching[];
11
9
  //# sourceMappingURL=index.worker.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.worker.d.ts","sourceRoot":"","sources":["../src/index.worker.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,YAAY,EAAwB,MAAM,qBAAqB,CAAC;AAGrF,eAAO,MAAM,YAAY;;;;;;;;IAoGG,CAAC"}
1
+ {"version":3,"file":"index.worker.d.ts","sourceRoot":"","sources":["../src/index.worker.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AAG9C;;;;;GAKG;AACH,eAAO,MAAM,YAAY,EAAE,cAAc,EA8GpC,CAAC"}
@@ -1,7 +1,6 @@
1
- import { ExpirationPlugin } from '@serwist/expiration';
2
- import { CacheFirst, StaleWhileRevalidate, NetworkFirst } from '@serwist/strategies';
1
+ import { CacheFirst, ExpirationPlugin, StaleWhileRevalidate, NetworkFirst } from 'serwist';
3
2
 
4
- const defaultCache = [
3
+ const defaultCache = import.meta.env.DEV ? [] : [
5
4
  {
6
5
  matcher: /^https:\/\/fonts\.(?:googleapis|gstatic)\.com\/.*/i,
7
6
  handler: new CacheFirst({
@@ -9,7 +8,8 @@ const defaultCache = [
9
8
  plugins: [
10
9
  new ExpirationPlugin({
11
10
  maxEntries: 4,
12
- maxAgeSeconds: 365 * 24 * 60 * 60
11
+ maxAgeSeconds: 365 * 24 * 60 * 60,
12
+ maxAgeFrom: "last-used"
13
13
  })
14
14
  ]
15
15
  })
@@ -21,7 +21,8 @@ const defaultCache = [
21
21
  plugins: [
22
22
  new ExpirationPlugin({
23
23
  maxEntries: 4,
24
- maxAgeSeconds: 7 * 24 * 60 * 60
24
+ maxAgeSeconds: 7 * 24 * 60 * 60,
25
+ maxAgeFrom: "last-used"
25
26
  })
26
27
  ]
27
28
  })
@@ -33,7 +34,8 @@ const defaultCache = [
33
34
  plugins: [
34
35
  new ExpirationPlugin({
35
36
  maxEntries: 64,
36
- maxAgeSeconds: 24 * 60 * 60
37
+ maxAgeSeconds: 24 * 60 * 60,
38
+ maxAgeFrom: "last-used"
37
39
  })
38
40
  ]
39
41
  })
@@ -45,7 +47,8 @@ const defaultCache = [
45
47
  plugins: [
46
48
  new ExpirationPlugin({
47
49
  maxEntries: 32,
48
- maxAgeSeconds: 24 * 60 * 60
50
+ maxAgeSeconds: 24 * 60 * 60,
51
+ maxAgeFrom: "last-used"
49
52
  })
50
53
  ]
51
54
  })
@@ -57,7 +60,8 @@ const defaultCache = [
57
60
  plugins: [
58
61
  new ExpirationPlugin({
59
62
  maxEntries: 32,
60
- maxAgeSeconds: 24 * 60 * 60
63
+ maxAgeSeconds: 24 * 60 * 60,
64
+ maxAgeFrom: "last-used"
61
65
  })
62
66
  ]
63
67
  })
@@ -69,7 +73,8 @@ const defaultCache = [
69
73
  plugins: [
70
74
  new ExpirationPlugin({
71
75
  maxEntries: 32,
72
- maxAgeSeconds: 24 * 60 * 60
76
+ maxAgeSeconds: 24 * 60 * 60,
77
+ maxAgeFrom: "last-used"
73
78
  })
74
79
  ]
75
80
  })
@@ -82,7 +87,8 @@ const defaultCache = [
82
87
  plugins: [
83
88
  new ExpirationPlugin({
84
89
  maxEntries: 16,
85
- maxAgeSeconds: 24 * 60 * 60
90
+ maxAgeSeconds: 24 * 60 * 60,
91
+ maxAgeFrom: "last-used"
86
92
  })
87
93
  ],
88
94
  networkTimeoutSeconds: 10
@@ -95,7 +101,8 @@ const defaultCache = [
95
101
  plugins: [
96
102
  new ExpirationPlugin({
97
103
  maxEntries: 32,
98
- maxAgeSeconds: 24 * 60 * 60
104
+ maxAgeSeconds: 24 * 60 * 60,
105
+ maxAgeFrom: "last-used"
99
106
  })
100
107
  ],
101
108
  networkTimeoutSeconds: 10
@@ -0,0 +1 @@
1
+ {"version":3,"file":"api.d.ts","sourceRoot":"","sources":["../../src/lib/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"}
@@ -0,0 +1,3 @@
1
+ export declare const SERWIST_VIRTUAL = "virtual:serwist";
2
+ export declare const RESOLVED_SERWIST_VIRTUAL = "\0virtual:serwist";
3
+ //# sourceMappingURL=constants.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../../src/lib/constants.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,eAAe,oBAAoB,CAAC;AACjD,eAAO,MAAM,wBAAwB,sBAAyB,CAAC"}
@@ -1,5 +1,5 @@
1
1
  import type { ResolvedConfig } from "vite";
2
- import type { PluginOptions, ResolvedPluginOptions } from "./types.js";
2
+ import type { PluginOptions, PluginOptionsComplete } from "./types.js";
3
3
  export type SerwistViteFrameworks = "nuxt";
4
4
  export interface SerwistViteContext {
5
5
  /**
@@ -18,7 +18,7 @@ export interface SerwistViteContext {
18
18
  * Note: this is different from `userOptions` in that it has been parsed, whereas
19
19
  * `userOptions` is the raw configuration that the user provides us.
20
20
  */
21
- options: ResolvedPluginOptions;
21
+ options: PluginOptionsComplete;
22
22
  useImportRegister: boolean;
23
23
  /**
24
24
  * Is the plugin running on dev?
@@ -0,0 +1 @@
1
+ {"version":3,"file":"context.d.ts","sourceRoot":"","sources":["../../src/lib/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"}
@@ -0,0 +1 @@
1
+ {"version":3,"file":"log.d.ts","sourceRoot":"","sources":["../../src/lib/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"}
@@ -4,7 +4,7 @@ export declare const loadSerwistBuild: () => Promise<typeof SerwistBuild>;
4
4
  interface BuildResult extends SerwistBuild.GetManifestResult {
5
5
  manifestString: string;
6
6
  }
7
- export declare const injectManifest: (config: SerwistBuild.ViteInjectManifestOptions) => Promise<BuildResult>;
7
+ export declare const injectManifest: (options: SerwistBuild.GetManifestOptionsComplete) => Promise<BuildResult>;
8
8
  export declare const generateServiceWorker: (ctx: SerwistViteContext) => Promise<BuildResult | undefined>;
9
9
  export {};
10
10
  //# sourceMappingURL=modules.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"modules.d.ts","sourceRoot":"","sources":["../../src/lib/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,YAAmB,aAAa,0BAA0B,KAAG,QAAQ,WAAW,CAW1G,CAAC;AAEF,eAAO,MAAM,qBAAqB,QAAe,kBAAkB,qCAwIlE,CAAC"}
@@ -0,0 +1,4 @@
1
+ import type { ResolvedConfig } from "vite";
2
+ import type { PluginOptions, PluginOptionsComplete } from "./types.js";
3
+ export declare const resolveOptions: (options: PluginOptions, viteConfig: ResolvedConfig) => Promise<PluginOptionsComplete>;
4
+ //# sourceMappingURL=options.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"options.d.ts","sourceRoot":"","sources":["../../src/lib/options.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,MAAM,CAAC;AAE3C,OAAO,KAAK,EAAE,aAAa,EAAE,qBAAqB,EAAE,MAAM,YAAY,CAAC;AAwBvE,eAAO,MAAM,cAAc,YAAmB,aAAa,cAAc,cAAc,KAAG,QAAQ,qBAAqB,CA8CtH,CAAC"}