@serwist/build 10.0.0-preview.1 → 10.0.0-preview.11

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 (114) hide show
  1. package/dist/chunks/constants.js +59 -0
  2. package/dist/chunks/get-manifest.js +11 -0
  3. package/dist/chunks/glob.js +50 -30
  4. package/dist/chunks/inject-manifest.js +29 -0
  5. package/dist/get-manifest.d.ts +1 -1
  6. package/dist/get-manifest.d.ts.map +1 -1
  7. package/dist/index.d.ts +3 -11
  8. package/dist/index.d.ts.map +1 -1
  9. package/dist/index.js +16 -151
  10. package/dist/index.schema.d.ts +9 -9
  11. package/dist/index.schema.d.ts.map +1 -1
  12. package/dist/index.schema.js +4 -4
  13. package/dist/inject-manifest.d.ts.map +1 -1
  14. package/dist/lib/additional-precache-entries-transform.d.ts +3 -7
  15. package/dist/lib/additional-precache-entries-transform.d.ts.map +1 -1
  16. package/dist/lib/constants.d.ts +2 -0
  17. package/dist/lib/constants.d.ts.map +1 -0
  18. package/dist/lib/escape-regexp.d.ts.map +1 -1
  19. package/dist/lib/get-file-details.d.ts +2 -1
  20. package/dist/lib/get-file-details.d.ts.map +1 -1
  21. package/dist/lib/get-file-manifest-entries.d.ts.map +1 -1
  22. package/dist/lib/modify-url-prefix-transform.d.ts.map +1 -1
  23. package/dist/lib/no-revision-for-urls-matching-transform.d.ts.map +1 -1
  24. package/dist/lib/transform-manifest.d.ts +2 -1
  25. package/dist/lib/transform-manifest.d.ts.map +1 -1
  26. package/dist/lib/validate-options.d.ts.map +1 -1
  27. package/dist/schema/{assertType.d.ts → assert-type.d.ts} +1 -1
  28. package/dist/schema/assert-type.d.ts.map +1 -0
  29. package/dist/schema/base.d.ts +5 -166
  30. package/dist/schema/base.d.ts.map +1 -1
  31. package/dist/schema/{serwistConfigError.d.ts → error.d.ts} +3 -1
  32. package/dist/schema/error.d.ts.map +1 -0
  33. package/dist/schema/get-manifest.d.ts +16 -0
  34. package/dist/schema/get-manifest.d.ts.map +1 -0
  35. package/dist/schema/glob.d.ts +6 -26
  36. package/dist/schema/glob.d.ts.map +1 -1
  37. package/dist/schema/inject-manifest.d.ts +23 -0
  38. package/dist/schema/inject-manifest.d.ts.map +1 -0
  39. package/dist/schema/manifest-entry.d.ts +16 -0
  40. package/dist/schema/manifest-entry.d.ts.map +1 -0
  41. package/dist/schema/manifest-transform.d.ts +8 -0
  42. package/dist/schema/manifest-transform.d.ts.map +1 -0
  43. package/dist/schema/sw-dest.d.ts +8 -0
  44. package/dist/schema/sw-dest.d.ts.map +1 -0
  45. package/dist/schema/utils.d.ts +16 -0
  46. package/dist/schema/utils.d.ts.map +1 -0
  47. package/dist/types.d.ts +12 -28
  48. package/dist/types.d.ts.map +1 -1
  49. package/package.json +13 -12
  50. package/src/get-manifest.ts +1 -1
  51. package/src/index.schema.ts +10 -8
  52. package/src/index.ts +5 -11
  53. package/src/inject-manifest.ts +2 -1
  54. package/src/lib/additional-precache-entries-transform.ts +5 -6
  55. package/src/lib/constants.ts +1 -0
  56. package/src/lib/get-file-details.ts +3 -4
  57. package/src/lib/get-file-manifest-entries.ts +3 -4
  58. package/src/lib/modify-url-prefix-transform.ts +1 -1
  59. package/src/lib/no-revision-for-urls-matching-transform.ts +1 -1
  60. package/src/lib/rebase-path.ts +1 -1
  61. package/src/lib/transform-manifest.ts +3 -3
  62. package/src/lib/translate-url-to-sourcemap-paths.ts +4 -4
  63. package/src/lib/validate-options.ts +12 -6
  64. package/src/schema/base.ts +13 -15
  65. package/src/schema/error.ts +53 -0
  66. package/src/schema/{getManifest.ts → get-manifest.ts} +7 -6
  67. package/src/schema/glob.ts +15 -20
  68. package/src/schema/inject-manifest.ts +24 -0
  69. package/src/schema/manifest-entry.ts +11 -0
  70. package/src/schema/manifest-transform.ts +14 -0
  71. package/src/schema/{swDest.ts → sw-dest.ts} +7 -11
  72. package/src/schema/utils.ts +48 -0
  73. package/src/types.ts +9 -32
  74. package/dist/chunks/getManifest.js +0 -6
  75. package/dist/chunks/injectManifest.js +0 -23
  76. package/dist/chunks/validationErrorMap.js +0 -54
  77. package/dist/lib/errors.d.ts +0 -47
  78. package/dist/lib/errors.d.ts.map +0 -1
  79. package/dist/lib/get-composite-details.d.ts +0 -3
  80. package/dist/lib/get-composite-details.d.ts.map +0 -1
  81. package/dist/lib/get-file-hash.d.ts +0 -2
  82. package/dist/lib/get-file-hash.d.ts.map +0 -1
  83. package/dist/lib/get-file-size.d.ts +0 -2
  84. package/dist/lib/get-file-size.d.ts.map +0 -1
  85. package/dist/lib/get-string-details.d.ts +0 -3
  86. package/dist/lib/get-string-details.d.ts.map +0 -1
  87. package/dist/lib/get-string-hash.d.ts +0 -3
  88. package/dist/lib/get-string-hash.d.ts.map +0 -1
  89. package/dist/schema/assertType.d.ts.map +0 -1
  90. package/dist/schema/getManifest.d.ts +0 -192
  91. package/dist/schema/getManifest.d.ts.map +0 -1
  92. package/dist/schema/injectManifest.d.ts +0 -213
  93. package/dist/schema/injectManifest.d.ts.map +0 -1
  94. package/dist/schema/manifestEntry.d.ts +0 -15
  95. package/dist/schema/manifestEntry.d.ts.map +0 -1
  96. package/dist/schema/manifestTransform.d.ts +0 -125
  97. package/dist/schema/manifestTransform.d.ts.map +0 -1
  98. package/dist/schema/serwistConfigError.d.ts.map +0 -1
  99. package/dist/schema/swDest.d.ts +0 -16
  100. package/dist/schema/swDest.d.ts.map +0 -1
  101. package/dist/schema/validationErrorMap.d.ts +0 -3
  102. package/dist/schema/validationErrorMap.d.ts.map +0 -1
  103. package/src/lib/errors.ts +0 -93
  104. package/src/lib/get-composite-details.ts +0 -31
  105. package/src/lib/get-file-hash.ts +0 -21
  106. package/src/lib/get-file-size.ts +0 -22
  107. package/src/lib/get-string-details.ts +0 -16
  108. package/src/lib/get-string-hash.ts +0 -15
  109. package/src/schema/injectManifest.ts +0 -25
  110. package/src/schema/manifestEntry.ts +0 -9
  111. package/src/schema/manifestTransform.ts +0 -15
  112. package/src/schema/serwistConfigError.ts +0 -6
  113. package/src/schema/validationErrorMap.ts +0 -36
  114. /package/src/schema/{assertType.ts → assert-type.ts} +0 -0
