@serwist/vite 8.3.0 → 8.4.0
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 +23 -1
- package/dist/index.d.ts +8 -0
- package/dist/index.js +12 -3
- package/dist/integration/svelte/{serwist-svelte-plugin.d.ts → build.d.ts} +1 -1
- package/dist/integration/svelte/index.js +22 -27
- package/dist/main.js +155 -46
- package/dist/modules.d.ts +7 -3
- package/dist/plugins/build.d.ts +7 -0
- package/dist/plugins/dev.d.ts +7 -0
- package/dist/plugins/main.d.ts +7 -0
- package/dist/types.d.ts +5 -4
- package/dist/utils.d.ts +17 -0
- package/package.json +6 -5
- package/dist/integration/svelte/log.d.ts +0 -3
- package/dist/log.js +0 -28
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,13 +1,22 @@
|
|
|
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';
|
|
3
|
+
import 'node:assert';
|
|
2
4
|
import 'node:fs/promises';
|
|
3
5
|
import 'node:path';
|
|
4
6
|
import 'vite';
|
|
5
7
|
import 'node:process';
|
|
8
|
+
import '@serwist/build';
|
|
6
9
|
import 'node:crypto';
|
|
7
10
|
import 'node:fs';
|
|
8
11
|
import 'fast-glob';
|
|
9
12
|
|
|
10
|
-
|
|
13
|
+
/**
|
|
14
|
+
* Internal build plugin used by `@serwist/vite`.
|
|
15
|
+
* @internal
|
|
16
|
+
* @param ctx
|
|
17
|
+
* @param api
|
|
18
|
+
* @returns
|
|
19
|
+
*/ const buildPlugin = (ctx, api)=>{
|
|
11
20
|
return {
|
|
12
21
|
name: "@serwist/vite:build",
|
|
13
22
|
enforce: "post",
|
|
@@ -32,7 +41,7 @@ const buildPlugin = (ctx, api)=>{
|
|
|
32
41
|
* @param userOptions
|
|
33
42
|
* @returns
|
|
34
43
|
*/ const serwist = (userOptions)=>{
|
|
35
|
-
const ctx = createContext(userOptions);
|
|
44
|
+
const ctx = createContext(userOptions, undefined);
|
|
36
45
|
const api = createApi(ctx);
|
|
37
46
|
return [
|
|
38
47
|
mainPlugin(ctx, api),
|
|
@@ -41,4 +50,4 @@ const buildPlugin = (ctx, api)=>{
|
|
|
41
50
|
];
|
|
42
51
|
};
|
|
43
52
|
|
|
44
|
-
export { serwist };
|
|
53
|
+
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
|
|
4
|
+
export declare const buildPlugin: (ctx: SerwistViteContext, api: SerwistViteApi) => Plugin<any>;
|
|
@@ -1,14 +1,32 @@
|
|
|
1
|
-
import { r as resolveEntry,
|
|
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';
|
|
5
5
|
import { errors } from '@serwist/build';
|
|
6
|
+
import 'node:assert';
|
|
6
7
|
import 'node:fs/promises';
|
|
7
8
|
import 'vite';
|
|
8
9
|
import 'node:process';
|
|
9
10
|
import 'node:fs';
|
|
10
11
|
import 'fast-glob';
|
|
11
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
|
+
};
|
|
29
|
+
|
|
12
30
|
const configurateSvelteKitOptions = (viteConfig, kit, options)=>{
|
|
13
31
|
const clientOutDir = path.resolve(viteConfig.root, viteConfig.build.outDir, "../client");
|
|
14
32
|
// Kit fixes the service worker's name to 'service-worker.js'
|
|
@@ -124,30 +142,7 @@ function buildGlobIgnores(globIgnores) {
|
|
|
124
142
|
];
|
|
125
143
|
}
|
|
126
144
|
|
|
127
|
-
|
|
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
|
-
|
|
159
|
+
buildPlugin(ctx, api)
|
|
165
160
|
];
|
|
166
161
|
};
|
|
167
162
|
|
package/dist/main.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
|
+
import assert from 'node:assert';
|
|
1
2
|
import fs from 'node:fs/promises';
|
|
2
3
|
import path, { resolve } from 'node:path';
|
|
3
4
|
import { normalizePath } from 'vite';
|
|
4
5
|
import process$1 from 'node:process';
|
|
6
|
+
import { validateInjectManifestOptions } from '@serwist/build';
|
|
5
7
|
import crypto from 'node:crypto';
|
|
6
8
|
import fs$1 from 'node:fs';
|
|
7
9
|
import fg from 'fast-glob';
|
|
@@ -66,7 +68,7 @@ const green = kolorist(32, 39);
|
|
|
66
68
|
const yellow = kolorist(33, 39);
|
|
67
69
|
const cyan = kolorist(36, 39);
|
|
68
70
|
|
|
69
|
-
var version = "8.
|
|
71
|
+
var version = "8.4.0";
|
|
70
72
|
|
|
71
73
|
const logSerwistResult = (buildResult, viteOptions)=>{
|
|
72
74
|
const { logLevel = "info" } = viteOptions;
|
|
@@ -93,35 +95,124 @@ const loadSerwistBuild = async ()=>{
|
|
|
93
95
|
try {
|
|
94
96
|
return await import('@serwist/build');
|
|
95
97
|
} catch (_) {
|
|
98
|
+
// We don't have a default export, don't worry.
|
|
96
99
|
return require("@serwist/build");
|
|
97
100
|
}
|
|
98
101
|
};
|
|
99
|
-
const
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
102
|
+
const injectManifest = async (config)=>{
|
|
103
|
+
const { validateViteInjectManifestOptions, getFileManifestEntries, stringify } = await loadSerwistBuild();
|
|
104
|
+
const options = validateViteInjectManifestOptions(config);
|
|
105
|
+
const { count, size, manifestEntries, warnings } = await getFileManifestEntries(options);
|
|
106
|
+
const manifestString = manifestEntries === undefined ? "undefined" : stringify(manifestEntries);
|
|
107
|
+
return {
|
|
108
|
+
warnings,
|
|
109
|
+
size,
|
|
110
|
+
count,
|
|
111
|
+
manifestEntries,
|
|
112
|
+
manifestString
|
|
109
113
|
};
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
const
|
|
113
|
-
|
|
114
|
+
};
|
|
115
|
+
const generateServiceWorker = async (ctx)=>{
|
|
116
|
+
const { format, plugins, rollupOptions } = ctx.options.injectManifestRollupOptions;
|
|
117
|
+
const parsedSwDest = path.parse(ctx.options.injectManifest.swDest);
|
|
118
|
+
let injectManifestResult = undefined;
|
|
119
|
+
if (ctx.options.injectManifest.injectionPoint) {
|
|
120
|
+
await ctx.options.integration?.beforeBuildServiceWorker?.(ctx.options);
|
|
121
|
+
injectManifestResult = await injectManifest(ctx.options.injectManifest);
|
|
122
|
+
}
|
|
123
|
+
const isProduction = ctx.options.mode === "production";
|
|
124
|
+
const isDev = ctx.options.mode === "development";
|
|
125
|
+
if (isProduction && ctx.framework === "sveltekit" || isDev && !ctx.options.devOptions.bundle) {
|
|
126
|
+
if (!injectManifestResult) {
|
|
127
|
+
throw new Error(`injectManifest failed to generate results. This is likely a bug.`);
|
|
128
|
+
}
|
|
129
|
+
const { errors, escapeRegExp, getSourceMapURL, rebasePath, replaceAndUpdateSourceMap, translateURLToSourcemapPaths } = await loadSerwistBuild();
|
|
130
|
+
// Make sure we leave swSrc and swDest out of the precache manifest.
|
|
131
|
+
for (const file of [
|
|
132
|
+
ctx.options.injectManifest.swSrc,
|
|
133
|
+
ctx.options.injectManifest.swDest
|
|
134
|
+
]){
|
|
135
|
+
ctx.options.injectManifest.globIgnores.push(rebasePath({
|
|
136
|
+
file,
|
|
137
|
+
baseDirectory: ctx.options.injectManifest.globDirectory
|
|
138
|
+
}));
|
|
139
|
+
}
|
|
140
|
+
const injectionPoint = ctx.options.injectManifest.injectionPoint;
|
|
141
|
+
const globalRegexp = new RegExp(escapeRegExp(injectionPoint), "g");
|
|
142
|
+
let swFileContents;
|
|
143
|
+
try {
|
|
144
|
+
swFileContents = await fs.readFile(ctx.options.injectManifest.swSrc, "utf8");
|
|
145
|
+
} catch (error) {
|
|
146
|
+
throw new Error(`${errors["invalid-sw-src"]} ${error instanceof Error && error.message ? error.message : ""}`);
|
|
147
|
+
}
|
|
148
|
+
const injectionResults = swFileContents.match(globalRegexp);
|
|
149
|
+
// See https://github.com/GoogleChrome/workbox/issues/2230
|
|
150
|
+
if (!injectionResults) {
|
|
151
|
+
throw new Error(`${errors["injection-point-not-found"]} ${injectionPoint}`);
|
|
152
|
+
}
|
|
153
|
+
assert(injectionResults.length === 1, `${errors["multiple-injection-points"]} ${injectionPoint}`);
|
|
154
|
+
const filesToWrite = {};
|
|
155
|
+
const url = getSourceMapURL(swFileContents);
|
|
156
|
+
// See https://github.com/GoogleChrome/workbox/issues/2957
|
|
157
|
+
const { destPath, srcPath, warning } = translateURLToSourcemapPaths(url, ctx.options.injectManifest.swSrc, ctx.options.injectManifest.swDest);
|
|
158
|
+
if (warning) {
|
|
159
|
+
injectManifestResult.warnings.push(warning);
|
|
160
|
+
}
|
|
161
|
+
// If our swSrc file contains a sourcemap, we would invalidate that
|
|
162
|
+
// mapping if we just replaced injectionPoint with the stringified manifest.
|
|
163
|
+
// Instead, we need to update the swDest contents as well as the sourcemap
|
|
164
|
+
// (assuming it's a real file, not a data: URL) at the same time.
|
|
165
|
+
// See https://github.com/GoogleChrome/workbox/issues/2235
|
|
166
|
+
// and https://github.com/GoogleChrome/workbox/issues/2648
|
|
167
|
+
if (srcPath && destPath) {
|
|
168
|
+
const { map, source } = await replaceAndUpdateSourceMap({
|
|
169
|
+
originalMap: JSON.parse(await fs.readFile(srcPath, "utf8")),
|
|
170
|
+
jsFilename: path.basename(ctx.options.injectManifest.swDest),
|
|
171
|
+
originalSource: swFileContents,
|
|
172
|
+
replaceString: injectManifestResult.manifestString,
|
|
173
|
+
searchString: injectionPoint
|
|
174
|
+
});
|
|
175
|
+
filesToWrite[ctx.options.injectManifest.swDest] = source;
|
|
176
|
+
filesToWrite[destPath] = map;
|
|
177
|
+
} else {
|
|
178
|
+
// If there's no sourcemap associated with swSrc, a simple string
|
|
179
|
+
// replacement will suffice.
|
|
180
|
+
filesToWrite[ctx.options.injectManifest.swDest] = swFileContents.replace(globalRegexp, injectManifestResult.manifestString);
|
|
181
|
+
}
|
|
182
|
+
for (const [file, contents] of Object.entries(filesToWrite)){
|
|
183
|
+
try {
|
|
184
|
+
await fs.mkdir(path.dirname(file), {
|
|
185
|
+
recursive: true
|
|
186
|
+
});
|
|
187
|
+
} catch (error) {
|
|
188
|
+
throw new Error(errors["unable-to-make-sw-directory"] + ` '${error instanceof Error && error.message ? error.message : ""}'`);
|
|
189
|
+
}
|
|
190
|
+
await fs.writeFile(file, contents);
|
|
191
|
+
}
|
|
192
|
+
} else {
|
|
193
|
+
const define = {
|
|
194
|
+
// Nuxt does some really weird stuff. During the build, they MANUALLY
|
|
195
|
+
// set browser APIs, such as window, document, location,..., to `undefined`??
|
|
196
|
+
// Probably some Vue or server stuff. Their `define` doesn't seem to have anything
|
|
197
|
+
// particularly useful for the service worker anyway, so we don't extend it.
|
|
198
|
+
...ctx.framework === "nuxt" ? undefined : ctx.viteConfig.define,
|
|
199
|
+
"process.env.NODE_ENV": `"${ctx.options.mode}"`
|
|
200
|
+
};
|
|
201
|
+
if (ctx.options.injectManifest.injectionPoint && injectManifestResult) {
|
|
202
|
+
define[ctx.options.injectManifest.injectionPoint] = injectManifestResult.manifestString;
|
|
203
|
+
}
|
|
204
|
+
const { build } = await import('vite');
|
|
114
205
|
await build({
|
|
115
|
-
logLevel:
|
|
116
|
-
root:
|
|
117
|
-
base:
|
|
118
|
-
resolve:
|
|
206
|
+
logLevel: ctx.viteConfig.isProduction ? "info" : "warn",
|
|
207
|
+
root: ctx.viteConfig.root,
|
|
208
|
+
base: ctx.viteConfig.base,
|
|
209
|
+
resolve: ctx.viteConfig.resolve,
|
|
119
210
|
// Don't copy anything from public folder
|
|
120
211
|
publicDir: false,
|
|
121
212
|
build: {
|
|
122
|
-
sourcemap:
|
|
213
|
+
sourcemap: ctx.viteConfig.build.sourcemap,
|
|
123
214
|
lib: {
|
|
124
|
-
entry: options.injectManifest.swSrc,
|
|
215
|
+
entry: ctx.options.injectManifest.swSrc,
|
|
125
216
|
name: "app",
|
|
126
217
|
formats: [
|
|
127
218
|
format
|
|
@@ -136,25 +227,13 @@ const generateInjectManifest = async (options, viteOptions)=>{
|
|
|
136
227
|
},
|
|
137
228
|
outDir: parsedSwDest.dir,
|
|
138
229
|
emptyOutDir: false,
|
|
139
|
-
minify:
|
|
230
|
+
minify: isProduction || ctx.options.devOptions.minify
|
|
140
231
|
},
|
|
141
232
|
configFile: false,
|
|
142
233
|
define
|
|
143
234
|
});
|
|
144
|
-
} else {
|
|
145
|
-
await fs.copyFile(options.injectManifest.swSrc, options.injectManifest.swDest);
|
|
146
235
|
}
|
|
147
|
-
|
|
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);
|
|
236
|
+
return injectManifestResult;
|
|
158
237
|
};
|
|
159
238
|
|
|
160
239
|
const createApi = (ctx)=>{
|
|
@@ -166,7 +245,7 @@ const createApi = (ctx)=>{
|
|
|
166
245
|
if (ctx.options.disable) {
|
|
167
246
|
return undefined;
|
|
168
247
|
}
|
|
169
|
-
const buildResult = await
|
|
248
|
+
const buildResult = await generateServiceWorker(ctx);
|
|
170
249
|
if (buildResult) {
|
|
171
250
|
if (ctx.viteConfig.isProduction) {
|
|
172
251
|
// Log Serwist result
|
|
@@ -191,13 +270,14 @@ const createApi = (ctx)=>{
|
|
|
191
270
|
};
|
|
192
271
|
};
|
|
193
272
|
|
|
194
|
-
const createContext = (userOptions)=>{
|
|
273
|
+
const createContext = (userOptions, framework)=>{
|
|
195
274
|
return {
|
|
196
275
|
userOptions,
|
|
197
276
|
options: undefined,
|
|
198
277
|
viteConfig: undefined,
|
|
199
278
|
useImportRegister: false,
|
|
200
|
-
devEnvironment: false
|
|
279
|
+
devEnvironment: false,
|
|
280
|
+
framework
|
|
201
281
|
};
|
|
202
282
|
};
|
|
203
283
|
|
|
@@ -213,7 +293,15 @@ const isAbsolute = (url)=>{
|
|
|
213
293
|
};
|
|
214
294
|
// Source: https://github.com/sveltejs/kit/blob/6419d3eaa7bf1b0a756b28f06a73f71fe042de0a/packages/kit/src/utils/filesystem.js
|
|
215
295
|
// License: MIT
|
|
216
|
-
|
|
296
|
+
/**
|
|
297
|
+
* Internal function used by `@serwist/vite`.
|
|
298
|
+
* Resolves a file path without extension. Also handles `/index` if the path
|
|
299
|
+
* actually points to a directory.
|
|
300
|
+
* @internal
|
|
301
|
+
* @param ctx
|
|
302
|
+
* @param api
|
|
303
|
+
* @returns
|
|
304
|
+
*/ const resolveEntry = (entry)=>{
|
|
217
305
|
if (fs$1.existsSync(entry)) {
|
|
218
306
|
const stats = fs$1.statSync(entry);
|
|
219
307
|
if (stats.isDirectory()) {
|
|
@@ -233,7 +321,14 @@ const resolveEntry = (entry)=>{
|
|
|
233
321
|
};
|
|
234
322
|
// Source: https://github.com/sveltejs/kit/blob/6419d3eaa7bf1b0a756b28f06a73f71fe042de0a/packages/kit/src/utils/filesystem.js
|
|
235
323
|
// License: MIT
|
|
236
|
-
|
|
324
|
+
/**
|
|
325
|
+
* Internal function used by `@serwist/vite`.
|
|
326
|
+
* Converts a filesystem path to a Vite `@fs` URL.
|
|
327
|
+
* @internal
|
|
328
|
+
* @param ctx
|
|
329
|
+
* @param api
|
|
330
|
+
* @returns
|
|
331
|
+
*/ function toFs(str) {
|
|
237
332
|
str = str.replace(/\\/g, "/");
|
|
238
333
|
// Windows/Linux separation - Windows starts with a drive letter, we need a / in front there
|
|
239
334
|
return `/@fs${str.startsWith("/") ? "" : "/"}${str}`;
|
|
@@ -245,7 +340,13 @@ function toFs(str) {
|
|
|
245
340
|
// - Otherwise, run `injectManifest` and return the service worker through `async load(id)`. Although
|
|
246
341
|
// `precacheEntries` is always `undefined`, we still do this to check the user's `injectManifest` options
|
|
247
342
|
// in dev mode.
|
|
248
|
-
|
|
343
|
+
/**
|
|
344
|
+
* Internal dev plugin used by `@serwist/vite`.
|
|
345
|
+
* @internal
|
|
346
|
+
* @param ctx
|
|
347
|
+
* @param api
|
|
348
|
+
* @returns
|
|
349
|
+
*/ const devPlugin = (ctx, api)=>{
|
|
249
350
|
return {
|
|
250
351
|
name: "@serwist/vite:dev",
|
|
251
352
|
apply: "serve",
|
|
@@ -350,19 +451,21 @@ const configureStaticAssets = async (resolvedPluginOptions, viteConfig)=>{
|
|
|
350
451
|
};
|
|
351
452
|
|
|
352
453
|
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;
|
|
454
|
+
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
455
|
const basePath = resolveBasePath(base);
|
|
355
456
|
// check typescript service worker for injectManifest strategy
|
|
356
|
-
const scope =
|
|
457
|
+
const scope = _scope || basePath;
|
|
357
458
|
let assetsDir = slash(viteConfig.build.assetsDir ?? "assets");
|
|
358
459
|
if (assetsDir[assetsDir.length - 1] !== "/") assetsDir += "/";
|
|
359
460
|
const resolvedDevOptions = {
|
|
360
461
|
bundle: true,
|
|
462
|
+
minify: false,
|
|
361
463
|
...devOptions
|
|
362
464
|
};
|
|
363
465
|
// remove './' prefix from assetsDir
|
|
364
466
|
const dontCacheBustURLsMatching = new RegExp(`^${assetsDir.replace(/^\.*?\//, "")}`);
|
|
365
|
-
|
|
467
|
+
validateInjectManifestOptions(injectManifest);
|
|
468
|
+
const { swSrc, swDest, ...userInjectManifest } = injectManifest || {};
|
|
366
469
|
const resolvedPluginOptions = {
|
|
367
470
|
base: basePath,
|
|
368
471
|
type,
|
|
@@ -397,7 +500,13 @@ const resolveOptions = async (options, viteConfig)=>{
|
|
|
397
500
|
return resolvedPluginOptions;
|
|
398
501
|
};
|
|
399
502
|
|
|
400
|
-
|
|
503
|
+
/**
|
|
504
|
+
* Internal plugin used by `@serwist/vite`.
|
|
505
|
+
* @internal
|
|
506
|
+
* @param ctx
|
|
507
|
+
* @param api
|
|
508
|
+
* @returns
|
|
509
|
+
*/ const mainPlugin = (ctx, api)=>{
|
|
401
510
|
return {
|
|
402
511
|
name: "@serwist/vite",
|
|
403
512
|
enforce: "pre",
|
|
@@ -431,4 +540,4 @@ export const swType = "${ctx.devEnvironment ? "module" : ctx.options.type}";`;
|
|
|
431
540
|
};
|
|
432
541
|
};
|
|
433
542
|
|
|
434
|
-
export { createApi as a,
|
|
543
|
+
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 {
|
|
3
|
-
import type { ResolvedPluginOptions } from "./types.js";
|
|
2
|
+
import type { SerwistViteContext } from "./context.js";
|
|
4
3
|
export declare const loadSerwistBuild: () => Promise<typeof SerwistBuild>;
|
|
5
|
-
|
|
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 {};
|
package/dist/plugins/build.d.ts
CHANGED
|
@@ -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>;
|
package/dist/plugins/dev.d.ts
CHANGED
|
@@ -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;
|
package/dist/plugins/main.d.ts
CHANGED
|
@@ -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 {
|
|
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<
|
|
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:
|
|
159
|
+
injectManifest: ViteInjectManifestOptions;
|
|
159
160
|
injectManifestRollupOptions: InjectManifestRollupOptions;
|
|
160
161
|
devOptions: Required<DevOptions>;
|
|
161
162
|
}
|
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
|
+
"version": "8.4.0",
|
|
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.
|
|
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.
|
|
81
|
-
"@serwist/window": "8.
|
|
80
|
+
"@serwist/build": "8.4.0",
|
|
81
|
+
"@serwist/window": "8.4.0"
|
|
82
82
|
},
|
|
83
83
|
"devDependencies": {
|
|
84
84
|
"@playwright/test": "1.40.1",
|
|
@@ -100,7 +100,7 @@
|
|
|
100
100
|
"typescript": "5.4.0-dev.20231226",
|
|
101
101
|
"vite": "5.0.10",
|
|
102
102
|
"vue": "3.3.13",
|
|
103
|
-
"@serwist/constants": "8.
|
|
103
|
+
"@serwist/constants": "8.4.0"
|
|
104
104
|
},
|
|
105
105
|
"peerDependencies": {
|
|
106
106
|
"@sveltejs/kit": "^1.0.0 || ^2.0.0",
|
|
@@ -133,6 +133,7 @@
|
|
|
133
133
|
},
|
|
134
134
|
"scripts": {
|
|
135
135
|
"build": "rimraf dist && cross-env NODE_ENV=production rollup --config rollup.config.js",
|
|
136
|
+
"dev": "rollup --config rollup.config.js --watch",
|
|
136
137
|
"lint": "eslint src --ext ts,tsx,js,jsx,cjs,mjs",
|
|
137
138
|
"typecheck": "tsc"
|
|
138
139
|
}
|
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 };
|