@docusaurus/plugin-pwa 4.0.0-canary-7078 → 4.0.0-canary-7102

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.
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+ import type { PluginOptions } from '@docusaurus/plugin-pwa';
8
+ /** The files to precache: the regular static assets, plus the user ones. */
9
+ export declare function getGlobPatterns(injectManifestConfig: PluginOptions['injectManifestConfig']): string[];
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ /**
3
+ * Copyright (c) Facebook, Inc. and its affiliates.
4
+ *
5
+ * This source code is licensed under the MIT license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.getGlobPatterns = getGlobPatterns;
10
+ /** The files to precache: the regular static assets, plus the user ones. */
11
+ function getGlobPatterns(injectManifestConfig) {
12
+ return [
13
+ '**/*.{js,json,css,html}',
14
+ '**/*.{png,jpg,jpeg,gif,svg,ico}',
15
+ '**/*.{woff,woff2,eot,ttf,otf}',
16
+ ...(injectManifestConfig.globPatterns ?? []),
17
+ ];
18
+ }
package/lib/index.js CHANGED
@@ -11,10 +11,11 @@ exports.default = pluginPWA;
11
11
  const tslib_1 = require("tslib");
12
12
  const node_path_1 = tslib_1.__importDefault(require("node:path"));
13
13
  const bundler_1 = require("@docusaurus/bundler");
14
- const workbox_build_1 = require("workbox-build");
15
14
  const utils_1 = require("@docusaurus/utils");
16
15
  const logger_1 = tslib_1.__importDefault(require("@docusaurus/logger"));
17
16
  const theme_translations_1 = require("@docusaurus/theme-translations");
17
+ const globPatterns_1 = require("./globPatterns");
18
+ const precacheManifest_1 = require("./precacheManifest");
18
19
  const PluginName = 'docusaurus-plugin-pwa';