@@ -7,12 +7,11 @@
7
7
  */
8
8
 
9
9
  import assert from "node:assert";
10
+ import type { FileDetails } from "@serwist/utils";
11
+ import { errors, getCompositeDetails, getStringDetails } from "@serwist/utils/node";
10
12
 
11
- import type { FileDetails, GetManifestOptionsComplete, GetManifestResult } from "../types.js";
12
- import { errors } from "./errors.js";
13
- import { getCompositeDetails } from "./get-composite-details.js";
13
+ import type { GetManifestOptionsComplete, GetManifestResult } from "../types.js";
14
14
  import { getFileDetails } from "./get-file-details.js";
15
- import { getStringDetails } from "./get-string-details.js";
16
15
  import { transformManifest } from "./transform-manifest.js";
17
16
 
18
17
  export const getFileManifestEntries = async ({
@@ -5,9 +5,9 @@
5
5
  license that can be found in the LICENSE file or at
6
6
  https://opensource.org/licenses/MIT.
7
7
  */
8
+ import { errors } from "@serwist/utils/node";
8
9
 
9
10
  import type { ManifestTransform } from "../types.js";
10
- import { errors } from "./errors.js";
11
11
  import { escapeRegExp } from "./escape-regexp.js";
12
12
 
13
13
  export function modifyURLPrefixTransform(modifyURLPrefix: { [key: string]: string }): ManifestTransform {
@@ -5,9 +5,9 @@
5
5
  license that can be found in the LICENSE file or at
6
6
  https://opensource.org/licenses/MIT.
7
7
  */
8
+ import { errors } from "@serwist/utils/node";
8
9
 
9
10
  import type { ManifestTransform } from "../types.js";
10
- import { errors } from "./errors.js";
11
11
 
12
12
  export function noRevisionForURLsMatchingTransform(regexp: RegExp): ManifestTransform {
13
13
  if (!(regexp instanceof RegExp)) {
@@ -6,8 +6,8 @@
6
6
  https://opensource.org/licenses/MIT.
7
7
  */
8
8
 
9
- import { toUnix } from "@serwist/utils";
10
9
  import path from "node:path";
10
+ import { toUnix } from "@serwist/utils";
11
11
 
12
12
  export function rebasePath({ baseDirectory, file }: { baseDirectory: string; file: string }): string {
13
13
  // The initial path is relative to the current directory, so make it absolute.
@@ -5,10 +5,11 @@
5
5
  license that can be found in the LICENSE file or at
6
6
  https://opensource.org/licenses/MIT.
7
7
  */
8
+ import type { FileDetails } from "@serwist/utils";
9
+ import { errors } from "@serwist/utils/node";
8
10
 
9
- import type { BaseResolved, FileDetails, ManifestEntry, ManifestTransform } from "../types.js";
11
+ import type { BaseResolved, ManifestEntry, ManifestTransform } from "../types.js";
10
12
  import { additionalPrecacheEntriesTransform } from "./additional-precache-entries-transform.js";
11
- import { errors } from "./errors.js";
12
13
  import { maximumSizeTransform } from "./maximum-size-transform.js";
13
14
  import { modifyURLPrefixTransform } from "./modify-url-prefix-transform.js";
14
15
  import { noRevisionForURLsMatchingTransform } from "./no-revision-for-urls-matching-transform.js";
@@ -155,7 +156,6 @@ export async function transformManifest({
155
156
  let size = 0;
156
157
  for (const manifestEntry of transformedManifest as (ManifestEntry & { size?: number })[]) {
157
158
  size += manifestEntry.size || 0;
158
- // biome-ignore lint/performance/noDelete: These values are no longer necessary.
159
159
  delete manifestEntry.size;
160
160
  }
161
161
 
@@ -8,7 +8,7 @@
8
8
  import fs from "node:fs";
9
9
  import path from "node:path";
10
10
  import { toUnix } from "@serwist/utils";
11
- import { errors } from "./errors.js";
11
+ import { errors } from "@serwist/utils/node";
12
12
 
13
13
  export function translateURLToSourcemapPaths(
14
14
  url: string | null,
@@ -19,9 +19,9 @@ export function translateURLToSourcemapPaths(
19
19
  srcPath: string | undefined;
20
20
  warning: string | undefined;
21
21
  } {
22
- let destPath: string | undefined = undefined;
23
- let srcPath: string | undefined = undefined;
24
- let warning: string | undefined = undefined;
22
+ let destPath: string | undefined;
23
+ let srcPath: string | undefined;
24
+ let warning: string | undefined;
25
25
 
26
26
  if (url && !url.startsWith("data:")) {
27
27
  const possibleSrcPath = path.resolve(path.dirname(swSrc), url);
@@ -1,4 +1,4 @@
1
- import { SerwistConfigError } from "../schema/serwistConfigError.js";
1
+ import { z } from "zod";
2
2
  /*
3
3
  Copyright 2021 Google LLC
4
4
 
@@ -6,21 +6,27 @@ import { SerwistConfigError } from "../schema/serwistConfigError.js";
6
6
  license that can be found in the LICENSE file or at
7
7
  https://opensource.org/licenses/MIT.
8
8
  */
9
- import { validationErrorMap } from "../schema/validationErrorMap.js";
9
+ import { SerwistConfigError, validationErrorMap } from "../schema/error.js";
10
10
  import type { GetManifestOptionsComplete, InjectManifestOptionsComplete } from "../types.js";
11
11
 
12
12
  export const validateGetManifestOptions = async (input: unknown): Promise<GetManifestOptionsComplete> => {
13
- const result = await (await import("../schema/getManifest.js")).getManifestOptions.spa(input, { errorMap: validationErrorMap });
13
+ const result = await (await import("../schema/get-manifest.js")).getManifestOptions.spa(input, { error: validationErrorMap });
14
14
  if (!result.success) {
15
- throw new SerwistConfigError({ moduleName: "@serwist/build", message: JSON.stringify(result.error.format(), null, 2) });
15
+ throw new SerwistConfigError({
16
+ moduleName: "@serwist/build",
17
+ message: z.prettifyError(result.error),
18
+ });
16
19
  }
17
20
  return result.data;
18
21
  };
19
22
 
20
23
  export const validateInjectManifestOptions = async (input: unknown): Promise<InjectManifestOptionsComplete> => {
21
- const result = await (await import("../schema/injectManifest.js")).injectManifestOptions.spa(input, { errorMap: validationErrorMap });
24
+ const result = await (await import("../schema/inject-manifest.js")).injectManifestOptions.spa(input, { error: validationErrorMap });
22
25
  if (!result.success) {
23
- throw new SerwistConfigError({ moduleName: "@serwist/build", message: JSON.stringify(result.error.format(), null, 2) });
26
+ throw new SerwistConfigError({
27
+ moduleName: "@serwist/build",
28
+ message: z.prettifyError(result.error),
29
+ });
24
30
  }
25
31
  return result.data;
26
32
  };
@@ -1,20 +1,18 @@
1
1
  import { z } from "zod";
2
+ import type { BasePartial, BaseResolved, ManifestEntry, ManifestTransform } from "../types.js";
3
+ import { assertType, type Equals } from "./assert-type.js";
4
+ import { manifestEntry } from "./manifest-entry.js";
5
+ import { manifestTransform } from "./manifest-transform.js";
6
+ import { reference } from "./utils.js";
2
7
 
3
- import type { BasePartial, BaseResolved } from "../types.js";
4
- import { type Equals, assertType } from "./assertType.js";
5
- import { manifestEntry } from "./manifestEntry.js";
6
- import { manifestTransform } from "./manifestTransform.js";
7
-
8
- export const basePartial = z
9
- .object({
10
- additionalPrecacheEntries: z.array(z.union([z.string(), manifestEntry])).optional(),
11
- disablePrecacheManifest: z.boolean().default(false),
12
- dontCacheBustURLsMatching: z.instanceof(RegExp).optional(),
13
- manifestTransforms: z.array(manifestTransform).optional(),
14
- maximumFileSizeToCacheInBytes: z.number().default(2097152),
15
- modifyURLPrefix: z.record(z.string(), z.string()).optional(),
16
- })
17
- .strict("Do not pass invalid properties to BasePartial!");
8
+ export const basePartial = z.strictObject({
9
+ additionalPrecacheEntries: z.array(z.union([z.string(), reference<ManifestEntry>(manifestEntry)])).optional(),
10
+ disablePrecacheManifest: z.boolean().default(false),
11
+ dontCacheBustURLsMatching: z.instanceof(RegExp).optional(),
12
+ manifestTransforms: z.array(reference<ManifestTransform>(manifestTransform)).optional(),
13
+ maximumFileSizeToCacheInBytes: z.number().default(2097152),
14
+ modifyURLPrefix: z.record(z.string(), z.string()).optional(),
15
+ });
18
16
 
19
17
  assertType<Equals<BasePartial, z.input<typeof basePartial>>>();
20
18
  assertType<Equals<BaseResolved, z.output<typeof basePartial>>>();
@@ -0,0 +1,53 @@
1
+ import type { z } from "zod";
2
+
3
+ export class SerwistConfigError extends Error {
4
+ constructor({ moduleName, message }: { moduleName?: string; message?: string }) {
5
+ super(`Invalid ${moduleName ?? "Serwist"} configuration:\n${message}`);
6
+ Object.setPrototypeOf(this, new.target.prototype);
7
+ }
8
+ }
9
+
10
+ const parsedType = (data: any): string => {
11
+ const t = typeof data;
12
+
13
+ switch (t) {
14
+ case "number": {
15
+ return Number.isNaN(data) ? "NaN" : "number";
16
+ }
17
+ case "object": {
18
+ if (Array.isArray(data)) {
19
+ return "array";
20
+ }
21
+ if (data === null) {
22
+ return "null";
23
+ }
24
+
25
+ if (Object.getPrototypeOf(data) !== Object.prototype && data.constructor) {
26
+ return data.constructor.name;
27
+ }
28
+ }
29
+ }
30
+ return t;
31
+ };
32
+
33
+ export const validationErrorMap: z.core.$ZodErrorMap = (error) => {
34
+ /* This is where you override the various error codes */
35
+ switch (error.code) {
36
+ case "invalid_type": {
37
+ return `${error.message ?? "Received invalid type"}: expected ${error.expected}, received ${parsedType(error.input)}.`;
38
+ }
39
+ case "invalid_value": {
40
+ return `${error.message ?? "Received invalid value"}: expected ${error.expected}, received ${parsedType(error.input)}.`;
41
+ }
42
+ case "invalid_union": {
43
+ return `${error.message ?? "Received invalid union"}:\n${error.errors.flatMap((err) => err.map((e) => ` → ${e.message}`)).join("\n")}`;
44
+ }
45
+ case "unrecognized_keys": {
46
+ return `${error.message ?? "Received unrecognized keys"}: ${error.keys.join(".")}`;
47
+ }
48
+ case "custom": {
49
+ return error.message ?? undefined;
50
+ }
51
+ }
52
+ return undefined;
53
+ };
@@ -1,13 +1,14 @@
1
- import type { z } from "zod";
1
+ import { z } from "zod";
2
2
  import type { GetManifestOptions, GetManifestOptionsComplete } from "../types.js";
3
- import { type Equals, assertType } from "./assertType.js";
3
+ import { assertType, type Equals } from "./assert-type.js";
4
4
  import { basePartial } from "./base.js";
5
5
  import { globPartial, requiredGlobDirectoryPartial } from "./glob.js";
6
6
 
7
- export const getManifestOptions = basePartial
8
- .merge(globPartial)
9
- .merge(requiredGlobDirectoryPartial)
10
- .strict("Do not pass invalid properties to GetManifestOptions!");
7
+ export const getManifestOptions = z.strictObject({
8
+ ...basePartial.shape,
9
+ ...globPartial.shape,
10
+ ...requiredGlobDirectoryPartial.shape,
11
+ });
11
12
 
12
13
  assertType<Equals<GetManifestOptions, z.input<typeof getManifestOptions>>>();
13
14
  assertType<Equals<GetManifestOptionsComplete, z.output<typeof getManifestOptions>>>();
@@ -1,4 +1,5 @@
1
1
  import { z } from "zod";
2
+ import { DEFAULT_GLOB_PATTERNS } from "../lib/constants.js";
2
3
  import type {
3
4
  GlobPartial,
4
5
  GlobResolved,
@@ -7,31 +8,25 @@ import type {
7
8
  RequiredGlobDirectoryPartial,
8
9
  RequiredGlobDirectoryResolved,
9
10
  } from "../types.js";
10
- import { type Equals, assertType } from "./assertType.js";
11
+ import { assertType, type Equals } from "./assert-type.js";
11
12
 
12
- export const globPartial = z
13
- .object({
14
- globFollow: z.boolean().default(true),
15
- globIgnores: z.array(z.string()).default(["**/node_modules/**/*"]),
16
- globPatterns: z.array(z.string()).default(["**/*.{js,css,html}"]),
17
- globStrict: z.boolean().default(true),
18
- templatedURLs: z.record(z.string(), z.union([z.string(), z.array(z.string())])).optional(),
19
- })
20
- .strict("Do not pass invalid properties to GlobPartial!");
13
+ export const globPartial = z.strictObject({
14
+ globFollow: z.boolean().default(true),
15
+ globIgnores: z.array(z.string()).default(["**/node_modules/**/*"]),
16
+ globPatterns: z.array(z.string()).default(DEFAULT_GLOB_PATTERNS),
17
+ globStrict: z.boolean().default(true),
18
+ templatedURLs: z.record(z.string(), z.union([z.string(), z.array(z.string())])).optional(),
19
+ });
21
20
 
22
- export const optionalGlobDirectoryPartial = z
23
- .object({
24
- globDirectory: z.string().optional(),
25
- })
26
- .strict("Do not pass invalid properties to OptionalGlobDirectoryPartial!");
21
+ export const optionalGlobDirectoryPartial = z.strictObject({
22
+ globDirectory: z.string().optional(),
23
+ });
27
24
 
28
25
  // This needs to be set when using GetManifest or InjectManifest. This is
29
26
  // enforced via runtime validation, and needs to be documented.
30
- export const requiredGlobDirectoryPartial = z
31
- .object({
32
- globDirectory: z.string(),
33
- })
34
- .strict("Do not pass invalid properties to RequiredGlobDirectoryPartial!");
27
+ export const requiredGlobDirectoryPartial = z.strictObject({
28
+ globDirectory: z.string(),
29
+ });
35
30
 
36
31
  assertType<Equals<GlobPartial, z.input<typeof globPartial>>>();
37
32
  assertType<Equals<GlobResolved, z.output<typeof globPartial>>>();
@@ -0,0 +1,24 @@
1
+ import { z } from "zod";
2
+ import type { InjectManifestOptions, InjectManifestOptionsComplete, InjectPartial, InjectResolved } from "../types.js";
3
+ import { assertType, type Equals } from "./assert-type.js";
4
+ import { basePartial } from "./base.js";
5
+ import { globPartial, requiredGlobDirectoryPartial } from "./glob.js";
6
+ import { requiredSwDestPartial } from "./sw-dest.js";
7
+
8
+ export const baseInjectPartial = z.strictObject({
9
+ injectionPoint: z.string().prefault("self.__SW_MANIFEST"),
10
+ swSrc: z.string(),
11
+ });
12
+
13
+ export const injectManifestOptions = z.strictObject({
14
+ ...basePartial.shape,
15
+ ...globPartial.shape,
16
+ ...baseInjectPartial.shape,
17
+ ...requiredSwDestPartial.shape,
18
+ ...requiredGlobDirectoryPartial.shape,
19
+ });
20
+
21
+ assertType<Equals<InjectPartial, z.input<typeof baseInjectPartial>>>();
22
+ assertType<Equals<InjectResolved, z.output<typeof baseInjectPartial>>>();
23
+ assertType<Equals<InjectManifestOptions, z.input<typeof injectManifestOptions>>>();
24
+ assertType<Equals<InjectManifestOptionsComplete, z.output<typeof injectManifestOptions>>>();
@@ -0,0 +1,11 @@
1
+ import { z } from "zod";
2
+
3
+ export const manifestEntry = z.strictObject({
4
+ integrity: z.string().optional(),
5
+ revision: z.string().nullable().optional(),
6
+ url: z.string(),
7
+ });
8
+
9
+ export const sizeObject = z.object({ size: z.number() });
10
+
11
+ export const manifestEntryWithSize = z.object({ ...manifestEntry.shape, ...sizeObject.shape });
@@ -0,0 +1,14 @@
1
+ import { z } from "zod";
2
+ import type { ManifestEntryWithSize, ManifestTransformResult } from "../types.js";
3
+ import { manifestEntryWithSize } from "./manifest-entry.js";
4
+ import { asyncFn, reference } from "./utils.js";
5
+
6
+ export const manifestTransformResult = z.strictObject({
7
+ manifest: z.array(reference<ManifestEntryWithSize>(manifestEntryWithSize)),
8
+ warnings: z.array(z.string()).optional(),
9
+ });
10
+
11
+ export const manifestTransform = asyncFn({
12
+ input: [z.array(reference<ManifestEntryWithSize>(manifestEntryWithSize)), z.unknown().optional()],
13
+ output: reference<ManifestTransformResult>(manifestTransformResult),
14
+ });
@@ -1,18 +1,14 @@
1
1
  import { z } from "zod";
2
2
  import type { OptionalSwDestPartial, OptionalSwDestResolved, RequiredSwDestPartial, RequiredSwDestResolved } from "../types.js";
3
- import { type Equals, assertType } from "./assertType.js";
3
+ import { assertType, type Equals } from "./assert-type.js";
4
4
 
5
- export const optionalSwDestPartial = z
6
- .object({
7
- swDest: z.string().optional(),
8
- })
9
- .strict("Do not pass invalid properties to OptionalSwDest!");
5
+ export const optionalSwDestPartial = z.strictObject({
6
+ swDest: z.string().optional(),
7
+ });
10
8
 
11
- export const requiredSwDestPartial = z
12
- .object({
13
- swDest: z.string(),
14
- })
15
- .strict("Do not pass invalid properties to RequiredSwDest!");
9
+ export const requiredSwDestPartial = z.strictObject({
10
+ swDest: z.string(),
11
+ });
16
12
 
17
13
  assertType<Equals<OptionalSwDestPartial, z.input<typeof optionalSwDestPartial>>>();
18
14
  assertType<Equals<OptionalSwDestResolved, z.output<typeof optionalSwDestPartial>>>();
@@ -0,0 +1,48 @@
1
+ import type { MaybePromise } from "@serwist/utils";
2
+ import { z } from "zod";
3
+
4
+ /**
5
+ * Prevents TS from inlining a schema’s inferred type.
6
+ * @internal
7
+ */
8
+ export const reference = <Output, Input = Output>(schema: z.ZodType<Output, Input>) => {
9
+ return schema;
10
+ };
11
+
12
+ export const asyncFn = <TInput extends [z.ZodType, ...z.ZodType[]], TOutput extends z.ZodType>({
13
+ input,
14
+ output,
15
+ }: {
16
+ input: TInput;
17
+ output: TOutput;
18
+ }): z.ZodType<
19
+ (...args: z.input<z.ZodTuple<TInput, null>>) => MaybePromise<z.output<TOutput>>,
20
+ (...args: z.output<z.ZodTuple<TInput, null>>) => MaybePromise<z.input<TOutput>>
21
+ > => {
22
+ const schema = z.function({
23
+ input: z.tuple(input),
24
+ output,
25
+ });
26
+ return z
27
+ .custom<(...args: z.output<z.ZodTuple<TInput, null>>) => MaybePromise<z.input<TOutput>>>((arg) => typeof arg === "function")
28
+ .transform((arg) => schema.implementAsync(arg));
29
+ };
30
+
31
+ export const fn = <TInput extends [z.ZodType, ...z.ZodType[]], TOutput extends z.ZodType>({
32
+ input,
33
+ output,
34
+ }: {
35
+ input: TInput;
36
+ output: TOutput;
37
+ }): z.ZodType<
38
+ (...args: z.input<z.ZodTuple<TInput, null>>) => z.output<TOutput>,
39
+ (...args: z.output<z.ZodTuple<TInput, null>>) => z.input<TOutput>
40
+ > => {
41
+ const schema = z.function({
42
+ input: z.tuple(input),
43
+ output,
44
+ });
45
+ return z
46
+ .custom<(...args: z.output<z.ZodTuple<TInput, null>>) => z.input<TOutput>>((arg) => typeof arg === "function")
47
+ .transform((arg) => schema.implement(arg));
48
+ };
package/src/types.ts CHANGED
@@ -1,14 +1,15 @@
1
- import type { Require, Prettify } from "@serwist/utils";
2
- import type { PackageJson } from "type-fest";
1
+ import type { Prettify, Require } from "@serwist/utils";
3
2
  import type { z } from "zod";
4
- import type { manifestEntry } from "./schema/manifestEntry.js";
5
- import type { manifestTransform, manifestTransformResult } from "./schema/manifestTransform.js";
3
+ import type { manifestEntry, manifestEntryWithSize } from "./schema/manifest-entry.js";
4
+ import type { manifestTransform, manifestTransformResult } from "./schema/manifest-transform.js";
6
5
 
7
- export type ManifestEntry = z.input<typeof manifestEntry>;
6
+ export interface ManifestTransformResult extends z.infer<typeof manifestTransformResult> {}
8
7
 
9
- export type ManifestTransformResult = z.input<typeof manifestTransformResult>;
8
+ export interface ManifestEntry extends z.infer<typeof manifestEntry> {}
10
9
 
11
- export type ManifestTransform = z.input<typeof manifestTransform>;
10
+ export interface ManifestEntryWithSize extends z.infer<typeof manifestEntryWithSize> {}
11
+
12
+ export interface ManifestTransform extends z.infer<typeof manifestTransform> {}
12
13
 
13
14
  export interface BasePartial {
14
15
  /**
@@ -116,7 +117,7 @@ export interface GlobPartial {
116
117
  * [`node-glob`'s Glob Primer](https://github.com/isaacs/node-glob#glob-primer).
117
118
  * @default
118
119
  * ```
119
- * ["**\/*.{js,css,html}"]
120
+ * ["**\/*.{js,css,html,ico,apng,png,avif,jpg,jpeg,jfif,pjpeg,pjp,gif,svg,webp,json,webmanifest}"]
120
121
  * ```
121
122
  */
122
123
  globPatterns?: string[];
@@ -201,27 +202,3 @@ export interface GetManifestResult {
201
202
  export type BuildResult = Omit<GetManifestResult, "manifestEntries"> & {
202
203
  filePaths: string[];
203
204
  };
204
-
205
- /**
206
- * @private
207
- */
208
- export interface FileDetails {
209
- file: string;
210
- hash: string | null;
211
- size: number;
212
- }
213
-
214
- /**
215
- * @private
216
- */
217
- export type BuildType = "dev" | "prod";
218
-
219
- /**
220
- * @private
221
- */
222
- export type SerwistPackageJSON = Prettify<PackageJson>;
223
-
224
- /**
225
- * @private
226
- */
227
- export type MethodNames = "GetManifest" | "InjectManifest" | "WebpackInjectManifest" | "ViteInjectManifest";
@@ -1,6 +0,0 @@
1
- import { b as basePartial, g as globPartial, r as requiredGlobDirectoryPartial } from './glob.js';
2
- import 'zod';
3
-
4
- const getManifestOptions = basePartial.merge(globPartial).merge(requiredGlobDirectoryPartial).strict("Do not pass invalid properties to GetManifestOptions!");
5
-
6
- export { getManifestOptions };
@@ -1,23 +0,0 @@
1
- import { z } from 'zod';
2
- import { b as basePartial, g as globPartial, r as requiredGlobDirectoryPartial } from './glob.js';
3
-
4
- const optionalSwDestPartial = z.object({
5
- swDest: z.string().optional()
6
- }).strict("Do not pass invalid properties to OptionalSwDest!");
7
- const requiredSwDestPartial = z.object({
8
- swDest: z.string()
9
- }).strict("Do not pass invalid properties to RequiredSwDest!");
10
-
11
- const baseInjectPartial = z.object({
12
- injectionPoint: z.string().min(1, "`injectionPoint` must not be empty!").default("self.__SW_MANIFEST"),
13
- swSrc: z.string().min(1, "`swSrc` must not be empty!")
14
- }).strict("Do not pass invalid properties to InjectPartial!");
15
- const injectManifestOptions = basePartial.merge(globPartial).merge(baseInjectPartial).merge(requiredSwDestPartial).merge(requiredGlobDirectoryPartial).strict("Do not pass invalid properties to InjectManifestOptions!");
16
-
17
- var injectManifest = /*#__PURE__*/Object.freeze({
18
- __proto__: null,
19
- baseInjectPartial: baseInjectPartial,
20
- injectManifestOptions: injectManifestOptions
21
- });
22
-
23
- export { injectManifest as a, baseInjectPartial as b, injectManifestOptions as i, optionalSwDestPartial as o, requiredSwDestPartial as r };
@@ -1,54 +0,0 @@
1
- import { z } from 'zod';
2
-
3
- class SerwistConfigError extends Error {
4
- constructor({ moduleName, message }){
5
- super(`Received an invalid ${moduleName ?? "Serwist"} configuration: ${message}`);
6
- Object.setPrototypeOf(this, new.target.prototype);
7
- }
8
- }
9
-
10
- const validationErrorMap = (error, ctx)=>{
11
- switch(error.code){
12
- case z.ZodIssueCode.invalid_type:
13
- {
14
- return {
15
- message: `${error.message ?? "Received invalid type"}: expected ${error.expected}, received ${error.received}.`
16
- };
17
- }
18
- case z.ZodIssueCode.invalid_literal:
19
- {
20
- return {
21
- message: `${error.message ?? "Received invalid literal"}: expected ${error.expected}, received ${error.received}.`
22
- };
23
- }
24
- case z.ZodIssueCode.unrecognized_keys:
25
- {
26
- return {
27
- message: `${error.message ?? "Received unrecognized keys"}: ${error.keys.join(",")}`
28
- };
29
- }
30
- case z.ZodIssueCode.invalid_arguments:
31
- {
32
- return {
33
- message: `${error.message ?? "Received invalid arguments"}: ${error.argumentsError.errors.map((e)=>validationErrorMap(e, ctx)).join(",")}.`
34
- };
35
- }
36
- case z.ZodIssueCode.invalid_return_type:
37
- {
38
- return {
39
- message: `${error.message ?? "Received invalid return type"}: ${error.returnTypeError.errors.map((e)=>validationErrorMap(e, ctx)).join(",")}.`
40
- };
41
- }
42
- case z.ZodIssueCode.custom:
43
- {
44
- return {
45
- message: error.message ?? ctx.defaultError
46
- };
47
- }
48
- }
49
- return {
50
- message: ctx.defaultError
51
- };
52
- };
53
-
54
- export { SerwistConfigError as S, validationErrorMap as v };
@@ -1,47 +0,0 @@
1
- export declare const errors: {
2
- "unable-to-get-rootdir": string;
3
- "no-extension": string;
4
- "invalid-file-manifest-name": string;
5
- "unable-to-get-file-manifest-name": string;
6
- "invalid-sw-dest": string;
7
- "unable-to-get-sw-name": string;
8
- "unable-to-get-save-config": string;
9
- "unable-to-get-file-hash": string;
10
- "unable-to-get-file-size": string;
11
- "unable-to-glob-files": string;
12
- "unable-to-make-manifest-directory": string;
13
- "read-manifest-template-failure": string;
14
- "populating-manifest-tmpl-failed": string;
15
- "manifest-file-write-failure": string;
16
- "unable-to-make-sw-directory": string;
17
- "sw-write-failure": string;
18
- "sw-write-failure-directory": string;
19
- "unable-to-copy-serwist-libraries": string;
20
- "invalid-glob-directory": string;
21
- "invalid-dont-cache-bust": string;
22
- "invalid-exclude-files": string;
23
- "invalid-get-manifest-entries-input": string;
24
- "invalid-manifest-path": string;
25
- "invalid-manifest-entries": string;
26
- "invalid-manifest-format": string;
27
- "invalid-static-file-globs": string;
28
- "invalid-templated-urls": string;
29
- "templated-url-matches-glob": string;
30
- "invalid-glob-ignores": string;
31
- "manifest-entry-bad-url": string;
32
- "modify-url-prefix-bad-prefixes": string;
33
- "invalid-inject-manifest-arg": string;
34
- "injection-point-not-found": string;
35
- "multiple-injection-points": string;
36
- "useless-glob-pattern": string;
37
- "bad-template-urls-asset": string;
38
- "invalid-generate-file-manifest-arg": string;
39
- "invalid-sw-src": string;
40
- "same-src-and-dest": string;
41
- "no-module-name": string;
42
- "bad-manifest-transforms-return-value": string;
43
- "string-entry-warning": string;
44
- "cant-find-sourcemap": string;
45
- "manifest-transforms": string;
46
- };
47
- //# sourceMappingURL=errors.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../src/lib/errors.ts"],"names":[],"mappings":"AAUA,eAAO,MAAM,MAAM;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;CAkFlB,CAAC"}
@@ -1,3 +0,0 @@
1
- import type { FileDetails } from "../types.js";
2
- export declare const getCompositeDetails: (compositeURL: string, dependencyDetails: FileDetails[]) => FileDetails;
3
- //# sourceMappingURL=get-composite-details.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"get-composite-details.d.ts","sourceRoot":"","sources":["../../src/lib/get-composite-details.ts"],"names":[],"mappings":"AAUA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAE/C,eAAO,MAAM,mBAAmB,iBAAkB,MAAM,qBAAqB,WAAW,EAAE,KAAG,WAkB5F,CAAC"}
@@ -1,2 +0,0 @@
1
- export declare const getFileHash: (file: string) => string;
2
- //# sourceMappingURL=get-file-hash.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"get-file-hash.d.ts","sourceRoot":"","sources":["../../src/lib/get-file-hash.ts"],"names":[],"mappings":"AAaA,eAAO,MAAM,WAAW,SAAU,MAAM,KAAG,MAO1C,CAAC"}
@@ -1,2 +0,0 @@
1
- export declare const getFileSize: (file: string) => number | null;
2
- //# sourceMappingURL=get-file-size.d.ts.map