@powerhousedao/builder-tools 6.2.2-dev.4 → 6.2.2-dev.40
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/index.d.mts +301 -1
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +342 -102
- package/dist/index.mjs.map +1 -1
- package/dist/service-worker/service-worker.ts +352 -0
- package/dist/service-worker/virtual-sw-config.d.ts +22 -0
- package/package.json +10 -4
package/dist/index.mjs
CHANGED
|
@@ -7,12 +7,14 @@ import { fileURLToPath } from "node:url";
|
|
|
7
7
|
import MagicString from "magic-string";
|
|
8
8
|
import { readFile, writeFile } from "node:fs/promises";
|
|
9
9
|
import { cwd } from "node:process";
|
|
10
|
-
import { DEFAULT_CONNECT_CONFIG, buildRuntimeConfig, deepMerge, loadConnectEnv, normalizeBasePath, phConnectRuntimeConfigSchema, powerhousePackageSchema, setConnectEnv } from "@powerhousedao/shared/connect";
|
|
10
|
+
import { DEFAULT_CONNECT_CONFIG, PWA_FILE_HANDLER_ACTION, buildRuntimeConfig, deepMerge, loadConnectEnv, mergeManifest, mergePwaConfig, normalizeBasePath, phConnectRuntimeConfigSchema, powerhousePackageSchema, setConnectEnv, unionStrings, withInferredCategory } from "@powerhousedao/shared/connect";
|
|
11
11
|
import { getConfig } from "@powerhousedao/config/node";
|
|
12
12
|
import tailwind from "@tailwindcss/vite";
|
|
13
13
|
import react from "@vitejs/plugin-react";
|
|
14
14
|
import { createLogger, esmExternalRequirePlugin, loadEnv, searchForWorkspaceRoot } from "vite";
|
|
15
15
|
import { createHtmlPlugin } from "vite-plugin-html";
|
|
16
|
+
import { PwaConfigSchema } from "@powerhousedao/shared/document-model";
|
|
17
|
+
import { toCdnUrl } from "@powerhousedao/shared/registry/urls";
|
|
16
18
|
import { VitePWA } from "vite-plugin-pwa";
|
|
17
19
|
//#region connect-utils/constants.ts
|
|
18
20
|
const EXTERNAL_PACKAGES_IMPORT = "PH:EXTERNAL_PACKAGES";
|
|
@@ -1130,8 +1132,170 @@ function phConfigPlugin(options) {
|
|
|
1130
1132
|
};
|
|
1131
1133
|
}
|
|
1132
1134
|
//#endregion
|
|
1135
|
+
//#region connect-utils/vite-plugins/pwa-packages.ts
|
|
1136
|
+
const REGISTRY_FETCH_TIMEOUT_MS = 1e4;
|
|
1137
|
+
function isPlainObject(value) {
|
|
1138
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
1139
|
+
}
|
|
1140
|
+
function formatZodIssues(error) {
|
|
1141
|
+
return error.issues.map((issue) => `${issue.path.join(".") || "(root)"}: ${issue.message}`).join("; ");
|
|
1142
|
+
}
|
|
1143
|
+
/** Turn a parsed manifest JSON into a contribution: its `pwa` fragment (a
|
|
1144
|
+
* malformed one → warn + skip that fragment, not the whole contribution) plus a
|
|
1145
|
+
* `categories` entry derived from the manifest's `category` field. Returns null
|
|
1146
|
+
* only when the manifest yields neither — no `pwa` and no `category`. */
|
|
1147
|
+
function toPwaContribution(manifest, fallbackLabel, onWarn) {
|
|
1148
|
+
if (!isPlainObject(manifest)) return null;
|
|
1149
|
+
const label = typeof manifest.name === "string" && manifest.name ? manifest.name : fallbackLabel;
|
|
1150
|
+
let config = {};
|
|
1151
|
+
if (manifest.pwa !== void 0) if (!isPlainObject(manifest.pwa)) onWarn(`PWA config: ${label} declares a non-object 'pwa' field in its manifest; ignored.`);
|
|
1152
|
+
else {
|
|
1153
|
+
const parsed = PwaConfigSchema.safeParse(manifest.pwa);
|
|
1154
|
+
if (!parsed.success) onWarn(`PWA config: ${label}'s pwa fragment is invalid; ignored. ${formatZodIssues(parsed.error)}`);
|
|
1155
|
+
else config = parsed.data;
|
|
1156
|
+
}
|
|
1157
|
+
config = withInferredCategory(config, manifest.category);
|
|
1158
|
+
if (Object.keys(config).length === 0) return null;
|
|
1159
|
+
return {
|
|
1160
|
+
source: label,
|
|
1161
|
+
config
|
|
1162
|
+
};
|
|
1163
|
+
}
|
|
1164
|
+
/** Read the first parseable manifest among `candidates` (relative to `dir`)
|
|
1165
|
+
* and extract its pwa contribution. Candidates that resolve outside `dir`
|
|
1166
|
+
* (a hostile `./manifest` export) are warned about and skipped. */
|
|
1167
|
+
function readPwaFragmentFromDir(dir, candidates, fallbackLabel, onWarn) {
|
|
1168
|
+
for (const rel of candidates) {
|
|
1169
|
+
const manifestPath = path.resolve(dir, rel);
|
|
1170
|
+
if (path.relative(dir, manifestPath).startsWith("..")) {
|
|
1171
|
+
onWarn(`PWA config: ${fallbackLabel} declares a manifest path outside its package directory (${rel}); ignored.`);
|
|
1172
|
+
continue;
|
|
1173
|
+
}
|
|
1174
|
+
if (!fs.existsSync(manifestPath)) continue;
|
|
1175
|
+
try {
|
|
1176
|
+
return toPwaContribution(JSON.parse(fs.readFileSync(manifestPath, "utf-8")), fallbackLabel, onWarn);
|
|
1177
|
+
} catch {
|
|
1178
|
+
onWarn(`PWA config: could not parse ${fallbackLabel}'s manifest at ${rel}; ignored.`);
|
|
1179
|
+
return null;
|
|
1180
|
+
}
|
|
1181
|
+
}
|
|
1182
|
+
return null;
|
|
1183
|
+
}
|
|
1184
|
+
function readLocalPackagePwaFragment(projectRoot, name, onWarn) {
|
|
1185
|
+
const pkgDir = path.join(projectRoot, "node_modules", name);
|
|
1186
|
+
let manifestRel;
|
|
1187
|
+
try {
|
|
1188
|
+
const exported = JSON.parse(fs.readFileSync(path.join(pkgDir, "package.json"), "utf-8")).exports?.["./manifest"];
|
|
1189
|
+
if (typeof exported === "string") manifestRel = exported;
|
|
1190
|
+
} catch {}
|
|
1191
|
+
return readPwaFragmentFromDir(pkgDir, [
|
|
1192
|
+
manifestRel,
|
|
1193
|
+
"dist/powerhouse.manifest.json",
|
|
1194
|
+
"powerhouse.manifest.json"
|
|
1195
|
+
].filter((rel) => typeof rel === "string"), name, onWarn);
|
|
1196
|
+
}
|
|
1197
|
+
async function fetchRegistryPwaFragment(cdnUrl, pkg, onWarn, fetchImpl) {
|
|
1198
|
+
const spec = pkg.version ? `${pkg.packageName}@${pkg.version}` : pkg.packageName;
|
|
1199
|
+
const url = `${cdnUrl}/${spec}/powerhouse.manifest.json`;
|
|
1200
|
+
try {
|
|
1201
|
+
const response = await fetchImpl(url, { signal: AbortSignal.timeout(REGISTRY_FETCH_TIMEOUT_MS) });
|
|
1202
|
+
if (response.status === 404) return null;
|
|
1203
|
+
if (!response.ok) {
|
|
1204
|
+
onWarn(`PWA config: registry returned ${response.status} for ${spec}'s manifest; its pwa fragment (if any) is skipped.`);
|
|
1205
|
+
return null;
|
|
1206
|
+
}
|
|
1207
|
+
return toPwaContribution(await response.json(), pkg.packageName, onWarn);
|
|
1208
|
+
} catch (error) {
|
|
1209
|
+
onWarn(`PWA config: could not fetch ${spec}'s manifest from the registry (${error instanceof Error ? error.message : String(error)}); its pwa fragment (if any) is skipped.`);
|
|
1210
|
+
return null;
|
|
1211
|
+
}
|
|
1212
|
+
}
|
|
1213
|
+
/**
|
|
1214
|
+
* Read the `pwa` fragment of each package in `packages`, in the given order
|
|
1215
|
+
* (which becomes their merge precedence — later packages win scalar
|
|
1216
|
+
* conflicts). `provider: "local"` packages are read from node_modules;
|
|
1217
|
+
* everything else is fetched from the registry CDN (skipped with a warning
|
|
1218
|
+
* when `registryUrl` is missing or the registry is unreachable). Packages
|
|
1219
|
+
* with no `pwa` fragment are simply absent from the result.
|
|
1220
|
+
*/
|
|
1221
|
+
async function collectPackagePwaContributions(options) {
|
|
1222
|
+
const projectRoot = options.projectRoot ?? process.cwd();
|
|
1223
|
+
const onWarn = options.onWarn ?? (() => {});
|
|
1224
|
+
const fetchImpl = options.fetchImpl ?? fetch;
|
|
1225
|
+
const cdnUrl = options.registryUrl ? toCdnUrl(options.registryUrl) : null;
|
|
1226
|
+
return (await Promise.all(options.packages.map(async (pkg) => {
|
|
1227
|
+
if (pkg.provider === "local") return readLocalPackagePwaFragment(projectRoot, pkg.packageName, onWarn);
|
|
1228
|
+
if (!cdnUrl) {
|
|
1229
|
+
onWarn(`PWA config: no packageRegistryUrl configured; cannot read ${pkg.packageName}'s pwa fragment (if any) from the registry.`);
|
|
1230
|
+
return null;
|
|
1231
|
+
}
|
|
1232
|
+
return fetchRegistryPwaFragment(cdnUrl, pkg, onWarn, fetchImpl);
|
|
1233
|
+
}))).filter((contribution) => contribution !== null);
|
|
1234
|
+
}
|
|
1235
|
+
/**
|
|
1236
|
+
* Read the pwa fragment from the project's OWN manifest
|
|
1237
|
+
* (`powerhouse.manifest.json` under the project root, falling back to the
|
|
1238
|
+
* `dist/` copy the project build emits). The root file comes first — it is
|
|
1239
|
+
* the source the dist copy is made from, and a stale dist left by an older
|
|
1240
|
+
* build must not shadow it. Returns null silently when no manifest exists —
|
|
1241
|
+
* not every project ships one — or when it carries neither a `pwa` block nor a
|
|
1242
|
+
* `category` to derive `categories` from.
|
|
1243
|
+
*
|
|
1244
|
+
* The `pwa` block is parsed STRICTLY, like `connect.pwa` and unlike third-party
|
|
1245
|
+
* package fragments: the project's own manifest is the developer's config, so
|
|
1246
|
+
* an invalid `pwa` block (a typo, or a removed/unknown field such as
|
|
1247
|
+
* `protocol_handlers`) FAILS the build instead of being silently skipped — a
|
|
1248
|
+
* silently-dropped field would be far harder to notice.
|
|
1249
|
+
*/
|
|
1250
|
+
function collectProjectPwaContribution(options) {
|
|
1251
|
+
const projectRoot = options.projectRoot ?? process.cwd();
|
|
1252
|
+
for (const rel of ["powerhouse.manifest.json", "dist/powerhouse.manifest.json"]) {
|
|
1253
|
+
const manifestPath = path.resolve(projectRoot, rel);
|
|
1254
|
+
if (!fs.existsSync(manifestPath)) continue;
|
|
1255
|
+
let manifest;
|
|
1256
|
+
try {
|
|
1257
|
+
manifest = JSON.parse(fs.readFileSync(manifestPath, "utf-8"));
|
|
1258
|
+
} catch (error) {
|
|
1259
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
1260
|
+
throw new Error(`Could not parse ${manifestPath}: ${message}`);
|
|
1261
|
+
}
|
|
1262
|
+
if (!isPlainObject(manifest)) return null;
|
|
1263
|
+
const label = typeof manifest.name === "string" && manifest.name ? manifest.name : "project manifest";
|
|
1264
|
+
const withCategory = withInferredCategory(manifest.pwa === void 0 ? {} : parsePwaConfigStrict(manifest.pwa, `pwa in ${manifestPath}`), manifest.category);
|
|
1265
|
+
if (Object.keys(withCategory).length === 0) return null;
|
|
1266
|
+
return {
|
|
1267
|
+
source: label,
|
|
1268
|
+
config: withCategory
|
|
1269
|
+
};
|
|
1270
|
+
}
|
|
1271
|
+
return null;
|
|
1272
|
+
}
|
|
1273
|
+
/** Strictly parse a PWA config block, throwing a build-failing error (naming
|
|
1274
|
+
* the offending field) when it is invalid. Used for the developer's own config
|
|
1275
|
+
* (`connect.pwa` and the project manifest's `pwa`), where a silent drop would
|
|
1276
|
+
* be a footgun — unlike third-party package fragments, which warn + skip. */
|
|
1277
|
+
function parsePwaConfigStrict(config, description) {
|
|
1278
|
+
const parsed = PwaConfigSchema.safeParse(config);
|
|
1279
|
+
if (!parsed.success) throw new Error(`Invalid ${description}: ${formatZodIssues(parsed.error)}. Fix it or remove the field.`);
|
|
1280
|
+
return parsed.data;
|
|
1281
|
+
}
|
|
1282
|
+
/**
|
|
1283
|
+
* Validate the project's `connect.pwa` block. Unlike package fragments
|
|
1284
|
+
* (third-party, warn + skip), the user's own config fails the build: a typo
|
|
1285
|
+
* that silently dropped offline coverage would be far harder to notice.
|
|
1286
|
+
*/
|
|
1287
|
+
function validateProjectPwaConfig(config, configPath) {
|
|
1288
|
+
return parsePwaConfigStrict(config, `connect.pwa in ${configPath}`);
|
|
1289
|
+
}
|
|
1290
|
+
//#endregion
|
|
1133
1291
|
//#region connect-utils/vite-plugins/pwa-icons.ts
|
|
1134
|
-
const PWA_ICONS = [
|
|
1292
|
+
const PWA_ICONS = [
|
|
1293
|
+
"pwa-192x192.png",
|
|
1294
|
+
"pwa-512x512.png",
|
|
1295
|
+
"pwa-512x512-maskable.png",
|
|
1296
|
+
"document-icon-192x192.png",
|
|
1297
|
+
"document-icon-512x512.png"
|
|
1298
|
+
];
|
|
1135
1299
|
function connectPwaIconsPlugin() {
|
|
1136
1300
|
return {
|
|
1137
1301
|
name: "copy-connect-pwa-icons",
|
|
@@ -1150,23 +1314,154 @@ function connectPwaIconsPlugin() {
|
|
|
1150
1314
|
};
|
|
1151
1315
|
}
|
|
1152
1316
|
//#endregion
|
|
1317
|
+
//#region connect-utils/vite-plugins/pwa-overrides.ts
|
|
1318
|
+
/**
|
|
1319
|
+
* Lay an effective PWA fragment over the plugin's hardcoded manifest + precache
|
|
1320
|
+
* base. The manifest is merged by the shared `mergeManifest` with
|
|
1321
|
+
* `fragment-wins` scalars (the build-time effective config is the authority);
|
|
1322
|
+
* icons and file handlers concatenate after the base set (contributed handlers
|
|
1323
|
+
* get Connect's fixed action injected — the open route is not configurable);
|
|
1324
|
+
* globs union; the size ceiling takes the max.
|
|
1325
|
+
*
|
|
1326
|
+
* The serialisable `runtimeCaching` / `navigateFallbackDenylist` overrides are
|
|
1327
|
+
* NOT handled here — they are passed straight to the service worker (which
|
|
1328
|
+
* registers them after its built-in rules), because injectManifest has no
|
|
1329
|
+
* declarative runtimeCaching and Workbox is first-match-wins, so an override
|
|
1330
|
+
* can only be appended after the built-ins (intentional for v1).
|
|
1331
|
+
*/
|
|
1332
|
+
function applyPwaOverrides(base, override) {
|
|
1333
|
+
return {
|
|
1334
|
+
manifest: mergeManifest(base.manifest, override.manifest, { scalarPolicy: "fragment-wins" }),
|
|
1335
|
+
precache: {
|
|
1336
|
+
globPatterns: override.globPatterns?.length ? unionStrings(base.precache.globPatterns, override.globPatterns) : base.precache.globPatterns,
|
|
1337
|
+
globIgnores: override.globIgnores?.length ? unionStrings(base.precache.globIgnores, override.globIgnores) : base.precache.globIgnores,
|
|
1338
|
+
maximumFileSizeToCacheInBytes: typeof override.maximumFileSizeToCacheInBytes === "number" ? Math.max(base.precache.maximumFileSizeToCacheInBytes, override.maximumFileSizeToCacheInBytes) : base.precache.maximumFileSizeToCacheInBytes
|
|
1339
|
+
}
|
|
1340
|
+
};
|
|
1341
|
+
}
|
|
1342
|
+
//#endregion
|
|
1153
1343
|
//#region connect-utils/vite-plugins/pwa.ts
|
|
1154
1344
|
/**
|
|
1345
|
+
* Connect's hardcoded PWA manifest. The base layer of the override ladder —
|
|
1346
|
+
* package `pwa` fragments and the project's `connect.pwa` block are laid on
|
|
1347
|
+
* top of this by `applyPwaOverrides`.
|
|
1348
|
+
*/
|
|
1349
|
+
const BASE_MANIFEST = {
|
|
1350
|
+
name: "Powerhouse Connect",
|
|
1351
|
+
short_name: "Connect",
|
|
1352
|
+
description: "A navigation, collaboration and reporting tool for decentralised and open organisations.",
|
|
1353
|
+
theme_color: "#ffffff",
|
|
1354
|
+
background_color: "#ffffff",
|
|
1355
|
+
display: "standalone",
|
|
1356
|
+
start_url: ".",
|
|
1357
|
+
scope: ".",
|
|
1358
|
+
icons: [
|
|
1359
|
+
{
|
|
1360
|
+
src: "pwa-192x192.png",
|
|
1361
|
+
sizes: "192x192",
|
|
1362
|
+
type: "image/png"
|
|
1363
|
+
},
|
|
1364
|
+
{
|
|
1365
|
+
src: "pwa-512x512.png",
|
|
1366
|
+
sizes: "512x512",
|
|
1367
|
+
type: "image/png"
|
|
1368
|
+
},
|
|
1369
|
+
{
|
|
1370
|
+
src: "pwa-512x512-maskable.png",
|
|
1371
|
+
sizes: "512x512",
|
|
1372
|
+
type: "image/png",
|
|
1373
|
+
purpose: "maskable"
|
|
1374
|
+
}
|
|
1375
|
+
],
|
|
1376
|
+
file_handlers: [{
|
|
1377
|
+
action: PWA_FILE_HANDLER_ACTION,
|
|
1378
|
+
accept: {
|
|
1379
|
+
"application/vnd.powerhouse.document+zip": [".phd"],
|
|
1380
|
+
"application/vnd.powerhouse.document-model+zip": [".phdm"]
|
|
1381
|
+
},
|
|
1382
|
+
icons: [{
|
|
1383
|
+
src: "document-icon-192x192.png",
|
|
1384
|
+
sizes: "192x192",
|
|
1385
|
+
type: "image/png"
|
|
1386
|
+
}, {
|
|
1387
|
+
src: "document-icon-512x512.png",
|
|
1388
|
+
sizes: "512x512",
|
|
1389
|
+
type: "image/png"
|
|
1390
|
+
}]
|
|
1391
|
+
}],
|
|
1392
|
+
launch_handler: { client_mode: "focus-existing" }
|
|
1393
|
+
};
|
|
1394
|
+
/**
|
|
1395
|
+
* Connect's hardcoded precache config — the base layer for the `injectManifest`
|
|
1396
|
+
* precache. Overridable additively (extra globs) or by raising the size ceiling
|
|
1397
|
+
* via package/project `pwa` config. The rest of the old Workbox config
|
|
1398
|
+
* (runtime-caching rules with their function urlPatterns, the navigation
|
|
1399
|
+
* fallback, clientsClaim/skipWaiting/cleanupOutdatedCaches) now lives as
|
|
1400
|
+
* imperative code in the hand-written service worker
|
|
1401
|
+
* (../service-worker/service-worker.ts), because `injectManifest` has no
|
|
1402
|
+
* declarative runtimeCaching option.
|
|
1403
|
+
*/
|
|
1404
|
+
const BASE_PRECACHE = {
|
|
1405
|
+
maximumFileSizeToCacheInBytes: 16 * 1024 * 1024,
|
|
1406
|
+
globPatterns: ["**/*.{js,css,html,wasm,data,ico,png,svg,webp,woff,woff2}"],
|
|
1407
|
+
globIgnores: ["**/powerhouse.config.json", "**/*.map"]
|
|
1408
|
+
};
|
|
1409
|
+
const SW_FILENAME = "service-worker.ts";
|
|
1410
|
+
/**
|
|
1411
|
+
* Absolute directory holding the hand-written service-worker source. Resolved
|
|
1412
|
+
* relative to THIS module so it works from source
|
|
1413
|
+
* (connect-utils/vite-plugins → ../service-worker) and from the bundled dist
|
|
1414
|
+
* (dist/index.mjs → ./service-worker, where tsdown copies it). vite-plugin-pwa
|
|
1415
|
+
* resolves `swSrc = path.resolve(root, srcDir, filename)`, and `path.resolve`
|
|
1416
|
+
* ignores `root` when `srcDir` is absolute — so the SW ships with
|
|
1417
|
+
* builder-tools instead of every project needing its own copy.
|
|
1418
|
+
*/
|
|
1419
|
+
function resolveServiceWorkerDir() {
|
|
1420
|
+
const here = dirname(fileURLToPath(import.meta.url));
|
|
1421
|
+
const candidates = [resolve(here, "../service-worker"), resolve(here, "service-worker")];
|
|
1422
|
+
return candidates.find((dir) => existsSync(join(dir, SW_FILENAME))) ?? candidates[0];
|
|
1423
|
+
}
|
|
1424
|
+
/**
|
|
1425
|
+
* Virtual module that feeds the hand-written SW its build-time data. Passed
|
|
1426
|
+
* into vite-plugin-pwa's SEPARATE injectManifest build via
|
|
1427
|
+
* `injectManifest.buildPlugins.vite`: that build runs with `configFile: false`,
|
|
1428
|
+
* so the app's own plugins aren't present, but buildPlugins (and `define`) are.
|
|
1429
|
+
*/
|
|
1430
|
+
function phSwConfigPlugin(data) {
|
|
1431
|
+
const virtualId = "virtual:ph-sw-config";
|
|
1432
|
+
const resolvedId = `\0${virtualId}`;
|
|
1433
|
+
return {
|
|
1434
|
+
name: "ph-sw-config",
|
|
1435
|
+
resolveId(id) {
|
|
1436
|
+
if (id === virtualId) return resolvedId;
|
|
1437
|
+
},
|
|
1438
|
+
load(id) {
|
|
1439
|
+
if (id !== resolvedId) return;
|
|
1440
|
+
return Object.entries(data).map(([key, value]) => `export const ${key} = ${JSON.stringify(value)};`).join("\n");
|
|
1441
|
+
}
|
|
1442
|
+
};
|
|
1443
|
+
}
|
|
1444
|
+
/**
|
|
1155
1445
|
* Service-worker / PWA support for Connect, gated by `connect.app.offline`.
|
|
1156
1446
|
*
|
|
1157
|
-
* When enabled (the default), Workbox `
|
|
1158
|
-
*
|
|
1159
|
-
*
|
|
1160
|
-
*
|
|
1161
|
-
*
|
|
1162
|
-
*
|
|
1447
|
+
* When enabled (the default), Workbox `injectManifest` bundles Connect's
|
|
1448
|
+
* hand-written service worker (../service-worker/service-worker.ts), which
|
|
1449
|
+
* precaches the built app shell so Connect loads with no network, runtime-
|
|
1450
|
+
* caches the Google-hosted Inter font + the registry CDN + the runtime config,
|
|
1451
|
+
* AND serves a dynamic web-app manifest so packages installed AT RUNTIME can
|
|
1452
|
+
* extend it (their fragments are mirrored into IndexedDB by the SPA; the base
|
|
1453
|
+
* the SW merges onto is embedded here at build time). The manifest and precache
|
|
1454
|
+
* config start from the BASE_* defaults above and are extended by `pwa` — the
|
|
1455
|
+
* effective, already-merged overrides from build-time packages and the
|
|
1456
|
+
* project's `connect.pwa` block (see mergePwaConfig). Registration is left to
|
|
1457
|
+
* the Connect SPA (`serviceWorkerManager`, `injectRegister: null`).
|
|
1163
1458
|
*
|
|
1164
1459
|
* When disabled, a self-destroying worker is emitted at the same URL so any
|
|
1165
1460
|
* worker a previous offline-enabled build installed unregisters itself and
|
|
1166
1461
|
* clears its caches on the browser's next service-worker update check.
|
|
1167
1462
|
*/
|
|
1168
1463
|
function connectPwaPlugins(options) {
|
|
1169
|
-
const { offlineEnabled } = options;
|
|
1464
|
+
const { offlineEnabled, pwa } = options;
|
|
1170
1465
|
if (!offlineEnabled) return [VitePWA({
|
|
1171
1466
|
selfDestroying: true,
|
|
1172
1467
|
strategies: "generateSW",
|
|
@@ -1174,102 +1469,29 @@ function connectPwaPlugins(options) {
|
|
|
1174
1469
|
filename: "service-worker.js",
|
|
1175
1470
|
devOptions: { enabled: false }
|
|
1176
1471
|
})];
|
|
1472
|
+
const { manifest, precache } = applyPwaOverrides({
|
|
1473
|
+
manifest: BASE_MANIFEST,
|
|
1474
|
+
precache: BASE_PRECACHE
|
|
1475
|
+
}, pwa ?? {});
|
|
1476
|
+
const swConfig = {
|
|
1477
|
+
EMBEDDED_BASE_MANIFEST: manifest,
|
|
1478
|
+
EXTRA_RUNTIME_CACHING: pwa?.runtimeCaching ?? [],
|
|
1479
|
+
NAVIGATE_FALLBACK_DENYLIST_EXTRA: pwa?.navigateFallbackDenylist ?? []
|
|
1480
|
+
};
|
|
1177
1481
|
return [connectPwaIconsPlugin(), VitePWA({
|
|
1178
|
-
strategies: "
|
|
1482
|
+
strategies: "injectManifest",
|
|
1483
|
+
srcDir: resolveServiceWorkerDir(),
|
|
1484
|
+
filename: SW_FILENAME,
|
|
1179
1485
|
registerType: "prompt",
|
|
1180
1486
|
injectRegister: null,
|
|
1181
|
-
filename: "service-worker.js",
|
|
1182
1487
|
devOptions: { enabled: false },
|
|
1183
1488
|
includeManifestIcons: false,
|
|
1184
|
-
manifest
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
display: "standalone",
|
|
1191
|
-
start_url: ".",
|
|
1192
|
-
scope: ".",
|
|
1193
|
-
icons: [
|
|
1194
|
-
{
|
|
1195
|
-
src: "pwa-192x192.png",
|
|
1196
|
-
sizes: "192x192",
|
|
1197
|
-
type: "image/png"
|
|
1198
|
-
},
|
|
1199
|
-
{
|
|
1200
|
-
src: "pwa-512x512.png",
|
|
1201
|
-
sizes: "512x512",
|
|
1202
|
-
type: "image/png"
|
|
1203
|
-
},
|
|
1204
|
-
{
|
|
1205
|
-
src: "pwa-512x512.png",
|
|
1206
|
-
sizes: "512x512",
|
|
1207
|
-
type: "image/png",
|
|
1208
|
-
purpose: "maskable"
|
|
1209
|
-
}
|
|
1210
|
-
]
|
|
1211
|
-
},
|
|
1212
|
-
workbox: {
|
|
1213
|
-
clientsClaim: true,
|
|
1214
|
-
skipWaiting: false,
|
|
1215
|
-
cleanupOutdatedCaches: true,
|
|
1216
|
-
maximumFileSizeToCacheInBytes: 16 * 1024 * 1024,
|
|
1217
|
-
globPatterns: ["**/*.{js,css,html,wasm,data,ico,png,svg,webp,woff,woff2}"],
|
|
1218
|
-
globIgnores: ["**/powerhouse.config.json", "**/*.map"],
|
|
1219
|
-
navigateFallback: "index.html",
|
|
1220
|
-
navigateFallbackDenylist: [
|
|
1221
|
-
/\/powerhouse\.config\.json$/,
|
|
1222
|
-
/^\/health$/,
|
|
1223
|
-
/\/__/
|
|
1224
|
-
],
|
|
1225
|
-
runtimeCaching: [
|
|
1226
|
-
{
|
|
1227
|
-
urlPattern: ({ url }) => url.origin === "https://fonts.googleapis.com",
|
|
1228
|
-
handler: "StaleWhileRevalidate",
|
|
1229
|
-
options: { cacheName: "google-fonts-stylesheets" }
|
|
1230
|
-
},
|
|
1231
|
-
{
|
|
1232
|
-
urlPattern: ({ url }) => url.origin === "https://fonts.gstatic.com",
|
|
1233
|
-
handler: "CacheFirst",
|
|
1234
|
-
options: {
|
|
1235
|
-
cacheName: "google-fonts-webfonts",
|
|
1236
|
-
expiration: {
|
|
1237
|
-
maxEntries: 30,
|
|
1238
|
-
maxAgeSeconds: 3600 * 24 * 365
|
|
1239
|
-
},
|
|
1240
|
-
cacheableResponse: { statuses: [0, 200] }
|
|
1241
|
-
}
|
|
1242
|
-
},
|
|
1243
|
-
{
|
|
1244
|
-
urlPattern: ({ url }) => url.pathname.includes("/-/cdn/") && (url.pathname.endsWith("/browser/index.js") || url.pathname.endsWith("/style.css") || url.pathname.endsWith("/package.json")),
|
|
1245
|
-
handler: "StaleWhileRevalidate",
|
|
1246
|
-
options: {
|
|
1247
|
-
cacheName: "ph-package-cdn-entry",
|
|
1248
|
-
expiration: {
|
|
1249
|
-
maxEntries: 60,
|
|
1250
|
-
maxAgeSeconds: 3600 * 24 * 30
|
|
1251
|
-
},
|
|
1252
|
-
cacheableResponse: { statuses: [0, 200] }
|
|
1253
|
-
}
|
|
1254
|
-
},
|
|
1255
|
-
{
|
|
1256
|
-
urlPattern: ({ url }) => url.pathname.includes("/-/cdn/"),
|
|
1257
|
-
handler: "CacheFirst",
|
|
1258
|
-
options: {
|
|
1259
|
-
cacheName: "ph-package-cdn",
|
|
1260
|
-
expiration: {
|
|
1261
|
-
maxEntries: 200,
|
|
1262
|
-
maxAgeSeconds: 3600 * 24 * 30
|
|
1263
|
-
},
|
|
1264
|
-
cacheableResponse: { statuses: [0, 200] }
|
|
1265
|
-
}
|
|
1266
|
-
},
|
|
1267
|
-
{
|
|
1268
|
-
urlPattern: ({ url }) => url.pathname.endsWith("/powerhouse.config.json"),
|
|
1269
|
-
handler: "NetworkFirst",
|
|
1270
|
-
options: { cacheName: "ph-runtime-config" }
|
|
1271
|
-
}
|
|
1272
|
-
]
|
|
1489
|
+
manifest,
|
|
1490
|
+
injectManifest: {
|
|
1491
|
+
globPatterns: precache.globPatterns,
|
|
1492
|
+
globIgnores: [...precache.globIgnores, "**/manifest.webmanifest"],
|
|
1493
|
+
maximumFileSizeToCacheInBytes: precache.maximumFileSizeToCacheInBytes,
|
|
1494
|
+
buildPlugins: { vite: [phSwConfigPlugin(swConfig)] }
|
|
1273
1495
|
}
|
|
1274
1496
|
})];
|
|
1275
1497
|
}
|
|
@@ -1650,6 +1872,24 @@ function getConnectBaseViteConfig(options) {
|
|
|
1650
1872
|
warnings: ["@import must precede all other statements (besides @charset or empty @layer)"],
|
|
1651
1873
|
errors: ["Unterminated string literal"]
|
|
1652
1874
|
} });
|
|
1875
|
+
const projectPwa = validateProjectPwaConfig(deepMerge(phConfig.connect?.pwa ?? {}, options.cliConnectOverride?.pwa ?? {}), phConfigPath);
|
|
1876
|
+
const pwaWarn = (msg) => (customLogger ?? console).warn(msg);
|
|
1877
|
+
const pwaPackagesForFragments = mode === "production" ? phPackages : phPackages.filter((p) => p.provider === "local");
|
|
1878
|
+
const pwaPlugins = (async () => {
|
|
1879
|
+
if (!offlineEnabled) return connectPwaPlugins({ offlineEnabled });
|
|
1880
|
+
const contributions = await collectPackagePwaContributions({
|
|
1881
|
+
packages: pwaPackagesForFragments,
|
|
1882
|
+
projectRoot: options.dirname,
|
|
1883
|
+
registryUrl: phPackageRegistryUrl,
|
|
1884
|
+
onWarn: pwaWarn
|
|
1885
|
+
});
|
|
1886
|
+
const projectContribution = collectProjectPwaContribution({ projectRoot: options.dirname });
|
|
1887
|
+
if (projectContribution) contributions.push(projectContribution);
|
|
1888
|
+
return connectPwaPlugins({
|
|
1889
|
+
offlineEnabled,
|
|
1890
|
+
pwa: mergePwaConfig(contributions, projectPwa, pwaWarn)
|
|
1891
|
+
});
|
|
1892
|
+
})();
|
|
1653
1893
|
const reactExternal = [
|
|
1654
1894
|
"react",
|
|
1655
1895
|
"react-dom",
|
|
@@ -1714,7 +1954,7 @@ function getConnectBaseViteConfig(options) {
|
|
|
1714
1954
|
connectFaviconPlugin({ faviconPath: options.favicon }),
|
|
1715
1955
|
connectThemeBootPlugin(),
|
|
1716
1956
|
...options.dynamicBase ? [connectDynamicBasePlugin()] : [],
|
|
1717
|
-
|
|
1957
|
+
pwaPlugins
|
|
1718
1958
|
],
|
|
1719
1959
|
worker: {
|
|
1720
1960
|
format: "es",
|
|
@@ -1724,6 +1964,6 @@ function getConnectBaseViteConfig(options) {
|
|
|
1724
1964
|
};
|
|
1725
1965
|
}
|
|
1726
1966
|
//#endregion
|
|
1727
|
-
export { DEFAULT_CONNECT_OUTDIR, DEFAULT_VENDOR_INCLUDE, DYNAMIC_BASE_PLACEHOLDER, EXTERNAL_PACKAGES_IMPORT, IMPORT_SCRIPT_FILE, LOCAL_PACKAGE_ID, PH_DIR_NAME, RUNTIME_CONFIG_SCHEMA_ID, RUNTIME_CONFIG_SCHEMA_URL, THEME_BOOT_MARKER, VENDOR_EXTERNAL, VENDOR_URL_PREFIX, appendToHtmlHead, backupIndexHtml, connectDynamicBasePlugin, connectThemeBootPlugin, copyConnect, ensureNodeVersion, getConnectBaseViteConfig, getConnectHtmlTags, makeImportScriptFromPackages, phConfigPlugin, prebuildConnectVendor, prependToHtmlHead, readJsonFile, removeBase64EnvValues, resolveConnectBundle, resolveConnectPackageJson, resolveConnectPublicDir, resolvePackage, resolveViteConfigPath, runShellScriptPlugin, runTsc, runtimeConfigSchema, stripVersionFromPackage };
|
|
1967
|
+
export { DEFAULT_CONNECT_OUTDIR, DEFAULT_VENDOR_INCLUDE, DYNAMIC_BASE_PLACEHOLDER, EXTERNAL_PACKAGES_IMPORT, IMPORT_SCRIPT_FILE, LOCAL_PACKAGE_ID, PH_DIR_NAME, RUNTIME_CONFIG_SCHEMA_ID, RUNTIME_CONFIG_SCHEMA_URL, THEME_BOOT_MARKER, VENDOR_EXTERNAL, VENDOR_URL_PREFIX, appendToHtmlHead, backupIndexHtml, connectDynamicBasePlugin, connectThemeBootPlugin, copyConnect, ensureNodeVersion, escapeForRegExp, getConnectBaseViteConfig, getConnectHtmlTags, makeImportScriptFromPackages, phConfigPlugin, prebuildConnectVendor, prependToHtmlHead, readJsonFile, removeBase64EnvValues, resolveConnectBundle, resolveConnectPackageJson, resolveConnectPublicDir, resolvePackage, resolveViteConfigPath, runShellScriptPlugin, runTsc, runtimeConfigSchema, stripVersionFromPackage };
|
|
1728
1968
|
|
|
1729
1969
|
//# sourceMappingURL=index.mjs.map
|