@serwist/vite 9.0.0-preview.6 → 9.0.0-preview.8
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 +1 -1
- package/dist/context.d.ts.map +1 -1
- package/dist/index.js +469 -11
- package/package.json +8 -15
- package/src/context.ts +1 -1
- package/src/modules.ts +1 -1
- package/src/rollup.js +0 -22
- 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/main.js +0 -470
- 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
|
@@ -1,145 +0,0 @@
|
|
|
1
|
-
import { r as resolveEntry, m as mainPlugin, d as devPlugin, c as createContext, a as createApi } from '../../main.js';
|
|
2
|
-
import crypto from 'node:crypto';
|
|
3
|
-
import os from 'node:os';
|
|
4
|
-
import path from 'node:path';
|
|
5
|
-
import { errors } from '@serwist/build';
|
|
6
|
-
import 'node:fs/promises';
|
|
7
|
-
import 'vite';
|
|
8
|
-
import 'node:process';
|
|
9
|
-
import 'node:fs';
|
|
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
|
-
};
|
|
29
|
-
|
|
30
|
-
const configurateSvelteKitOptions = (viteConfig, kit, options)=>{
|
|
31
|
-
const clientOutDir = path.resolve(viteConfig.root, viteConfig.build.outDir, "../client");
|
|
32
|
-
if (viteConfig.isProduction) {
|
|
33
|
-
options.swSrc = path.resolve(clientOutDir, "service-worker.js");
|
|
34
|
-
options.swDest = path.resolve(clientOutDir, "service-worker.js");
|
|
35
|
-
} else {
|
|
36
|
-
const swSrc = resolveEntry(path.join(viteConfig.root, kit.files?.serviceWorker ?? "src/service-worker"));
|
|
37
|
-
if (swSrc) {
|
|
38
|
-
options.swSrc = swSrc;
|
|
39
|
-
options.swDest = path.join(os.tmpdir(), `serwist-vite-integration-svelte-${crypto.randomUUID()}.js`);
|
|
40
|
-
} else {
|
|
41
|
-
throw new Error(errors["invalid-sw-src"]);
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
options.swUrl = "/service-worker.js";
|
|
45
|
-
if (!options.globDirectory) {
|
|
46
|
-
options.globDirectory = path.resolve(clientOutDir, "..");
|
|
47
|
-
}
|
|
48
|
-
if (!options.manifestTransforms) {
|
|
49
|
-
options.manifestTransforms = [
|
|
50
|
-
createManifestTransform(viteConfig.base, undefined, kit)
|
|
51
|
-
];
|
|
52
|
-
}
|
|
53
|
-
let buildAssetsDir = kit.appDir ?? "_app/";
|
|
54
|
-
if (buildAssetsDir[0] === "/") buildAssetsDir = buildAssetsDir.slice(1);
|
|
55
|
-
if (buildAssetsDir[buildAssetsDir.length - 1] !== "/") buildAssetsDir += "/";
|
|
56
|
-
if (!options.modifyURLPrefix) {
|
|
57
|
-
options.globPatterns = buildGlobPatterns(options.globPatterns);
|
|
58
|
-
if (kit.includeVersionFile) {
|
|
59
|
-
options.globPatterns.push(`client/${buildAssetsDir}version.json`);
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
options.globIgnores = buildGlobIgnores(options.globIgnores);
|
|
63
|
-
if (!("dontCacheBustURLsMatching" in options)) {
|
|
64
|
-
options.dontCacheBustURLsMatching = new RegExp(`${buildAssetsDir}immutable/`);
|
|
65
|
-
}
|
|
66
|
-
if (!options.injectionPoint) {
|
|
67
|
-
options.injectionPoint = "self.__SW_MANIFEST";
|
|
68
|
-
}
|
|
69
|
-
};
|
|
70
|
-
function createManifestTransform(base, webManifestName, options) {
|
|
71
|
-
return async (entries)=>{
|
|
72
|
-
const defaultAdapterFallback = "prerendered/fallback.html";
|
|
73
|
-
const suffix = options?.trailingSlash === "always" ? "/" : "";
|
|
74
|
-
let adapterFallback = options?.adapterFallback;
|
|
75
|
-
let excludeFallback = false;
|
|
76
|
-
if (!adapterFallback) {
|
|
77
|
-
adapterFallback = defaultAdapterFallback;
|
|
78
|
-
excludeFallback = true;
|
|
79
|
-
}
|
|
80
|
-
const manifest = entries.filter(({ url })=>!(excludeFallback && url === defaultAdapterFallback)).map((e)=>{
|
|
81
|
-
let url = e.url;
|
|
82
|
-
if (url.startsWith("client/")) url = url.slice(7);
|
|
83
|
-
else if (url.startsWith("prerendered/pages/")) url = url.slice(18);
|
|
84
|
-
else if (url === defaultAdapterFallback) url = adapterFallback;
|
|
85
|
-
if (url.endsWith(".html")) {
|
|
86
|
-
if (url.startsWith("/")) url = url.slice(1);
|
|
87
|
-
if (url === "index.html") {
|
|
88
|
-
url = base;
|
|
89
|
-
} else {
|
|
90
|
-
const idx = url.lastIndexOf("/");
|
|
91
|
-
if (idx > -1) {
|
|
92
|
-
if (url.endsWith("/index.html")) url = `${url.slice(0, idx)}${suffix}`;
|
|
93
|
-
else url = `${url.substring(0, url.lastIndexOf("."))}${suffix}`;
|
|
94
|
-
} else {
|
|
95
|
-
url = `${url.substring(0, url.lastIndexOf("."))}${suffix}`;
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
e.url = url;
|
|
100
|
-
return e;
|
|
101
|
-
});
|
|
102
|
-
if (!webManifestName) return {
|
|
103
|
-
manifest
|
|
104
|
-
};
|
|
105
|
-
return {
|
|
106
|
-
manifest: manifest.filter((e)=>e.url !== webManifestName)
|
|
107
|
-
};
|
|
108
|
-
};
|
|
109
|
-
}
|
|
110
|
-
function buildGlobPatterns(globPatterns) {
|
|
111
|
-
if (globPatterns) {
|
|
112
|
-
if (!globPatterns.some((g)=>g.startsWith("prerendered/"))) globPatterns.push("prerendered/**/*.html");
|
|
113
|
-
if (!globPatterns.some((g)=>g.startsWith("client/"))) globPatterns.push("client/**/*.{js,css,ico,png,svg,webp,webmanifest}");
|
|
114
|
-
if (!globPatterns.some((g)=>g.includes("webmanifest"))) globPatterns.push("client/*.webmanifest");
|
|
115
|
-
return globPatterns;
|
|
116
|
-
}
|
|
117
|
-
return [
|
|
118
|
-
"client/**/*.{js,css,ico,png,svg,webp,webmanifest}",
|
|
119
|
-
"prerendered/**/*.html"
|
|
120
|
-
];
|
|
121
|
-
}
|
|
122
|
-
function buildGlobIgnores(globIgnores) {
|
|
123
|
-
if (globIgnores) {
|
|
124
|
-
if (!globIgnores.some((g)=>g.startsWith("server/"))) globIgnores.push("server/*.*");
|
|
125
|
-
return globIgnores;
|
|
126
|
-
}
|
|
127
|
-
return [
|
|
128
|
-
"server/*.*"
|
|
129
|
-
];
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
const serwist = (userOptions = {})=>{
|
|
133
|
-
if (!userOptions.integration) userOptions.integration = {};
|
|
134
|
-
userOptions.integration.closeBundleOrder = "pre";
|
|
135
|
-
userOptions.integration.configureOptions = (viteConfig, options)=>configurateSvelteKitOptions(viteConfig, userOptions.kit ?? {}, options);
|
|
136
|
-
const ctx = createContext(userOptions, "sveltekit");
|
|
137
|
-
const api = createApi(ctx);
|
|
138
|
-
return [
|
|
139
|
-
mainPlugin(ctx, api),
|
|
140
|
-
devPlugin(ctx, api),
|
|
141
|
-
buildPlugin(ctx, api)
|
|
142
|
-
];
|
|
143
|
-
};
|
|
144
|
-
|
|
145
|
-
export { serwist };
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import type { KitConfig } from "@sveltejs/kit";
|
|
2
|
-
import type { PluginOptions as BasePluginOptions } from "../../types.js";
|
|
3
|
-
import type { Optional } from "../../utils-types.js";
|
|
4
|
-
export interface KitOptions extends Pick<KitConfig, "appDir" | "files"> {
|
|
5
|
-
/**
|
|
6
|
-
* @see https://kit.svelte.dev/docs/adapter-static#options-fallback
|
|
7
|
-
*/
|
|
8
|
-
adapterFallback?: string;
|
|
9
|
-
/**
|
|
10
|
-
* `trailingSlash` in `+page.{ts,js}` or `+layout.{ts,js}` files.
|
|
11
|
-
* @default "never"
|
|
12
|
-
*/
|
|
13
|
-
trailingSlash?: "never" | "always" | "ignore";
|
|
14
|
-
/**
|
|
15
|
-
* Should `"${appDir}/version.json"` be included in the service worker's precache manifest?
|
|
16
|
-
*
|
|
17
|
-
* @default false
|
|
18
|
-
*/
|
|
19
|
-
includeVersionFile?: boolean;
|
|
20
|
-
}
|
|
21
|
-
export interface PluginOptions extends Optional<Omit<BasePluginOptions, "swSrc" | "swDest" | "swUrl">, "globDirectory"> {
|
|
22
|
-
kit?: KitOptions;
|
|
23
|
-
}
|
|
24
|
-
//# sourceMappingURL=types.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/integration/svelte/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,eAAe,CAAC;AAE/C,OAAO,KAAK,EAAE,aAAa,IAAI,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;AACzE,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AAErD,MAAM,WAAW,UAAW,SAAQ,IAAI,CAAC,SAAS,EAAE,QAAQ,GAAG,OAAO,CAAC;IACrE;;OAEG;IACH,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB;;;OAGG;IACH,aAAa,CAAC,EAAE,OAAO,GAAG,QAAQ,GAAG,QAAQ,CAAC;IAC9C;;;;OAIG;IACH,kBAAkB,CAAC,EAAE,OAAO,CAAC;CAC9B;AAED,MAAM,WAAW,aAAc,SAAQ,QAAQ,CAAC,IAAI,CAAC,iBAAiB,EAAE,OAAO,GAAG,QAAQ,GAAG,OAAO,CAAC,EAAE,eAAe,CAAC;IACrH,GAAG,CAAC,EAAE,UAAU,CAAC;CAClB"}
|
package/dist/main.js
DELETED
|
@@ -1,470 +0,0 @@
|
|
|
1
|
-
import fs from 'node:fs/promises';
|
|
2
|
-
import path, { resolve } from 'node:path';
|
|
3
|
-
import { normalizePath } from 'vite';
|
|
4
|
-
import process$1 from 'node:process';
|
|
5
|
-
import crypto from 'node:crypto';
|
|
6
|
-
import fs$1 from 'node:fs';
|
|
7
|
-
import fg from 'fast-glob';
|
|
8
|
-
import assert from 'node:assert';
|
|
9
|
-
|
|
10
|
-
let enabled = true;
|
|
11
|
-
const globalVar = typeof self !== 'undefined' ? self : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : {};
|
|
12
|
-
let supportLevel = 0;
|
|
13
|
-
if (globalVar.process && globalVar.process.env && globalVar.process.stdout) {
|
|
14
|
-
const { FORCE_COLOR, NODE_DISABLE_COLORS, NO_COLOR, TERM, COLORTERM } = globalVar.process.env;
|
|
15
|
-
if (NODE_DISABLE_COLORS || NO_COLOR || FORCE_COLOR === '0') {
|
|
16
|
-
enabled = false;
|
|
17
|
-
} else if (FORCE_COLOR === '1' || FORCE_COLOR === '2' || FORCE_COLOR === '3') {
|
|
18
|
-
enabled = true;
|
|
19
|
-
} else if (TERM === 'dumb') {
|
|
20
|
-
enabled = false;
|
|
21
|
-
} else if ('CI' in globalVar.process.env && [
|
|
22
|
-
'TRAVIS',
|
|
23
|
-
'CIRCLECI',
|
|
24
|
-
'APPVEYOR',
|
|
25
|
-
'GITLAB_CI',
|
|
26
|
-
'GITHUB_ACTIONS',
|
|
27
|
-
'BUILDKITE',
|
|
28
|
-
'DRONE'
|
|
29
|
-
].some((vendor)=>vendor in globalVar.process.env)) {
|
|
30
|
-
enabled = true;
|
|
31
|
-
} else {
|
|
32
|
-
enabled = process.stdout.isTTY;
|
|
33
|
-
}
|
|
34
|
-
if (enabled) {
|
|
35
|
-
if (process.platform === 'win32') {
|
|
36
|
-
supportLevel = 3;
|
|
37
|
-
} else {
|
|
38
|
-
if (COLORTERM && (COLORTERM === 'truecolor' || COLORTERM === '24bit')) {
|
|
39
|
-
supportLevel = 3;
|
|
40
|
-
} else if (TERM && (TERM.endsWith('-256color') || TERM.endsWith('256'))) {
|
|
41
|
-
supportLevel = 2;
|
|
42
|
-
} else {
|
|
43
|
-
supportLevel = 1;
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
let options = {
|
|
49
|
-
enabled,
|
|
50
|
-
supportLevel
|
|
51
|
-
};
|
|
52
|
-
function kolorist(start, end, level = 1) {
|
|
53
|
-
const open = `\x1b[${start}m`;
|
|
54
|
-
const close = `\x1b[${end}m`;
|
|
55
|
-
const regex = new RegExp(`\\x1b\\[${end}m`, 'g');
|
|
56
|
-
return (str)=>{
|
|
57
|
-
return options.enabled && options.supportLevel >= level ? open + ('' + str).replace(regex, open) + close : '' + str;
|
|
58
|
-
};
|
|
59
|
-
}
|
|
60
|
-
const dim = kolorist(2, 22);
|
|
61
|
-
const green = kolorist(32, 39);
|
|
62
|
-
const yellow = kolorist(33, 39);
|
|
63
|
-
const cyan = kolorist(36, 39);
|
|
64
|
-
|
|
65
|
-
var version = "9.0.0-preview.6";
|
|
66
|
-
|
|
67
|
-
const logSerwistResult = (buildResult, viteOptions)=>{
|
|
68
|
-
const { logLevel = "info" } = viteOptions;
|
|
69
|
-
if (logLevel === "silent") return;
|
|
70
|
-
const { count, size, warnings } = buildResult;
|
|
71
|
-
if (logLevel === "info") {
|
|
72
|
-
console.info([
|
|
73
|
-
"",
|
|
74
|
-
`${cyan(`@serwist/vite v${version}`)} ${green("files generated.")}`,
|
|
75
|
-
`${green("✓")} ${count} precache entries ${dim(`(${(size / 1024).toFixed(2)} KiB)`)}`,
|
|
76
|
-
warnings && warnings.length > 0 ? yellow([
|
|
77
|
-
"⚠ warnings",
|
|
78
|
-
...warnings.map((w)=>` ${w}`),
|
|
79
|
-
""
|
|
80
|
-
].join("\n")) : ""
|
|
81
|
-
].join("\n"));
|
|
82
|
-
}
|
|
83
|
-
};
|
|
84
|
-
|
|
85
|
-
const loadSerwistBuild = async ()=>{
|
|
86
|
-
try {
|
|
87
|
-
return await import('@serwist/build');
|
|
88
|
-
} catch (_) {
|
|
89
|
-
return require("@serwist/build");
|
|
90
|
-
}
|
|
91
|
-
};
|
|
92
|
-
const injectManifest = async (config)=>{
|
|
93
|
-
const { validateViteInjectManifestOptions, getFileManifestEntries, stringify } = await loadSerwistBuild();
|
|
94
|
-
const options = await validateViteInjectManifestOptions(config);
|
|
95
|
-
const { count, size, manifestEntries, warnings } = await getFileManifestEntries(options);
|
|
96
|
-
const manifestString = manifestEntries === undefined ? "undefined" : stringify(manifestEntries);
|
|
97
|
-
return {
|
|
98
|
-
warnings,
|
|
99
|
-
size,
|
|
100
|
-
count,
|
|
101
|
-
manifestEntries,
|
|
102
|
-
manifestString
|
|
103
|
-
};
|
|
104
|
-
};
|
|
105
|
-
const generateServiceWorker = async (ctx)=>{
|
|
106
|
-
const { format, plugins, rollupOptions } = ctx.options.injectManifestRollupOptions;
|
|
107
|
-
const parsedSwDest = path.parse(ctx.options.injectManifest.swDest);
|
|
108
|
-
let injectManifestResult = undefined;
|
|
109
|
-
if (ctx.options.injectManifest.injectionPoint) {
|
|
110
|
-
await ctx.options.integration?.beforeBuildServiceWorker?.(ctx.options);
|
|
111
|
-
injectManifestResult = await injectManifest(ctx.options.injectManifest);
|
|
112
|
-
}
|
|
113
|
-
const isProduction = ctx.options.mode === "production";
|
|
114
|
-
const isDev = ctx.options.mode === "development";
|
|
115
|
-
if (isProduction && ctx.framework === "sveltekit" || isDev && !ctx.options.devOptions.bundle) {
|
|
116
|
-
if (!injectManifestResult) {
|
|
117
|
-
throw new Error("injectManifest failed to generate results. This is likely a bug.");
|
|
118
|
-
}
|
|
119
|
-
const { errors, escapeRegExp, getSourceMapURL, rebasePath, replaceAndUpdateSourceMap, translateURLToSourcemapPaths } = await loadSerwistBuild();
|
|
120
|
-
for (const file of [
|
|
121
|
-
ctx.options.injectManifest.swSrc,
|
|
122
|
-
ctx.options.injectManifest.swDest
|
|
123
|
-
]){
|
|
124
|
-
ctx.options.injectManifest.globIgnores.push(rebasePath({
|
|
125
|
-
file,
|
|
126
|
-
baseDirectory: ctx.options.injectManifest.globDirectory
|
|
127
|
-
}));
|
|
128
|
-
}
|
|
129
|
-
const injectionPoint = ctx.options.injectManifest.injectionPoint;
|
|
130
|
-
const globalRegexp = new RegExp(escapeRegExp(injectionPoint), "g");
|
|
131
|
-
let swFileContents;
|
|
132
|
-
try {
|
|
133
|
-
swFileContents = await fs.readFile(ctx.options.injectManifest.swSrc, "utf8");
|
|
134
|
-
} catch (error) {
|
|
135
|
-
throw new Error(`${errors["invalid-sw-src"]} ${error instanceof Error && error.message ? error.message : ""}`);
|
|
136
|
-
}
|
|
137
|
-
const injectionResults = swFileContents.match(globalRegexp);
|
|
138
|
-
if (!injectionResults) {
|
|
139
|
-
throw new Error(`${errors["injection-point-not-found"]} ${injectionPoint}`);
|
|
140
|
-
}
|
|
141
|
-
assert(injectionResults.length === 1, `${errors["multiple-injection-points"]} ${injectionPoint}`);
|
|
142
|
-
const filesToWrite = {};
|
|
143
|
-
const url = getSourceMapURL(swFileContents);
|
|
144
|
-
const { destPath, srcPath, warning } = translateURLToSourcemapPaths(url, ctx.options.injectManifest.swSrc, ctx.options.injectManifest.swDest);
|
|
145
|
-
if (warning) {
|
|
146
|
-
injectManifestResult.warnings.push(warning);
|
|
147
|
-
}
|
|
148
|
-
if (srcPath && destPath) {
|
|
149
|
-
const { map, source } = await replaceAndUpdateSourceMap({
|
|
150
|
-
originalMap: JSON.parse(await fs.readFile(srcPath, "utf8")),
|
|
151
|
-
jsFilename: path.basename(ctx.options.injectManifest.swDest),
|
|
152
|
-
originalSource: swFileContents,
|
|
153
|
-
replaceString: injectManifestResult.manifestString,
|
|
154
|
-
searchString: injectionPoint
|
|
155
|
-
});
|
|
156
|
-
filesToWrite[ctx.options.injectManifest.swDest] = source;
|
|
157
|
-
filesToWrite[destPath] = map;
|
|
158
|
-
} else {
|
|
159
|
-
filesToWrite[ctx.options.injectManifest.swDest] = swFileContents.replace(globalRegexp, injectManifestResult.manifestString);
|
|
160
|
-
}
|
|
161
|
-
for (const [file, contents] of Object.entries(filesToWrite)){
|
|
162
|
-
try {
|
|
163
|
-
await fs.mkdir(path.dirname(file), {
|
|
164
|
-
recursive: true
|
|
165
|
-
});
|
|
166
|
-
} catch (error) {
|
|
167
|
-
throw new Error(`${errors["unable-to-make-sw-directory"]} '${error instanceof Error && error.message ? error.message : ""}'`);
|
|
168
|
-
}
|
|
169
|
-
await fs.writeFile(file, contents);
|
|
170
|
-
}
|
|
171
|
-
} else {
|
|
172
|
-
const define = {
|
|
173
|
-
...ctx.framework === "nuxt" ? undefined : ctx.viteConfig.define,
|
|
174
|
-
"process.env.NODE_ENV": `"${ctx.options.mode}"`
|
|
175
|
-
};
|
|
176
|
-
if (ctx.options.injectManifest.injectionPoint && injectManifestResult) {
|
|
177
|
-
define[ctx.options.injectManifest.injectionPoint] = injectManifestResult.manifestString;
|
|
178
|
-
}
|
|
179
|
-
const { build } = await import('vite');
|
|
180
|
-
await build({
|
|
181
|
-
logLevel: ctx.viteConfig.isProduction ? "info" : "warn",
|
|
182
|
-
root: ctx.viteConfig.root,
|
|
183
|
-
base: ctx.viteConfig.base,
|
|
184
|
-
resolve: ctx.viteConfig.resolve,
|
|
185
|
-
publicDir: false,
|
|
186
|
-
build: {
|
|
187
|
-
sourcemap: ctx.viteConfig.build.sourcemap,
|
|
188
|
-
lib: {
|
|
189
|
-
entry: ctx.options.injectManifest.swSrc,
|
|
190
|
-
name: "app",
|
|
191
|
-
formats: [
|
|
192
|
-
format
|
|
193
|
-
]
|
|
194
|
-
},
|
|
195
|
-
rollupOptions: {
|
|
196
|
-
...rollupOptions,
|
|
197
|
-
plugins,
|
|
198
|
-
output: {
|
|
199
|
-
entryFileNames: parsedSwDest.base
|
|
200
|
-
}
|
|
201
|
-
},
|
|
202
|
-
outDir: parsedSwDest.dir,
|
|
203
|
-
emptyOutDir: false,
|
|
204
|
-
minify: isProduction || ctx.options.devOptions.minify
|
|
205
|
-
},
|
|
206
|
-
configFile: false,
|
|
207
|
-
define
|
|
208
|
-
});
|
|
209
|
-
}
|
|
210
|
-
return injectManifestResult;
|
|
211
|
-
};
|
|
212
|
-
|
|
213
|
-
const createApi = (ctx)=>{
|
|
214
|
-
return {
|
|
215
|
-
get disabled () {
|
|
216
|
-
return ctx?.options?.disable;
|
|
217
|
-
},
|
|
218
|
-
async generateSW () {
|
|
219
|
-
if (ctx.options.disable) {
|
|
220
|
-
return undefined;
|
|
221
|
-
}
|
|
222
|
-
const buildResult = await generateServiceWorker(ctx);
|
|
223
|
-
if (buildResult) {
|
|
224
|
-
if (ctx.viteConfig.isProduction) {
|
|
225
|
-
logSerwistResult(buildResult, ctx.viteConfig);
|
|
226
|
-
} else if (buildResult.warnings && buildResult.warnings.length > 0) {
|
|
227
|
-
console.warn(yellow([
|
|
228
|
-
"[@serwist/vite] Warnings",
|
|
229
|
-
...buildResult.warnings.map((w)=>` - ${w}`),
|
|
230
|
-
""
|
|
231
|
-
].join("\n")));
|
|
232
|
-
}
|
|
233
|
-
}
|
|
234
|
-
},
|
|
235
|
-
extendManifestEntries (fn) {
|
|
236
|
-
const { options } = ctx;
|
|
237
|
-
if (options.disable) return;
|
|
238
|
-
const result = fn(options.injectManifest.additionalPrecacheEntries || []);
|
|
239
|
-
if (result != null) {
|
|
240
|
-
options.injectManifest.additionalPrecacheEntries = result;
|
|
241
|
-
}
|
|
242
|
-
}
|
|
243
|
-
};
|
|
244
|
-
};
|
|
245
|
-
|
|
246
|
-
const createContext = (userOptions, framework)=>{
|
|
247
|
-
return {
|
|
248
|
-
userOptions,
|
|
249
|
-
options: undefined,
|
|
250
|
-
viteConfig: undefined,
|
|
251
|
-
useImportRegister: false,
|
|
252
|
-
devEnvironment: false,
|
|
253
|
-
framework
|
|
254
|
-
};
|
|
255
|
-
};
|
|
256
|
-
|
|
257
|
-
const slash = (str)=>{
|
|
258
|
-
return str.replace(/\\/g, "/");
|
|
259
|
-
};
|
|
260
|
-
const resolveBasePath = (base)=>{
|
|
261
|
-
if (isAbsolute(base)) return base;
|
|
262
|
-
return !base.startsWith("/") && !base.startsWith("./") ? `/${base}` : base;
|
|
263
|
-
};
|
|
264
|
-
const isAbsolute = (url)=>{
|
|
265
|
-
return url.match(/^(?:[a-z]+:)?\/\//i);
|
|
266
|
-
};
|
|
267
|
-
const resolveEntry = (entry)=>{
|
|
268
|
-
if (fs$1.existsSync(entry)) {
|
|
269
|
-
const stats = fs$1.statSync(entry);
|
|
270
|
-
if (stats.isDirectory()) {
|
|
271
|
-
return resolveEntry(path.join(entry, "index"));
|
|
272
|
-
}
|
|
273
|
-
return entry;
|
|
274
|
-
}
|
|
275
|
-
const dir = path.dirname(entry);
|
|
276
|
-
if (fs$1.existsSync(dir)) {
|
|
277
|
-
const base = path.basename(entry);
|
|
278
|
-
const files = fs$1.readdirSync(dir);
|
|
279
|
-
const found = files.find((file)=>file.replace(/\.[^.]+$/, "") === base);
|
|
280
|
-
if (found) return path.join(dir, found);
|
|
281
|
-
}
|
|
282
|
-
return null;
|
|
283
|
-
};
|
|
284
|
-
const toFs = (str)=>{
|
|
285
|
-
str = str.replace(/\\/g, "/");
|
|
286
|
-
return `/@fs${str.startsWith("/") ? "" : "/"}${str}`;
|
|
287
|
-
};
|
|
288
|
-
|
|
289
|
-
const devPlugin = (ctx, api)=>{
|
|
290
|
-
return {
|
|
291
|
-
name: "@serwist/vite:dev",
|
|
292
|
-
apply: "serve",
|
|
293
|
-
configureServer (server) {
|
|
294
|
-
ctx.devEnvironment = true;
|
|
295
|
-
server.middlewares.use(async (req, res, next)=>{
|
|
296
|
-
if (!ctx.options.disable && req.url === ctx.options.swUrl) {
|
|
297
|
-
if (ctx.options.devOptions.bundle) {
|
|
298
|
-
await api.generateSW();
|
|
299
|
-
const content = await fs.readFile(ctx.options.injectManifest.swDest, "utf-8");
|
|
300
|
-
await fs.rm(ctx.options.injectManifest.swDest);
|
|
301
|
-
res.setHeader("Content-Type", "application/javascript");
|
|
302
|
-
res.write(content);
|
|
303
|
-
res.end();
|
|
304
|
-
} else {
|
|
305
|
-
res.setHeader("Content-Type", "application/javascript");
|
|
306
|
-
res.write(`import "${toFs(path.resolve(ctx.options.injectManifest.swSrc))}";`);
|
|
307
|
-
res.end();
|
|
308
|
-
}
|
|
309
|
-
} else {
|
|
310
|
-
next();
|
|
311
|
-
}
|
|
312
|
-
});
|
|
313
|
-
},
|
|
314
|
-
async load (id) {
|
|
315
|
-
if (!ctx.options.disable && !ctx.options.devOptions.bundle) {
|
|
316
|
-
const swSrcId = normalizePath(ctx.options.injectManifest.swSrc);
|
|
317
|
-
if (id === swSrcId) {
|
|
318
|
-
await api.generateSW();
|
|
319
|
-
const content = await fs.readFile(ctx.options.injectManifest.swDest, "utf-8");
|
|
320
|
-
await fs.rm(ctx.options.injectManifest.swDest);
|
|
321
|
-
return content;
|
|
322
|
-
}
|
|
323
|
-
}
|
|
324
|
-
return undefined;
|
|
325
|
-
}
|
|
326
|
-
};
|
|
327
|
-
};
|
|
328
|
-
|
|
329
|
-
const INTERNAL_SERWIST_VIRTUAL = "virtual:internal-serwist";
|
|
330
|
-
const RESOLVED_INTERNAL_SERWIST_VIRTUAL = `\0${INTERNAL_SERWIST_VIRTUAL}`;
|
|
331
|
-
|
|
332
|
-
const buildManifestEntry = (publicDir, url)=>{
|
|
333
|
-
return new Promise((resolve$1, reject)=>{
|
|
334
|
-
const cHash = crypto.createHash("MD5");
|
|
335
|
-
const stream = fs$1.createReadStream(resolve(publicDir, url));
|
|
336
|
-
stream.on("error", (err)=>{
|
|
337
|
-
reject(err);
|
|
338
|
-
});
|
|
339
|
-
stream.on("data", (chunk)=>{
|
|
340
|
-
cHash.update(chunk);
|
|
341
|
-
});
|
|
342
|
-
stream.on("end", ()=>{
|
|
343
|
-
return resolve$1({
|
|
344
|
-
url,
|
|
345
|
-
revision: `${cHash.digest("hex")}`
|
|
346
|
-
});
|
|
347
|
-
});
|
|
348
|
-
});
|
|
349
|
-
};
|
|
350
|
-
const lookupAdditionalPrecacheEntries = (serwistOptions)=>{
|
|
351
|
-
return serwistOptions.additionalPrecacheEntries || [];
|
|
352
|
-
};
|
|
353
|
-
const normalizeIconPath = (path)=>{
|
|
354
|
-
return path.startsWith("/") ? path.substring(1) : path;
|
|
355
|
-
};
|
|
356
|
-
const configureStaticAssets = async (resolvedPluginOptions, viteConfig)=>{
|
|
357
|
-
const { injectManifest, includeAssets } = resolvedPluginOptions;
|
|
358
|
-
const { publicDir } = viteConfig;
|
|
359
|
-
const globs = [];
|
|
360
|
-
const manifestEntries = lookupAdditionalPrecacheEntries(injectManifest);
|
|
361
|
-
if (includeAssets) {
|
|
362
|
-
if (Array.isArray(includeAssets)) globs.push(...includeAssets.map(normalizeIconPath));
|
|
363
|
-
else globs.push(normalizeIconPath(includeAssets));
|
|
364
|
-
}
|
|
365
|
-
if (globs.length > 0) {
|
|
366
|
-
let assets = await fg(globs, {
|
|
367
|
-
cwd: publicDir,
|
|
368
|
-
onlyFiles: true,
|
|
369
|
-
unique: true
|
|
370
|
-
});
|
|
371
|
-
if (manifestEntries.length > 0) {
|
|
372
|
-
const included = manifestEntries.map((me)=>{
|
|
373
|
-
if (typeof me === "string") return me;
|
|
374
|
-
return me.url;
|
|
375
|
-
});
|
|
376
|
-
assets = assets.filter((a)=>!included.includes(a));
|
|
377
|
-
}
|
|
378
|
-
const assetsEntries = await Promise.all(assets.map((a)=>{
|
|
379
|
-
return buildManifestEntry(publicDir, a);
|
|
380
|
-
}));
|
|
381
|
-
manifestEntries.push(...assetsEntries);
|
|
382
|
-
}
|
|
383
|
-
if (manifestEntries.length > 0) {
|
|
384
|
-
injectManifest.additionalPrecacheEntries = manifestEntries;
|
|
385
|
-
}
|
|
386
|
-
};
|
|
387
|
-
|
|
388
|
-
const resolveOptions = async (options, viteConfig)=>{
|
|
389
|
-
const { type = "classic", mode = process$1.env.NODE_ENV === "production" || process$1.env.NODE_ENV === "development" ? 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;
|
|
390
|
-
const basePath = resolveBasePath(base);
|
|
391
|
-
const scope = _scope || basePath;
|
|
392
|
-
let assetsDir = slash(viteConfig.build.assetsDir ?? "assets");
|
|
393
|
-
if (assetsDir[assetsDir.length - 1] !== "/") assetsDir += "/";
|
|
394
|
-
const resolvedDevOptions = {
|
|
395
|
-
bundle: true,
|
|
396
|
-
minify: false,
|
|
397
|
-
...devOptions
|
|
398
|
-
};
|
|
399
|
-
const dontCacheBustURLsMatching = new RegExp(`^${assetsDir.replace(/^\.*?\//, "")}`);
|
|
400
|
-
const { validateViteInjectManifestOptions } = await loadSerwistBuild();
|
|
401
|
-
const validatedInjectManifest = await validateViteInjectManifestOptions(injectManifest);
|
|
402
|
-
const { swSrc, swDest, ...userInjectManifest } = validatedInjectManifest || {};
|
|
403
|
-
const resolvedPluginOptions = {
|
|
404
|
-
base: basePath,
|
|
405
|
-
type,
|
|
406
|
-
mode,
|
|
407
|
-
injectRegister,
|
|
408
|
-
registerType,
|
|
409
|
-
useCredentials,
|
|
410
|
-
swUrl,
|
|
411
|
-
injectManifest: {
|
|
412
|
-
dontCacheBustURLsMatching,
|
|
413
|
-
...userInjectManifest,
|
|
414
|
-
swSrc: path.resolve(viteConfig.root, swSrc),
|
|
415
|
-
swDest: path.resolve(viteConfig.root, viteConfig.build.outDir, swDest),
|
|
416
|
-
disablePrecacheManifest: !viteConfig.isProduction
|
|
417
|
-
},
|
|
418
|
-
scope,
|
|
419
|
-
minify,
|
|
420
|
-
includeAssets,
|
|
421
|
-
disable,
|
|
422
|
-
integration,
|
|
423
|
-
buildBase: buildBase ?? basePath,
|
|
424
|
-
injectManifestRollupOptions: {
|
|
425
|
-
plugins,
|
|
426
|
-
rollupOptions,
|
|
427
|
-
format: rollupFormat
|
|
428
|
-
},
|
|
429
|
-
devOptions: resolvedDevOptions
|
|
430
|
-
};
|
|
431
|
-
const calculateHash = !resolvedPluginOptions.disable && resolvedPluginOptions.includeAssets && viteConfig.command === "build";
|
|
432
|
-
if (calculateHash) await configureStaticAssets(resolvedPluginOptions, viteConfig);
|
|
433
|
-
return resolvedPluginOptions;
|
|
434
|
-
};
|
|
435
|
-
|
|
436
|
-
const mainPlugin = (ctx, api)=>{
|
|
437
|
-
return {
|
|
438
|
-
name: "@serwist/vite",
|
|
439
|
-
enforce: "pre",
|
|
440
|
-
config () {
|
|
441
|
-
return {
|
|
442
|
-
ssr: {
|
|
443
|
-
noExternal: []
|
|
444
|
-
}
|
|
445
|
-
};
|
|
446
|
-
},
|
|
447
|
-
async configResolved (config) {
|
|
448
|
-
ctx.viteConfig = config;
|
|
449
|
-
ctx.userOptions?.integration?.configureOptions?.(config, ctx.userOptions);
|
|
450
|
-
ctx.options = await resolveOptions(ctx.userOptions, config);
|
|
451
|
-
},
|
|
452
|
-
resolveId (id) {
|
|
453
|
-
if (id === INTERNAL_SERWIST_VIRTUAL) {
|
|
454
|
-
return RESOLVED_INTERNAL_SERWIST_VIRTUAL;
|
|
455
|
-
}
|
|
456
|
-
return undefined;
|
|
457
|
-
},
|
|
458
|
-
load (id) {
|
|
459
|
-
if (id === RESOLVED_INTERNAL_SERWIST_VIRTUAL) {
|
|
460
|
-
return `export const swUrl = "${path.posix.join(ctx.options.buildBase, ctx.options.swUrl)}";
|
|
461
|
-
export const swScope = "${ctx.options.scope}";
|
|
462
|
-
export const swType = "${ctx.devEnvironment ? "module" : ctx.options.type}";`;
|
|
463
|
-
}
|
|
464
|
-
return undefined;
|
|
465
|
-
},
|
|
466
|
-
api
|
|
467
|
-
};
|
|
468
|
-
};
|
|
469
|
-
|
|
470
|
-
export { createApi as a, createContext as c, devPlugin as d, mainPlugin as m, resolveEntry as r, toFs as t };
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import type { Plugin } from "vite";
|
|
2
|
-
|
|
3
|
-
import type { SerwistViteContext } from "../../context.js";
|
|
4
|
-
import type { SerwistViteApi } from "../../types.js";
|
|
5
|
-
|
|
6
|
-
export const buildPlugin = (ctx: SerwistViteContext, api: SerwistViteApi) => {
|
|
7
|
-
return <Plugin>{
|
|
8
|
-
name: "@serwist/vite/integration-svelte:build",
|
|
9
|
-
apply: "build",
|
|
10
|
-
enforce: "pre",
|
|
11
|
-
closeBundle: {
|
|
12
|
-
sequential: true,
|
|
13
|
-
enforce: "pre",
|
|
14
|
-
async handler() {
|
|
15
|
-
if (api && !api.disabled && ctx.viteConfig.build.ssr) {
|
|
16
|
-
await api.generateSW();
|
|
17
|
-
}
|
|
18
|
-
},
|
|
19
|
-
},
|
|
20
|
-
};
|
|
21
|
-
};
|