@serwist/vite 9.0.0-preview.5 → 9.0.0-preview.7

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.
@@ -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
- }