@serwist/vite 9.0.0-preview.9 → 9.0.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/chunks/schema.js +56 -0
- package/dist/index.d.ts +8 -7
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +77 -180
- 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 +8 -10
- package/dist/index.worker.d.ts.map +1 -1
- package/dist/index.worker.js +18 -11
- package/dist/lib/api.d.ts.map +1 -0
- package/dist/lib/constants.d.ts +3 -0
- package/dist/lib/constants.d.ts.map +1 -0
- package/dist/{context.d.ts → lib/context.d.ts} +2 -2
- 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 +130 -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 +29 -65
- package/src/index.schema.ts +3 -0
- package/src/index.ts +8 -7
- package/src/index.worker.ts +111 -96
- package/src/lib/constants.ts +2 -0
- package/src/{context.ts → lib/context.ts} +2 -2
- package/src/{log.ts → lib/log.ts} +1 -1
- package/src/{modules.ts → lib/modules.ts} +4 -5
- 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 +3 -3
- package/src/plugins/dev.ts +3 -3
- package/src/plugins/main.ts +15 -9
- package/src/rollup.js +4 -5
- 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 +0 -3
- package/dist/constants.d.ts.map +0 -1
- package/dist/context.d.ts.map +0 -1
- package/dist/index.browser.d.ts +0 -2
- package/dist/index.browser.d.ts.map +0 -1
- package/dist/index.browser.js +0 -13
- package/dist/log.d.ts.map +0 -1
- 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/constants.ts +0 -2
- package/src/index.browser.ts +0 -8
- package/src/options.ts +0 -90
- package/src/types.ts +0 -207
- package/src/utils-types.ts +0 -1
- package/src/virtual.d.ts +0 -5
- /package/dist/{api.d.ts → lib/api.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/{utils.ts → lib/utils.ts} +0 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@serwist/vite",
|
|
3
|
-
"version": "9.0.0
|
|
3
|
+
"version": "9.0.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "A module that integrates Serwist into your Vite application.",
|
|
6
6
|
"files": [
|
|
@@ -30,7 +30,7 @@
|
|
|
30
30
|
},
|
|
31
31
|
"author": "antfu <anthonyfu117@hotmail.com>, Serwist's Team",
|
|
32
32
|
"license": "MIT",
|
|
33
|
-
"repository": "serwist/serwist",
|
|
33
|
+
"repository": "https://github.com/serwist/serwist",
|
|
34
34
|
"bugs": "https://github.com/serwist/serwist/issues",
|
|
35
35
|
"homepage": "https://serwist.pages.dev",
|
|
36
36
|
"sideEffects": false,
|
|
@@ -38,11 +38,14 @@
|
|
|
38
38
|
"types": "./dist/index.d.ts",
|
|
39
39
|
"typesVersions": {
|
|
40
40
|
"*": {
|
|
41
|
-
"browser": [
|
|
42
|
-
"./dist/index.browser.d.ts"
|
|
43
|
-
],
|
|
44
41
|
"worker": [
|
|
45
42
|
"./dist/index.worker.d.ts"
|
|
43
|
+
],
|
|
44
|
+
"schema": [
|
|
45
|
+
"./dist/index.schema.d.ts"
|
|
46
|
+
],
|
|
47
|
+
"typings": [
|
|
48
|
+
"./typings.d.ts"
|
|
46
49
|
]
|
|
47
50
|
}
|
|
48
51
|
},
|
|
@@ -51,82 +54,43 @@
|
|
|
51
54
|
"types": "./dist/index.d.ts",
|
|
52
55
|
"default": "./dist/index.js"
|
|
53
56
|
},
|
|
54
|
-
"./browser": {
|
|
55
|
-
"types": "./dist/index.browser.d.ts",
|
|
56
|
-
"default": "./dist/index.browser.js"
|
|
57
|
-
},
|
|
58
57
|
"./worker": {
|
|
59
58
|
"types": "./dist/index.worker.d.ts",
|
|
60
59
|
"default": "./dist/index.worker.js"
|
|
61
60
|
},
|
|
61
|
+
"./schema": {
|
|
62
|
+
"types": "./dist/index.schema.d.ts",
|
|
63
|
+
"default": "./dist/index.schema.js"
|
|
64
|
+
},
|
|
65
|
+
"./typings": {
|
|
66
|
+
"types": "./typings.d.ts"
|
|
67
|
+
},
|
|
62
68
|
"./package.json": "./package.json"
|
|
63
69
|
},
|
|
64
70
|
"dependencies": {
|
|
65
|
-
"
|
|
66
|
-
"
|
|
67
|
-
"
|
|
68
|
-
"@serwist/build": "9.0.0
|
|
69
|
-
"
|
|
70
|
-
"@serwist/
|
|
71
|
-
"@serwist/window": "9.0.0-preview.9"
|
|
71
|
+
"glob": "10.3.12",
|
|
72
|
+
"kolorist": "1.8.0",
|
|
73
|
+
"zod": "3.22.4",
|
|
74
|
+
"@serwist/build": "9.0.0",
|
|
75
|
+
"serwist": "9.0.0",
|
|
76
|
+
"@serwist/window": "9.0.0"
|
|
72
77
|
},
|
|
73
78
|
"devDependencies": {
|
|
74
|
-
"@
|
|
75
|
-
"
|
|
76
|
-
"
|
|
77
|
-
"
|
|
78
|
-
"@
|
|
79
|
-
"@
|
|
80
|
-
"
|
|
81
|
-
"kolorist": "1.8.0",
|
|
82
|
-
"preact": "10.19.3",
|
|
83
|
-
"prompts": "2.4.2",
|
|
84
|
-
"publint": "0.2.7",
|
|
85
|
-
"react": "18.2.0",
|
|
86
|
-
"rollup": "4.9.6",
|
|
87
|
-
"solid-js": "1.8.14",
|
|
88
|
-
"svelte": "5.0.0-next.48",
|
|
89
|
-
"typescript": "5.4.0-dev.20240206",
|
|
90
|
-
"vite": "5.0.12",
|
|
91
|
-
"vue": "3.4.15",
|
|
92
|
-
"@serwist/constants": "9.0.0-preview.9",
|
|
93
|
-
"@serwist/sw": "9.0.0-preview.9"
|
|
79
|
+
"@types/node": "20.12.7",
|
|
80
|
+
"rollup": "4.14.3",
|
|
81
|
+
"typescript": "5.5.0-dev.20240415",
|
|
82
|
+
"vite": "5.2.8",
|
|
83
|
+
"@serwist/configs": "9.0.0",
|
|
84
|
+
"@serwist/utils": "9.0.0",
|
|
85
|
+
"serwist": "9.0.0"
|
|
94
86
|
},
|
|
95
87
|
"peerDependencies": {
|
|
96
|
-
"@sveltejs/kit": "^1.0.0 || ^2.0.0",
|
|
97
|
-
"preact": "^10.0.0",
|
|
98
|
-
"react": "^18.0.0",
|
|
99
|
-
"solid-js": "^1.8.7",
|
|
100
|
-
"svelte": "^4.0.0 || ^5.0.0",
|
|
101
88
|
"typescript": ">=5.0.0",
|
|
102
|
-
"vite": "^5.0.0"
|
|
103
|
-
"vue": "^3.0.0",
|
|
104
|
-
"@serwist/sw": "9.0.0-preview.9"
|
|
89
|
+
"vite": "^5.0.0"
|
|
105
90
|
},
|
|
106
91
|
"peerDependenciesMeta": {
|
|
107
|
-
"@serwist/sw": {
|
|
108
|
-
"optional": true
|
|
109
|
-
},
|
|
110
|
-
"@sveltejs/kit": {
|
|
111
|
-
"optional": true
|
|
112
|
-
},
|
|
113
|
-
"preact": {
|
|
114
|
-
"optional": true
|
|
115
|
-
},
|
|
116
|
-
"react": {
|
|
117
|
-
"optional": true
|
|
118
|
-
},
|
|
119
|
-
"solid-js": {
|
|
120
|
-
"optional": true
|
|
121
|
-
},
|
|
122
|
-
"svelte": {
|
|
123
|
-
"optional": true
|
|
124
|
-
},
|
|
125
92
|
"typescript": {
|
|
126
93
|
"optional": true
|
|
127
|
-
},
|
|
128
|
-
"vue": {
|
|
129
|
-
"optional": true
|
|
130
94
|
}
|
|
131
95
|
},
|
|
132
96
|
"scripts": {
|
package/src/index.ts
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import type { Plugin } from "vite";
|
|
2
2
|
|
|
3
|
-
import { createApi } from "./api.js";
|
|
4
|
-
import { createContext } from "./context.js";
|
|
3
|
+
import { createApi } from "./lib/api.js";
|
|
4
|
+
import { createContext } from "./lib/context.js";
|
|
5
|
+
import type { DevOptions, ExtendManifestEntriesHook, Hooks, PluginOptions, PluginOptionsComplete, SerwistViteApi } from "./lib/types.js";
|
|
6
|
+
import { resolveEntry, toFs } from "./lib/utils.js";
|
|
7
|
+
import { validateInjectManifestOptions } from "./lib/validator.js";
|
|
5
8
|
import { buildPlugin } from "./plugins/build.js";
|
|
6
9
|
import { devPlugin } from "./plugins/dev.js";
|
|
7
10
|
import { mainPlugin } from "./plugins/main.js";
|
|
8
|
-
import type { PluginOptions } from "./types.js";
|
|
9
|
-
import { resolveEntry, toFs } from "./utils.js";
|
|
10
11
|
|
|
11
12
|
/**
|
|
12
13
|
* Integrates Serwist into your Vite app.
|
|
@@ -20,6 +21,6 @@ export const serwist = (userOptions: PluginOptions): Plugin[] => {
|
|
|
20
21
|
};
|
|
21
22
|
|
|
22
23
|
// This allows for customization.
|
|
23
|
-
export { buildPlugin as build, createApi, createContext, devPlugin as dev, mainPlugin as main, resolveEntry, toFs };
|
|
24
|
-
export type { SerwistViteContext } from "./context.js";
|
|
25
|
-
export
|
|
24
|
+
export { buildPlugin as build, createApi, createContext, devPlugin as dev, mainPlugin as main, resolveEntry, toFs, validateInjectManifestOptions };
|
|
25
|
+
export type { SerwistViteContext } from "./lib/context.js";
|
|
26
|
+
export type { PluginOptions, PluginOptionsComplete, Hooks, ExtendManifestEntriesHook, DevOptions, SerwistViteApi };
|
package/src/index.worker.ts
CHANGED
|
@@ -1,105 +1,120 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { CacheFirst, NetworkFirst, StaleWhileRevalidate } from "
|
|
3
|
-
import type { RuntimeCaching } from "@serwist/sw";
|
|
1
|
+
import type { RuntimeCaching } from "serwist";
|
|
2
|
+
import { CacheFirst, ExpirationPlugin, NetworkFirst, StaleWhileRevalidate } from "serwist";
|
|
4
3
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
4
|
+
/**
|
|
5
|
+
* The default, recommended list of caching strategies for applications
|
|
6
|
+
* built with Vite.
|
|
7
|
+
*
|
|
8
|
+
* @see https://serwist.pages.dev/docs/vite/worker-exports#default-cache
|
|
9
|
+
*/
|
|
10
|
+
export const defaultCache: RuntimeCaching[] = import.meta.env.DEV
|
|
11
|
+
? []
|
|
12
|
+
: [
|
|
13
|
+
{
|
|
14
|
+
matcher: /^https:\/\/fonts\.(?:googleapis|gstatic)\.com\/.*/i,
|
|
15
|
+
handler: new CacheFirst({
|
|
16
|
+
cacheName: "google-fonts",
|
|
17
|
+
plugins: [
|
|
18
|
+
new ExpirationPlugin({
|
|
19
|
+
maxEntries: 4,
|
|
20
|
+
maxAgeSeconds: 365 * 24 * 60 * 60, // 365 days
|
|
21
|
+
maxAgeFrom: "last-used",
|
|
22
|
+
}),
|
|
23
|
+
],
|
|
14
24
|
}),
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
25
|
+
},
|
|
26
|
+
{
|
|
27
|
+
matcher: /\.(?:eot|otf|ttc|ttf|woff|woff2|font.css)$/i,
|
|
28
|
+
handler: new StaleWhileRevalidate({
|
|
29
|
+
cacheName: "static-font-assets",
|
|
30
|
+
plugins: [
|
|
31
|
+
new ExpirationPlugin({
|
|
32
|
+
maxEntries: 4,
|
|
33
|
+
maxAgeSeconds: 7 * 24 * 60 * 60, // 7 days
|
|
34
|
+
maxAgeFrom: "last-used",
|
|
35
|
+
}),
|
|
36
|
+
],
|
|
26
37
|
}),
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
matcher: /\.(?:jpg|jpeg|gif|png|svg|ico|webp)$/i,
|
|
41
|
+
handler: new StaleWhileRevalidate({
|
|
42
|
+
cacheName: "static-image-assets",
|
|
43
|
+
plugins: [
|
|
44
|
+
new ExpirationPlugin({
|
|
45
|
+
maxEntries: 64,
|
|
46
|
+
maxAgeSeconds: 24 * 60 * 60, // 24 hours
|
|
47
|
+
maxAgeFrom: "last-used",
|
|
48
|
+
}),
|
|
49
|
+
],
|
|
38
50
|
}),
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
51
|
+
},
|
|
52
|
+
{
|
|
53
|
+
matcher: /\.(?:js)$/i,
|
|
54
|
+
handler: new StaleWhileRevalidate({
|
|
55
|
+
cacheName: "static-js-assets",
|
|
56
|
+
plugins: [
|
|
57
|
+
new ExpirationPlugin({
|
|
58
|
+
maxEntries: 32,
|
|
59
|
+
maxAgeSeconds: 24 * 60 * 60, // 24 hours
|
|
60
|
+
maxAgeFrom: "last-used",
|
|
61
|
+
}),
|
|
62
|
+
],
|
|
50
63
|
}),
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
64
|
+
},
|
|
65
|
+
{
|
|
66
|
+
matcher: /\.(?:css|less)$/i,
|
|
67
|
+
handler: new StaleWhileRevalidate({
|
|
68
|
+
cacheName: "static-style-assets",
|
|
69
|
+
plugins: [
|
|
70
|
+
new ExpirationPlugin({
|
|
71
|
+
maxEntries: 32,
|
|
72
|
+
maxAgeSeconds: 24 * 60 * 60, // 24 hours
|
|
73
|
+
maxAgeFrom: "last-used",
|
|
74
|
+
}),
|
|
75
|
+
],
|
|
62
76
|
}),
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
77
|
+
},
|
|
78
|
+
{
|
|
79
|
+
matcher: /\.(?:json|xml|csv)$/i,
|
|
80
|
+
handler: new NetworkFirst({
|
|
81
|
+
cacheName: "static-data-assets",
|
|
82
|
+
plugins: [
|
|
83
|
+
new ExpirationPlugin({
|
|
84
|
+
maxEntries: 32,
|
|
85
|
+
maxAgeSeconds: 24 * 60 * 60, // 24 hours
|
|
86
|
+
maxAgeFrom: "last-used",
|
|
87
|
+
}),
|
|
88
|
+
],
|
|
74
89
|
}),
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
90
|
+
},
|
|
91
|
+
{
|
|
92
|
+
matcher: /\/api\/.*$/i,
|
|
93
|
+
method: "GET",
|
|
94
|
+
handler: new NetworkFirst({
|
|
95
|
+
cacheName: "apis",
|
|
96
|
+
plugins: [
|
|
97
|
+
new ExpirationPlugin({
|
|
98
|
+
maxEntries: 16,
|
|
99
|
+
maxAgeSeconds: 24 * 60 * 60, // 24 hours
|
|
100
|
+
maxAgeFrom: "last-used",
|
|
101
|
+
}),
|
|
102
|
+
],
|
|
103
|
+
networkTimeoutSeconds: 10, // fallback to cache if API does not response within 10 seconds
|
|
87
104
|
}),
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
105
|
+
},
|
|
106
|
+
{
|
|
107
|
+
matcher: /.*/i,
|
|
108
|
+
handler: new NetworkFirst({
|
|
109
|
+
cacheName: "others",
|
|
110
|
+
plugins: [
|
|
111
|
+
new ExpirationPlugin({
|
|
112
|
+
maxEntries: 32,
|
|
113
|
+
maxAgeSeconds: 24 * 60 * 60, // 24 hours
|
|
114
|
+
maxAgeFrom: "last-used",
|
|
115
|
+
}),
|
|
116
|
+
],
|
|
117
|
+
networkTimeoutSeconds: 10,
|
|
100
118
|
}),
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
}),
|
|
104
|
-
},
|
|
105
|
-
] satisfies RuntimeCaching[];
|
|
119
|
+
},
|
|
120
|
+
];
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { ResolvedConfig } from "vite";
|
|
2
2
|
|
|
3
|
-
import type { PluginOptions,
|
|
3
|
+
import type { PluginOptions, PluginOptionsComplete } from "./types.js";
|
|
4
4
|
|
|
5
5
|
export type SerwistViteFrameworks = "nuxt";
|
|
6
6
|
|
|
@@ -21,7 +21,7 @@ export interface SerwistViteContext {
|
|
|
21
21
|
* Note: this is different from `userOptions` in that it has been parsed, whereas
|
|
22
22
|
* `userOptions` is the raw configuration that the user provides us.
|
|
23
23
|
*/
|
|
24
|
-
options:
|
|
24
|
+
options: PluginOptionsComplete;
|
|
25
25
|
useImportRegister: boolean;
|
|
26
26
|
/**
|
|
27
27
|
* Is the plugin running on dev?
|
|
@@ -2,7 +2,7 @@ import type { BuildResult } from "@serwist/build";
|
|
|
2
2
|
import { cyan, dim, green, yellow } from "kolorist";
|
|
3
3
|
import type { ResolvedConfig } from "vite";
|
|
4
4
|
|
|
5
|
-
import { version } from "
|
|
5
|
+
import { version } from "../../package.json";
|
|
6
6
|
|
|
7
7
|
export const logSerwistResult = (buildResult: Pick<BuildResult, "count" | "size" | "warnings">, viteOptions: ResolvedConfig) => {
|
|
8
8
|
const { logLevel = "info" } = viteOptions;
|
|
@@ -21,9 +21,8 @@ interface BuildResult extends SerwistBuild.GetManifestResult {
|
|
|
21
21
|
manifestString: string;
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
-
export const injectManifest = async (
|
|
25
|
-
const {
|
|
26
|
-
const options = await validateViteInjectManifestOptions(config);
|
|
24
|
+
export const injectManifest = async (options: SerwistBuild.GetManifestOptionsComplete): Promise<BuildResult> => {
|
|
25
|
+
const { getFileManifestEntries, stringify } = await loadSerwistBuild();
|
|
27
26
|
const { count, size, manifestEntries, warnings } = await getFileManifestEntries(options);
|
|
28
27
|
const manifestString = manifestEntries === undefined ? "undefined" : stringify(manifestEntries);
|
|
29
28
|
return {
|
|
@@ -36,7 +35,7 @@ export const injectManifest = async (config: SerwistBuild.ViteInjectManifestOpti
|
|
|
36
35
|
};
|
|
37
36
|
|
|
38
37
|
export const generateServiceWorker = async (ctx: SerwistViteContext) => {
|
|
39
|
-
const {
|
|
38
|
+
const { plugins, rollupOptions, rollupFormat } = ctx.options;
|
|
40
39
|
|
|
41
40
|
const parsedSwDest = path.parse(ctx.options.injectManifest.swDest);
|
|
42
41
|
|
|
@@ -152,7 +151,7 @@ export const generateServiceWorker = async (ctx: SerwistViteContext) => {
|
|
|
152
151
|
lib: {
|
|
153
152
|
entry: ctx.options.injectManifest.swSrc,
|
|
154
153
|
name: "app",
|
|
155
|
-
formats: [
|
|
154
|
+
formats: [rollupFormat],
|
|
156
155
|
},
|
|
157
156
|
rollupOptions: {
|
|
158
157
|
...rollupOptions,
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import path from "node:path";
|
|
2
|
+
import process from "node:process";
|
|
3
|
+
|
|
4
|
+
import type { ResolvedConfig } from "vite";
|
|
5
|
+
|
|
6
|
+
import type { PluginOptions, PluginOptionsComplete } from "./types.js";
|
|
7
|
+
import { resolveBasePath, slash } from "./utils.js";
|
|
8
|
+
import { validateInjectManifestOptions } from "./validator.js";
|
|
9
|
+
|
|
10
|
+
const prepareConfigForValidation = (
|
|
11
|
+
viteConfig: ResolvedConfig,
|
|
12
|
+
{
|
|
13
|
+
mode = process.env.NODE_ENV === "production" || process.env.NODE_ENV === "development" ? process.env.NODE_ENV : "production",
|
|
14
|
+
base = viteConfig.base,
|
|
15
|
+
scope: _scope,
|
|
16
|
+
devOptions,
|
|
17
|
+
...injectManifest
|
|
18
|
+
}: PluginOptions,
|
|
19
|
+
) => {
|
|
20
|
+
const basePath = resolveBasePath(base);
|
|
21
|
+
return {
|
|
22
|
+
mode,
|
|
23
|
+
base: basePath,
|
|
24
|
+
scope: _scope || basePath,
|
|
25
|
+
devOptions,
|
|
26
|
+
...injectManifest,
|
|
27
|
+
};
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export const resolveOptions = async (options: PluginOptions, viteConfig: ResolvedConfig): Promise<PluginOptionsComplete> => {
|
|
31
|
+
const {
|
|
32
|
+
mode,
|
|
33
|
+
type,
|
|
34
|
+
scope,
|
|
35
|
+
base,
|
|
36
|
+
disable,
|
|
37
|
+
integration,
|
|
38
|
+
swUrl,
|
|
39
|
+
swSrc,
|
|
40
|
+
swDest,
|
|
41
|
+
plugins,
|
|
42
|
+
rollupFormat,
|
|
43
|
+
rollupOptions,
|
|
44
|
+
devOptions,
|
|
45
|
+
...userInjectManifest
|
|
46
|
+
} = await validateInjectManifestOptions(prepareConfigForValidation(viteConfig, options));
|
|
47
|
+
|
|
48
|
+
let assetsDir = slash(viteConfig.build.assetsDir ?? "assets");
|
|
49
|
+
if (assetsDir[assetsDir.length - 1] !== "/") assetsDir += "/";
|
|
50
|
+
|
|
51
|
+
// remove './' prefix from assetsDir
|
|
52
|
+
const dontCacheBustURLsMatching = new RegExp(`^${assetsDir.replace(/^\.*?\//, "")}`);
|
|
53
|
+
|
|
54
|
+
const resolvedPluginOptions = {
|
|
55
|
+
mode,
|
|
56
|
+
type,
|
|
57
|
+
scope,
|
|
58
|
+
base,
|
|
59
|
+
disable,
|
|
60
|
+
integration,
|
|
61
|
+
swUrl,
|
|
62
|
+
plugins,
|
|
63
|
+
rollupFormat,
|
|
64
|
+
rollupOptions,
|
|
65
|
+
devOptions,
|
|
66
|
+
injectManifest: {
|
|
67
|
+
dontCacheBustURLsMatching,
|
|
68
|
+
...userInjectManifest,
|
|
69
|
+
swSrc: path.resolve(viteConfig.root, swSrc),
|
|
70
|
+
swDest: path.resolve(viteConfig.root, viteConfig.build.outDir, swDest),
|
|
71
|
+
disablePrecacheManifest: !viteConfig.isProduction,
|
|
72
|
+
},
|
|
73
|
+
} satisfies PluginOptionsComplete;
|
|
74
|
+
|
|
75
|
+
return resolvedPluginOptions;
|
|
76
|
+
};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { basePartial, globPartial, injectPartial, requiredGlobDirectoryPartial, requiredSwDestPartial } from "@serwist/build/schema";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
|
|
4
|
+
export const hooks = z.object({
|
|
5
|
+
beforeBuildServiceWorker: z.function(z.tuple([z.any()]), z.union([z.promise(z.void()), z.void()])).optional(),
|
|
6
|
+
closeBundleOrder: z.union([z.literal("pre"), z.literal("post"), z.null()]).optional(),
|
|
7
|
+
configureOptions: z.function(z.tuple([z.any(), z.any()]), z.union([z.promise(z.void()), z.void()])).optional(),
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
export const devOptions = z.object({
|
|
11
|
+
bundle: z.boolean().default(true),
|
|
12
|
+
minify: z.union([z.boolean(), z.literal("terser"), z.literal("esbuild")]).default(false),
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
export const injectManifestPartial = z.object({
|
|
16
|
+
mode: z.union([z.literal("development"), z.literal("production")]),
|
|
17
|
+
type: z.union([z.literal("classic"), z.literal("module")]).default("classic"),
|
|
18
|
+
scope: z.string(),
|
|
19
|
+
base: z.string(),
|
|
20
|
+
disable: z.boolean().default(false),
|
|
21
|
+
integration: hooks.default({}),
|
|
22
|
+
swUrl: z.string().default("/sw.js"),
|
|
23
|
+
plugins: z.array(z.any()).default([]),
|
|
24
|
+
rollupFormat: z.union([z.literal("es"), z.literal("iife")]).default("es"),
|
|
25
|
+
rollupOptions: z.record(z.string(), z.any()).default({}),
|
|
26
|
+
devOptions: devOptions.default({}),
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
export const injectManifestOptions = basePartial
|
|
30
|
+
.merge(globPartial)
|
|
31
|
+
.merge(injectPartial)
|
|
32
|
+
.merge(requiredSwDestPartial)
|
|
33
|
+
.merge(requiredGlobDirectoryPartial)
|
|
34
|
+
.merge(injectManifestPartial)
|
|
35
|
+
.strict("Do not pass invalid properties to InjectManifestOptions!");
|