@serwist/vite 9.0.0-preview.2 → 9.0.0-preview.21
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
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { basePartial, globPartial, injectPartial, requiredSwDestPartial, requiredGlobDirectoryPartial } from '@serwist/build/schema';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
|
|
4
|
+
const hooks = z.object({
|
|
5
|
+
beforeBuildServiceWorker: z.function(z.tuple([
|
|
6
|
+
z.any()
|
|
7
|
+
]), z.union([
|
|
8
|
+
z.promise(z.void()),
|
|
9
|
+
z.void()
|
|
10
|
+
])).optional(),
|
|
11
|
+
closeBundleOrder: z.union([
|
|
12
|
+
z.literal("pre"),
|
|
13
|
+
z.literal("post"),
|
|
14
|
+
z.null()
|
|
15
|
+
]).optional(),
|
|
16
|
+
configureOptions: z.function(z.tuple([
|
|
17
|
+
z.any(),
|
|
18
|
+
z.any()
|
|
19
|
+
]), z.union([
|
|
20
|
+
z.promise(z.void()),
|
|
21
|
+
z.void()
|
|
22
|
+
])).optional()
|
|
23
|
+
});
|
|
24
|
+
const devOptions = z.object({
|
|
25
|
+
bundle: z.boolean().default(true),
|
|
26
|
+
minify: z.union([
|
|
27
|
+
z.boolean(),
|
|
28
|
+
z.literal("terser"),
|
|
29
|
+
z.literal("esbuild")
|
|
30
|
+
]).default(false)
|
|
31
|
+
});
|
|
32
|
+
const injectManifestPartial = z.object({
|
|
33
|
+
mode: z.union([
|
|
34
|
+
z.literal("development"),
|
|
35
|
+
z.literal("production")
|
|
36
|
+
]),
|
|
37
|
+
type: z.union([
|
|
38
|
+
z.literal("classic"),
|
|
39
|
+
z.literal("module")
|
|
40
|
+
]).default("classic"),
|
|
41
|
+
scope: z.string(),
|
|
42
|
+
base: z.string(),
|
|
43
|
+
disable: z.boolean().default(false),
|
|
44
|
+
integration: hooks.default({}),
|
|
45
|
+
swUrl: z.string().default("/sw.js"),
|
|
46
|
+
plugins: z.array(z.any()).default([]),
|
|
47
|
+
rollupFormat: z.union([
|
|
48
|
+
z.literal("es"),
|
|
49
|
+
z.literal("iife")
|
|
50
|
+
]).default("es"),
|
|
51
|
+
rollupOptions: z.record(z.string(), z.any()).default({}),
|
|
52
|
+
devOptions: devOptions.default({})
|
|
53
|
+
});
|
|
54
|
+
const injectManifestOptions = basePartial.merge(globPartial).merge(injectPartial).merge(requiredSwDestPartial).merge(requiredGlobDirectoryPartial).merge(injectManifestPartial).strict("Do not pass invalid properties to InjectManifestOptions!");
|
|
55
|
+
|
|
56
|
+
export { devOptions, hooks, injectManifestOptions, injectManifestPartial };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,18 +1,19 @@
|
|
|
1
1
|
import type { Plugin } from "vite";
|
|
2
|
-
import { createApi } from "./api.js";
|
|
3
|
-
import { createContext } from "./context.js";
|
|
2
|
+
import { createApi } from "./lib/api.js";
|
|
3
|
+
import { createContext } from "./lib/context.js";
|
|
4
|
+
import type { DevOptions, ExtendManifestEntriesHook, Hooks, PluginOptions, PluginOptionsComplete, SerwistViteApi } from "./lib/types.js";
|
|
5
|
+
import { resolveEntry, toFs } from "./lib/utils.js";
|
|
6
|
+
import { validateInjectManifestOptions } from "./lib/validator.js";
|
|
4
7
|
import { buildPlugin } from "./plugins/build.js";
|
|
5
8
|
import { devPlugin } from "./plugins/dev.js";
|
|
6
9
|
import { mainPlugin } from "./plugins/main.js";
|
|
7
|
-
import type { PluginOptions } from "./types.js";
|
|
8
|
-
import { resolveEntry, toFs } from "./utils.js";
|
|
9
10
|
/**
|
|
10
11
|
* Integrates Serwist into your Vite app.
|
|
11
12
|
* @param userOptions
|
|
12
13
|
* @returns
|
|
13
14
|
*/
|
|
14
15
|
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";
|
|
17
|
-
export
|
|
16
|
+
export { buildPlugin as build, createApi, createContext, devPlugin as dev, mainPlugin as main, resolveEntry, toFs, validateInjectManifestOptions };
|
|
17
|
+
export type { SerwistViteContext } from "./lib/context.js";
|
|
18
|
+
export type { PluginOptions, PluginOptionsComplete, Hooks, ExtendManifestEntriesHook, DevOptions, SerwistViteApi };
|
|
18
19
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,MAAM,CAAC;AAEnC,OAAO,EAAE,SAAS,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,MAAM,CAAC;AAEnC,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AACzC,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AACjD,OAAO,KAAK,EAAE,UAAU,EAAE,yBAAyB,EAAE,KAAK,EAAE,aAAa,EAAE,qBAAqB,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AACzI,OAAO,EAAE,YAAY,EAAE,IAAI,EAAE,MAAM,gBAAgB,CAAC;AACpD,OAAO,EAAE,6BAA6B,EAAE,MAAM,oBAAoB,CAAC;AACnE,OAAO,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AACjD,OAAO,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAC7C,OAAO,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAE/C;;;;GAIG;AACH,eAAO,MAAM,OAAO,gBAAiB,aAAa,KAAG,MAAM,EAI1D,CAAC;AAGF,OAAO,EAAE,WAAW,IAAI,KAAK,EAAE,SAAS,EAAE,aAAa,EAAE,SAAS,IAAI,GAAG,EAAE,UAAU,IAAI,IAAI,EAAE,YAAY,EAAE,IAAI,EAAE,6BAA6B,EAAE,CAAC;AACnJ,YAAY,EAAE,kBAAkB,EAAE,MAAM,kBAAkB,CAAC;AAC3D,YAAY,EAAE,aAAa,EAAE,qBAAqB,EAAE,KAAK,EAAE,yBAAyB,EAAE,UAAU,EAAE,cAAc,EAAE,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,13 +1,247 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
import 'node:fs/promises';
|
|
4
|
-
import 'node:path';
|
|
5
|
-
import '
|
|
6
|
-
import '
|
|
7
|
-
import '
|
|
8
|
-
import 'node:
|
|
9
|
-
|
|
10
|
-
|
|
1
|
+
import { cyan, green, dim, yellow } from 'kolorist';
|
|
2
|
+
import assert from 'node:assert';
|
|
3
|
+
import fs from 'node:fs/promises';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import fs$1 from 'node:fs';
|
|
6
|
+
import { validationErrorMap, SerwistConfigError } from '@serwist/build/schema';
|
|
7
|
+
import { normalizePath } from 'vite';
|
|
8
|
+
import process from 'node:process';
|
|
9
|
+
|
|
10
|
+
var version = "9.0.0-preview.20";
|
|
11
|
+
|
|
12
|
+
const logSerwistResult = (buildResult, viteOptions)=>{
|
|
13
|
+
const { logLevel = "info" } = viteOptions;
|
|
14
|
+
if (logLevel === "silent") return;
|
|
15
|
+
const { count, size, warnings } = buildResult;
|
|
16
|
+
if (logLevel === "info") {
|
|
17
|
+
console.info([
|
|
18
|
+
"",
|
|
19
|
+
`${cyan(`@serwist/vite v${version}`)} ${green("files generated.")}`,
|
|
20
|
+
`${green("✓")} ${count} precache entries ${dim(`(${(size / 1024).toFixed(2)} KiB)`)}`,
|
|
21
|
+
warnings && warnings.length > 0 ? yellow([
|
|
22
|
+
"⚠ warnings",
|
|
23
|
+
...warnings.map((w)=>` ${w}`),
|
|
24
|
+
""
|
|
25
|
+
].join("\n")) : ""
|
|
26
|
+
].join("\n"));
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
const loadSerwistBuild = async ()=>{
|
|
31
|
+
try {
|
|
32
|
+
return await import('@serwist/build');
|
|
33
|
+
} catch (_) {
|
|
34
|
+
return require("@serwist/build");
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
const injectManifest = async (options)=>{
|
|
38
|
+
const { getFileManifestEntries, stringify } = await loadSerwistBuild();
|
|
39
|
+
const { count, size, manifestEntries, warnings } = await getFileManifestEntries(options);
|
|
40
|
+
const manifestString = manifestEntries === undefined ? "undefined" : stringify(manifestEntries);
|
|
41
|
+
return {
|
|
42
|
+
warnings,
|
|
43
|
+
size,
|
|
44
|
+
count,
|
|
45
|
+
manifestEntries,
|
|
46
|
+
manifestString
|
|
47
|
+
};
|
|
48
|
+
};
|
|
49
|
+
const generateServiceWorker = async (ctx)=>{
|
|
50
|
+
const { plugins, rollupOptions, rollupFormat } = ctx.options;
|
|
51
|
+
const parsedSwDest = path.parse(ctx.options.injectManifest.swDest);
|
|
52
|
+
let injectManifestResult = undefined;
|
|
53
|
+
if (ctx.options.injectManifest.injectionPoint) {
|
|
54
|
+
await ctx.options.integration?.beforeBuildServiceWorker?.(ctx.options);
|
|
55
|
+
injectManifestResult = await injectManifest(ctx.options.injectManifest);
|
|
56
|
+
}
|
|
57
|
+
const isProduction = ctx.options.mode === "production";
|
|
58
|
+
const isDev = ctx.options.mode === "development";
|
|
59
|
+
if (isDev && !ctx.options.devOptions.bundle) {
|
|
60
|
+
if (!injectManifestResult) {
|
|
61
|
+
throw new Error("injectManifest failed to generate results. This is likely a bug.");
|
|
62
|
+
}
|
|
63
|
+
const { errors, escapeRegExp, getSourceMapURL, rebasePath, replaceAndUpdateSourceMap, translateURLToSourcemapPaths } = await loadSerwistBuild();
|
|
64
|
+
for (const file of [
|
|
65
|
+
ctx.options.injectManifest.swSrc,
|
|
66
|
+
ctx.options.injectManifest.swDest
|
|
67
|
+
]){
|
|
68
|
+
ctx.options.injectManifest.globIgnores.push(rebasePath({
|
|
69
|
+
file,
|
|
70
|
+
baseDirectory: ctx.options.injectManifest.globDirectory
|
|
71
|
+
}));
|
|
72
|
+
}
|
|
73
|
+
const injectionPoint = ctx.options.injectManifest.injectionPoint;
|
|
74
|
+
const globalRegexp = new RegExp(escapeRegExp(injectionPoint), "g");
|
|
75
|
+
let swFileContents;
|
|
76
|
+
try {
|
|
77
|
+
swFileContents = await fs.readFile(ctx.options.injectManifest.swSrc, "utf8");
|
|
78
|
+
} catch (error) {
|
|
79
|
+
throw new Error(`${errors["invalid-sw-src"]} ${error instanceof Error && error.message ? error.message : ""}`);
|
|
80
|
+
}
|
|
81
|
+
const injectionResults = swFileContents.match(globalRegexp);
|
|
82
|
+
if (!injectionResults) {
|
|
83
|
+
throw new Error(`${errors["injection-point-not-found"]} ${injectionPoint}`);
|
|
84
|
+
}
|
|
85
|
+
assert(injectionResults.length === 1, `${errors["multiple-injection-points"]} ${injectionPoint}`);
|
|
86
|
+
const filesToWrite = {};
|
|
87
|
+
const url = getSourceMapURL(swFileContents);
|
|
88
|
+
const { destPath, srcPath, warning } = translateURLToSourcemapPaths(url, ctx.options.injectManifest.swSrc, ctx.options.injectManifest.swDest);
|
|
89
|
+
if (warning) {
|
|
90
|
+
injectManifestResult.warnings.push(warning);
|
|
91
|
+
}
|
|
92
|
+
if (srcPath && destPath) {
|
|
93
|
+
const { map, source } = await replaceAndUpdateSourceMap({
|
|
94
|
+
originalMap: JSON.parse(await fs.readFile(srcPath, "utf8")),
|
|
95
|
+
jsFilename: path.basename(ctx.options.injectManifest.swDest),
|
|
96
|
+
originalSource: swFileContents,
|
|
97
|
+
replaceString: injectManifestResult.manifestString,
|
|
98
|
+
searchString: injectionPoint
|
|
99
|
+
});
|
|
100
|
+
filesToWrite[ctx.options.injectManifest.swDest] = source;
|
|
101
|
+
filesToWrite[destPath] = map;
|
|
102
|
+
} else {
|
|
103
|
+
filesToWrite[ctx.options.injectManifest.swDest] = swFileContents.replace(globalRegexp, injectManifestResult.manifestString);
|
|
104
|
+
}
|
|
105
|
+
for (const [file, contents] of Object.entries(filesToWrite)){
|
|
106
|
+
try {
|
|
107
|
+
await fs.mkdir(path.dirname(file), {
|
|
108
|
+
recursive: true
|
|
109
|
+
});
|
|
110
|
+
} catch (error) {
|
|
111
|
+
throw new Error(`${errors["unable-to-make-sw-directory"]} '${error instanceof Error && error.message ? error.message : ""}'`);
|
|
112
|
+
}
|
|
113
|
+
await fs.writeFile(file, contents);
|
|
114
|
+
}
|
|
115
|
+
} else {
|
|
116
|
+
const define = {
|
|
117
|
+
...ctx.framework === "nuxt" ? undefined : ctx.viteConfig.define,
|
|
118
|
+
"process.env.NODE_ENV": `"${ctx.options.mode}"`
|
|
119
|
+
};
|
|
120
|
+
if (ctx.options.injectManifest.injectionPoint && injectManifestResult) {
|
|
121
|
+
define[ctx.options.injectManifest.injectionPoint] = injectManifestResult.manifestString;
|
|
122
|
+
}
|
|
123
|
+
const { build } = await import('vite');
|
|
124
|
+
await build({
|
|
125
|
+
logLevel: ctx.viteConfig.isProduction ? "info" : "warn",
|
|
126
|
+
root: ctx.viteConfig.root,
|
|
127
|
+
base: ctx.viteConfig.base,
|
|
128
|
+
resolve: ctx.viteConfig.resolve,
|
|
129
|
+
publicDir: false,
|
|
130
|
+
build: {
|
|
131
|
+
sourcemap: ctx.viteConfig.build.sourcemap,
|
|
132
|
+
lib: {
|
|
133
|
+
entry: ctx.options.injectManifest.swSrc,
|
|
134
|
+
name: "app",
|
|
135
|
+
formats: [
|
|
136
|
+
rollupFormat
|
|
137
|
+
]
|
|
138
|
+
},
|
|
139
|
+
rollupOptions: {
|
|
140
|
+
...rollupOptions,
|
|
141
|
+
plugins,
|
|
142
|
+
output: {
|
|
143
|
+
entryFileNames: parsedSwDest.base
|
|
144
|
+
}
|
|
145
|
+
},
|
|
146
|
+
outDir: parsedSwDest.dir,
|
|
147
|
+
emptyOutDir: false,
|
|
148
|
+
minify: isProduction || ctx.options.devOptions.minify
|
|
149
|
+
},
|
|
150
|
+
configFile: false,
|
|
151
|
+
define
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
return injectManifestResult;
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
const createApi = (ctx)=>{
|
|
158
|
+
return {
|
|
159
|
+
get disabled () {
|
|
160
|
+
return ctx?.options?.disable;
|
|
161
|
+
},
|
|
162
|
+
async generateSW () {
|
|
163
|
+
if (ctx.options.disable) {
|
|
164
|
+
return undefined;
|
|
165
|
+
}
|
|
166
|
+
const buildResult = await generateServiceWorker(ctx);
|
|
167
|
+
if (buildResult) {
|
|
168
|
+
if (ctx.viteConfig.isProduction) {
|
|
169
|
+
logSerwistResult(buildResult, ctx.viteConfig);
|
|
170
|
+
} else if (buildResult.warnings && buildResult.warnings.length > 0) {
|
|
171
|
+
console.warn(yellow([
|
|
172
|
+
"[@serwist/vite] Warnings",
|
|
173
|
+
...buildResult.warnings.map((w)=>` - ${w}`),
|
|
174
|
+
""
|
|
175
|
+
].join("\n")));
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
},
|
|
179
|
+
extendManifestEntries (fn) {
|
|
180
|
+
const { options } = ctx;
|
|
181
|
+
if (options.disable) return;
|
|
182
|
+
const result = fn(options.injectManifest.additionalPrecacheEntries || []);
|
|
183
|
+
if (result != null) {
|
|
184
|
+
options.injectManifest.additionalPrecacheEntries = result;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
};
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
const createContext = (userOptions, framework)=>{
|
|
191
|
+
return {
|
|
192
|
+
userOptions,
|
|
193
|
+
options: undefined,
|
|
194
|
+
viteConfig: undefined,
|
|
195
|
+
useImportRegister: false,
|
|
196
|
+
devEnvironment: false,
|
|
197
|
+
framework
|
|
198
|
+
};
|
|
199
|
+
};
|
|
200
|
+
|
|
201
|
+
const slash = (str)=>{
|
|
202
|
+
return str.replace(/\\/g, "/");
|
|
203
|
+
};
|
|
204
|
+
const resolveBasePath = (base)=>{
|
|
205
|
+
if (isAbsolute(base)) return base;
|
|
206
|
+
return !base.startsWith("/") && !base.startsWith("./") ? `/${base}` : base;
|
|
207
|
+
};
|
|
208
|
+
const isAbsolute = (url)=>{
|
|
209
|
+
return url.match(/^(?:[a-z]+:)?\/\//i);
|
|
210
|
+
};
|
|
211
|
+
const resolveEntry = (entry)=>{
|
|
212
|
+
if (fs$1.existsSync(entry)) {
|
|
213
|
+
const stats = fs$1.statSync(entry);
|
|
214
|
+
if (stats.isDirectory()) {
|
|
215
|
+
return resolveEntry(path.join(entry, "index"));
|
|
216
|
+
}
|
|
217
|
+
return entry;
|
|
218
|
+
}
|
|
219
|
+
const dir = path.dirname(entry);
|
|
220
|
+
if (fs$1.existsSync(dir)) {
|
|
221
|
+
const base = path.basename(entry);
|
|
222
|
+
const files = fs$1.readdirSync(dir);
|
|
223
|
+
const found = files.find((file)=>file.replace(/\.[^.]+$/, "") === base);
|
|
224
|
+
if (found) return path.join(dir, found);
|
|
225
|
+
}
|
|
226
|
+
return null;
|
|
227
|
+
};
|
|
228
|
+
const toFs = (str)=>{
|
|
229
|
+
str = str.replace(/\\/g, "/");
|
|
230
|
+
return `/@fs${str.startsWith("/") ? "" : "/"}${str}`;
|
|
231
|
+
};
|
|
232
|
+
|
|
233
|
+
const validateInjectManifestOptions = async (input)=>{
|
|
234
|
+
const result = await (await import('./chunks/schema.js')).injectManifestOptions.spa(input, {
|
|
235
|
+
errorMap: validationErrorMap
|
|
236
|
+
});
|
|
237
|
+
if (!result.success) {
|
|
238
|
+
throw new SerwistConfigError({
|
|
239
|
+
moduleName: "@serwist/vite",
|
|
240
|
+
message: JSON.stringify(result.error.format(), null, 2)
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
return result.data;
|
|
244
|
+
};
|
|
11
245
|
|
|
12
246
|
const buildPlugin = (ctx, api)=>{
|
|
13
247
|
return {
|
|
@@ -29,6 +263,121 @@ const buildPlugin = (ctx, api)=>{
|
|
|
29
263
|
};
|
|
30
264
|
};
|
|
31
265
|
|
|
266
|
+
const devPlugin = (ctx, api)=>{
|
|
267
|
+
return {
|
|
268
|
+
name: "@serwist/vite:dev",
|
|
269
|
+
apply: "serve",
|
|
270
|
+
configureServer (server) {
|
|
271
|
+
ctx.devEnvironment = true;
|
|
272
|
+
server.middlewares.use(async (req, res, next)=>{
|
|
273
|
+
if (!ctx.options.disable && req.url === ctx.options.swUrl) {
|
|
274
|
+
if (ctx.options.devOptions.bundle) {
|
|
275
|
+
await api.generateSW();
|
|
276
|
+
const content = await fs.readFile(ctx.options.injectManifest.swDest, "utf-8");
|
|
277
|
+
await fs.rm(ctx.options.injectManifest.swDest);
|
|
278
|
+
res.setHeader("Content-Type", "application/javascript");
|
|
279
|
+
res.write(content);
|
|
280
|
+
res.end();
|
|
281
|
+
} else {
|
|
282
|
+
res.setHeader("Content-Type", "application/javascript");
|
|
283
|
+
res.write(`import "${toFs(path.resolve(ctx.options.injectManifest.swSrc))}";`);
|
|
284
|
+
res.end();
|
|
285
|
+
}
|
|
286
|
+
} else {
|
|
287
|
+
next();
|
|
288
|
+
}
|
|
289
|
+
});
|
|
290
|
+
},
|
|
291
|
+
async load (id) {
|
|
292
|
+
if (!ctx.options.disable && !ctx.options.devOptions.bundle) {
|
|
293
|
+
const swSrcId = normalizePath(ctx.options.injectManifest.swSrc);
|
|
294
|
+
if (id === swSrcId) {
|
|
295
|
+
await api.generateSW();
|
|
296
|
+
const content = await fs.readFile(ctx.options.injectManifest.swDest, "utf-8");
|
|
297
|
+
await fs.rm(ctx.options.injectManifest.swDest);
|
|
298
|
+
return content;
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
return undefined;
|
|
302
|
+
}
|
|
303
|
+
};
|
|
304
|
+
};
|
|
305
|
+
|
|
306
|
+
const INTERNAL_SERWIST_VIRTUAL = "virtual:internal-serwist";
|
|
307
|
+
const RESOLVED_INTERNAL_SERWIST_VIRTUAL = `\0${INTERNAL_SERWIST_VIRTUAL}`;
|
|
308
|
+
|
|
309
|
+
const prepareConfigForValidation = (viteConfig, { mode = process.env.NODE_ENV === "production" || process.env.NODE_ENV === "development" ? process.env.NODE_ENV : "production", base = viteConfig.base, scope: _scope, devOptions, ...injectManifest })=>{
|
|
310
|
+
const basePath = resolveBasePath(base);
|
|
311
|
+
return {
|
|
312
|
+
mode,
|
|
313
|
+
base: basePath,
|
|
314
|
+
scope: _scope || basePath,
|
|
315
|
+
devOptions,
|
|
316
|
+
...injectManifest
|
|
317
|
+
};
|
|
318
|
+
};
|
|
319
|
+
const resolveOptions = async (options, viteConfig)=>{
|
|
320
|
+
const { mode, type, scope, base, disable, integration, swUrl, swSrc, swDest, plugins, rollupFormat, rollupOptions, devOptions, ...userInjectManifest } = await validateInjectManifestOptions(prepareConfigForValidation(viteConfig, options));
|
|
321
|
+
let assetsDir = slash(viteConfig.build.assetsDir ?? "assets");
|
|
322
|
+
if (assetsDir[assetsDir.length - 1] !== "/") assetsDir += "/";
|
|
323
|
+
const dontCacheBustURLsMatching = new RegExp(`^${assetsDir.replace(/^\.*?\//, "")}`);
|
|
324
|
+
const resolvedPluginOptions = {
|
|
325
|
+
mode,
|
|
326
|
+
type,
|
|
327
|
+
scope,
|
|
328
|
+
base,
|
|
329
|
+
disable,
|
|
330
|
+
integration,
|
|
331
|
+
swUrl,
|
|
332
|
+
plugins,
|
|
333
|
+
rollupFormat,
|
|
334
|
+
rollupOptions,
|
|
335
|
+
devOptions,
|
|
336
|
+
injectManifest: {
|
|
337
|
+
dontCacheBustURLsMatching,
|
|
338
|
+
...userInjectManifest,
|
|
339
|
+
swSrc: path.resolve(viteConfig.root, swSrc),
|
|
340
|
+
swDest: path.resolve(viteConfig.root, viteConfig.build.outDir, swDest),
|
|
341
|
+
disablePrecacheManifest: !viteConfig.isProduction
|
|
342
|
+
}
|
|
343
|
+
};
|
|
344
|
+
return resolvedPluginOptions;
|
|
345
|
+
};
|
|
346
|
+
|
|
347
|
+
const mainPlugin = (ctx, api)=>{
|
|
348
|
+
return {
|
|
349
|
+
name: "@serwist/vite",
|
|
350
|
+
enforce: "pre",
|
|
351
|
+
config () {
|
|
352
|
+
return {
|
|
353
|
+
ssr: {
|
|
354
|
+
noExternal: []
|
|
355
|
+
}
|
|
356
|
+
};
|
|
357
|
+
},
|
|
358
|
+
async configResolved (config) {
|
|
359
|
+
ctx.viteConfig = config;
|
|
360
|
+
ctx.userOptions?.integration?.configureOptions?.(config, ctx.userOptions);
|
|
361
|
+
ctx.options = await resolveOptions(ctx.userOptions, config);
|
|
362
|
+
},
|
|
363
|
+
resolveId (id) {
|
|
364
|
+
if (id === INTERNAL_SERWIST_VIRTUAL) {
|
|
365
|
+
return RESOLVED_INTERNAL_SERWIST_VIRTUAL;
|
|
366
|
+
}
|
|
367
|
+
return undefined;
|
|
368
|
+
},
|
|
369
|
+
load (id) {
|
|
370
|
+
if (id === RESOLVED_INTERNAL_SERWIST_VIRTUAL) {
|
|
371
|
+
return `export const swUrl = "${path.posix.join(ctx.options.base, ctx.options.swUrl)}";
|
|
372
|
+
export const swScope = "${ctx.options.scope}";
|
|
373
|
+
export const swType = "${ctx.devEnvironment ? "module" : ctx.options.type}";`;
|
|
374
|
+
}
|
|
375
|
+
return undefined;
|
|
376
|
+
},
|
|
377
|
+
api
|
|
378
|
+
};
|
|
379
|
+
};
|
|
380
|
+
|
|
32
381
|
const serwist = (userOptions)=>{
|
|
33
382
|
const ctx = createContext(userOptions, undefined);
|
|
34
383
|
const api = createApi(ctx);
|
|
@@ -39,4 +388,4 @@ const serwist = (userOptions)=>{
|
|
|
39
388
|
];
|
|
40
389
|
};
|
|
41
390
|
|
|
42
|
-
export { buildPlugin as build, createApi, createContext, devPlugin as dev, mainPlugin as main, serwist };
|
|
391
|
+
export { buildPlugin as build, createApi, createContext, devPlugin as dev, mainPlugin as main, resolveEntry, serwist, toFs, validateInjectManifestOptions };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.schema.d.ts","sourceRoot":"","sources":["../src/index.schema.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,KAAK,EAAE,qBAAqB,EAAE,qBAAqB,EAAE,MAAM,iBAAiB,CAAC;AAElG,OAAO,EAAE,KAAK,EAAE,UAAU,EAAE,qBAAqB,EAAE,qBAAqB,EAAE,CAAC"}
|
package/dist/index.worker.d.ts
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
1
|
import type { RuntimeCaching } from "@serwist/sw";
|
|
2
|
+
/**
|
|
3
|
+
* The default, recommended list of caching strategies for applications
|
|
4
|
+
* built with Vite.
|
|
5
|
+
*
|
|
6
|
+
* @see https://serwist.pages.dev/docs/vite/worker-exports#default-cache
|
|
7
|
+
*/
|
|
2
8
|
export declare const defaultCache: RuntimeCaching[];
|
|
3
9
|
//# sourceMappingURL=index.worker.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.worker.d.ts","sourceRoot":"","sources":["../src/index.worker.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"index.worker.d.ts","sourceRoot":"","sources":["../src/index.worker.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAIlD;;;;;GAKG;AACH,eAAO,MAAM,YAAY,EAAE,cAAc,EA8GpC,CAAC"}
|