@serwist/build 10.0.0-preview.1 → 10.0.0-preview.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/chunks/constants.js +59 -0
- package/dist/chunks/get-manifest.js +11 -0
- package/dist/chunks/glob.js +45 -28
- package/dist/chunks/inject-manifest.js +29 -0
- package/dist/get-manifest.d.ts.map +1 -1
- package/dist/index.d.ts +3 -11
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +16 -151
- package/dist/index.schema.d.ts +9 -9
- package/dist/index.schema.d.ts.map +1 -1
- package/dist/index.schema.js +4 -4
- package/dist/inject-manifest.d.ts.map +1 -1
- package/dist/lib/additional-precache-entries-transform.d.ts.map +1 -1
- package/dist/lib/constants.d.ts +2 -0
- package/dist/lib/constants.d.ts.map +1 -0
- package/dist/lib/escape-regexp.d.ts.map +1 -1
- package/dist/lib/get-file-details.d.ts +2 -1
- package/dist/lib/get-file-details.d.ts.map +1 -1
- package/dist/lib/get-file-manifest-entries.d.ts.map +1 -1
- package/dist/lib/modify-url-prefix-transform.d.ts.map +1 -1
- package/dist/lib/no-revision-for-urls-matching-transform.d.ts.map +1 -1
- package/dist/lib/transform-manifest.d.ts +2 -1
- package/dist/lib/transform-manifest.d.ts.map +1 -1
- package/dist/lib/validate-options.d.ts.map +1 -1
- package/dist/schema/{assertType.d.ts → assert-type.d.ts} +1 -1
- package/dist/schema/assert-type.d.ts.map +1 -0
- package/dist/schema/base.d.ts +45 -151
- package/dist/schema/base.d.ts.map +1 -1
- package/dist/schema/{serwistConfigError.d.ts → error.d.ts} +3 -1
- package/dist/schema/error.d.ts.map +1 -0
- package/dist/schema/get-manifest.d.ts +72 -0
- package/dist/schema/get-manifest.d.ts.map +1 -0
- package/dist/schema/glob.d.ts +6 -26
- package/dist/schema/glob.d.ts.map +1 -1
- package/dist/schema/inject-manifest.d.ts +79 -0
- package/dist/schema/inject-manifest.d.ts.map +1 -0
- package/dist/schema/manifest-entry.d.ts +7 -0
- package/dist/schema/manifest-entry.d.ts.map +1 -0
- package/dist/schema/manifest-transform.d.ts +64 -0
- package/dist/schema/manifest-transform.d.ts.map +1 -0
- package/dist/schema/sw-dest.d.ts +8 -0
- package/dist/schema/sw-dest.d.ts.map +1 -0
- package/dist/schema/utils.d.ts +10 -0
- package/dist/schema/utils.d.ts.map +1 -0
- package/dist/types.d.ts +12 -13
- package/dist/types.d.ts.map +1 -1
- package/package.json +14 -12
- package/src/index.schema.ts +10 -8
- package/src/index.ts +5 -11
- package/src/inject-manifest.ts +2 -1
- package/src/lib/additional-precache-entries-transform.ts +1 -2
- package/src/lib/constants.ts +1 -0
- package/src/lib/get-file-details.ts +3 -4
- package/src/lib/get-file-manifest-entries.ts +3 -4
- package/src/lib/modify-url-prefix-transform.ts +1 -1
- package/src/lib/no-revision-for-urls-matching-transform.ts +1 -1
- package/src/lib/rebase-path.ts +1 -1
- package/src/lib/transform-manifest.ts +3 -3
- package/src/lib/translate-url-to-sourcemap-paths.ts +4 -4
- package/src/lib/validate-options.ts +12 -6
- package/src/schema/base.ts +11 -13
- package/src/schema/error.ts +53 -0
- package/src/schema/{getManifest.ts → get-manifest.ts} +7 -6
- package/src/schema/glob.ts +15 -20
- package/src/schema/inject-manifest.ts +24 -0
- package/src/schema/manifest-entry.ts +7 -0
- package/src/schema/manifest-transform.ts +17 -0
- package/src/schema/{swDest.ts → sw-dest.ts} +7 -11
- package/src/schema/utils.ts +27 -0
- package/src/types.ts +11 -14
- package/dist/chunks/getManifest.js +0 -6
- package/dist/chunks/injectManifest.js +0 -23
- package/dist/chunks/validationErrorMap.js +0 -54
- package/dist/lib/errors.d.ts +0 -47
- package/dist/lib/errors.d.ts.map +0 -1
- package/dist/lib/get-composite-details.d.ts +0 -3
- package/dist/lib/get-composite-details.d.ts.map +0 -1
- package/dist/lib/get-file-hash.d.ts +0 -2
- package/dist/lib/get-file-hash.d.ts.map +0 -1
- package/dist/lib/get-file-size.d.ts +0 -2
- package/dist/lib/get-file-size.d.ts.map +0 -1
- package/dist/lib/get-string-details.d.ts +0 -3
- package/dist/lib/get-string-details.d.ts.map +0 -1
- package/dist/lib/get-string-hash.d.ts +0 -3
- package/dist/lib/get-string-hash.d.ts.map +0 -1
- package/dist/schema/assertType.d.ts.map +0 -1
- package/dist/schema/getManifest.d.ts +0 -192
- package/dist/schema/getManifest.d.ts.map +0 -1
- package/dist/schema/injectManifest.d.ts +0 -213
- package/dist/schema/injectManifest.d.ts.map +0 -1
- package/dist/schema/manifestEntry.d.ts +0 -15
- package/dist/schema/manifestEntry.d.ts.map +0 -1
- package/dist/schema/manifestTransform.d.ts +0 -125
- package/dist/schema/manifestTransform.d.ts.map +0 -1
- package/dist/schema/serwistConfigError.d.ts.map +0 -1
- package/dist/schema/swDest.d.ts +0 -16
- package/dist/schema/swDest.d.ts.map +0 -1
- package/dist/schema/validationErrorMap.d.ts +0 -3
- package/dist/schema/validationErrorMap.d.ts.map +0 -1
- package/src/lib/errors.ts +0 -93
- package/src/lib/get-composite-details.ts +0 -31
- package/src/lib/get-file-hash.ts +0 -21
- package/src/lib/get-file-size.ts +0 -22
- package/src/lib/get-string-details.ts +0 -16
- package/src/lib/get-string-hash.ts +0 -15
- package/src/schema/injectManifest.ts +0 -25
- package/src/schema/manifestEntry.ts +0 -9
- package/src/schema/manifestTransform.ts +0 -15
- package/src/schema/serwistConfigError.ts +0 -6
- package/src/schema/validationErrorMap.ts +0 -36
- /package/src/schema/{assertType.ts → assert-type.ts} +0 -0
package/package.json
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@serwist/build",
|
|
3
|
-
"version": "10.0.0-preview.
|
|
3
|
+
"version": "10.0.0-preview.10",
|
|
4
4
|
"type": "module",
|
|
5
|
+
"sideEffects": false,
|
|
5
6
|
"description": "A module that integrates into your build process, helping you generate a manifest of local files that should be precached.",
|
|
6
7
|
"files": [
|
|
7
8
|
"src",
|
|
@@ -17,7 +18,7 @@
|
|
|
17
18
|
"file manifest"
|
|
18
19
|
],
|
|
19
20
|
"engines": {
|
|
20
|
-
"node": ">=
|
|
21
|
+
"node": ">=20.0.0"
|
|
21
22
|
},
|
|
22
23
|
"author": "Google's Web DevRel Team",
|
|
23
24
|
"contributors": [
|
|
@@ -48,20 +49,20 @@
|
|
|
48
49
|
"./package.json": "./package.json"
|
|
49
50
|
},
|
|
50
51
|
"dependencies": {
|
|
51
|
-
"
|
|
52
|
-
"
|
|
53
|
-
"pretty-bytes": "6.1.1",
|
|
52
|
+
"glob": "11.0.3",
|
|
53
|
+
"pretty-bytes": "7.0.0",
|
|
54
54
|
"source-map": "0.8.0-beta.0",
|
|
55
|
-
"zod": "
|
|
56
|
-
"@serwist/utils": "10.0.0-preview.
|
|
55
|
+
"zod": "4.0.5",
|
|
56
|
+
"@serwist/utils": "10.0.0-preview.10"
|
|
57
57
|
},
|
|
58
58
|
"devDependencies": {
|
|
59
59
|
"@types/common-tags": "1.8.4",
|
|
60
|
-
"@types/node": "
|
|
61
|
-
"rollup": "4.
|
|
62
|
-
"type-fest": "4.
|
|
63
|
-
"typescript": "5.
|
|
64
|
-
"@serwist/
|
|
60
|
+
"@types/node": "24.0.14",
|
|
61
|
+
"rollup": "4.45.1",
|
|
62
|
+
"type-fest": "4.41.0",
|
|
63
|
+
"typescript": "5.8.3",
|
|
64
|
+
"@serwist/utils": "10.0.0-preview.10",
|
|
65
|
+
"@serwist/configs": "10.0.0-preview.10"
|
|
65
66
|
},
|
|
66
67
|
"peerDependencies": {
|
|
67
68
|
"typescript": ">=5.0.0"
|
|
@@ -75,6 +76,7 @@
|
|
|
75
76
|
"build": "rimraf dist && NODE_OPTIONS='--max-old-space-size=4096' NODE_ENV=production rollup --config rollup.config.js",
|
|
76
77
|
"dev": "rollup --config rollup.config.js --watch",
|
|
77
78
|
"lint": "biome lint ./src",
|
|
79
|
+
"qcheck": "biome check ./src",
|
|
78
80
|
"typecheck": "tsc"
|
|
79
81
|
}
|
|
80
82
|
}
|
package/src/index.schema.ts
CHANGED
|
@@ -1,16 +1,18 @@
|
|
|
1
|
-
import { type Assignable, type Equals
|
|
1
|
+
import { type Assignable, assertType, type Equals } from "./schema/assert-type.js";
|
|
2
2
|
import { basePartial } from "./schema/base.js";
|
|
3
|
-
import {
|
|
3
|
+
import { SerwistConfigError, validationErrorMap } from "./schema/error.js";
|
|
4
|
+
import { getManifestOptions } from "./schema/get-manifest.js";
|
|
4
5
|
import { globPartial, optionalGlobDirectoryPartial, requiredGlobDirectoryPartial } from "./schema/glob.js";
|
|
5
|
-
import { baseInjectPartial, injectManifestOptions } from "./schema/
|
|
6
|
-
import { manifestEntry } from "./schema/
|
|
7
|
-
import { manifestTransform, manifestTransformResult } from "./schema/
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
import { validationErrorMap } from "./schema/validationErrorMap.js";
|
|
6
|
+
import { baseInjectPartial, injectManifestOptions } from "./schema/inject-manifest.js";
|
|
7
|
+
import { manifestEntry } from "./schema/manifest-entry.js";
|
|
8
|
+
import { manifestTransform, manifestTransformResult } from "./schema/manifest-transform.js";
|
|
9
|
+
import { optionalSwDestPartial, requiredSwDestPartial } from "./schema/sw-dest.js";
|
|
10
|
+
import { asyncFn, fn } from "./schema/utils.js";
|
|
11
11
|
|
|
12
12
|
export {
|
|
13
13
|
assertType,
|
|
14
|
+
fn,
|
|
15
|
+
asyncFn,
|
|
14
16
|
basePartial,
|
|
15
17
|
globPartial,
|
|
16
18
|
baseInjectPartial as injectPartial,
|
package/src/index.ts
CHANGED
|
@@ -4,10 +4,12 @@
|
|
|
4
4
|
Use of this source code is governed by an MIT-style
|
|
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";
|
|
9
|
+
|
|
8
10
|
import { getManifest } from "./get-manifest.js";
|
|
9
11
|
import { injectManifest } from "./inject-manifest.js";
|
|
10
|
-
import {
|
|
12
|
+
import { DEFAULT_GLOB_PATTERNS } from "./lib/constants.js";
|
|
11
13
|
import { escapeRegExp } from "./lib/escape-regexp.js";
|
|
12
14
|
import { getFileManifestEntries } from "./lib/get-file-manifest-entries.js";
|
|
13
15
|
import { getSourceMapURL } from "./lib/get-source-map-url.js";
|
|
@@ -17,16 +19,9 @@ import { transformManifest } from "./lib/transform-manifest.js";
|
|
|
17
19
|
import { translateURLToSourcemapPaths } from "./lib/translate-url-to-sourcemap-paths.js";
|
|
18
20
|
import { validateGetManifestOptions, validateInjectManifestOptions } from "./lib/validate-options.js";
|
|
19
21
|
|
|
20
|
-
// TODO: remove in v10.
|
|
21
|
-
/**
|
|
22
|
-
* Use `JSON.stringify` instead.
|
|
23
|
-
*
|
|
24
|
-
* @deprecated
|
|
25
|
-
*/
|
|
26
|
-
const stringify = JSON.stringify;
|
|
27
|
-
|
|
28
22
|
export {
|
|
29
23
|
errors,
|
|
24
|
+
DEFAULT_GLOB_PATTERNS,
|
|
30
25
|
escapeRegExp,
|
|
31
26
|
getFileManifestEntries,
|
|
32
27
|
getManifest,
|
|
@@ -34,7 +29,6 @@ export {
|
|
|
34
29
|
injectManifest,
|
|
35
30
|
rebasePath,
|
|
36
31
|
replaceAndUpdateSourceMap,
|
|
37
|
-
stringify,
|
|
38
32
|
transformManifest,
|
|
39
33
|
translateURLToSourcemapPaths,
|
|
40
34
|
validateGetManifestOptions,
|
package/src/inject-manifest.ts
CHANGED
|
@@ -9,8 +9,9 @@ import assert from "node:assert";
|
|
|
9
9
|
import fsp from "node:fs/promises";
|
|
10
10
|
import path from "node:path";
|
|
11
11
|
import { toUnix } from "@serwist/utils";
|
|
12
|
+
import { errors } from "@serwist/utils/node";
|
|
12
13
|
import type { RawSourceMap } from "source-map";
|
|
13
|
-
|
|
14
|
+
|
|
14
15
|
import { escapeRegExp } from "./lib/escape-regexp.js";
|
|
15
16
|
import { getFileManifestEntries } from "./lib/get-file-manifest-entries.js";
|
|
16
17
|
import { getSourceMapURL } from "./lib/get-source-map-url.js";
|
|
@@ -5,9 +5,8 @@
|
|
|
5
5
|
license that can be found in the LICENSE file or at
|
|
6
6
|
https://opensource.org/licenses/MIT.
|
|
7
7
|
*/
|
|
8
|
-
|
|
8
|
+
import { errors } from "@serwist/utils/node";
|
|
9
9
|
import type { ManifestEntry } from "../types.js";
|
|
10
|
-
import { errors } from "./errors.js";
|
|
11
10
|
|
|
12
11
|
type AdditionalManifestEntriesTransform = (manifest: (ManifestEntry & { size: number })[]) => {
|
|
13
12
|
manifest: (ManifestEntry & { size: number })[];
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const DEFAULT_GLOB_PATTERNS = ["**/*.{js,css,html,ico,apng,png,avif,jpg,jpeg,jfif,pjpeg,pjp,gif,svg,webp,json,webmanifest}"];
|
|
@@ -6,11 +6,10 @@
|
|
|
6
6
|
https://opensource.org/licenses/MIT.
|
|
7
7
|
*/
|
|
8
8
|
import path from "node:path";
|
|
9
|
+
import type { FileDetails } from "@serwist/utils";
|
|
10
|
+
import { errors, getFileHash, getFileSize } from "@serwist/utils/node";
|
|
9
11
|
import { globSync } from "glob";
|
|
10
|
-
import type {
|
|
11
|
-
import { errors } from "./errors.js";
|
|
12
|
-
import { getFileHash } from "./get-file-hash.js";
|
|
13
|
-
import { getFileSize } from "./get-file-size.js";
|
|
12
|
+
import type { GlobPartial } from "../types.js";
|
|
14
13
|
|
|
15
14
|
export const getFileDetails = ({
|
|
16
15
|
globDirectory,
|
|
@@ -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 {
|
|
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)) {
|
package/src/lib/rebase-path.ts
CHANGED
|
@@ -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,
|
|
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 "
|
|
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
|
|
23
|
-
let srcPath: string | undefined
|
|
24
|
-
let warning: string | 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 {
|
|
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/
|
|
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/
|
|
13
|
+
const result = await (await import("../schema/get-manifest.js")).getManifestOptions.spa(input, { error: validationErrorMap });
|
|
14
14
|
if (!result.success) {
|
|
15
|
-
throw new SerwistConfigError({
|
|
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/
|
|
24
|
+
const result = await (await import("../schema/inject-manifest.js")).injectManifestOptions.spa(input, { error: validationErrorMap });
|
|
22
25
|
if (!result.success) {
|
|
23
|
-
throw new SerwistConfigError({
|
|
26
|
+
throw new SerwistConfigError({
|
|
27
|
+
moduleName: "@serwist/build",
|
|
28
|
+
message: z.prettifyError(result.error),
|
|
29
|
+
});
|
|
24
30
|
}
|
|
25
31
|
return result.data;
|
|
26
32
|
};
|
package/src/schema/base.ts
CHANGED
|
@@ -1,20 +1,18 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
2
|
|
|
3
3
|
import type { BasePartial, BaseResolved } from "../types.js";
|
|
4
|
-
import { type Equals
|
|
5
|
-
import { manifestEntry } from "./
|
|
6
|
-
import { manifestTransform } from "./
|
|
4
|
+
import { assertType, type Equals } from "./assert-type.js";
|
|
5
|
+
import { manifestEntry } from "./manifest-entry.js";
|
|
6
|
+
import { manifestTransform } from "./manifest-transform.js";
|
|
7
7
|
|
|
8
|
-
export const basePartial = z
|
|
9
|
-
.
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
})
|
|
17
|
-
.strict("Do not pass invalid properties to BasePartial!");
|
|
8
|
+
export const basePartial = z.strictObject({
|
|
9
|
+
additionalPrecacheEntries: z.array(z.union([z.string(), manifestEntry])).optional(),
|
|
10
|
+
disablePrecacheManifest: z.boolean().default(false),
|
|
11
|
+
dontCacheBustURLsMatching: z.instanceof(RegExp).optional(),
|
|
12
|
+
manifestTransforms: z.array(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
|
|
1
|
+
import { z } from "zod";
|
|
2
2
|
import type { GetManifestOptions, GetManifestOptionsComplete } from "../types.js";
|
|
3
|
-
import { type Equals
|
|
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 =
|
|
8
|
-
.
|
|
9
|
-
.
|
|
10
|
-
.
|
|
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>>>();
|
package/src/schema/glob.ts
CHANGED
|
@@ -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
|
|
11
|
+
import { assertType, type Equals } from "./assert-type.js";
|
|
11
12
|
|
|
12
|
-
export const globPartial = z
|
|
13
|
-
.
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
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
|
-
.
|
|
24
|
-
|
|
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
|
-
.
|
|
32
|
-
|
|
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,17 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { manifestEntry } from "./manifest-entry.js";
|
|
3
|
+
import { asyncFn } from "./utils.js";
|
|
4
|
+
|
|
5
|
+
const sizeObject = z.object({ size: z.number() });
|
|
6
|
+
|
|
7
|
+
const manifestEntryWithSize = z.object({ ...manifestEntry.shape, ...sizeObject.shape });
|
|
8
|
+
|
|
9
|
+
export const manifestTransformResult = z.strictObject({
|
|
10
|
+
manifest: z.array(manifestEntryWithSize),
|
|
11
|
+
warnings: z.array(z.string()).optional(),
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
export const manifestTransform = asyncFn({
|
|
15
|
+
input: [z.array(manifestEntryWithSize), z.unknown().optional()],
|
|
16
|
+
output: manifestTransformResult,
|
|
17
|
+
});
|
|
@@ -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
|
|
3
|
+
import { assertType, type Equals } from "./assert-type.js";
|
|
4
4
|
|
|
5
|
-
export const optionalSwDestPartial = z
|
|
6
|
-
.
|
|
7
|
-
|
|
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
|
-
.
|
|
13
|
-
|
|
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,27 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
|
|
3
|
+
export const fn = <TInput extends [z.ZodType, ...z.ZodType[]], TOutput extends z.ZodType>({ input, output }: { input: TInput; output: TOutput }) => {
|
|
4
|
+
const schema = z.function({
|
|
5
|
+
input: z.tuple(input),
|
|
6
|
+
output,
|
|
7
|
+
});
|
|
8
|
+
return z
|
|
9
|
+
.custom<z.core.$InferInnerFunctionType<z.ZodTuple<TInput, null>, TOutput>>((arg) => typeof arg === "function")
|
|
10
|
+
.transform((arg) => schema.implement(arg));
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export const asyncFn = <TInput extends [z.ZodType, ...z.ZodType[]], TOutput extends z.ZodType>({
|
|
14
|
+
input,
|
|
15
|
+
output,
|
|
16
|
+
}: {
|
|
17
|
+
input: TInput;
|
|
18
|
+
output: TOutput;
|
|
19
|
+
}) => {
|
|
20
|
+
const schema = z.function({
|
|
21
|
+
input: z.tuple(input),
|
|
22
|
+
output,
|
|
23
|
+
});
|
|
24
|
+
return z
|
|
25
|
+
.custom<z.core.$InferInnerFunctionTypeAsync<z.ZodTuple<TInput, null>, TOutput>>((arg) => typeof arg === "function")
|
|
26
|
+
.transform((arg) => schema.implementAsync(arg));
|
|
27
|
+
};
|
package/src/types.ts
CHANGED
|
@@ -1,14 +1,20 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { MaybePromise, Prettify, Require } from "@serwist/utils";
|
|
2
2
|
import type { PackageJson } from "type-fest";
|
|
3
3
|
import type { z } from "zod";
|
|
4
|
-
import type { manifestEntry } from "./schema/
|
|
5
|
-
import type {
|
|
4
|
+
import type { manifestEntry } from "./schema/manifest-entry.js";
|
|
5
|
+
import type { manifestTransformResult } from "./schema/manifest-transform.js";
|
|
6
6
|
|
|
7
7
|
export type ManifestEntry = z.input<typeof manifestEntry>;
|
|
8
8
|
|
|
9
9
|
export type ManifestTransformResult = z.input<typeof manifestTransformResult>;
|
|
10
10
|
|
|
11
|
-
export type ManifestTransform =
|
|
11
|
+
export type ManifestTransform = (
|
|
12
|
+
entries: (ManifestEntry & { size: number })[],
|
|
13
|
+
params?: unknown,
|
|
14
|
+
) => MaybePromise<{
|
|
15
|
+
manifest: (ManifestEntry & { size: number })[];
|
|
16
|
+
warnings?: string[] | undefined;
|
|
17
|
+
}>;
|
|
12
18
|
|
|
13
19
|
export interface BasePartial {
|
|
14
20
|
/**
|
|
@@ -116,7 +122,7 @@ export interface GlobPartial {
|
|
|
116
122
|
* [`node-glob`'s Glob Primer](https://github.com/isaacs/node-glob#glob-primer).
|
|
117
123
|
* @default
|
|
118
124
|
* ```
|
|
119
|
-
* ["**\/*.{js,css,html}"]
|
|
125
|
+
* ["**\/*.{js,css,html,ico,apng,png,avif,jpg,jpeg,jfif,pjpeg,pjp,gif,svg,webp,json,webmanifest}"]
|
|
120
126
|
* ```
|
|
121
127
|
*/
|
|
122
128
|
globPatterns?: string[];
|
|
@@ -202,15 +208,6 @@ export type BuildResult = Omit<GetManifestResult, "manifestEntries"> & {
|
|
|
202
208
|
filePaths: string[];
|
|
203
209
|
};
|
|
204
210
|
|
|
205
|
-
/**
|
|
206
|
-
* @private
|
|
207
|
-
*/
|
|
208
|
-
export interface FileDetails {
|
|
209
|
-
file: string;
|
|
210
|
-
hash: string | null;
|
|
211
|
-
size: number;
|
|
212
|
-
}
|
|
213
|
-
|
|
214
211
|
/**
|
|
215
212
|
* @private
|
|
216
213
|
*/
|
|
@@ -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 };
|