@serwist/vite 9.0.0-preview.2 → 9.0.0-preview.20
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/schema.js +56 -0
- package/dist/index.d.ts +8 -7
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +360 -11
- package/dist/index.schema.d.ts +3 -0
- package/dist/index.schema.d.ts.map +1 -0
- package/dist/index.schema.js +3 -0
- package/dist/index.worker.d.ts +6 -0
- package/dist/index.worker.d.ts.map +1 -1
- package/dist/index.worker.js +84 -65
- package/dist/lib/api.d.ts.map +1 -0
- package/dist/lib/constants.d.ts.map +1 -0
- package/dist/{context.d.ts → lib/context.d.ts} +3 -3
- package/dist/lib/context.d.ts.map +1 -0
- package/dist/lib/log.d.ts.map +1 -0
- package/dist/{modules.d.ts → lib/modules.d.ts} +1 -1
- package/dist/lib/modules.d.ts.map +1 -0
- package/dist/lib/options.d.ts +4 -0
- package/dist/lib/options.d.ts.map +1 -0
- package/dist/lib/schema.d.ts +359 -0
- package/dist/lib/schema.d.ts.map +1 -0
- package/dist/lib/types.d.ts +127 -0
- package/dist/lib/types.d.ts.map +1 -0
- package/dist/lib/utils.d.ts.map +1 -0
- package/dist/lib/validator.d.ts +3 -0
- package/dist/lib/validator.d.ts.map +1 -0
- package/dist/plugins/build.d.ts +2 -2
- package/dist/plugins/build.d.ts.map +1 -1
- package/dist/plugins/dev.d.ts +2 -2
- package/dist/plugins/dev.d.ts.map +1 -1
- package/dist/plugins/main.d.ts +2 -2
- package/dist/plugins/main.d.ts.map +1 -1
- package/package.json +22 -62
- package/src/index.schema.ts +3 -0
- package/src/index.ts +8 -7
- package/src/index.worker.ts +111 -85
- package/src/{context.ts → lib/context.ts} +3 -3
- package/src/{log.ts → lib/log.ts} +1 -1
- package/src/{modules.ts → lib/modules.ts} +5 -6
- package/src/lib/options.ts +76 -0
- package/src/lib/schema.ts +35 -0
- package/src/lib/types.ts +163 -0
- package/src/lib/validator.ts +10 -0
- package/src/plugins/build.ts +2 -2
- package/src/plugins/dev.ts +3 -3
- package/src/plugins/main.ts +5 -5
- package/src/rollup.js +3 -24
- package/dist/api.d.ts.map +0 -1
- package/dist/assets.d.ts +0 -4
- package/dist/assets.d.ts.map +0 -1
- package/dist/constants.d.ts.map +0 -1
- package/dist/context.d.ts.map +0 -1
- package/dist/integration/svelte/build.d.ts +0 -5
- package/dist/integration/svelte/build.d.ts.map +0 -1
- package/dist/integration/svelte/config.d.ts +0 -9
- package/dist/integration/svelte/config.d.ts.map +0 -1
- package/dist/integration/svelte/index.d.ts +0 -10
- package/dist/integration/svelte/index.d.ts.map +0 -1
- package/dist/integration/svelte/index.js +0 -145
- package/dist/integration/svelte/types.d.ts +0 -24
- package/dist/integration/svelte/types.d.ts.map +0 -1
- package/dist/log.d.ts.map +0 -1
- package/dist/main.js +0 -470
- package/dist/modules.d.ts.map +0 -1
- package/dist/options.d.ts +0 -4
- package/dist/options.d.ts.map +0 -1
- package/dist/types.d.ts +0 -191
- package/dist/types.d.ts.map +0 -1
- package/dist/utils-types.d.ts +0 -2
- package/dist/utils-types.d.ts.map +0 -1
- package/dist/utils.d.ts.map +0 -1
- package/src/assets.ts +0 -76
- package/src/integration/svelte/build.ts +0 -21
- package/src/integration/svelte/config.ts +0 -141
- package/src/integration/svelte/index.ts +0 -27
- package/src/integration/svelte/types.ts +0 -26
- package/src/options.ts +0 -90
- package/src/types.ts +0 -207
- package/src/utils-types.ts +0 -1
- /package/dist/{api.d.ts → lib/api.d.ts} +0 -0
- /package/dist/{constants.d.ts → lib/constants.d.ts} +0 -0
- /package/dist/{log.d.ts → lib/log.d.ts} +0 -0
- /package/dist/{utils.d.ts → lib/utils.d.ts} +0 -0
- /package/src/{api.ts → lib/api.ts} +0 -0
- /package/src/{constants.ts → lib/constants.ts} +0 -0
- /package/src/{utils.ts → lib/utils.ts} +0 -0
package/dist/utils.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AAGA,eAAO,MAAM,KAAK,QAAS,MAAM,WAEhC,CAAC;AAEF,eAAO,MAAM,eAAe,SAAU,MAAM,WAG3C,CAAC;AAEF,eAAO,MAAM,UAAU,QAAS,MAAM,4BAErC,CAAC;AAEF,eAAO,MAAM,aAAa,SAAU,MAAM,WAEzC,CAAC;AAIF;;;;;;;;GAQG;AACH,eAAO,MAAM,YAAY,UAAW,MAAM,KAAG,MAAM,GAAG,IAqBrD,CAAC;AAIF;;;;;;;GAOG;AACH,eAAO,MAAM,IAAI,QAAS,MAAM,WAI/B,CAAC"}
|
package/src/assets.ts
DELETED
|
@@ -1,76 +0,0 @@
|
|
|
1
|
-
import crypto from "node:crypto";
|
|
2
|
-
import fs from "node:fs";
|
|
3
|
-
import { resolve as resolveFs } from "node:path";
|
|
4
|
-
|
|
5
|
-
import type { InjectManifestOptions, ManifestEntry } from "@serwist/build";
|
|
6
|
-
import fg from "fast-glob";
|
|
7
|
-
import type { ResolvedConfig } from "vite";
|
|
8
|
-
|
|
9
|
-
import type { ResolvedPluginOptions } from "./types.js";
|
|
10
|
-
|
|
11
|
-
const buildManifestEntry = (publicDir: string, url: string): Promise<ManifestEntry> => {
|
|
12
|
-
return new Promise((resolve, reject) => {
|
|
13
|
-
const cHash = crypto.createHash("MD5");
|
|
14
|
-
const stream = fs.createReadStream(resolveFs(publicDir, url));
|
|
15
|
-
stream.on("error", (err) => {
|
|
16
|
-
reject(err);
|
|
17
|
-
});
|
|
18
|
-
stream.on("data", (chunk) => {
|
|
19
|
-
cHash.update(chunk);
|
|
20
|
-
});
|
|
21
|
-
stream.on("end", () => {
|
|
22
|
-
return resolve({
|
|
23
|
-
url,
|
|
24
|
-
revision: `${cHash.digest("hex")}`,
|
|
25
|
-
});
|
|
26
|
-
});
|
|
27
|
-
});
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
const lookupAdditionalPrecacheEntries = (serwistOptions: Partial<InjectManifestOptions>): (string | ManifestEntry)[] => {
|
|
31
|
-
return serwistOptions.additionalPrecacheEntries || [];
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
// we need to make icons relative, we can have for example icon entries with: /pwa.png
|
|
35
|
-
// fast-glob will not resolve absolute paths
|
|
36
|
-
const normalizeIconPath = (path: string) => {
|
|
37
|
-
return path.startsWith("/") ? path.substring(1) : path;
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
export const configureStaticAssets = async (resolvedPluginOptions: ResolvedPluginOptions, viteConfig: ResolvedConfig) => {
|
|
41
|
-
const { injectManifest, includeAssets } = resolvedPluginOptions;
|
|
42
|
-
|
|
43
|
-
const { publicDir } = viteConfig;
|
|
44
|
-
const globs: string[] = [];
|
|
45
|
-
const manifestEntries: (string | ManifestEntry)[] = lookupAdditionalPrecacheEntries(injectManifest);
|
|
46
|
-
if (includeAssets) {
|
|
47
|
-
// we need to make icons relative, we can have for example icon entries with: /pwa.png
|
|
48
|
-
// fast-glob will not resolve absolute paths
|
|
49
|
-
if (Array.isArray(includeAssets)) globs.push(...includeAssets.map(normalizeIconPath));
|
|
50
|
-
else globs.push(normalizeIconPath(includeAssets));
|
|
51
|
-
}
|
|
52
|
-
if (globs.length > 0) {
|
|
53
|
-
let assets = await fg(globs, {
|
|
54
|
-
cwd: publicDir,
|
|
55
|
-
onlyFiles: true,
|
|
56
|
-
unique: true,
|
|
57
|
-
});
|
|
58
|
-
// we also need to remove from the list existing included by the user
|
|
59
|
-
if (manifestEntries.length > 0) {
|
|
60
|
-
const included = manifestEntries.map((me) => {
|
|
61
|
-
if (typeof me === "string") return me;
|
|
62
|
-
return me.url;
|
|
63
|
-
});
|
|
64
|
-
assets = assets.filter((a) => !included.includes(a));
|
|
65
|
-
}
|
|
66
|
-
const assetsEntries = await Promise.all(
|
|
67
|
-
assets.map((a) => {
|
|
68
|
-
return buildManifestEntry(publicDir, a);
|
|
69
|
-
}),
|
|
70
|
-
);
|
|
71
|
-
manifestEntries.push(...assetsEntries);
|
|
72
|
-
}
|
|
73
|
-
if (manifestEntries.length > 0) {
|
|
74
|
-
injectManifest.additionalPrecacheEntries = manifestEntries;
|
|
75
|
-
}
|
|
76
|
-
};
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import type { Plugin } from "vite";
|
|
2
|
-
|
|
3
|
-
import type { SerwistViteContext } from "../../context.js";
|
|
4
|
-
import type { SerwistViteApi } from "../../types.js";
|
|
5
|
-
|
|
6
|
-
export const buildPlugin = (ctx: SerwistViteContext, api: SerwistViteApi) => {
|
|
7
|
-
return <Plugin>{
|
|
8
|
-
name: "@serwist/vite/integration-svelte:build",
|
|
9
|
-
apply: "build",
|
|
10
|
-
enforce: "pre",
|
|
11
|
-
closeBundle: {
|
|
12
|
-
sequential: true,
|
|
13
|
-
enforce: "pre",
|
|
14
|
-
async handler() {
|
|
15
|
-
if (api && !api.disabled && ctx.viteConfig.build.ssr) {
|
|
16
|
-
await api.generateSW();
|
|
17
|
-
}
|
|
18
|
-
},
|
|
19
|
-
},
|
|
20
|
-
};
|
|
21
|
-
};
|
|
@@ -1,141 +0,0 @@
|
|
|
1
|
-
import crypto from "node:crypto";
|
|
2
|
-
import os from "node:os";
|
|
3
|
-
import path from "node:path";
|
|
4
|
-
|
|
5
|
-
import type { ManifestTransform } from "@serwist/build";
|
|
6
|
-
import { errors } from "@serwist/build";
|
|
7
|
-
import type { ResolvedConfig } from "vite";
|
|
8
|
-
|
|
9
|
-
import type { PluginOptions as BasePluginOptions } from "../../types.js";
|
|
10
|
-
import { resolveEntry } from "../../utils.js";
|
|
11
|
-
import type { KitOptions } from "./types.js";
|
|
12
|
-
|
|
13
|
-
export const configurateSvelteKitOptions = (viteConfig: ResolvedConfig, kit: KitOptions, options: BasePluginOptions) => {
|
|
14
|
-
const clientOutDir = path.resolve(viteConfig.root, viteConfig.build.outDir, "../client");
|
|
15
|
-
|
|
16
|
-
// Kit fixes the service worker's name to 'service-worker.js'
|
|
17
|
-
if (viteConfig.isProduction) {
|
|
18
|
-
options.swSrc = path.resolve(clientOutDir, "service-worker.js");
|
|
19
|
-
options.swDest = path.resolve(clientOutDir, "service-worker.js");
|
|
20
|
-
} else {
|
|
21
|
-
const swSrc = resolveEntry(path.join(viteConfig.root, kit.files?.serviceWorker ?? "src/service-worker"));
|
|
22
|
-
if (swSrc) {
|
|
23
|
-
options.swSrc = swSrc;
|
|
24
|
-
options.swDest = path.join(os.tmpdir(), `serwist-vite-integration-svelte-${crypto.randomUUID()}.js`);
|
|
25
|
-
} else {
|
|
26
|
-
throw new Error(errors["invalid-sw-src"]);
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
options.swUrl = "/service-worker.js";
|
|
30
|
-
|
|
31
|
-
// SvelteKit's outDir is `.svelte-kit/output/client`.
|
|
32
|
-
// We need to include the parent folder in globDirectory since SvelteKit will generate SSG in `.svelte-kit/output/prerendered` folder.
|
|
33
|
-
if (!options.globDirectory) {
|
|
34
|
-
options.globDirectory = path.resolve(clientOutDir, "..");
|
|
35
|
-
}
|
|
36
|
-
if (!options.manifestTransforms) {
|
|
37
|
-
options.manifestTransforms = [createManifestTransform(viteConfig.base, undefined, kit)];
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
let buildAssetsDir = kit.appDir ?? "_app/";
|
|
41
|
-
if (buildAssetsDir[0] === "/") buildAssetsDir = buildAssetsDir.slice(1);
|
|
42
|
-
if (buildAssetsDir[buildAssetsDir.length - 1] !== "/") buildAssetsDir += "/";
|
|
43
|
-
|
|
44
|
-
if (!options.modifyURLPrefix) {
|
|
45
|
-
options.globPatterns = buildGlobPatterns(options.globPatterns);
|
|
46
|
-
if (kit.includeVersionFile) {
|
|
47
|
-
options.globPatterns.push(`client/${buildAssetsDir}version.json`);
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
// Exclude server assets: sw is built on SSR build
|
|
52
|
-
options.globIgnores = buildGlobIgnores(options.globIgnores);
|
|
53
|
-
|
|
54
|
-
// Vite 5 support: allow override dontCacheBustURLsMatching
|
|
55
|
-
if (!("dontCacheBustURLsMatching" in options)) {
|
|
56
|
-
options.dontCacheBustURLsMatching = new RegExp(`${buildAssetsDir}immutable/`);
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
if (!options.injectionPoint) {
|
|
60
|
-
options.injectionPoint = "self.__SW_MANIFEST";
|
|
61
|
-
}
|
|
62
|
-
};
|
|
63
|
-
|
|
64
|
-
export function createManifestTransform(base: string, webManifestName?: string, options?: KitOptions): ManifestTransform {
|
|
65
|
-
return async (entries) => {
|
|
66
|
-
const defaultAdapterFallback = "prerendered/fallback.html";
|
|
67
|
-
const suffix = options?.trailingSlash === "always" ? "/" : "";
|
|
68
|
-
let adapterFallback = options?.adapterFallback;
|
|
69
|
-
let excludeFallback = false;
|
|
70
|
-
// the fallback will be always generated by SvelteKit.
|
|
71
|
-
// The adapter will copy the fallback only if it is provided in its options: we need to exclude it
|
|
72
|
-
if (!adapterFallback) {
|
|
73
|
-
adapterFallback = defaultAdapterFallback;
|
|
74
|
-
excludeFallback = true;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
// the fallback will be always in .svelte-kit/output/prerendered/fallback.html
|
|
78
|
-
const manifest = entries
|
|
79
|
-
.filter(({ url }) => !(excludeFallback && url === defaultAdapterFallback))
|
|
80
|
-
.map((e) => {
|
|
81
|
-
let url = e.url;
|
|
82
|
-
// client assets in `.svelte-kit/output/client` folder.
|
|
83
|
-
// SSG pages in `.svelte-kit/output/prerendered/pages` folder.
|
|
84
|
-
// fallback page in `.svelte-kit/output/prerendered` folder (fallback.html is the default).
|
|
85
|
-
if (url.startsWith("client/")) url = url.slice(7);
|
|
86
|
-
else if (url.startsWith("prerendered/pages/")) url = url.slice(18);
|
|
87
|
-
else if (url === defaultAdapterFallback) url = adapterFallback!;
|
|
88
|
-
|
|
89
|
-
if (url.endsWith(".html")) {
|
|
90
|
-
if (url.startsWith("/")) url = url.slice(1);
|
|
91
|
-
|
|
92
|
-
if (url === "index.html") {
|
|
93
|
-
url = base;
|
|
94
|
-
} else {
|
|
95
|
-
const idx = url.lastIndexOf("/");
|
|
96
|
-
if (idx > -1) {
|
|
97
|
-
// abc/index.html -> abc/?
|
|
98
|
-
if (url.endsWith("/index.html")) url = `${url.slice(0, idx)}${suffix}`;
|
|
99
|
-
// abc/def.html -> abc/def/?
|
|
100
|
-
else url = `${url.substring(0, url.lastIndexOf("."))}${suffix}`;
|
|
101
|
-
} else {
|
|
102
|
-
// xxx.html -> xxx/?
|
|
103
|
-
url = `${url.substring(0, url.lastIndexOf("."))}${suffix}`;
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
e.url = url;
|
|
109
|
-
|
|
110
|
-
return e;
|
|
111
|
-
});
|
|
112
|
-
|
|
113
|
-
if (!webManifestName) return { manifest };
|
|
114
|
-
|
|
115
|
-
return { manifest: manifest.filter((e) => e.url !== webManifestName) };
|
|
116
|
-
};
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
export function buildGlobPatterns(globPatterns?: string[]) {
|
|
120
|
-
if (globPatterns) {
|
|
121
|
-
if (!globPatterns.some((g) => g.startsWith("prerendered/"))) globPatterns.push("prerendered/**/*.html");
|
|
122
|
-
|
|
123
|
-
if (!globPatterns.some((g) => g.startsWith("client/"))) globPatterns.push("client/**/*.{js,css,ico,png,svg,webp,webmanifest}");
|
|
124
|
-
|
|
125
|
-
if (!globPatterns.some((g) => g.includes("webmanifest"))) globPatterns.push("client/*.webmanifest");
|
|
126
|
-
|
|
127
|
-
return globPatterns;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
return ["client/**/*.{js,css,ico,png,svg,webp,webmanifest}", "prerendered/**/*.html"];
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
export function buildGlobIgnores(globIgnores?: string[]) {
|
|
134
|
-
if (globIgnores) {
|
|
135
|
-
if (!globIgnores.some((g) => g.startsWith("server/"))) globIgnores.push("server/*.*");
|
|
136
|
-
|
|
137
|
-
return globIgnores;
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
return ["server/*.*"];
|
|
141
|
-
}
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import type { Plugin } from "vite";
|
|
2
|
-
|
|
3
|
-
import { createApi } from "../../api.js";
|
|
4
|
-
import { createContext } from "../../context.js";
|
|
5
|
-
import { devPlugin } from "../../plugins/dev.js";
|
|
6
|
-
import { mainPlugin } from "../../plugins/main.js";
|
|
7
|
-
import type { PluginOptions as BasePluginOptions } from "../../types.js";
|
|
8
|
-
import { buildPlugin } from "./build.js";
|
|
9
|
-
import { configurateSvelteKitOptions } from "./config.js";
|
|
10
|
-
import type { PluginOptions } from "./types.js";
|
|
11
|
-
|
|
12
|
-
// TODO: handle SvelteKit build errors.
|
|
13
|
-
/**
|
|
14
|
-
* Integrates Serwist into your SvelteKit app.
|
|
15
|
-
* @param userOptions
|
|
16
|
-
* @returns
|
|
17
|
-
*/
|
|
18
|
-
export const serwist = (userOptions: PluginOptions = {}): Plugin[] => {
|
|
19
|
-
if (!userOptions.integration) userOptions.integration = {};
|
|
20
|
-
userOptions.integration.closeBundleOrder = "pre";
|
|
21
|
-
userOptions.integration.configureOptions = (viteConfig, options) => configurateSvelteKitOptions(viteConfig, userOptions.kit ?? {}, options);
|
|
22
|
-
const ctx = createContext(userOptions as BasePluginOptions, "sveltekit");
|
|
23
|
-
const api = createApi(ctx);
|
|
24
|
-
return [mainPlugin(ctx, api), devPlugin(ctx, api), buildPlugin(ctx, api)];
|
|
25
|
-
};
|
|
26
|
-
|
|
27
|
-
export * from "./types.js";
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import type { KitConfig } from "@sveltejs/kit";
|
|
2
|
-
|
|
3
|
-
import type { PluginOptions as BasePluginOptions } from "../../types.js";
|
|
4
|
-
import type { Optional } from "../../utils-types.js";
|
|
5
|
-
|
|
6
|
-
export interface KitOptions extends Pick<KitConfig, "appDir" | "files"> {
|
|
7
|
-
/**
|
|
8
|
-
* @see https://kit.svelte.dev/docs/adapter-static#options-fallback
|
|
9
|
-
*/
|
|
10
|
-
adapterFallback?: string;
|
|
11
|
-
/**
|
|
12
|
-
* `trailingSlash` in `+page.{ts,js}` or `+layout.{ts,js}` files.
|
|
13
|
-
* @default "never"
|
|
14
|
-
*/
|
|
15
|
-
trailingSlash?: "never" | "always" | "ignore";
|
|
16
|
-
/**
|
|
17
|
-
* Should `"${appDir}/version.json"` be included in the service worker's precache manifest?
|
|
18
|
-
*
|
|
19
|
-
* @default false
|
|
20
|
-
*/
|
|
21
|
-
includeVersionFile?: boolean;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export interface PluginOptions extends Optional<Omit<BasePluginOptions, "swSrc" | "swDest" | "swUrl">, "globDirectory"> {
|
|
25
|
-
kit?: KitOptions;
|
|
26
|
-
}
|
package/src/options.ts
DELETED
|
@@ -1,90 +0,0 @@
|
|
|
1
|
-
import path from "node:path";
|
|
2
|
-
import process from "node:process";
|
|
3
|
-
|
|
4
|
-
import type { ResolvedConfig } from "vite";
|
|
5
|
-
|
|
6
|
-
import { configureStaticAssets } from "./assets.js";
|
|
7
|
-
import { loadSerwistBuild } from "./modules.js";
|
|
8
|
-
import type { PluginOptions, ResolvedPluginOptions } from "./types.js";
|
|
9
|
-
import { resolveBasePath, slash } from "./utils.js";
|
|
10
|
-
|
|
11
|
-
export const resolveOptions = async (options: PluginOptions, viteConfig: ResolvedConfig): Promise<ResolvedPluginOptions> => {
|
|
12
|
-
const {
|
|
13
|
-
type = "classic",
|
|
14
|
-
mode = (process.env.NODE_ENV || "production") as "production" | "development",
|
|
15
|
-
injectRegister = "auto",
|
|
16
|
-
registerType = "prompt",
|
|
17
|
-
minify = true,
|
|
18
|
-
base = viteConfig.base,
|
|
19
|
-
scope: _scope,
|
|
20
|
-
swUrl = "/sw.js",
|
|
21
|
-
includeAssets = undefined,
|
|
22
|
-
useCredentials = false,
|
|
23
|
-
disable = false,
|
|
24
|
-
integration = {},
|
|
25
|
-
buildBase,
|
|
26
|
-
devOptions,
|
|
27
|
-
plugins = [],
|
|
28
|
-
rollupOptions = {},
|
|
29
|
-
rollupFormat = "es",
|
|
30
|
-
...injectManifest
|
|
31
|
-
} = options;
|
|
32
|
-
|
|
33
|
-
const basePath = resolveBasePath(base);
|
|
34
|
-
// check typescript service worker for injectManifest strategy
|
|
35
|
-
const scope = _scope || basePath;
|
|
36
|
-
|
|
37
|
-
let assetsDir = slash(viteConfig.build.assetsDir ?? "assets");
|
|
38
|
-
if (assetsDir[assetsDir.length - 1] !== "/") assetsDir += "/";
|
|
39
|
-
|
|
40
|
-
const resolvedDevOptions: ResolvedPluginOptions["devOptions"] = {
|
|
41
|
-
bundle: true,
|
|
42
|
-
minify: false,
|
|
43
|
-
...devOptions,
|
|
44
|
-
};
|
|
45
|
-
|
|
46
|
-
// remove './' prefix from assetsDir
|
|
47
|
-
const dontCacheBustURLsMatching = new RegExp(`^${assetsDir.replace(/^\.*?\//, "")}`);
|
|
48
|
-
|
|
49
|
-
const { validateViteInjectManifestOptions } = await loadSerwistBuild();
|
|
50
|
-
|
|
51
|
-
const validatedInjectManifest = await validateViteInjectManifestOptions(injectManifest);
|
|
52
|
-
|
|
53
|
-
const { swSrc, swDest, ...userInjectManifest } = validatedInjectManifest || {};
|
|
54
|
-
|
|
55
|
-
const resolvedPluginOptions = {
|
|
56
|
-
base: basePath,
|
|
57
|
-
type,
|
|
58
|
-
mode,
|
|
59
|
-
injectRegister,
|
|
60
|
-
registerType,
|
|
61
|
-
useCredentials,
|
|
62
|
-
swUrl,
|
|
63
|
-
injectManifest: {
|
|
64
|
-
dontCacheBustURLsMatching,
|
|
65
|
-
...userInjectManifest,
|
|
66
|
-
swSrc: path.resolve(viteConfig.root, swSrc),
|
|
67
|
-
swDest: path.resolve(viteConfig.root, viteConfig.build.outDir, swDest),
|
|
68
|
-
disablePrecacheManifest: !viteConfig.isProduction,
|
|
69
|
-
},
|
|
70
|
-
scope,
|
|
71
|
-
minify,
|
|
72
|
-
includeAssets,
|
|
73
|
-
disable,
|
|
74
|
-
integration,
|
|
75
|
-
buildBase: buildBase ?? basePath,
|
|
76
|
-
injectManifestRollupOptions: {
|
|
77
|
-
plugins,
|
|
78
|
-
rollupOptions,
|
|
79
|
-
format: rollupFormat,
|
|
80
|
-
},
|
|
81
|
-
devOptions: resolvedDevOptions,
|
|
82
|
-
} satisfies ResolvedPluginOptions;
|
|
83
|
-
|
|
84
|
-
// calculate hash only when required
|
|
85
|
-
const calculateHash = !resolvedPluginOptions.disable && resolvedPluginOptions.includeAssets && viteConfig.command === "build";
|
|
86
|
-
|
|
87
|
-
if (calculateHash) await configureStaticAssets(resolvedPluginOptions, viteConfig);
|
|
88
|
-
|
|
89
|
-
return resolvedPluginOptions;
|
|
90
|
-
};
|
package/src/types.ts
DELETED
|
@@ -1,207 +0,0 @@
|
|
|
1
|
-
import type { ManifestEntry, ViteInjectManifestOptions } from "@serwist/build";
|
|
2
|
-
import type { RollupOptions } from "rollup";
|
|
3
|
-
import type { BuildOptions, Plugin, ResolvedConfig } from "vite";
|
|
4
|
-
|
|
5
|
-
export type InjectManifestVitePlugins = string[] | ((vitePluginIds: string[]) => string[]);
|
|
6
|
-
export interface CustomInjectManifestOptions extends Omit<ViteInjectManifestOptions, "disablePrecacheManifest"> {
|
|
7
|
-
/**
|
|
8
|
-
* The URL to the service worker.
|
|
9
|
-
* @default "/sw.js"
|
|
10
|
-
*/
|
|
11
|
-
swUrl?: string;
|
|
12
|
-
/**
|
|
13
|
-
* Configure the format to use in the Rollup build.
|
|
14
|
-
*
|
|
15
|
-
* @default 'es'
|
|
16
|
-
*/
|
|
17
|
-
rollupFormat?: "es" | "iife";
|
|
18
|
-
/**
|
|
19
|
-
* Since `v0.15.0` you can add plugins to build your service worker.
|
|
20
|
-
*
|
|
21
|
-
* When using `injectManifest` there are 2 builds, your application and the service worker.
|
|
22
|
-
* If you're using custom configuration for your service worker (for example custom plugins) you can use this option to configure the service worker build.
|
|
23
|
-
* Both configurations cannot be shared, and so you'll need to duplicate the configuration, with the exception of `define`.
|
|
24
|
-
*
|
|
25
|
-
* **WARN**: this option is for advanced usage, be aware that you may break your application build.
|
|
26
|
-
*/
|
|
27
|
-
plugins?: Plugin[];
|
|
28
|
-
/**
|
|
29
|
-
* Since `v0.15.0` you can add custom Rollup options to build your service worker: we expose the same configuration to build a worker using Vite.
|
|
30
|
-
*/
|
|
31
|
-
rollupOptions?: Omit<RollupOptions, "plugins" | "output">;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
export interface SerwistViteHooks {
|
|
35
|
-
beforeBuildServiceWorker?: (options: ResolvedPluginOptions) => void | Promise<void>;
|
|
36
|
-
closeBundleOrder?: "pre" | "post" | null;
|
|
37
|
-
configureOptions?: (viteOptions: ResolvedConfig, options: PluginOptions) => void | Promise<void>;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
export interface DevOptions {
|
|
41
|
-
/**
|
|
42
|
-
* Whether the service worker should be bundled in development mode.
|
|
43
|
-
*
|
|
44
|
-
* [Many browsers still do not support ES Modules in service workers.](https://caniuse.com/mdn-api_serviceworker_ecmascript_modules) However, in development
|
|
45
|
-
* mode, certain frameworks, such as SvelteKit, do not bundle the service worker. As a result, trying to register that service worker on browsers lacking
|
|
46
|
-
* support, such as Firefox or Safari, will fail, but doing so on browsers not lacking support will not fail. This option is provided to prevent that from
|
|
47
|
-
* happening. What the plugin does is intercepting any request to the service worker (requests for `swUrl`) and returning a bundled one.
|
|
48
|
-
*/
|
|
49
|
-
bundle?: boolean;
|
|
50
|
-
minify?: BuildOptions["minify"];
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* Plugin options.
|
|
55
|
-
*/
|
|
56
|
-
export interface BasePluginOptions {
|
|
57
|
-
/**
|
|
58
|
-
* Build mode
|
|
59
|
-
*
|
|
60
|
-
* @default
|
|
61
|
-
* process.env.NODE_ENV // or "production" if undefined
|
|
62
|
-
*/
|
|
63
|
-
mode?: "development" | "production";
|
|
64
|
-
/**
|
|
65
|
-
* The service worker type.
|
|
66
|
-
*
|
|
67
|
-
* @default "classic"
|
|
68
|
-
*/
|
|
69
|
-
type?: WorkerType;
|
|
70
|
-
/**
|
|
71
|
-
* The scope to register the Service Worker
|
|
72
|
-
*
|
|
73
|
-
* @default `viteOptions.base`
|
|
74
|
-
*/
|
|
75
|
-
scope?: string;
|
|
76
|
-
/**
|
|
77
|
-
* Inject the service worker register inlined in the index.html
|
|
78
|
-
*
|
|
79
|
-
* If set to "auto", depends on whether you used the `import { registerSW } from 'virtual:pwa-register'`
|
|
80
|
-
* it will do nothing or use the `script` mode
|
|
81
|
-
*
|
|
82
|
-
* `"inline"` - inject a simple register, inlined with the generated html
|
|
83
|
-
*
|
|
84
|
-
* `"script"` - inject `<script/>` in `<head>` with `src` attribute to a generated script to register the service worker
|
|
85
|
-
*
|
|
86
|
-
* `"script-defer"` - inject `<script defer />` in `<head>`, with `src` attribute to a generated script to register the service worker
|
|
87
|
-
*
|
|
88
|
-
* `null` - do nothing. You will need to register the service worker yourself or import `registerSW` from `virtual:pwa-register`.
|
|
89
|
-
*
|
|
90
|
-
* @default "auto"
|
|
91
|
-
*/
|
|
92
|
-
injectRegister: "inline" | "script" | "script-defer" | "auto" | null | false;
|
|
93
|
-
/**
|
|
94
|
-
* Mode for the virtual register.
|
|
95
|
-
* This is NOT available if `injectRegister` is set to `"inline"` or `"script"`
|
|
96
|
-
*
|
|
97
|
-
* `"prompt"` - you will need to show a popup/dialog to the user to confirm the reload.
|
|
98
|
-
*
|
|
99
|
-
* `"autoUpdate"` - when new content is available, the new service worker will update caches and reload all browser
|
|
100
|
-
* windows/tabs with the application open automatically, it must take the control for the application to work
|
|
101
|
-
* properly.
|
|
102
|
-
*
|
|
103
|
-
* @default "prompt"
|
|
104
|
-
*/
|
|
105
|
-
registerType?: "prompt" | "autoUpdate";
|
|
106
|
-
/**
|
|
107
|
-
* Minify the generated manifest
|
|
108
|
-
*
|
|
109
|
-
* @default true
|
|
110
|
-
*/
|
|
111
|
-
minify: boolean;
|
|
112
|
-
/**
|
|
113
|
-
* Whether to add the `crossorigin="use-credentials"` attribute to `<link rel="manifest">`
|
|
114
|
-
* @default false
|
|
115
|
-
*/
|
|
116
|
-
useCredentials?: boolean;
|
|
117
|
-
/**
|
|
118
|
-
* Override Vite's base options for `@serwist/vite`.
|
|
119
|
-
*
|
|
120
|
-
* @default viteOptions.base
|
|
121
|
-
*/
|
|
122
|
-
base?: string;
|
|
123
|
-
/**
|
|
124
|
-
* `public` resources to be added to the PWA manifest.
|
|
125
|
-
*
|
|
126
|
-
* You don't need to add `manifest` icons here, it will be auto included.
|
|
127
|
-
*
|
|
128
|
-
* The `public` directory will be resolved from Vite's `publicDir` option directory.
|
|
129
|
-
*/
|
|
130
|
-
includeAssets: string | string[] | undefined;
|
|
131
|
-
/**
|
|
132
|
-
* Whether Serwist should be disabled.
|
|
133
|
-
*
|
|
134
|
-
* @default false
|
|
135
|
-
*/
|
|
136
|
-
disable: boolean;
|
|
137
|
-
/**
|
|
138
|
-
* `@serwist/vite` integration.
|
|
139
|
-
*/
|
|
140
|
-
integration?: SerwistViteHooks;
|
|
141
|
-
/**
|
|
142
|
-
* When Vite's build folder is not the same as your base root folder, configure it here.
|
|
143
|
-
*
|
|
144
|
-
* This option will be useful for integrations like `vite-plugin-laravel` where Vite's build folder is `public/build` but Laravel's base path is `public`.
|
|
145
|
-
*
|
|
146
|
-
* This option will be used to configure the path for the service worker, "registerSW.js" and the web manifest assets.
|
|
147
|
-
*
|
|
148
|
-
* For example, if your base path is `/`, then, in your Laravel PWA configuration use `buildPath: '/build/'`.
|
|
149
|
-
*
|
|
150
|
-
* By default: `vite.base`.
|
|
151
|
-
*/
|
|
152
|
-
buildBase?: string;
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
export interface PluginOptions extends Partial<BasePluginOptions>, CustomInjectManifestOptions {
|
|
156
|
-
devOptions?: DevOptions;
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
export interface InjectManifestRollupOptions {
|
|
160
|
-
format: "es" | "iife";
|
|
161
|
-
plugins: Plugin[];
|
|
162
|
-
rollupOptions: RollupOptions;
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
export interface ResolvedPluginOptions extends Required<BasePluginOptions>, Required<Pick<CustomInjectManifestOptions, "swUrl">> {
|
|
166
|
-
injectManifest: ViteInjectManifestOptions;
|
|
167
|
-
injectManifestRollupOptions: InjectManifestRollupOptions;
|
|
168
|
-
devOptions: Required<DevOptions>;
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
export interface ShareTargetFiles {
|
|
172
|
-
name: string;
|
|
173
|
-
accept: string | string[];
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
/**
|
|
177
|
-
* @see https://developer.mozilla.org/en-US/docs/Web/Manifest/launch_handler#launch_handler_item_values
|
|
178
|
-
*/
|
|
179
|
-
export type LaunchHandlerClientMode = "auto" | "focus-existing" | "navigate-existing" | "navigate-new";
|
|
180
|
-
|
|
181
|
-
export type Display = "fullscreen" | "standalone" | "minimal-ui" | "browser";
|
|
182
|
-
export type DisplayOverride = Display | "window-controls-overlay";
|
|
183
|
-
export type IconPurpose = "monochrome" | "maskable" | "any";
|
|
184
|
-
|
|
185
|
-
// biome-ignore lint/complexity/noBannedTypes: We intentionally have this.
|
|
186
|
-
type Nothing = {};
|
|
187
|
-
|
|
188
|
-
/**
|
|
189
|
-
* type StringLiteralUnion<'maskable'> = 'maskable' | string
|
|
190
|
-
* This has auto completion whereas `'maskable' | string` doesn't
|
|
191
|
-
* Adapted from https://github.com/microsoft/TypeScript/issues/29729
|
|
192
|
-
*/
|
|
193
|
-
export type StringLiteralUnion<T extends U, U = string> = T | (U & Nothing);
|
|
194
|
-
|
|
195
|
-
export interface SerwistViteApi {
|
|
196
|
-
/**
|
|
197
|
-
* Is the plugin disabled?
|
|
198
|
-
*/
|
|
199
|
-
disabled: boolean;
|
|
200
|
-
extendManifestEntries(fn: ExtendManifestEntriesHook): void;
|
|
201
|
-
/*
|
|
202
|
-
* Generate the service worker.
|
|
203
|
-
*/
|
|
204
|
-
generateSW(): Promise<void>;
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
export type ExtendManifestEntriesHook = (manifestEntries: (string | ManifestEntry)[]) => (string | ManifestEntry)[] | undefined;
|
package/src/utils-types.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export type Optional<T, U extends keyof T> = Omit<T, U> & Partial<Pick<T, U>>;
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|