-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvite.main.config.ts
57 lines (54 loc) · 1.73 KB
/
vite.main.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
import { defineConfig } from 'vite';
import fs from 'fs';
import path from 'path';
export default defineConfig({
plugins: [
irsdkNativeModule(
['node_modules/@irsdk-node/native/build/Release/irsdk_node.node'],
'.vite/build/node_modules/@irsdk-node/native/build/Release/'
),
],
});
// this handles the native module for irsdk-node so vite can bundle it as its currently cjs only
// this plugin will import it using createRequire and copy the native module to the vite build directory
function irsdkNativeModule(nodeFiles: string[], outDir: string) {
const nodeFileMap = new Map(
nodeFiles.map((file) => [path.basename(file), file])
);
return {
name: 'irsdk-native-module-plugin',
resolveId(source: string) {
return nodeFileMap.has(path.basename(source)) ? source : null;
},
transform(code: string, id: string) {
// check platform
if (process.platform !== 'win32') {
return code;
}
const file = nodeFileMap.get(path.basename(id));
if (file) {
return `
import { createRequire } from 'module';
const customRequire = createRequire(import.meta.url);
export const iRacingSdkNode = customRequire('./${file}').iRacingSdkNode;
`;
}
return code;
},
load(id: string) {
return nodeFileMap.has(path.basename(id)) ? '' : null;
},
generateBundle() {
// check platform
if (process.platform !== 'win32') {
return;
}
nodeFileMap.forEach((fileAbs, file) => {
const out = `${outDir}/${file}`;
const nodeFile = fs.readFileSync(fileAbs);
fs.mkdirSync(path.dirname(out), { recursive: true });
fs.writeFileSync(out, nodeFile);
});
},
};
}