-
Notifications
You must be signed in to change notification settings - Fork 4
/
vite.config.ts
174 lines (167 loc) · 4.47 KB
/
vite.config.ts
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
import * as vite from 'vite'
import preact from '@preact/preset-vite'
import wyw from '@wyw-in-js/vite'
import path from 'node:path'
import templite from 'templite'
import * as fs from 'fs/promises'
import { apiUrl } from './src/api/api-url'
import crypto from 'crypto'
import sharp from 'sharp'
const outDir = 'dist'
const htmlPlugin = (): import('vite').Plugin => {
return {
name: 'html-transform',
transformIndexHtml(html) {
return templite(html, { apiUrl })
},
}
}
const writeIconsPlugin = () => ({
name: 'write-icons',
async writeBundle() {
const iconSrc = await fs.readFile('./src/logo.png')
const iconDir = path.join(outDir, 'icons')
await fs.mkdir(iconDir, { recursive: true })
const background = 'transparent'
const appleBg = '#800080'
const appleWidth = 180
const applePad = Math.round(0.07 * appleWidth)
await Promise.all([
...[512, 192, 180, 32, 16].map((width) =>
sharp(iconSrc)
.resize(width, width, { fit: 'contain', background })
.png()
.toFile(path.join(iconDir, `${width}.png`)),
),
sharp(iconSrc)
.resize(appleWidth - applePad * 2, 180 - applePad * 2, {
fit: 'contain',
background: appleBg,
})
.extend({
top: applePad,
bottom: applePad,
right: applePad,
left: applePad,
background: appleBg,
})
.flatten({ background: appleBg })
.png()
.toFile(path.join(iconDir, 'apple.png')),
])
},
})
let chunks: string[] | undefined
// Separate rollup config for compiling the service worker
// (only during build, not during dev)
const rollupSWConfig: import('rollup').RollupOptions = {
plugins: [
{
// Ensure the service worker is regenerated whenever the main bundle changes
// The service worker includes an array of the files (with their hashes) to cache
name: 'chunks',
resolveId(source) {
if (source === 'chunks') return '\0chunks'
return null
},
load(source) {
const chunksString = JSON.stringify(chunks)
if (source !== '\0chunks') return null
const chunksHash = crypto
.createHash('sha256')
.update(chunksString)
.digest('hex')
.slice(0, 10)
return `
const chunks = ${chunksString}
export default chunks
export const chunksHash = ${JSON.stringify(chunksHash)}
`
},
},
],
}
// https://vitejs.dev/config/
export default vite.defineConfig({
server: {
port: 2733,
},
preview: {
port: 2733,
},
plugins: [
preact(),
// for Linaria (CSS in JS)
{
...wyw({
include: ['**/*.{ts,tsx}'],
babelOptions: {
presets: ['@babel/preset-typescript'],
plugins: [
[
'@babel/transform-react-jsx',
{
runtime: 'automatic',
importSource: 'preact',
useBuiltIns: true, // object.assign instead of _extends
},
],
],
},
}),
enforce: 'pre', // This needs to run before preact/prefresh injects code that Linaria/WYW-in-js can't run in node
},
htmlPlugin(),
],
resolve: {
alias: {
'@': path.resolve(__dirname, './src'),
},
},
build: {
outDir,
rollupOptions: {
output: {
experimentalMinChunkSize: 10_000,
},
preserveEntrySignatures: false,
treeshake: {
preset: 'recommended',
},
plugins: [
{
name: 'rollup-plugin-chunks-json',
writeBundle(_, bundle) {
const chunksJSON = Object.values(bundle)
.filter(
(chunk) =>
chunk.type === 'chunk' || chunk.fileName.endsWith('.css'),
)
.map((chunk) => `/${chunk.fileName}`)
.sort()
chunks = chunksJSON
},
},
{
name: 'compile-sw',
async writeBundle() {
await vite.build({
configFile: false,
build: {
emptyOutDir: false,
lib: {
entry: 'src/sw.ts',
fileName: () => 'sw.js',
name: 'sw',
formats: ['iife'],
},
rollupOptions: rollupSWConfig,
},
})
},
},
writeIconsPlugin(),
],
},
},
})