-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.ts
More file actions
106 lines (103 loc) · 2.69 KB
/
vite.config.ts
File metadata and controls
106 lines (103 loc) · 2.69 KB
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
import build from '@hono/vite-build/cloudflare-workers';
import devServer, { defaultOptions } from '@hono/vite-dev-server';
import cloudflareAdapter from '@hono/vite-dev-server/cloudflare';
import preact from '@preact/preset-vite';
import { resolve } from 'node:path';
import { defineConfig, type Plugin } from 'vite';
import { visualizer } from 'rollup-plugin-visualizer';
function serverOnlyPlugin(isClientBuild: boolean): Plugin {
if (!isClientBuild) return { name: 'server-only-noop' };
return {
name: 'server-only',
enforce: 'pre',
load(id: string) {
if (/\.server\.[jt]sx?$/.test(id)) {
return `export const serverLoader = async () => ({});`;
}
},
};
}
export default defineConfig((env) => {
if (env.mode === 'client' || env.mode === 'visualizer') {
return {
resolve: {
alias: [{ find: '@', replacement: resolve(__dirname, './src') }],
},
build: {
target: 'esnext',
sourcemap: true,
cssCodeSplit: true,
assetsDir: 'static',
ssrEmitAssets: true,
outDir: resolve(__dirname, 'dist'),
lib: {
entry: resolve(__dirname, 'src/client'),
name: 'client',
fileName: 'client',
formats: ['es'],
},
rollupOptions: {
input: ['./src/client.tsx'],
output: {
entryFileNames: 'static/client.js',
chunkFileNames: 'static/[name]-[hash].js',
assetFileNames: 'static/[name]-[hash].[ext]',
},
},
copyPublicDir: false,
},
plugins: [
preact(),
serverOnlyPlugin(true),
...(env.mode === 'visualizer'
? [
visualizer({
open: true,
filename: 'dist/stats.html',
sourcemap: true,
gzipSize: true,
}),
]
: []),
],
};
}
return {
resolve: {
alias: [{ find: '@', replacement: resolve(__dirname, './src') }],
},
build: {
target: 'esnext',
assetsDir: 'static',
ssrEmitAssets: true,
minify: true,
rollupOptions: {
onwarn(warning, warn) {
if (
warning.code === 'MODULE_LEVEL_DIRECTIVE' &&
warning.message.includes(`"use client"`)
) {
return;
}
warn(warning);
},
},
},
plugins: [
build({
entry: 'src/server.tsx',
}),
devServer({
entry: 'src/server.tsx',
exclude: [
...defaultOptions.exclude,
/\.scss/,
/\.css/,
/\?url/,
/\?inline/,
],
adapter: cloudflareAdapter,
}),
],
};
});