@serwist/vite 8.3.0 → 8.4.1

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/context.d.ts CHANGED
@@ -1,10 +1,32 @@
1
1
  import type { ResolvedConfig } from "vite";
2
2
  import type { PluginOptions, ResolvedPluginOptions } from "./types.js";
3
+ export type SerwistViteFrameworks = "nuxt" | "sveltekit";
3
4
  export interface SerwistViteContext {
5
+ /**
6
+ * Resolved Vite config.
7
+ *
8
+ * Note: This value is set by our main plugin, located at plugins/main.ts.
9
+ */
4
10
  viteConfig: ResolvedConfig;
11
+ /**
12
+ * Provided options.
13
+ */
5
14
  userOptions: PluginOptions;
15
+ /**
16
+ * Resolved options.
17
+ *
18
+ * Note: this is different from `userOptions` in that it has been parsed, whereas
19
+ * `userOptions` is the raw configuration that the user provides us.
20
+ */
6
21
  options: ResolvedPluginOptions;
7
22
  useImportRegister: boolean;
23
+ /**
24
+ * Is the plugin running on dev?
25
+ *
26
+ * Note: This value is set by our dev plugin, located at plugins/dev.ts.
27
+ */
8
28
  devEnvironment: boolean;
29
+ /** To tailor our APIs to these frameworks. */
30
+ framework: SerwistViteFrameworks | undefined;
9
31
  }
10
- export declare const createContext: (userOptions: PluginOptions) => SerwistViteContext;
32
+ export declare const createContext: (userOptions: PluginOptions, framework: SerwistViteFrameworks | undefined) => SerwistViteContext;
package/dist/index.d.ts CHANGED
@@ -1,9 +1,17 @@
1
1
  import type { Plugin } from "vite";
2
+ import { createApi } from "./api.js";
3
+ import { createContext } from "./context.js";
4
+ import { buildPlugin } from "./plugins/build.js";
5
+ import { devPlugin } from "./plugins/dev.js";
6
+ import { mainPlugin } from "./plugins/main.js";
2
7
  import type { PluginOptions } from "./types.js";
8
+ import { resolveEntry, toFs } from "./utils.js";
3
9
  /**
4
10
  * Integrates Serwist into your Vite app.
5
11
  * @param userOptions
6
12
  * @returns
7
13
  */
8
14
  export declare const serwist: (userOptions: PluginOptions) => Plugin[];
15
+ export { buildPlugin as build, createApi, createContext, devPlugin as dev, mainPlugin as main, resolveEntry, toFs };
16
+ export type { SerwistViteContext } from "./context.js";
9
17
  export * from "./types.js";
package/dist/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  import { m as mainPlugin, d as devPlugin, c as createContext, a as createApi } from './main.js';
2
+ export { r as resolveEntry, t as toFs } from './main.js';
2
3
  import 'node:fs/promises';
3
4
  import 'node:path';
4
5
  import 'vite';
@@ -6,8 +7,15 @@ import 'node:process';
6
7
  import 'node:crypto';
7
8
  import 'node:fs';
8
9
  import 'fast-glob';
10
+ import 'node:assert';
9
11
 
