-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvite.config.js
184 lines (157 loc) · 5.29 KB
/
vite.config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import { sveltekit } from '@sveltejs/kit/vite';
import { VitePWA } from 'vite-plugin-pwa';
import replace from '@rollup/plugin-replace'
function configureOptions(viteOptions, options) {
if (!options.integration)
options.integration = {}
// will run closeBundle sequential hook before kit adapter
options.integration.closeBundleOrder = 'pre'
const {
base = viteOptions.build.base ?? '/',
adapterFallback,
outDir = '.svelte-kit',
} = options.integration ?? {}
// Vite will copy public folder to the globDirectory after pwa plugin runs:
// globDirectory is the build folder.
// SvelteKit will copy to the globDirectory before pwa plugin runs (via Vite client build in writeBundle hook):
// globDirectory is the kit client output folder.
// We need to disable includeManifestIcons: any icon in the static folder will be twice in the sw's precache manifest.
if (typeof options.includeManifestIcons === 'undefined')
options.includeManifestIcons = false
let config
if (options.strategies === 'injectManifest') {
options.injectManifest = options.injectManifest ?? {}
config = options.injectManifest
}
else {
options.workbox = options.workbox ?? {}
if (!options.workbox.navigateFallback)
options.workbox.navigateFallback = adapterFallback ?? base
config = options.workbox
}
// SvelteKit outDir is `.svelte-kit/output/client`.
// We need to include the parent folder since SvelteKit will generate SSG in `.svelte-kit/output/prerendered` folder.
if (!config.globDirectory)
config.globDirectory = `${outDir}/output`
if (!config.modifyURLPrefix)
config.globPatterns = buildGlobPatterns(config.globPatterns)
// Vite generates <name>.<hash>.<ext> layout while SvelteKit generates <name>-<hash>.<ext> (Rollup default)
// Vite and SvelteKit are not aligned: pwa plugin will use /\.[a-f0-9]{8}\./ by default: #164 optimize workbox work
if (!config.dontCacheBustURLsMatching)
config.dontCacheBustURLsMatching = /-[a-f0-9]{8}\./
if (!config.manifestTransforms)
config.manifestTransforms = [createManifestTransform(base, options.svelteKitOptions)]
}
function createManifestTransform(base, options) {
return async (entries) => {
const suffix = options?.trailingSlash === 'always' ? '/' : ''
let adapterFallback = options?.adapterFallback
let excludeFallback = false
// the fallback will be always generated by SvelteKit.
// The adapter will copy the fallback only if it is provided in its options: we need to exclude it
if (!adapterFallback) {
adapterFallback = 'prerendered/fallback.html'
excludeFallback = true
}
const manifest = entries.filter(({ url }) => !(excludeFallback && url === adapterFallback)).map((e) => {
let url = e.url
// client assets in `.svelte-kit/output/client` folder.
// SSG pages in `.svelte-kit/output/prerendered/pages` folder.
// fallback page in `.svelte-kit/output/prerendered` folder (fallback.html is the default).
if (url.startsWith('client/'))
url = url.slice(7)
else if (url.startsWith('prerendered/pages/'))
url = url.slice(18)
else if (url.startsWith('prerendered/'))
url = url.slice(12)
if (url.endsWith('.html')) {
if (url.startsWith('/'))
url = url.slice(1)
e.url = url === 'index.html' ? `${base}` : `${base}${url.slice(0, url.lastIndexOf('.'))}${suffix}`
}
else {
e.url = url
}
return e
})
return { manifest }
}
}
function buildGlobPatterns(globPatterns) {
if (globPatterns) {
if (!globPatterns.some(g => g.startsWith('prerendered/')))
globPatterns.push('prerendered/**/*.html')
if (!globPatterns.some(g => g.startsWith('client/')))
globPatterns.push('client/**/*.{js,css,ico,png,svg,webp}')
return globPatterns
}
return ['client/**/*.{js,css,ico,png,svg,webp}', 'prerendered/**/*.html']
}
/** @type {import('vite-plugin-pwa').VitePWAOptions} */
const pwa/*[main, build, dev]*/ = VitePWA({
srcDir: './src',
mode: 'development',
scope: '/',
base: '/',
manifest: {
short_name: 'PWA Router',
name: 'PWA Router',
start_url: '/',
scope: '/',
display: 'standalone',
theme_color: "#ffffff",
background_color: "#ffffff",
icons: [
{
src: '/pwa-192x192.png',
sizes: '192x192',
type: 'image/png',
},
{
src: '/pwa-512x512.png',
sizes: '512x512',
type: 'image/png',
},
{
src: '/pwa-512x512.png',
sizes: '512x512',
type: 'image/png',
purpose: 'any maskable',
},
],
},
workbox: {
globPatterns: ['client/**/*.{js,css,ico,png,svg,webp,woff,woff2}']
},
integration: {
closeBundleOrder: 'pre',
configureOptions,
}
})
/*
const { closeBundle, ...rest } = build
delete build.closeBundle
*/
/** @type {import('vite').UserConfig} */
const config = {
logLevel: 'info',
build: {
minify: false,
},
plugins: [
replace({ __DATE__: new Date().toISOString(), __RELOAD_SW__: 'false' }),
sveltekit(),
pwa,
/* [main, {
...rest,
closeBundle: {
sequential: true,
order: 'pre',
async handler() {
await closeBundle.apply(this)
},
}
}, dev],*/
]
};
export default config;