19
20
  function pluginPWA(context, options) {
20
21
  if (process.env.NODE_ENV !== 'production') {
@@ -129,15 +130,9 @@ function pluginPWA(context, options) {
129
130
  currentBundler: props.currentBundler,
130
131
  });
131
132
  const swDest = node_path_1.default.resolve(props.outDir, 'sw.js');
132
- await (0, workbox_build_1.injectManifest)({
133
+ await (0, precacheManifest_1.injectPrecacheManifest)({
133
134
  ...injectManifestConfig,
134
- globPatterns: [
135
- '**/*.{js,json,css,html}',
136
- '**/*.{png,jpg,jpeg,gif,svg,ico}',
137
- '**/*.{woff,woff2,eot,ttf,otf}',
138
- // @ts-expect-error: internal API?
139
- ...(workbox_build_1.injectManifest.globPatterns ?? []),
140
- ],
135
+ globPatterns: (0, globPatterns_1.getGlobPatterns)(injectManifestConfig),
141
136
  // Those attributes are not overrideable
142
137
  swDest,
143
138
  swSrc: swDest,
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+ import type { InjectManifestOptions } from 'workbox-build';
8
+ /**
9
+ * Same as Workbox `injectManifest()`, but falls back to SHA-256 to hash the
10
+ * files when MD5 is not available. Workbox hardcodes MD5, which FIPS mode
11
+ * rejects. See https://github.com/GoogleChrome/workbox/issues/3366
12
+ */
13
+ export declare function injectPrecacheManifest(config: InjectManifestOptions): Promise<void>;
@@ -0,0 +1,68 @@
1
+ "use strict";
2
+ /**
3
+ * Copyright (c) Facebook, Inc. and its affiliates.
4
+ *
5
+ * This source code is licensed under the MIT license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.injectPrecacheManifest = injectPrecacheManifest;
10
+ const tslib_1 = require("tslib");
11
+ const node_path_1 = tslib_1.__importDefault(require("node:path"));
12
+ const workbox_build_1 = require("workbox-build");
13
+ // Internal Workbox APIs, to apply the user options the same way Workbox does
14
+ const transform_manifest_1 = require("workbox-build/build/lib/transform-manifest");
15
+ const validate_options_1 = require("workbox-build/build/lib/validate-options");
16
+ const fs_1 = require("@docusaurus/fs");
17
+ const glob_1 = require("@docusaurus/glob");
18
+ const logger_1 = tslib_1.__importDefault(require("@docusaurus/logger"));
19
+ const utils_1 = require("@docusaurus/utils");
20
+ /**
21
+ * Same as Workbox `injectManifest()`, but falls back to SHA-256 to hash the
22
+ * files when MD5 is not available. Workbox hardcodes MD5, which FIPS mode
23
+ * rejects. See https://github.com/GoogleChrome/workbox/issues/3366
24
+ */
25
+ async function injectPrecacheManifest(config) {
26
+ const options = (0, validate_options_1.validateInjectManifestOptions)(config);
27
+ // Options our file hashing doesn't support: Workbox (and MD5) handles them
28
+ if (options.templatedURLs || !options.globFollow) {
29
+ await (0, workbox_build_1.injectManifest)(config);
30
+ return;
31
+ }
32
+ const files = await globFilesToPrecache(options);
33
+ const fileDetails = await getFileDetails(files, options.globDirectory);
34
+ const { manifestEntries } = await (0, transform_manifest_1.transformManifest)({ ...options, fileDetails });
35
+ await (0, workbox_build_1.injectManifest)({
36
+ swSrc: options.swSrc,
37
+ swDest: options.swDest,
38
+ globDirectory: options.globDirectory,
39
+ injectionPoint: options.injectionPoint,
40
+ globPatterns: [],
41
+ additionalManifestEntries: manifestEntries,
42
+ });
43
+ }
44
+ async function globFilesToPrecache({ globDirectory, globPatterns = [], globIgnores = [], swSrc, swDest, }) {
45
+ // Like Workbox, never precache the service worker itself
46
+ const swFiles = [swSrc, swDest].map((file) => (0, utils_1.posixPath)(node_path_1.default.relative(globDirectory, file)));
47
+ const files = await (0, glob_1.glob)(globPatterns, {
48
+ cwd: globDirectory,
49
+ ignore: [...globIgnores, ...swFiles],
50
+ // Like Workbox, don't precache all the files of a next.js/ directory
51
+ expandDirectories: false,
52
+ });
53
+ // Sorted for a deterministic sw.js output
54
+ return files.toSorted();
55
+ }
56
+ async function getFileDetails(files, globDirectory) {
57
+ return Promise.all(files.map((file) => getSingleFileDetails(file, globDirectory)));
58
+ }
59
+ async function getSingleFileDetails(file, globDirectory) {
60
+ const filePath = node_path_1.default.join(globDirectory, file);
61
+ try {
62
+ const content = await fs_1.fs.readFile(filePath);
63
+ return { file, hash: (0, utils_1.hashString)(content), size: content.length };
64
+ }
65
+ catch (err) {
66
+ throw new Error(logger_1.default.interpolate `Can't hash the file to precache at path=${node_path_1.default.relative(process.cwd(), filePath)}`, { cause: err });
67
+ }
68
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@docusaurus/plugin-pwa",
3
- "version": "4.0.0-canary-7078",
3
+ "version": "4.0.0-canary-7102",
4
4
  "description": "Docusaurus Plugin to add PWA support.",
5
5
  "main": "lib/index.js",
6
6
  "types": "src/plugin-pwa.d.ts",
@@ -14,14 +14,16 @@
14
14
  },
15
15
  "license": "MIT",
16
16
  "dependencies": {
17
- "@docusaurus/bundler": "4.0.0-canary-7078",
18
- "@docusaurus/core": "4.0.0-canary-7078",
19
- "@docusaurus/logger": "4.0.0-canary-7078",
20
- "@docusaurus/theme-common": "4.0.0-canary-7078",
21
- "@docusaurus/theme-translations": "4.0.0-canary-7078",
22
- "@docusaurus/types": "4.0.0-canary-7078",
23
- "@docusaurus/utils": "4.0.0-canary-7078",
24
- "@docusaurus/utils-validation": "4.0.0-canary-7078",
17
+ "@docusaurus/bundler": "4.0.0-canary-7102",
18
+ "@docusaurus/core": "4.0.0-canary-7102",
19
+ "@docusaurus/fs": "4.0.0-canary-7102",
20
+ "@docusaurus/glob": "4.0.0-canary-7102",
21
+ "@docusaurus/logger": "4.0.0-canary-7102",
22
+ "@docusaurus/theme-common": "4.0.0-canary-7102",
23
+ "@docusaurus/theme-translations": "4.0.0-canary-7102",
24
+ "@docusaurus/types": "4.0.0-canary-7102",
25
+ "@docusaurus/utils": "4.0.0-canary-7102",
26
+ "@docusaurus/utils-validation": "4.0.0-canary-7102",
25
27
  "clsx": "^2.1.1",
26
28
  "tslib": "^2.8.1",
27
29
  "webpack": "^5.111.0",
@@ -30,7 +32,7 @@
30
32
  "workbox-window": "^7.4.1"
31
33
  },
32
34
  "devDependencies": {
33
- "@docusaurus/module-type-aliases": "4.0.0-canary-7078",
35
+ "@docusaurus/module-type-aliases": "4.0.0-canary-7102",
34
36
  "@types/react-dom": "^19.3.0"
35
37
  },
36
38
  "peerDependencies": {
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+
8
+ import type {PluginOptions} from '@docusaurus/plugin-pwa';
9
+
10
+ /** The files to precache: the regular static assets, plus the user ones. */
11
+ export function getGlobPatterns(
12
+ injectManifestConfig: PluginOptions['injectManifestConfig'],
13
+ ): string[] {
14
+ return [
15
+ '**/*.{js,json,css,html}',
16
+ '**/*.{png,jpg,jpeg,gif,svg,ico}',
17
+ '**/*.{woff,woff2,eot,ttf,otf}',
18
+ ...(injectManifestConfig.globPatterns ?? []),
19
+ ];
20
+ }
package/src/index.ts CHANGED
@@ -13,10 +13,11 @@ import {
13
13
  getProgressBarPlugin,
14
14
  getMinimizers,
15
15
  } from '@docusaurus/bundler';
16
- import {injectManifest} from 'workbox-build';
17
16
  import {normalizeUrl} from '@docusaurus/utils';
18
17
  import logger from '@docusaurus/logger';
19
18
  import {readDefaultCodeTranslationMessages} from '@docusaurus/theme-translations';
19
+ import {getGlobPatterns} from './globPatterns';
20
+ import {injectPrecacheManifest} from './precacheManifest';
20
21
  import type {HtmlTags, LoadContext, Plugin} from '@docusaurus/types';
21
22
  import type {PluginOptions} from '@docusaurus/plugin-pwa';
22
23
 
@@ -174,15 +175,9 @@ export default function pluginPWA(
174
175
 
175
176
  const swDest = path.resolve(props.outDir, 'sw.js');
176
177
 
177
- await injectManifest({
178
+ await injectPrecacheManifest({
178
179
  ...injectManifestConfig,
179
- globPatterns: [
180
- '**/*.{js,json,css,html}',
181
- '**/*.{png,jpg,jpeg,gif,svg,ico}',
182
- '**/*.{woff,woff2,eot,ttf,otf}',
183
- // @ts-expect-error: internal API?
184
- ...((injectManifest.globPatterns as string[] | undefined) ?? []),
185
- ],
180
+ globPatterns: getGlobPatterns(injectManifestConfig),
186
181
  // Those attributes are not overrideable
187
182
  swDest,
188
183
  swSrc: swDest,
@@ -0,0 +1,93 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+
8
+ import path from 'node:path';
9
+ import {injectManifest} from 'workbox-build';
10
+ // Internal Workbox APIs, to apply the user options the same way Workbox does
11
+ import {transformManifest} from 'workbox-build/build/lib/transform-manifest';
12
+ import {validateInjectManifestOptions} from 'workbox-build/build/lib/validate-options';
13
+ import {fs} from '@docusaurus/fs';
14
+ import {glob} from '@docusaurus/glob';
15
+ import logger from '@docusaurus/logger';
16
+ import {hashString, posixPath} from '@docusaurus/utils';
17
+ import type {FileDetails, InjectManifestOptions} from 'workbox-build';
18
+
19
+ /**
20
+ * Same as Workbox `injectManifest()`, but falls back to SHA-256 to hash the
21
+ * files when MD5 is not available. Workbox hardcodes MD5, which FIPS mode
22
+ * rejects. See https://github.com/GoogleChrome/workbox/issues/3366
23
+ */
24
+ export async function injectPrecacheManifest(
25
+ config: InjectManifestOptions,
26
+ ): Promise<void> {
27
+ const options = validateInjectManifestOptions(config);
28
+
29
+ // Options our file hashing doesn't support: Workbox (and MD5) handles them
30
+ if (options.templatedURLs || !options.globFollow) {
31
+ await injectManifest(config);
32
+ return;
33
+ }
34
+
35
+ const files = await globFilesToPrecache(options);
36
+ const fileDetails = await getFileDetails(files, options.globDirectory);
37
+ const {manifestEntries} = await transformManifest({...options, fileDetails});
38
+
39
+ await injectManifest({
40
+ swSrc: options.swSrc,
41
+ swDest: options.swDest,
42
+ globDirectory: options.globDirectory,
43
+ injectionPoint: options.injectionPoint,
44
+ globPatterns: [],
45
+ additionalManifestEntries: manifestEntries,
46
+ });
47
+ }
48
+
49
+ async function globFilesToPrecache({
50
+ globDirectory,
51
+ globPatterns = [],
52
+ globIgnores = [],
53
+ swSrc,
54
+ swDest,
55
+ }: InjectManifestOptions): Promise<string[]> {
56
+ // Like Workbox, never precache the service worker itself
57
+ const swFiles = [swSrc, swDest].map((file) =>
58
+ posixPath(path.relative(globDirectory, file)),
59
+ );
60
+ const files = await glob(globPatterns, {
61
+ cwd: globDirectory,
62
+ ignore: [...globIgnores, ...swFiles],
63
+ // Like Workbox, don't precache all the files of a next.js/ directory
64
+ expandDirectories: false,
65
+ });
66
+ // Sorted for a deterministic sw.js output
67
+ return files.toSorted();
68
+ }
69
+
70
+ async function getFileDetails(
71
+ files: string[],
72
+ globDirectory: string,
73
+ ): Promise<FileDetails[]> {
74
+ return Promise.all(
75
+ files.map((file) => getSingleFileDetails(file, globDirectory)),
76
+ );
77
+ }
78
+
79
+ async function getSingleFileDetails(
80
+ file: string,
81
+ globDirectory: string,
82
+ ): Promise<FileDetails> {
83
+ const filePath = path.join(globDirectory, file);
84
+ try {
85
+ const content = await fs.readFile(filePath);
86
+ return {file, hash: hashString(content), size: content.length};
87
+ } catch (err) {
88
+ throw new Error(
89
+ logger.interpolate`Can't hash the file to precache at path=${path.relative(process.cwd(), filePath)}`,
90
+ {cause: err},
91
+ );
92
+ }
93
+ }