forked from tariknz/irdashies
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.main.config.ts
More file actions
92 lines (88 loc) · 2.58 KB
/
Copy pathvite.main.config.ts
File metadata and controls
92 lines (88 loc) · 2.58 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
import { defineConfig } from 'vite';
import { execSync } from 'child_process';
import path from 'path';
import fs from 'fs';
// Get git hash
const getGitHash = () => {
try {
return execSync('git rev-parse --short HEAD').toString().trim();
} catch (ex) {
console.warn('Error getting git hash', ex);
return 'unknown';
}
};
export default defineConfig({
plugins: [
irsdkNativeModule(
['build/Release/irsdk_node.node'],
'.vite/build/Release/'
),
],
build: {
rollupOptions: {
external: ['bufferutil', 'utf-8-validate'],
},
},
resolve: {
// Some dependencies have Node.js specific imports
// This ensures they are properly resolved in Electron
mainFields: ['module', 'jsnext:main', 'jsnext'],
tsconfigPaths: true,
},
define: {
APP_GIT_HASH: JSON.stringify(getGitHash()),
POSTHOG_KEY: JSON.stringify(process.env.POSTHOG_KEY || ''),
},
});
// 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 {
code: `
import { createRequire } from 'module';
const customRequire = createRequire(__filename);
export const iRacingSdkNode = customRequire('./Release/${path.basename(file)}').iRacingSdkNode;
`,
moduleType: 'js',
};
}
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}`;
if (!fs.existsSync(fileAbs)) {
console.warn(
`[irsdkNativeModule] Native module not found at: ${fileAbs}`
);
return;
}
const nodeFile = fs.readFileSync(fileAbs);
fs.mkdirSync(path.dirname(out), { recursive: true });
fs.writeFileSync(out, nodeFile);
});
},
};
}