-
Notifications
You must be signed in to change notification settings - Fork 1
/
vite.config.ts
76 lines (72 loc) · 2.96 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
/**
* Copyright (c) 2024, RTE (http://www.rte-france.com)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
import react from '@vitejs/plugin-react';
import { CommonServerOptions, defineConfig, PluginOption } from 'vite';
import eslint from 'vite-plugin-eslint';
import svgr from 'vite-plugin-svgr';
import tsconfigPaths from 'vite-tsconfig-paths';
import * as path from 'node:path';
import * as fs from 'node:fs/promises';
import * as url from 'node:url';
import { createRequire } from 'node:module';
const serverSettings: CommonServerOptions = {
port: 3000,
proxy: {
'/api/gateway': {
target: 'http://localhost:9000',
rewrite: (path: string) => path.replace(/^\/api\/gateway/, ''),
},
'/ws/gateway': {
target: 'http://localhost:9000',
rewrite: (path: string) => path.replace(/^\/ws\/gateway/, ''),
ws: true,
},
},
};
export default defineConfig((config) => ({
plugins: [
react(),
eslint({
failOnWarning: config.mode !== 'development',
lintOnStart: true,
}),
svgr(), // works on every import with the pattern "**/*.svg?react"
reactVirtualized(),
tsconfigPaths(), // to resolve absolute path via tsconfig cf https://stackoverflow.com/a/68250175/5092999
],
base: './',
server: serverSettings, // for npm run start
preview: serverSettings, // for npm run serve (use local build)
build: {
outDir: 'build',
},
}));
// Workaround for react-virtualized with vite
// https://github.com/bvaughn/react-virtualized/issues/1632#issuecomment-1483966063
function reactVirtualized(): PluginOption {
const WRONG_CODE = `import { bpfrpt_proptype_WindowScroller } from "../WindowScroller.js";`;
return {
name: 'flat:react-virtualized',
// Note: we cannot use the `transform` hook here
// because libraries are pre-bundled in vite directly,
// plugins aren't able to hack that step currently.
// so instead we manually edit the file in node_modules.
// all we need is to find the timing before pre-bundling.
configResolved: async () => {
const require = createRequire(import.meta.url);
const reactVirtualizedPath = require.resolve('react-virtualized');
const { pathname: reactVirtualizedFilePath } = new url.URL(reactVirtualizedPath, import.meta.url);
const file = reactVirtualizedFilePath.replace(
path.join('dist', 'commonjs', 'index.js'),
path.join('dist', 'es', 'WindowScroller', 'utils', 'onScroll.js')
);
const code = await fs.readFile(file, 'utf-8');
const modified = code.replace(WRONG_CODE, '');
await fs.writeFile(file, modified);
},
};
}