10
- const buildPlugin = (ctx, api)=>{
12
+ /**
13
+ * Internal build plugin used by `@serwist/vite`.
14
+ * @internal
15
+ * @param ctx
16
+ * @param api
17
+ * @returns
18
+ */ const buildPlugin = (ctx, api)=>{
11
19
  return {
12
20
  name: "@serwist/vite:build",
13
21
  enforce: "post",
@@ -32,7 +40,7 @@ const buildPlugin = (ctx, api)=>{
32
40
  * @param userOptions
33
41
  * @returns
34
42
  */ const serwist = (userOptions)=>{
35
- const ctx = createContext(userOptions);
43
+ const ctx = createContext(userOptions, undefined);
36
44
  const api = createApi(ctx);
37
45
  return [
38
46
  mainPlugin(ctx, api),
@@ -41,4 +49,4 @@ const buildPlugin = (ctx, api)=>{
41
49
  ];
42
50
  };
43
51
 
44
- export { serwist };
52
+ export { buildPlugin as build, createApi, createContext, devPlugin as dev, mainPlugin as main, serwist };
@@ -1,4 +1,4 @@
1
1
  import type { Plugin } from "vite";
2
2
  import type { SerwistViteContext } from "../../context.js";
3
3
  import type { SerwistViteApi } from "../../types.js";
4
- export declare const serwistSveltePlugin: (ctx: SerwistViteContext, api: SerwistViteApi) => Plugin<any>;
4
+ export declare const buildPlugin: (ctx: SerwistViteContext, api: SerwistViteApi) => Plugin<any>;
@@ -1,4 +1,4 @@
1
- import { r as resolveEntry, l as loadSerwistBuild, m as mainPlugin, d as devPlugin, c as createContext, a as createApi } from '../../main.js';
1
+ import { r as resolveEntry, m as mainPlugin, d as devPlugin, c as createContext, a as createApi } from '../../main.js';
2
2
  import crypto from 'node:crypto';
3
3
  import os from 'node:os';
4
4
  import path from 'node:path';
@@ -8,6 +8,24 @@ import 'vite';
8
8
  import 'node:process';
9
9
  import 'node:fs';
10
10
  import 'fast-glob';
11
+ import 'node:assert';
12
+
13
+ const buildPlugin = (ctx, api)=>{
14
+ return {
15
+ name: "@serwist/vite/integration-svelte:build",
16
+ apply: "build",
17
+ enforce: "pre",
18
+ closeBundle: {
19
+ sequential: true,
20
+ enforce: "pre",
21
+ async handler () {
22
+ if (api && !api.disabled && ctx.viteConfig.build.ssr) {
23
+ await api.generateSW();
24
+ }
25
+ }
26
+ }
27
+ };
28
+ };
11
29
 
12
30
  const configurateSvelteKitOptions = (viteConfig, kit, options)=>{
13
31
  const clientOutDir = path.resolve(viteConfig.root, viteConfig.build.outDir, "../client");
@@ -124,30 +142,7 @@ function buildGlobIgnores(globIgnores) {
124
142
  ];
125
143
  }
126
144
 
127
- const serwistSveltePlugin = (ctx, api)=>{
128
- return {
129
- name: "@serwist/vite/integration-svelte:build",
130
- apply: "build",
131
- enforce: "pre",
132
- closeBundle: {
133
- sequential: true,
134
- enforce: "pre",
135
- async handler () {
136
- if (api && !api.disabled && ctx.viteConfig.build.ssr) {
137
- const [injectManifest, logSerwistResult] = await Promise.all([
138
- loadSerwistBuild().then((m)=>m.injectManifest),
139
- import('../../log.js').then((m)=>m.logSerwistResult)
140
- ]);
141
- // Inject the manifest
142
- const buildResult = await injectManifest(ctx.options.injectManifest);
143
- // Log Serwist result
144
- logSerwistResult(buildResult, ctx.viteConfig);
145
- }
146
- }
147
- }
148
- };
149
- };
150
-
145
+ // TODO: handle SvelteKit build errors.
151
146
  /**
152
147
  * Integrates Serwist into your SvelteKit app.
153
148
  * @param userOptions
@@ -156,12 +151,12 @@ const serwistSveltePlugin = (ctx, api)=>{
156
151
  if (!userOptions.integration) userOptions.integration = {};
157
152
  userOptions.integration.closeBundleOrder = "pre";
158
153
  userOptions.integration.configureOptions = (viteConfig, options)=>configurateSvelteKitOptions(viteConfig, userOptions.kit ?? {}, options);
159
- const ctx = createContext(userOptions);
154
+ const ctx = createContext(userOptions, "sveltekit");
160
155
  const api = createApi(ctx);
161
156
  return [
162
157
  mainPlugin(ctx, api),
163
158
  devPlugin(ctx, api),
164
- serwistSveltePlugin(ctx, api)
159
+ buildPlugin(ctx, api)
165
160
  ];
166
161
  };
167
162
 
package/dist/main.js CHANGED
@@ -5,6 +5,7 @@ import process$1 from 'node:process';
5
5
  import crypto from 'node:crypto';
6
6
  import fs$1 from 'node:fs';
7
7
  import fg from 'fast-glob';
8
+ import assert from 'node:assert';
8
9
 
9
10
  let enabled = true;
10
11
  // Support both browser and node environments
@@ -66,7 +67,7 @@ const green = kolorist(32, 39);
66
67
  const yellow = kolorist(33, 39);
67
68
  const cyan = kolorist(36, 39);
68
69
 
69
- var version = "8.3.0";
70
+ var version = "8.4.1";
70
71
 
71
72
  const logSerwistResult = (buildResult, viteOptions)=>{
72
73
  const { logLevel = "info" } = viteOptions;
@@ -93,35 +94,124 @@ const loadSerwistBuild = async ()=>{
93
94
  try {
94
95
  return await import('@serwist/build');
95
96
  } catch (_) {
97
+ // We don't have a default export, don't worry.
96
98
  return require("@serwist/build");
97
99
  }
98
100
  };
99
- const generateInjectManifest = async (options, viteOptions)=>{
100
- // We will have something like this from swSrc:
101
- /*
102
- // sw.js
103
- import { precacheAndRoute } from 'workbox-precaching'
104
- // self.__WB_MANIFEST is default injection point
105
- precacheAndRoute(self.__WB_MANIFEST)
106
- */ const { build } = await import('vite');
107
- const define = {
108
- ...viteOptions.define ?? {}
101
+ const injectManifest = async (config)=>{
102
+ const { validateViteInjectManifestOptions, getFileManifestEntries, stringify } = await loadSerwistBuild();
103
+ const options = validateViteInjectManifestOptions(config);
104
+ const { count, size, manifestEntries, warnings } = await getFileManifestEntries(options);
105
+ const manifestString = manifestEntries === undefined ? "undefined" : stringify(manifestEntries);
106
+ return {
107
+ warnings,
108
+ size,
109
+ count,
110
+ manifestEntries,
111
+ manifestString
109
112
  };
110
- define["process.env.NODE_ENV"] = JSON.stringify(options.mode);
111
- const { format, plugins, rollupOptions } = options.injectManifestRollupOptions;
112
- const parsedSwDest = path.parse(options.injectManifest.swDest);
113
- if (viteOptions.isProduction || options.devOptions.bundle) {
113
+ };
114
+ const generateServiceWorker = async (ctx)=>{
115
+ const { format, plugins, rollupOptions } = ctx.options.injectManifestRollupOptions;
116
+ const parsedSwDest = path.parse(ctx.options.injectManifest.swDest);
117
+ let injectManifestResult = undefined;
118
+ if (ctx.options.injectManifest.injectionPoint) {
119
+ await ctx.options.integration?.beforeBuildServiceWorker?.(ctx.options);
120
+ injectManifestResult = await injectManifest(ctx.options.injectManifest);
121
+ }
122
+ const isProduction = ctx.options.mode === "production";
123
+ const isDev = ctx.options.mode === "development";
124
+ if (isProduction && ctx.framework === "sveltekit" || isDev && !ctx.options.devOptions.bundle) {
125
+ if (!injectManifestResult) {
126
+ throw new Error("injectManifest failed to generate results. This is likely a bug.");
127
+ }
128
+ const { errors, escapeRegExp, getSourceMapURL, rebasePath, replaceAndUpdateSourceMap, translateURLToSourcemapPaths } = await loadSerwistBuild();
129
+ // Make sure we leave swSrc and swDest out of the precache manifest.
130
+ for (const file of [
131
+ ctx.options.injectManifest.swSrc,
132
+ ctx.options.injectManifest.swDest
133
+ ]){
134
+ ctx.options.injectManifest.globIgnores.push(rebasePath({
135
+ file,
136
+ baseDirectory: ctx.options.injectManifest.globDirectory
137
+ }));
138
+ }
139
+ const injectionPoint = ctx.options.injectManifest.injectionPoint;
140
+ const globalRegexp = new RegExp(escapeRegExp(injectionPoint), "g");
141
+ let swFileContents;
142
+ try {
143
+ swFileContents = await fs.readFile(ctx.options.injectManifest.swSrc, "utf8");
144
+ } catch (error) {
145
+ throw new Error(`${errors["invalid-sw-src"]} ${error instanceof Error && error.message ? error.message : ""}`);
146
+ }
147
+ const injectionResults = swFileContents.match(globalRegexp);
148
+ // See https://github.com/GoogleChrome/workbox/issues/2230
149
+ if (!injectionResults) {
150
+ throw new Error(`${errors["injection-point-not-found"]} ${injectionPoint}`);
151
+ }
152
+ assert(injectionResults.length === 1, `${errors["multiple-injection-points"]} ${injectionPoint}`);
153
+ const filesToWrite = {};
154
+ const url = getSourceMapURL(swFileContents);
155
+ // See https://github.com/GoogleChrome/workbox/issues/2957
156
+ const { destPath, srcPath, warning } = translateURLToSourcemapPaths(url, ctx.options.injectManifest.swSrc, ctx.options.injectManifest.swDest);
157
+ if (warning) {
158
+ injectManifestResult.warnings.push(warning);
159
+ }
160
+ // If our swSrc file contains a sourcemap, we would invalidate that
161
+ // mapping if we just replaced injectionPoint with the stringified manifest.
162
+ // Instead, we need to update the swDest contents as well as the sourcemap
163
+ // (assuming it's a real file, not a data: URL) at the same time.
164
+ // See https://github.com/GoogleChrome/workbox/issues/2235
165
+ // and https://github.com/GoogleChrome/workbox/issues/2648
166
+ if (srcPath && destPath) {
167
+ const { map, source } = await replaceAndUpdateSourceMap({
168
+ originalMap: JSON.parse(await fs.readFile(srcPath, "utf8")),
169
+ jsFilename: path.basename(ctx.options.injectManifest.swDest),
170
+ originalSource: swFileContents,
171
+ replaceString: injectManifestResult.manifestString,
172
+ searchString: injectionPoint
173
+ });
174
+ filesToWrite[ctx.options.injectManifest.swDest] = source;
175
+ filesToWrite[destPath] = map;
176
+ } else {
177
+ // If there's no sourcemap associated with swSrc, a simple string
178
+ // replacement will suffice.
179
+ filesToWrite[ctx.options.injectManifest.swDest] = swFileContents.replace(globalRegexp, injectManifestResult.manifestString);
180
+ }
181
+ for (const [file, contents] of Object.entries(filesToWrite)){
182
+ try {
183
+ await fs.mkdir(path.dirname(file), {
184
+ recursive: true
185
+ });
186
+ } catch (error) {
187
+ throw new Error(`${errors["unable-to-make-sw-directory"]} '${error instanceof Error && error.message ? error.message : ""}'`);
188
+ }
189
+ await fs.writeFile(file, contents);
190
+ }
191
+ } else {
192
+ const define = {
193
+ // Nuxt does some really weird stuff. During the build, they MANUALLY
194
+ // set browser APIs, such as window, document, location,..., to `undefined`??
195
+ // Probably some Vue or server stuff. Their `define` doesn't seem to have anything
196
+ // particularly useful for the service worker anyway, so we don't extend it.
197
+ ...ctx.framework === "nuxt" ? undefined : ctx.viteConfig.define,
198
+ "process.env.NODE_ENV": `"${ctx.options.mode}"`
199
+ };
200
+ if (ctx.options.injectManifest.injectionPoint && injectManifestResult) {
201
+ define[ctx.options.injectManifest.injectionPoint] = injectManifestResult.manifestString;
202
+ }
203
+ const { build } = await import('vite');
114
204
  await build({
115
- logLevel: viteOptions.isProduction ? "info" : "warn",
116
- root: viteOptions.root,
117
- base: viteOptions.base,
118
- resolve: viteOptions.resolve,
205
+ logLevel: ctx.viteConfig.isProduction ? "info" : "warn",
206
+ root: ctx.viteConfig.root,
207
+ base: ctx.viteConfig.base,
208
+ resolve: ctx.viteConfig.resolve,
119
209
  // Don't copy anything from public folder
120
210
  publicDir: false,
121
211
  build: {
122
- sourcemap: viteOptions.build.sourcemap,
212
+ sourcemap: ctx.viteConfig.build.sourcemap,
123
213
  lib: {
124
- entry: options.injectManifest.swSrc,
214
+ entry: ctx.options.injectManifest.swSrc,
125
215
  name: "app",
126
216
  formats: [
127
217
  format
@@ -136,25 +226,13 @@ const generateInjectManifest = async (options, viteOptions)=>{
136
226
  },
137
227
  outDir: parsedSwDest.dir,
138
228
  emptyOutDir: false,
139
- minify: viteOptions.isProduction
229
+ minify: isProduction || ctx.options.devOptions.minify
140
230
  },
141
231
  configFile: false,
142
232
  define
143
233
  });
144
- } else {
145
- await fs.copyFile(options.injectManifest.swSrc, options.injectManifest.swDest);
146
234
  }
147
- // If the user doesn't have an injectionPoint, skip injectManifest.
148
- if (!options.injectManifest.injectionPoint) return;
149
- await options.integration?.beforeBuildServiceWorker?.(options);
150
- const resolvedInjectManifestOptions = {
151
- ...options.injectManifest,
152
- // This will not fail since there is an injectionPoint
153
- swSrc: options.injectManifest.swDest
154
- };
155
- const { injectManifest } = await loadSerwistBuild();
156
- // Inject the manifest
157
- return await injectManifest(resolvedInjectManifestOptions);
235
+ return injectManifestResult;
158
236
  };
159
237
 
160
238
  const createApi = (ctx)=>{
@@ -166,7 +244,7 @@ const createApi = (ctx)=>{
166
244
  if (ctx.options.disable) {
167
245
  return undefined;
168
246
  }
169
- const buildResult = await generateInjectManifest(ctx.options, ctx.viteConfig);
247
+ const buildResult = await generateServiceWorker(ctx);
170
248
  if (buildResult) {
171
249
  if (ctx.viteConfig.isProduction) {
172
250
  // Log Serwist result
@@ -191,13 +269,14 @@ const createApi = (ctx)=>{
191
269
  };
192
270
  };
193
271
 
194
- const createContext = (userOptions)=>{
272
+ const createContext = (userOptions, framework)=>{
195
273
  return {
196
274
  userOptions,
197
275
  options: undefined,
198
276
  viteConfig: undefined,
199
277
  useImportRegister: false,
200
- devEnvironment: false
278
+ devEnvironment: false,
279
+ framework
201
280
  };
202
281
  };
203
282
 
@@ -213,27 +292,41 @@ const isAbsolute = (url)=>{
213
292
  };
214
293
  // Source: https://github.com/sveltejs/kit/blob/6419d3eaa7bf1b0a756b28f06a73f71fe042de0a/packages/kit/src/utils/filesystem.js
215
294
  // License: MIT
216
- const resolveEntry = (entry)=>{
295
+ /**
296
+ * Internal function used by `@serwist/vite`.
297
+ * Resolves a file path without extension. Also handles `/index` if the path
298
+ * actually points to a directory.
299
+ * @internal
300
+ * @param ctx
301
+ * @param api
302
+ * @returns
303
+ */ const resolveEntry = (entry)=>{
217
304
  if (fs$1.existsSync(entry)) {
218
305
  const stats = fs$1.statSync(entry);
219
306
  if (stats.isDirectory()) {
220
307
  return resolveEntry(path.join(entry, "index"));
221
308
  }
222
309
  return entry;
223
- } else {
224
- const dir = path.dirname(entry);
225
- if (fs$1.existsSync(dir)) {
226
- const base = path.basename(entry);
227
- const files = fs$1.readdirSync(dir);
228
- const found = files.find((file)=>file.replace(/\.[^.]+$/, "") === base);
229
- if (found) return path.join(dir, found);
230
- }
310
+ }
311
+ const dir = path.dirname(entry);
312
+ if (fs$1.existsSync(dir)) {
313
+ const base = path.basename(entry);
314
+ const files = fs$1.readdirSync(dir);
315
+ const found = files.find((file)=>file.replace(/\.[^.]+$/, "") === base);
316
+ if (found) return path.join(dir, found);
231
317
  }
232
318
  return null;
233
319
  };
234
320
  // Source: https://github.com/sveltejs/kit/blob/6419d3eaa7bf1b0a756b28f06a73f71fe042de0a/packages/kit/src/utils/filesystem.js
235
321
  // License: MIT
236
- function toFs(str) {
322
+ /**
323
+ * Internal function used by `@serwist/vite`.
324
+ * Converts a filesystem path to a Vite `@fs` URL.
325
+ * @internal
326
+ * @param ctx
327
+ * @param api
328
+ * @returns
329
+ */ function toFs(str) {
237
330
  str = str.replace(/\\/g, "/");
238
331
  // Windows/Linux separation - Windows starts with a drive letter, we need a / in front there
239
332
  return `/@fs${str.startsWith("/") ? "" : "/"}${str}`;
@@ -245,7 +338,13 @@ function toFs(str) {
245
338
  // - Otherwise, run `injectManifest` and return the service worker through `async load(id)`. Although
246
339
  // `precacheEntries` is always `undefined`, we still do this to check the user's `injectManifest` options
247
340
  // in dev mode.
248
- const devPlugin = (ctx, api)=>{
341
+ /**
342
+ * Internal dev plugin used by `@serwist/vite`.
343
+ * @internal
344
+ * @param ctx
345
+ * @param api
346
+ * @returns
347
+ */ const devPlugin = (ctx, api)=>{
249
348
  return {
250
349
  name: "@serwist/vite:dev",
251
350
  apply: "serve",
@@ -335,7 +434,7 @@ const configureStaticAssets = async (resolvedPluginOptions, viteConfig)=>{
335
434
  if (manifestEntries.length > 0) {
336
435
  const included = manifestEntries.map((me)=>{
337
436
  if (typeof me === "string") return me;
338
- else return me.url;
437
+ return me.url;
339
438
  });
340
439
  assets = assets.filter((a)=>!included.includes(a));
341
440
  }
@@ -350,19 +449,22 @@ const configureStaticAssets = async (resolvedPluginOptions, viteConfig)=>{
350
449
  };
351
450
 
352
451
  const resolveOptions = async (options, viteConfig)=>{
353
- const { type = "classic", mode = process$1.env.NODE_ENV || "production", injectRegister = "auto", registerType = "prompt", minify = true, base = viteConfig.base, includeAssets = undefined, useCredentials = false, disable = false, integration = {}, buildBase, devOptions, ...injectManifest } = options;
452
+ const { type = "classic", mode = process$1.env.NODE_ENV || "production", injectRegister = "auto", registerType = "prompt", minify = true, base = viteConfig.base, scope: _scope, swUrl = "/sw.js", includeAssets = undefined, useCredentials = false, disable = false, integration = {}, buildBase, devOptions, plugins = [], rollupOptions = {}, rollupFormat = "es", ...injectManifest } = options;
354
453
  const basePath = resolveBasePath(base);
355
454
  // check typescript service worker for injectManifest strategy
356
- const scope = options.scope || basePath;
455
+ const scope = _scope || basePath;
357
456
  let assetsDir = slash(viteConfig.build.assetsDir ?? "assets");
358
457
  if (assetsDir[assetsDir.length - 1] !== "/") assetsDir += "/";
359
458
  const resolvedDevOptions = {
360
459
  bundle: true,
460
+ minify: false,
361
461
  ...devOptions
362
462
  };
363
463
  // remove './' prefix from assetsDir
364
464
  const dontCacheBustURLsMatching = new RegExp(`^${assetsDir.replace(/^\.*?\//, "")}`);
365
- const { plugins = [], rollupOptions = {}, rollupFormat = "es", swUrl = "/sw.js", swSrc, swDest, ...userInjectManifest } = injectManifest || {};
465
+ const { validateViteInjectManifestOptions } = await loadSerwistBuild();
466
+ validateViteInjectManifestOptions(injectManifest);
467
+ const { swSrc, swDest, ...userInjectManifest } = injectManifest || {};
366
468
  const resolvedPluginOptions = {
367
469
  base: basePath,
368
470
  type,
@@ -397,7 +499,13 @@ const resolveOptions = async (options, viteConfig)=>{
397
499
  return resolvedPluginOptions;
398
500
  };
399
501
 
400
- const mainPlugin = (ctx, api)=>{
502
+ /**
503
+ * Internal plugin used by `@serwist/vite`.
504
+ * @internal
505
+ * @param ctx
506
+ * @param api
507
+ * @returns
508
+ */ const mainPlugin = (ctx, api)=>{
401
509
  return {
402
510
  name: "@serwist/vite",
403
511
  enforce: "pre",
@@ -431,4 +539,4 @@ export const swType = "${ctx.devEnvironment ? "module" : ctx.options.type}";`;
431
539
  };
432
540
  };
433
541
 
434
- export { createApi as a, cyan as b, createContext as c, devPlugin as d, dim as e, green as g, loadSerwistBuild as l, mainPlugin as m, resolveEntry as r, version as v, yellow as y };
542
+ export { createApi as a, createContext as c, devPlugin as d, mainPlugin as m, resolveEntry as r, toFs as t };
package/dist/modules.d.ts CHANGED
@@ -1,5 +1,9 @@
1
1
  import type * as SerwistBuild from "@serwist/build";
2
- import type { ResolvedConfig } from "vite";
3
- import type { ResolvedPluginOptions } from "./types.js";
2
+ import type { SerwistViteContext } from "./context.js";
4
3
  export declare const loadSerwistBuild: () => Promise<typeof SerwistBuild>;
5
- export declare const generateInjectManifest: (options: ResolvedPluginOptions, viteOptions: ResolvedConfig) => Promise<SerwistBuild.BuildResult | undefined>;
4
+ interface BuildResult extends SerwistBuild.GetManifestResult {
5
+ manifestString: string;
6
+ }
7
+ export declare const injectManifest: (config: SerwistBuild.ViteInjectManifestOptions) => Promise<BuildResult>;
8
+ export declare const generateServiceWorker: (ctx: SerwistViteContext) => Promise<BuildResult | undefined>;
9
+ export {};
@@ -1,4 +1,11 @@
1
1
  import type { Plugin } from "vite";
2
2
  import type { SerwistViteContext } from "../context.js";
3
3
  import type { SerwistViteApi } from "../types.js";
4
+ /**
5
+ * Internal build plugin used by `@serwist/vite`.
6
+ * @internal
7
+ * @param ctx
8
+ * @param api
9
+ * @returns
10
+ */
4
11
  export declare const buildPlugin: (ctx: SerwistViteContext, api: SerwistViteApi) => Plugin<any>;
@@ -1,4 +1,11 @@
1
1
  import { type Plugin } from "vite";
2
2
  import type { SerwistViteContext } from "../context.js";
3
3
  import type { SerwistViteApi } from "../types.js";
4
+ /**
5
+ * Internal dev plugin used by `@serwist/vite`.
6
+ * @internal
7
+ * @param ctx
8
+ * @param api
9
+ * @returns
10
+ */
4
11
  export declare const devPlugin: (ctx: SerwistViteContext, api: SerwistViteApi) => Plugin;
@@ -1,4 +1,11 @@
1
1
  import type { Plugin } from "vite";
2
2
  import type { SerwistViteContext } from "../context.js";
3
3
  import type { SerwistViteApi } from "../types.js";
4
+ /**
5
+ * Internal plugin used by `@serwist/vite`.
6
+ * @internal
7
+ * @param ctx
8
+ * @param api
9
+ * @returns
10
+ */
4
11
  export declare const mainPlugin: (ctx: SerwistViteContext, api: SerwistViteApi) => Plugin<any>;
package/dist/types.d.ts CHANGED
@@ -1,8 +1,8 @@
1
- import type { InjectManifestOptions, ManifestEntry } from "@serwist/build";
1
+ import type { ManifestEntry, ViteInjectManifestOptions } from "@serwist/build";
2
2
  import type { RollupOptions } from "rollup";
3
- import type { Plugin, ResolvedConfig } from "vite";
3
+ import type { BuildOptions, Plugin, ResolvedConfig } from "vite";
4
4
  export type InjectManifestVitePlugins = string[] | ((vitePluginIds: string[]) => string[]);
5
- export interface CustomInjectManifestOptions extends Omit<InjectManifestOptions, "disablePrecacheManifest"> {
5
+ export interface CustomInjectManifestOptions extends Omit<ViteInjectManifestOptions, "disablePrecacheManifest"> {
6
6
  /**
7
7
  * The URL to the service worker.
8
8
  * @default "/sw.js"
@@ -44,6 +44,7 @@ export interface DevOptions {
44
44
  * happening. What the plugin does is intercepting any request to the service worker (requests for `swUrl`) and returning a bundled one.
45
45
  */
46
46
  bundle?: boolean;
47
+ minify?: BuildOptions["minify"];
47
48
  }
48
49
  /**
49
50
  * Plugin options.
@@ -155,7 +156,7 @@ export interface InjectManifestRollupOptions {
155
156
  rollupOptions: RollupOptions;
156
157
  }
157
158
  export interface ResolvedPluginOptions extends Required<BasePluginOptions>, Required<Pick<CustomInjectManifestOptions, "swUrl">> {
158
- injectManifest: InjectManifestOptions;
159
+ injectManifest: ViteInjectManifestOptions;
159
160
  injectManifestRollupOptions: InjectManifestRollupOptions;
160
161
  devOptions: Required<DevOptions>;
161
162
  }
@@ -170,8 +171,7 @@ export type LaunchHandlerClientMode = "auto" | "focus-existing" | "navigate-exis
170
171
  export type Display = "fullscreen" | "standalone" | "minimal-ui" | "browser";
171
172
  export type DisplayOverride = Display | "window-controls-overlay";
172
173
  export type IconPurpose = "monochrome" | "maskable" | "any";
173
- interface Nothing {
174
- }
174
+ type Nothing = {};
175
175
  /**
176
176
  * type StringLiteralUnion<'maskable'> = 'maskable' | string
177
177
  * This has auto completion whereas `'maskable' | string` doesn't
package/dist/utils.d.ts CHANGED
@@ -2,5 +2,22 @@ export declare const slash: (str: string) => string;
2
2
  export declare const resolveBasePath: (base: string) => string;
3
3
  export declare const isAbsolute: (url: string) => RegExpMatchArray | null;
4
4
  export declare const normalizePath: (path: string) => string;
5
+ /**
6
+ * Internal function used by `@serwist/vite`.
7
+ * Resolves a file path without extension. Also handles `/index` if the path
8
+ * actually points to a directory.
9
+ * @internal
10
+ * @param ctx
11
+ * @param api
12
+ * @returns
13
+ */
5
14
  export declare const resolveEntry: (entry: string) => string | null;
15
+ /**
16
+ * Internal function used by `@serwist/vite`.
17
+ * Converts a filesystem path to a Vite `@fs` URL.
18
+ * @internal
19
+ * @param ctx
20
+ * @param api
21
+ * @returns
22
+ */
6
23
  export declare function toFs(str: string): string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@serwist/vite",
3
- "version": "8.3.0",
3
+ "version": "8.4.1",
4
4
  "type": "module",
5
5
  "description": "A module that integrates Serwist into your Vite application.",
6
6
  "files": [
@@ -29,7 +29,7 @@
29
29
  "license": "MIT",
30
30
  "repository": "serwist/serwist",
31
31
  "bugs": "https://github.com/serwist/serwist/issues",
32
- "homepage": "https://serwist.vercel.app",
32
+ "homepage": "https://serwist.pages.dev",
33
33
  "sideEffects": false,
34
34
  "main": "./dist/index.js",
35
35
  "types": "./dist/index.d.ts",
@@ -77,8 +77,8 @@
77
77
  "debug": "4.3.4",
78
78
  "fast-glob": "3.3.2",
79
79
  "pretty-bytes": "6.1.1",
80
- "@serwist/build": "8.3.0",
81
- "@serwist/window": "8.3.0"
80
+ "@serwist/build": "8.4.1",
81
+ "@serwist/window": "8.4.1"
82
82
  },
83
83
  "devDependencies": {
84
84
  "@playwright/test": "1.40.1",
@@ -88,7 +88,6 @@
88
88
  "@types/prompts": "2.4.9",
89
89
  "@types/react": "18.2.45",
90
90
  "bumpp": "9.2.1",
91
- "eslint": "8.56.0",
92
91
  "kolorist": "1.8.0",
93
92
  "preact": "10.19.3",
94
93
  "prompts": "2.4.2",
@@ -100,7 +99,7 @@
100
99
  "typescript": "5.4.0-dev.20231226",
101
100
  "vite": "5.0.10",
102
101
  "vue": "3.3.13",
103
- "@serwist/constants": "8.3.0"
102
+ "@serwist/constants": "8.4.1"
104
103
  },
105
104
  "peerDependencies": {
106
105
  "@sveltejs/kit": "^1.0.0 || ^2.0.0",
@@ -133,7 +132,8 @@
133
132
  },
134
133
  "scripts": {
135
134
  "build": "rimraf dist && cross-env NODE_ENV=production rollup --config rollup.config.js",
136
- "lint": "eslint src --ext ts,tsx,js,jsx,cjs,mjs",
135
+ "dev": "rollup --config rollup.config.js --watch",
136
+ "lint": "biome lint ./src",
137
137
  "typecheck": "tsc"
138
138
  }
139
139
  }
@@ -1,3 +0,0 @@
1
- import type { BuildResult } from "@serwist/build";
2
- import type { ResolvedConfig } from "vite";
3
- export declare function logSerwistResult(buildResult: Pick<BuildResult, "count" | "size" | "warnings">, viteOptions: ResolvedConfig): void;
package/dist/log.js DELETED
@@ -1,28 +0,0 @@
1
- import { b as cyan, v as version, g as green, e as dim, y as yellow } from './main.js';
2
- import 'node:fs/promises';
3
- import 'node:path';
4
- import 'vite';
5
- import 'node:process';
6
- import 'node:crypto';
7
- import 'node:fs';
8
- import 'fast-glob';
9
-
10
- function logSerwistResult(buildResult, viteOptions) {
11
- const { logLevel = "info" } = viteOptions;
12
- if (logLevel === "silent") return;
13
- const { count, size, warnings } = buildResult;
14
- if (logLevel === "info") {
15
- console.info([
16
- "",
17
- `${cyan(`@serwist/vite/integration-svelte v${version}`)} ${green("files generated.")}`,
18
- `${green("✓")} ${count} precache entries ${dim(`(${(size / 1024).toFixed(2)} KiB)`)}`,
19
- warnings && warnings.length > 0 ? yellow([
20
- "⚠ warnings",
21
- ...warnings.map((w)=>` ${w}`),
22
- ""
23
- ].join("\n")) : ""
24
- ].join("\n"));
25
- }
26
- }
27
-
28
- export { logSerwistResult };