-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.ts
More file actions
164 lines (141 loc) · 3.9 KB
/
vite.config.ts
File metadata and controls
164 lines (141 loc) · 3.9 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
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
import {
defineConfig,
mergeConfig,
type ConfigEnv,
type ConfigPluginContext,
type Plugin,
type PluginOption,
type UserConfig,
} from 'vite';
import react from '@vitejs/plugin-react';
import { crx } from '@crxjs/vite-plugin';
import manifest from './manifest.json';
type CompatRoot = {
rollupOptions?: Record<string, unknown>;
rolldownOptions?: Record<string, unknown>;
[key: string]: unknown;
};
type ConfigResult = Omit<UserConfig, 'plugins'> | null | void;
type ConfigHook = (
this: ConfigPluginContext,
config: UserConfig,
env: ConfigEnv,
) => ConfigResult | Promise<ConfigResult>;
const COMPAT_ROOT_KEYS = ['build', 'worker', 'optimizeDeps'] as const;
function normalizePlugins(plugins: PluginOption): Plugin[] {
if (!plugins) {
return [];
}
if (Array.isArray(plugins)) {
return plugins.flatMap((plugin) => normalizePlugins(plugin));
}
return [plugins as Plugin];
}
function mergeCompatOptions(
modern?: Record<string, unknown>,
legacy?: Record<string, unknown>,
) {
if (!modern) {
return legacy;
}
if (!legacy) {
return modern;
}
return mergeConfig(modern, legacy);
}
function sanitizeCompatRoot<T extends CompatRoot | undefined>(root: T): T {
if (!root || (root.rollupOptions == null && root.rolldownOptions == null)) {
return root;
}
const sanitized = {
...root,
rolldownOptions: mergeCompatOptions(root.rolldownOptions, root.rollupOptions),
};
delete sanitized.rollupOptions;
return sanitized as T;
}
function sanitizeCrxCompatResult(result: unknown) {
if (!result || typeof result !== 'object') {
return result;
}
const config = { ...(result as Record<string, unknown>) };
for (const key of COMPAT_ROOT_KEYS) {
const root = config[key];
if (root && typeof root === 'object') {
config[key] = sanitizeCompatRoot(root as CompatRoot);
}
}
const ssr = config.ssr;
if (ssr && typeof ssr === 'object') {
const nextSsr = { ...(ssr as Record<string, unknown>) };
const optimizeDeps = nextSsr.optimizeDeps;
if (optimizeDeps && typeof optimizeDeps === 'object') {
nextSsr.optimizeDeps = sanitizeCompatRoot(optimizeDeps as CompatRoot);
config.ssr = nextSsr;
}
}
return config;
}
function wrapCrxCompatPlugins(plugins: PluginOption): Plugin[] {
return normalizePlugins(plugins).map((plugin) => {
if (typeof plugin.config !== 'function' || !plugin.name.startsWith('crx:')) {
return plugin;
}
const originalConfig = plugin.config as ConfigHook;
return {
...plugin,
async config(
this: ConfigPluginContext,
config: UserConfig,
env: ConfigEnv,
): Promise<ConfigResult> {
const result = await originalConfig.call(this, config, env);
// Vite 8 exposes `rollupOptions` as a compat alias to `rolldownOptions`.
// CRX plugin hooks spread the resolved config back into their return value,
// which can reintroduce both keys and trigger noisy warnings.
return sanitizeCrxCompatResult(result) as ConfigResult;
},
};
});
}
// https://vitejs.dev/config/
export default defineConfig({
define: {
'process.env.NODE_ENV': '"development"',
},
resolve: {
dedupe: ['react', 'react-dom', 'react/jsx-runtime'],
},
optimizeDeps: {
include: ['react', 'react-dom', 'react/jsx-runtime'],
},
plugins: [react(), ...wrapCrxCompatPlugins(crx({ manifest }))],
server: {
port: 5173,
strictPort: true,
hmr: {
port: 5173,
},
},
build: {
outDir: 'dist',
minify: false,
rollupOptions: {
input: {
index: 'index.html',
settings: 'settings.html',
},
output: {
manualChunks(id) {
if (
id.includes('node_modules/react/') ||
id.includes('node_modules/react-dom/') ||
id.includes('react/jsx-runtime')
) {
return 'react';
}
},
},
},
},
});