@serwist/build 8.4.3 → 9.0.0-preview.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 (125) hide show
  1. package/dist/chunks/getManifest.js +6 -0
  2. package/dist/chunks/glob.js +60 -0
  3. package/dist/chunks/injectManifest.js +23 -0
  4. package/dist/chunks/serwist-config-error.js +57 -0
  5. package/dist/chunks/vite.js +7 -0
  6. package/dist/chunks/webpack.js +34 -0
  7. package/dist/get-manifest.d.ts +2 -1
  8. package/dist/get-manifest.d.ts.map +1 -0
  9. package/dist/index.d.ts +2 -1
  10. package/dist/index.d.ts.map +1 -0
  11. package/dist/index.js +53 -813
  12. package/dist/index.next.d.ts +3 -0
  13. package/dist/index.next.d.ts.map +1 -0
  14. package/dist/index.next.js +35 -0
  15. package/dist/inject-manifest.d.ts +2 -1
  16. package/dist/inject-manifest.d.ts.map +1 -0
  17. package/dist/lib/additional-precache-entries-transform.d.ts +6 -5
  18. package/dist/lib/additional-precache-entries-transform.d.ts.map +1 -0
  19. package/dist/lib/errors.d.ts +1 -2
  20. package/dist/lib/errors.d.ts.map +1 -0
  21. package/dist/lib/escape-regexp.d.ts +2 -1
  22. package/dist/lib/escape-regexp.d.ts.map +1 -0
  23. package/dist/lib/get-composite-details.d.ts +2 -1
  24. package/dist/lib/get-composite-details.d.ts.map +1 -0
  25. package/dist/lib/get-file-details.d.ts +2 -1
  26. package/dist/lib/get-file-details.d.ts.map +1 -0
  27. package/dist/lib/get-file-hash.d.ts +1 -0
  28. package/dist/lib/get-file-hash.d.ts.map +1 -0
  29. package/dist/lib/get-file-manifest-entries.d.ts +1 -0
  30. package/dist/lib/get-file-manifest-entries.d.ts.map +1 -0
  31. package/dist/lib/get-file-size.d.ts +1 -0
  32. package/dist/lib/get-file-size.d.ts.map +1 -0
  33. package/dist/lib/get-source-map-url.d.ts +1 -0
  34. package/dist/lib/get-source-map-url.d.ts.map +1 -0
  35. package/dist/lib/get-string-details.d.ts +1 -0
  36. package/dist/lib/get-string-details.d.ts.map +1 -0
  37. package/dist/lib/get-string-hash.d.ts +1 -0
  38. package/dist/lib/get-string-hash.d.ts.map +1 -0
  39. package/dist/lib/maximum-size-transform.d.ts +1 -0
  40. package/dist/lib/maximum-size-transform.d.ts.map +1 -0
  41. package/dist/lib/modify-url-prefix-transform.d.ts +1 -0
  42. package/dist/lib/modify-url-prefix-transform.d.ts.map +1 -0
  43. package/dist/lib/no-revision-for-urls-matching-transform.d.ts +1 -0
  44. package/dist/lib/no-revision-for-urls-matching-transform.d.ts.map +1 -0
  45. package/dist/lib/rebase-path.d.ts +1 -0
  46. package/dist/lib/rebase-path.d.ts.map +1 -0
  47. package/dist/lib/replace-and-update-source-map.d.ts +1 -0
  48. package/dist/lib/replace-and-update-source-map.d.ts.map +1 -0
  49. package/dist/lib/serwist-config-error.d.ts +7 -0
  50. package/dist/lib/serwist-config-error.d.ts.map +1 -0
  51. package/dist/lib/transform-manifest.d.ts +2 -1
  52. package/dist/lib/transform-manifest.d.ts.map +1 -0
  53. package/dist/lib/translate-url-to-sourcemap-paths.d.ts +1 -0
  54. package/dist/lib/translate-url-to-sourcemap-paths.d.ts.map +1 -0
  55. package/dist/lib/validate-next-options.d.ts +3 -0
  56. package/dist/lib/validate-next-options.d.ts.map +1 -0
  57. package/dist/lib/validate-options.d.ts +6 -8
  58. package/dist/lib/validate-options.d.ts.map +1 -0
  59. package/dist/schema/base.d.ts +169 -0
  60. package/dist/schema/base.d.ts.map +1 -0
  61. package/dist/schema/getManifest.d.ts +187 -0
  62. package/dist/schema/getManifest.d.ts.map +1 -0
  63. package/dist/schema/glob.d.ts +35 -0
  64. package/dist/schema/glob.d.ts.map +1 -0
  65. package/dist/schema/injectManifest.d.ts +206 -0
  66. package/dist/schema/injectManifest.d.ts.map +1 -0
  67. package/dist/schema/manifestEntry.d.ts +15 -0
  68. package/dist/schema/manifestEntry.d.ts.map +1 -0
  69. package/dist/schema/manifestTransform.d.ts +121 -0
  70. package/dist/schema/manifestTransform.d.ts.map +1 -0
  71. package/dist/schema/next.d.ts +243 -0
  72. package/dist/schema/next.d.ts.map +1 -0
  73. package/dist/schema/swDest.d.ts +16 -0
  74. package/dist/schema/swDest.d.ts.map +1 -0
  75. package/dist/schema/validationErrorMap.d.ts +3 -0
  76. package/dist/schema/validationErrorMap.d.ts.map +1 -0
  77. package/dist/schema/vite.d.ts +196 -0
  78. package/dist/schema/vite.d.ts.map +1 -0
  79. package/dist/schema/webpack.d.ts +231 -0
  80. package/dist/schema/webpack.d.ts.map +1 -0
  81. package/dist/types.d.ts +147 -175
  82. package/dist/types.d.ts.map +1 -0
  83. package/package.json +42 -28
  84. package/src/_types.js +112 -0
  85. package/src/get-manifest.ts +33 -0
  86. package/src/index.next.ts +3 -0
  87. package/{dist/index.d.cts → src/index.ts} +27 -2
  88. package/src/inject-manifest.ts +140 -0
  89. package/src/lib/additional-precache-entries-transform.ts +58 -0
  90. package/src/lib/errors.ts +99 -0
  91. package/src/lib/escape-regexp.ts +12 -0
  92. package/src/lib/get-composite-details.ts +31 -0
  93. package/src/lib/get-file-details.ts +68 -0
  94. package/src/lib/get-file-hash.ts +21 -0
  95. package/src/lib/get-file-manifest-entries.ts +126 -0
  96. package/src/lib/get-file-size.ts +23 -0
  97. package/src/lib/get-source-map-url.ts +17 -0
  98. package/src/lib/get-string-details.ts +18 -0
  99. package/src/lib/get-string-hash.ts +15 -0
  100. package/src/lib/maximum-size-transform.ts +29 -0
  101. package/src/lib/modify-url-prefix-transform.ts +55 -0
  102. package/src/lib/no-revision-for-urls-matching-transform.ts +32 -0
  103. package/src/lib/rebase-path.ts +22 -0
  104. package/src/lib/replace-and-update-source-map.ts +122 -0
  105. package/src/lib/serwist-config-error.ts +6 -0
  106. package/src/lib/transform-manifest.ts +158 -0
  107. package/src/lib/translate-url-to-sourcemap-paths.ts +38 -0
  108. package/src/lib/validate-next-options.ts +14 -0
  109. package/src/lib/validate-options.ts +47 -0
  110. package/src/schema/base.ts +16 -0
  111. package/src/schema/getManifest.ts +14 -0
  112. package/src/schema/glob.ts +41 -0
  113. package/src/schema/injectManifest.ts +23 -0
  114. package/src/schema/manifestEntry.ts +9 -0
  115. package/src/schema/manifestTransform.ts +15 -0
  116. package/src/schema/next.ts +35 -0
  117. package/src/schema/swDest.ts +14 -0
  118. package/src/schema/validationErrorMap.ts +36 -0
  119. package/src/schema/vite.ts +18 -0
  120. package/src/schema/webpack.ts +47 -0
  121. package/src/types.ts +407 -0
  122. package/dist/index.cjs +0 -1632
  123. package/dist/lib/cdn-utils.d.ts +0 -1
  124. package/dist/lib/copy-serwist-libraries.d.ts +0 -16
  125. package/dist/schema/index.d.ts +0 -605
