-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathvite.config.ts
More file actions
222 lines (192 loc) · 6 KB
/
Copy pathvite.config.ts
File metadata and controls
222 lines (192 loc) · 6 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/// <reference types="vitest" />
import {defineConfig} from 'vitest/config';
import react from '@vitejs/plugin-react';
import {resolve} from 'path';
import {watch as fsWatch, readdirSync, lstatSync, existsSync} from 'fs';
import {execSync} from 'child_process';
import {playwright} from '@vitest/browser-playwright';
const commitHash = execSync('git rev-parse HEAD', {encoding: 'utf8'}).trim();
const vegaUtils = {
isVegaPackage: (packageName: string): boolean => packageName === 'vega' || packageName.startsWith('vega-'),
isVegaModule: (url: string): boolean => url?.includes('vega-') || url?.includes('vega'),
getNodeModulesPath: () => resolve(process.cwd(), 'node_modules'),
getVegaPackageNames: (nodeModulesPath: string): string[] => {
const items = readdirSync(nodeModulesPath);
return items.filter((item) => vegaUtils.isVegaPackage(item));
},
findEntryPoint: (realPath: string): string | null => {
const entryPoints = [
resolve(realPath, 'index.js'),
resolve(realPath, 'src', 'index.js'),
resolve(realPath, 'src', 'index.ts'),
];
return entryPoints.find(existsSync) || null;
},
resolvePackagePath: (packageName: string, nodeModulesPath: string) => {
const packagePath = resolve(nodeModulesPath, packageName);
if (!existsSync(packagePath)) return null;
const stats = lstatSync(packagePath);
if (stats.isSymbolicLink()) {
const realPath = resolve(packagePath);
return {
realPath,
entryPoint: vegaUtils.findEntryPoint(realPath),
srcPath: resolve(realPath, 'src'),
};
}
return null;
},
handleVegaFileChange: (server: any) => {
const moduleGraph = server.moduleGraph;
const modules = Array.from(moduleGraph.urlToModuleMap.values());
const modulesToUpdate: any[] = [];
modules.forEach((module: any) => {
const isVegaModule = vegaUtils.isVegaModule(module.url);
const importsVega =
module.importedModules &&
Array.from(module.importedModules).some((importedModule: any) =>
vegaUtils.isVegaModule(importedModule.id || importedModule.url),
);
if (isVegaModule || importsVega) {
modulesToUpdate.push(module);
moduleGraph.invalidateModule(module);
}
});
if (modulesToUpdate.length > 0) {
server.ws.send({
type: 'update',
updates: modulesToUpdate.map((module) => ({
type: 'js-update' as const,
path: module.url,
acceptedPath: module.url,
timestamp: Date.now(),
})),
});
server.ws.send({
type: 'custom',
event: 'vega-package-updating',
data: {timestamp: Date.now()},
});
}
},
};
function createVegaHMRPlugin() {
return {
name: 'vega-packages-hmr',
enforce: 'pre' as const,
configureServer(server: any) {
const nodeModulesPath = resolve(__dirname, 'node_modules');
const vegaPackageNames = vegaUtils.getVegaPackageNames(nodeModulesPath);
const {vegaPackagePaths, vegaPackageAliases} = vegaPackageNames.reduce(
(acc, packageName) => {
const resolved = vegaUtils.resolvePackagePath(packageName, nodeModulesPath);
if (resolved) {
if (existsSync(resolved.srcPath)) {
acc.vegaPackagePaths.push(resolved.srcPath);
}
if (resolved.entryPoint) {
acc.vegaPackageAliases[packageName] = resolved.entryPoint;
}
}
return acc;
},
{vegaPackagePaths: [] as string[], vegaPackageAliases: {} as Record<string, string>},
);
server.vegaPackageAliases = vegaPackageAliases;
const watchers = vegaPackagePaths.map((srcPath) =>
fsWatch(srcPath, {recursive: true}, (_, filename) => {
if (filename?.match(/\.(ts|js)$/)) {
vegaUtils.handleVegaFileChange(server);
}
}),
);
server.httpServer?.on('close', () => {
watchers.forEach((watcher) => watcher.close());
});
},
resolveId(id: string) {
if (vegaUtils.isVegaPackage(id)) {
const nodeModulesPath = resolve(__dirname, 'node_modules');
const resolved = vegaUtils.resolvePackagePath(id, nodeModulesPath);
return resolved?.entryPoint || null;
}
return null;
},
async transform(code: string, id: string) {
if (id.includes('/vega') && id.includes('/src/')) {
return {
code: `
if (import.meta.hot) {
import.meta.hot.accept();
}
${code}`,
map: null,
};
}
},
};
}
export default defineConfig({
plugins: [
react(),
createVegaHMRPlugin(),
],
define: {
'process.env.VITE_COMMIT_HASH': JSON.stringify(commitHash),
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV),
},
base: '/editor/',
build: {
outDir: 'dist',
emptyOutDir: true,
},
resolve: {
alias: {
'vega-lite/vega-lite-schema.json': resolve(__dirname, 'node_modules/vega-lite/build/vega-lite-schema.json'),
'vega/vega-schema.json': resolve(__dirname, 'node_modules/vega/build/vega-schema.json'),
},
preserveSymlinks: false,
},
server: {
port: 1234,
open: true,
watch: {
ignored: ['!**/node_modules/vega-lite/**'],
followSymlinks: true,
},
fs: {
allow: ['..', resolve(__dirname, '../..')],
},
},
optimizeDeps: {
include: [],
exclude: vegaUtils.getVegaPackageNames(vegaUtils.getNodeModulesPath()),
},
publicDir: 'public',
test: {
projects: [
{
test: {
include: ['tests/unit/*.test.{ts,tsx}'],
name: 'unit',
environment: 'jsdom',
globals: true,
setupFiles: ['tests/setup.tsx'],
},
},
{
test: {
include: ['tests/e2e/**/*.test.ts'],
name: 'runtime',
browser: {
provider: playwright(),
enabled: true,
headless: false,
instances: [{browser: 'chromium'}],
},
globals: true,
},
},
],
},
});