Repository navigation
Expand file tree
/
Copy pathvite.config.ts
More file actions
294 lines (274 loc) · 7.94 KB
/
Copy pathvite.config.ts
File metadata and controls
294 lines (274 loc) · 7.94 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/// <reference types="vitest" />
import * as path from 'node:path';
import * as fs from 'node:fs';
import { execSync } from 'node:child_process';
import { createRequire } from 'node:module';
import { Plugin, defineConfig, normalizePath } from 'vite';
import vue from '@vitejs/plugin-vue';
import vuetify, { transformAssetUrls } from 'vite-plugin-vuetify';
import { createHtmlPlugin } from 'vite-plugin-html';
import { viteStaticCopy } from 'vite-plugin-static-copy';
import { visualizer } from 'rollup-plugin-visualizer';
import { sentryVitePlugin } from '@sentry/vite-plugin';
import replace from '@rollup/plugin-replace';
import { BASE_URL } from './tests/e2ePorts';
function resolveNodeModulePath(moduleName: string) {
const require = createRequire(import.meta.url);
let modulePath = normalizePath(require.resolve(moduleName));
while (!modulePath.endsWith(moduleName)) {
const newPath = path.posix.dirname(modulePath);
if (newPath === modulePath)
throw new Error(`Could not resolve ${moduleName}`);
modulePath = newPath;
}
return modulePath;
}
function resolvePath(...args: string[]) {
return normalizePath(path.resolve(...args));
}
const PLACEHOLDER_VERSION = '0.0.0';
function getPackageInfo() {
const mainPkgPath = path.resolve(__dirname, 'package.json');
const mainPkg = JSON.parse(fs.readFileSync(mainPkgPath, 'utf-8'));
const vtkJsPath = path.join(
resolveNodeModulePath('@kitware/vtk.js'),
'package.json'
);
const vtkJsPkg = JSON.parse(fs.readFileSync(vtkJsPath, 'utf-8'));
const itkWasmPath = path.join(
resolveNodeModulePath('itk-wasm'),
'package.json'
);
const itkWasmPkg = JSON.parse(fs.readFileSync(itkWasmPath, 'utf-8'));
return {
versions: {
volview: getVolViewVersion(mainPkg.version),
'vtk.js': vtkJsPkg.version,
'itk-wasm': itkWasmPkg.version,
},
};
}
// package.json carries the placeholder 0.0.0 on main; the release workflow
// stamps the real version from the pushed tag before it builds. Builds that
// skip that step (Netlify, local dev) fall back to describing the tag directly.
function getVolViewVersion(pkgVersion: string) {
if (pkgVersion !== PLACEHOLDER_VERSION) return pkgVersion;
return getGitDescribe() ?? pkgVersion;
}
function getGitDescribe() {
try {
const described = execSync('git describe --tags --always')
.toString()
.trim();
return described.replace(/^v/, '');
} catch {
return undefined;
}
}
function getGitShortSha() {
try {
return execSync('git rev-parse --short HEAD').toString().trim();
} catch {
return 'unknown';
}
}
const rootDir = resolvePath(__dirname);
const distDir = resolvePath(rootDir, 'dist');
const { ANALYZE_BUNDLE, SENTRY_AUTH_TOKEN, SENTRY_ORG, SENTRY_PROJECT } =
process.env;
const pkgInfo = getPackageInfo();
function configureSentryPlugin() {
return SENTRY_AUTH_TOKEN && SENTRY_ORG && SENTRY_PROJECT
? sentryVitePlugin({
telemetry: false,
org: SENTRY_ORG,
project: SENTRY_PROJECT,
authToken: SENTRY_AUTH_TOKEN,
})
: ({} as Plugin);
}
// @mdi/font also lists eot, woff and ttf sources. Every supported browser
// takes woff2, but the build would copy all four.
function woff2IconFont(): Plugin {
return {
name: 'woff2-icon-font',
enforce: 'pre',
transform(code, id) {
if (!id.includes('@mdi/font/css/')) return null;
const woff2 = code.match(/url\([^)]*\.woff2[^)]*\)\s*format\("woff2"\)/);
const fontFace = /@font-face\{[^}]*\}/;
if (!woff2 || !fontFace.test(code)) {
return this.error('@mdi/font no longer declares its woff2 font face');
}
return code.replace(
fontFace,
`@font-face{font-family:"Material Design Icons";src:${woff2[0]};font-weight:normal;font-style:normal}`
);
},
};
}
export default defineConfig({
base: './',
build: {
outDir: distDir,
// An inlined font is downloaded with the render blocking stylesheet. As a
// file, a unicode-range subset is only fetched when the page needs it.
assetsInlineLimit: (filePath) =>
/\.(woff2?|ttf|eot)$/.test(filePath) ? false : undefined,
rollupOptions: {
output: {
manualChunks(id) {
if (id.includes('vuetify')) {
return 'vuetify';
}
if (id.includes('vtk.js')) {
return 'vtk.js';
}
if (id.includes('node_modules')) {
return 'vendor';
}
return undefined;
},
},
},
sourcemap: true,
},
define: {
__VERSIONS__: {
volview: pkgInfo.versions.volview,
'vtk.js': pkgInfo.versions['vtk.js'],
'itk-wasm': pkgInfo.versions['itk-wasm'],
},
__GIT_SHORT_SHA__: JSON.stringify(getGitShortSha()),
},
resolve: {
alias: [
{
find: '@',
replacement: rootDir,
},
{
find: '@src',
replacement: resolvePath(rootDir, 'src'),
},
],
},
plugins: [
woff2IconFont(),
{
name: 'virtual-modules',
load(id) {
if (id.includes('@kitware/vtk.js')) {
if (id.includes('ColorMaps.json.js')) {
// We don't use the built-in colormaps
return 'export default [];';
}
// We don't use these classes
if (id.includes('CubeAxesActor') || id.includes('ScalarBarActor')) {
return 'export default {}';
}
// TODO: vtk.js WebGPU isn't ready as of mid-2023
if (id.includes('WebGPU')) {
return 'export default {}';
}
}
return null;
},
},
replace({
preventAssignment: true,
// better sentry treeshaking
__SENTRY_DEBUG__: false,
__SENTRY_TRACING__: false,
}),
vue({ template: { transformAssetUrls } }),
vuetify({
autoImport: true,
}),
createHtmlPlugin({
minify: true,
template: 'index.html',
}),
viteStaticCopy({
targets: [
{
src: resolvePath(
resolveNodeModulePath('itk-wasm'),
'dist/pipeline/web-workers/bundles/itk-wasm-pipeline.min.worker.js'
),
dest: 'itk',
},
{
src: resolvePath(
resolveNodeModulePath('@itk-wasm/image-io'),
'dist/pipelines/*{.wasm,.js,.zst}'
),
dest: 'itk/image-io',
},
{
src: resolvePath(
resolveNodeModulePath('@itk-wasm/dicom'),
'dist/pipelines/*{.wasm,.js,.zst}'
),
dest: 'itk/pipelines',
},
{
src: resolvePath(
resolveNodeModulePath(
'@itk-wasm/morphological-contour-interpolation'
),
'dist/pipelines/*{.wasm,.js,.zst}'
),
dest: 'itk/pipelines',
},
{
src: resolvePath(
rootDir,
'src/io/itk-dicom/emscripten-build/**/dicom*'
),
dest: 'itk/pipelines',
},
{
src: resolvePath(
rootDir,
'src/io/resample/emscripten-build/**/resample*'
),
dest: 'itk/pipelines',
},
],
}),
ANALYZE_BUNDLE
? visualizer({
template: 'treemap',
open: true,
gzipSize: true,
brotliSize: true,
filename: 'bundle-analysis.html',
})
: ({} as Plugin),
configureSentryPlugin(),
],
server: {
// so `npm run test:e2e:dev` can access the webdriver static server temp directory
proxy: {
'/tmp': BASE_URL,
// Local Girder stack, so girder-launched sessions (urls=/api/v1/...)
// work same-origin against the dev server.
'/api': 'http://localhost:8080',
},
},
optimizeDeps: {
exclude: ['itk-wasm'],
},
test: {
environment: 'happy-dom',
// canvas support. See: https://github.com/vitest-dev/vitest/issues/740
maxWorkers: 1,
server: {
deps: {
inline: ['vuetify'],
},
},
setupFiles: ['./tests/setupVitest.ts'],
},
});