package/src/types.ts ADDED
@@ -0,0 +1,407 @@
1
+ import type { Require } from "@serwist/utils";
2
+ import type { Pattern as GlobPattern } from "fast-glob";
3
+ import type { PackageJson } from "type-fest";
4
+ import type { z } from "zod";
5
+ import type { manifestEntry } from "./schema/manifestEntry.js";
6
+ import type { manifestTransform, manifestTransformResult } from "./schema/manifestTransform.js";
7
+
8
+ export type ManifestEntry = z.input<typeof manifestEntry>;
9
+
10
+ export type ManifestTransformResult = z.input<typeof manifestTransformResult>;
11
+
12
+ export type ManifestTransform = z.input<typeof manifestTransform>;
13
+
14
+ export interface BasePartial {
15
+ /**
16
+ * A list of entries to be precached, in addition to any entries that are
17
+ * generated as part of the build configuration.
18
+ */
19
+ additionalPrecacheEntries?: (string | ManifestEntry)[];
20
+ /**
21
+ * Whether the precache manifest should be set to `undefined`. Essentially whether `@serwist/build` should
22
+ * be disabled. Mostly useful when you want it to only check if the provided options are valid.
23
+ * @default false
24
+ */
25
+ disablePrecacheManifest?: boolean;
26
+ /**
27
+ * Assets that match this will be assumed to be uniquely versioned via their
28
+ * URL, and exempted from the normal HTTP cache-busting that's done when
29
+ * populating the precache. While not required, it's recommended that if your
30
+ * existing build process already inserts a `[hash]` value into each filename,
31
+ * you provide a RegExp that will detect that, as it will reduce the bandwidth
32
+ * consumed when precaching.
33
+ */
34
+ dontCacheBustURLsMatching?: RegExp;
35
+ /**
36
+ * One or more functions which will be applied sequentially against the
37
+ * generated manifest. If `modifyURLPrefix` or `dontCacheBustURLsMatching` are
38
+ * also specified, their corresponding transformations will be applied first.
39
+ */
40
+ manifestTransforms?: ManifestTransform[];
41
+ /**
42
+ * This value can be used to determine the maximum size of files that will be
43
+ * precached. This prevents you from inadvertently precaching very large files
44
+ * that might have accidentally matched one of your patterns.
45
+ * @default 2097152
46
+ */
47
+ maximumFileSizeToCacheInBytes?: number;
48
+ /**
49
+ * An object mapping string prefixes to replacement string values. This can be
50
+ * used to, e.g., remove or add a path prefix from a manifest entry if your
51
+ * web hosting setup doesn't match your local filesystem setup. As an
52
+ * alternative with more flexibility, you can use the `manifestTransforms`
53
+ * option and provide a function that modifies the entries in the manifest
54
+ * using whatever logic you provide.
55
+ *
56
+ * Example usage:
57
+ *
58
+ * ```
59
+ * // Replace a '/dist/' prefix with '/', and also prepend
60
+ * // '/static' to every URL.
61
+ * modifyURLPrefix: {
62
+ * '/dist/': '/',
63
+ * '': '/static',
64
+ * }
65
+ * ```
66
+ */
67
+ modifyURLPrefix?: {
68
+ [key: string]: string;
69
+ };
70
+ }
71
+
72
+ export type BaseResolved = Require<BasePartial, "disablePrecacheManifest" | "maximumFileSizeToCacheInBytes">;
73
+
74
+ // This needs to be set when using GetManifest or InjectManifest. This is
75
+ // enforced via runtime validation, and needs to be documented.
76
+ export interface RequiredGlobDirectoryPartial {
77
+ /**
78
+ * The local directory you wish to match `globPatterns` against. The path is
79
+ * relative to the current directory.
80
+ */
81
+ globDirectory: string;
82
+ }
83
+
84
+ export type RequiredGlobDirectoryResolved = RequiredGlobDirectoryPartial;
85
+
86
+ export interface OptionalGlobDirectoryPartial {
87
+ /**
88
+ * The local directory you wish to match `globPatterns` against. The path is
89
+ * relative to the current directory.
90
+ */
91
+ globDirectory?: string;
92
+ }
93
+
94
+ export type OptionalGlobDirectoryResolved = OptionalGlobDirectoryPartial;
95
+
96
+ export interface GlobPartial {
97
+ /**
98
+ * Determines whether or not symlinks are followed when generating the
99
+ * precache manifest. For more information, see the definition of `follow` in
100
+ * [`glob`'s documentation](https://github.com/isaacs/node-glob#options).
101
+ * @default true
102
+ */
103
+ globFollow?: boolean;
104
+ /**
105
+ * A set of patterns matching files to always exclude when generating the
106
+ * precache manifest. For more information, see the definition of `ignore` in
107
+ * [`glob`'s documentation](https://github.com/isaacs/node-glob#options).
108
+ * @default
109
+ * ```
110
+ * ["**\/node_modules\/**\/*"]
111
+ * ```
112
+ */
113
+ globIgnores?: string[];
114
+ /**
115
+ * Files matching any of these patterns will be included in the precache
116
+ * manifest. For more information, see
117
+ * [`glob`'s Glob Primer](https://github.com/isaacs/node-glob#glob-primer).
118
+ * @default
119
+ * ```
120
+ * ["**\/*.{js,css,html}"]
121
+ * ```
122
+ */
123
+ globPatterns?: string[];
124
+ /**
125
+ * If true, an error reading a directory when generating a precache manifest
126
+ * will cause the build to fail. If false, the problematic directory will be
127
+ * skipped. For more information, see the definition of `strict` in
128
+ * [`glob`'s documentation](https://github.com/isaacs/node-glob#options).
129
+ * @default true
130
+ */
131
+ globStrict?: boolean;
132
+ /**
133
+ * If a URL is rendered based on some server-side logic, its contents may
134
+ * depend on multiple files or on some other unique string value. The keys in
135
+ * this object are server-rendered URLs. If the values are an array of
136
+ * strings, they will be interpreted as `glob` patterns, and the contents of
137
+ * any files matching the patterns will be used to uniquely version the URL.
138
+ * If used with a single string, it will be interpreted as unique versioning
139
+ * information that you've generated for a given URL.
140
+ */
141
+ templatedURLs?: {
142
+ [key: string]: string | string[];
143
+ };
144
+ }
145
+
146
+ export type GlobResolved = Require<GlobPartial, "globFollow" | "globIgnores" | "globPatterns" | "globStrict">;
147
+
148
+ export interface InjectPartial {
149
+ /**
150
+ * The string to find inside of the `swSrc` file. Once found, it will be
151
+ * replaced by the generated precache manifest.
152
+ * @default "self.__SW_MANIFEST"
153
+ */
154
+ injectionPoint?: string;
155
+ /**
156
+ * The path to the service worker file that will be read during
157
+ * the build process, relative to the current working directory.
158
+ */
159
+ swSrc: string;
160
+ }
161
+
162
+ export type InjectResolved = Require<InjectPartial, "injectionPoint">;
163
+
164
+ export interface WebpackPartial {
165
+ /**
166
+ * One or more chunk names whose corresponding output files should be included
167
+ * in the precache manifest.
168
+ */
169
+ chunks?: string[];
170
+ // We can't use the @default annotation here to assign the value via AJV, as
171
+ // an (RegExp)[] can't be serialized into JSON.
172
+ // The default value of [/\.map$/, /^manifest.*\.js$/] will be assigned by
173
+ // the validation function, and we need to reflect that in the docs.
174
+ /**
175
+ * One or more specifiers used to exclude assets from the precache manifest.
176
+ * This is interpreted following
177
+ * [the same rules](https://webpack.js.org/configuration/module/#condition)
178
+ * as `webpack`'s standard `exclude` option.
179
+ * @default
180
+ * ```
181
+ * [/\.map$/, /^manifest.*\.js$]
182
+ * ```
183
+ */
184
+ exclude?: (string | RegExp | ((arg0: any) => boolean))[];
185
+ /**
186
+ * One or more chunk names whose corresponding output files should be excluded
187
+ * from the precache manifest.
188
+ */
189
+ excludeChunks?: string[];
190
+ /**
191
+ * One or more specifiers used to include assets in the precache manifest.
192
+ * This is interpreted following
193
+ * [the same rules](https://webpack.js.org/configuration/module/#condition)
194
+ * as `webpack`'s standard `include` option.
195
+ */
196
+ include?: (string | RegExp | ((arg0: any) => boolean))[];
197
+ /**
198
+ * If set to 'production', then an optimized service worker bundle that
199
+ * excludes debugging info will be produced. If not explicitly configured
200
+ * here, the `mode` value configured in the current `webpack` compilation
201
+ * will be used.
202
+ */
203
+ mode?: string | null;
204
+ }
205
+
206
+ export type WebpackResolved = Require<WebpackPartial, "exclude">;
207
+
208
+ export interface RequiredSwDestPartial {
209
+ /**
210
+ * The path and filename of the service worker file that will be created by
211
+ * the build process. It must end in '.js'.
212
+ */
213
+ swDest: string;
214
+ }
215
+
216
+ export type RequiredSwDestResolved = RequiredSwDestPartial;
217
+
218
+ export interface OptionalSwDestPartial {
219
+ /**
220
+ * The path and filename of the service worker file that will be created by
221
+ * the build process. It must end in '.js'. If omitted, the path will be
222
+ * based on `swSrc`.
223
+ */
224
+ swDest?: string;
225
+ }
226
+
227
+ export type OptionalSwDestResolved = OptionalSwDestPartial;
228
+
229
+ export interface WebpackInjectManifestPartial {
230
+ /**
231
+ * When `true` (the default), the `swSrc` file will be compiled by `webpack`.
232
+ * When `false`, compilation will not occur (and `webpackCompilationPlugins`
233
+ * can't be used.) Set to `false` if you want to inject the manifest into,
234
+ * e.g., a JSON file.
235
+ * @default true
236
+ */
237
+ compileSrc?: boolean;
238
+ // This can only be set if `compileSrc` is true, but that restriction can't be
239
+ // represented in TypeScript. It's enforced via custom runtime validation
240
+ // logic and needs to be documented.
241
+ /**
242
+ * Optional `webpack` plugins that will be used when compiling the `swSrc`
243
+ * input file. Only valid if `compileSrc` is `true`.
244
+ */
245
+ webpackCompilationPlugins?: any[];
246
+ }
247
+
248
+ export type WebpackInjectManifestResolved = Require<WebpackInjectManifestPartial, "compileSrc">;
249
+
250
+ export interface NextInjectManifestPartial {
251
+ /**
252
+ * Enable additional route caching when users navigate through pages with
253
+ * `next/link`. This improves the user experience in some cases but it
254
+ * also adds a bit of overhead due to additional network calls.
255
+ * @default false
256
+ */
257
+ cacheOnFrontEndNav?: boolean;
258
+ /**
259
+ * Whether Serwist should be disabled.
260
+ * @default false
261
+ */
262
+ disable?: boolean;
263
+ /**
264
+ * Whether `@serwist/next` should automatically register the service worker for you. If
265
+ * you want to register the service worker yourself, set this to `false` and run
266
+ * `window.serwist.register()` in `componentDidMount` or `useEffect`.
267
+ * @example
268
+ * ```tsx
269
+ * // app/register-pwa.tsx
270
+ * "use client";
271
+ * import { useEffect } from "react";
272
+ * import type { Serwist } from "@serwist/window";
273
+ *
274
+ * declare global {
275
+ * interface Window {
276
+ * serwist: Serwist;
277
+ * }
278
+ * }
279
+ *
280
+ * export default function RegisterPWA() {
281
+ * useEffect(() => {
282
+ * if ("serviceWorker" in navigator && window.serwist !== undefined) {
283
+ * window.serwist.register();
284
+ * }
285
+ * }, []);
286
+ * return <></>;
287
+ * }
288
+ *
289
+ * // app/layout.tsx
290
+ * import RegisterPWA from "./register-pwa";
291
+ *
292
+ * export default function RootLayout({
293
+ * children,
294
+ * }: {
295
+ * children: React.ReactNode;
296
+ * }) {
297
+ * return (
298
+ * <html lang="en">
299
+ * <head />
300
+ * <body>
301
+ * <RegisterPWA />
302
+ * {children}
303
+ * </body>
304
+ * </html>
305
+ * );
306
+ * }
307
+ * ```
308
+ * @default true
309
+ */
310
+ register?: boolean;
311
+ /**
312
+ * Whether Serwist should reload the app when it goes online.
313
+ * @default true
314
+ */
315
+ reloadOnOnline?: boolean;
316
+ /**
317
+ * The service worker's URL scope. Set to `/foo/` so that paths under `/foo/` are under the service
318
+ * worker's control while others are not.
319
+ * @default nextConfig.basePath
320
+ */
321
+ scope?: string;
322
+ /**
323
+ * The URL to the service worker.
324
+ * @default "/sw.js"
325
+ */
326
+ swUrl?: string;
327
+ /**
328
+ * Files in the public directory matching any of these patterns
329
+ * will be included in the precache manifest. For more information,
330
+ * see [`fast-glob`'s documentation](https://github.com/mrmlnc/fast-glob?tab=readme-ov-file#pattern-syntax).
331
+ * @default
332
+ * ```
333
+ * ["**\/*"]
334
+ * ```
335
+ */
336
+ globPublicPatterns?: GlobPattern | GlobPattern[];
337
+ }
338
+
339
+ export type NextInjectManifestResolved = Require<
340
+ NextInjectManifestPartial,
341
+ "cacheOnFrontEndNav" | "disable" | "register" | "reloadOnOnline" | "swUrl" | "globPublicPatterns"
342
+ >;
343
+
344
+ export type GetManifestOptions = BasePartial & GlobPartial & RequiredGlobDirectoryPartial;
345
+
346
+ export type GetManifestOptionsComplete = BaseResolved & GlobResolved & RequiredGlobDirectoryResolved;
347
+
348
+ export type InjectManifestOptions = BasePartial & GlobPartial & InjectPartial & RequiredSwDestPartial & RequiredGlobDirectoryPartial;
349
+
350
+ export type InjectManifestOptionsComplete = BaseResolved & GlobResolved & InjectResolved & RequiredSwDestResolved & RequiredGlobDirectoryResolved;
351
+
352
+ export type WebpackInjectManifestOptions = BasePartial & WebpackPartial & InjectPartial & OptionalSwDestPartial & WebpackInjectManifestPartial;
353
+
354
+ export type WebpackInjectManifestOptionsComplete = BaseResolved &
355
+ WebpackResolved &
356
+ InjectResolved &
357
+ OptionalSwDestResolved &
358
+ WebpackInjectManifestResolved;
359
+
360
+ export type ViteInjectManifestOptions = BasePartial & GlobPartial & InjectPartial & RequiredSwDestPartial & RequiredGlobDirectoryPartial;
361
+
362
+ export type ViteInjectManifestOptionsComplete = BaseResolved & GlobResolved & InjectResolved & RequiredSwDestResolved & RequiredGlobDirectoryResolved;
363
+
364
+ export type NextInjectManifestOptions = Omit<
365
+ WebpackInjectManifestOptions & RequiredSwDestPartial & NextInjectManifestPartial,
366
+ "disablePrecacheManifest"
367
+ >;
368
+
369
+ export type NextInjectManifestOptionsComplete = Omit<
370
+ WebpackInjectManifestOptionsComplete & RequiredSwDestResolved & NextInjectManifestResolved,
371
+ "disablePrecacheManifest"
372
+ >;
373
+
374
+ export interface GetManifestResult {
375
+ count: number;
376
+ manifestEntries: ManifestEntry[] | undefined;
377
+ size: number;
378
+ warnings: string[];
379
+ }
380
+
381
+ export type BuildResult = Omit<GetManifestResult, "manifestEntries"> & {
382
+ filePaths: string[];
383
+ };
384
+
385
+ /**
386
+ * @private
387
+ */
388
+ export interface FileDetails {
389
+ file: string;
390
+ hash: string;
391
+ size: number;
392
+ }
393
+
394
+ /**
395
+ * @private
396
+ */
397
+ export type BuildType = "dev" | "prod";
398
+
399
+ /**
400
+ * @private
401
+ */
402
+ export type SerwistPackageJSON = PackageJson;
403
+
404
+ /**
405
+ * @private
406
+ */
407
+ export type MethodNames = "GetManifest" | "InjectManifest" | "WebpackInjectManifest" | "ViteInjectManifest";