-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathvite-plugin.nativeNodeFile.ts
More file actions
123 lines (100 loc) · 3.36 KB
/
vite-plugin.nativeNodeFile.ts
File metadata and controls
123 lines (100 loc) · 3.36 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
import { type Plugin } from 'vite';
import path from 'path';
import { cp, constants } from 'node:fs/promises';
export type Config = {
/**
* Import path the code uses to import this native node addon dependency.
*
* Imports of this path will be replaced with a require to the bundled `.node` file.
* This allows using a TypeScript declaration file as the import for a native node addon dependency.
*/
import?: string,
/**
* Path to the built node file. Ex: `./build/Release/native.node`.
*/
built: string,
/**
* Path for the .node file. Ex: `thisThing.node`.
*
* If not defined the `.node` file is placed at the root with the same name as the built `.node` file.
*/
includePath?: string,
/**
* When false: only includes this native dependency if it is imported.
* Default: `false`
*/
alwaysInclude?: boolean,
}[];
//https://prosopo.io/articles/vite-node-files/
/**
* Note: outputs ESM.
*/
export function nativeNodeFile(config: Config, root: string = process.cwd()): Plugin {
const natives = config.map(native => {
const importFullPath = native.import === undefined
? undefined
: path.resolve(root, native.import);
const nodeFileFullPath = path.resolve(root, native.built);
const requirePath = native.includePath === undefined
? `./${path.basename(nodeFileFullPath)}`
: native.includePath;
return {
config: native,
used: false,
importFullPath,
nodeFileFullPath,
requirePath,
}
});
const nativeByImport = new Map(natives.map(native => [native.importFullPath, native]));
const nativeByNodeFile = new Map(natives.map(native => [native.nodeFileFullPath, native]));
return {
name: 'native-node-files',
buildStart(options) {
// console.log('---vite.buildStart---', options);
for(const native of natives) {
native.used = false;
}
},
async resolveId(source, importer, options) {
// console.log('---vite.resolveId---', source, importer, options);
// sample output - source: `../src-native/native`, importer: `C:/ProjectPath/src-main/main.ts`, options: `{ attributes: {}, custom: {}, isEntry: false }`
if(importer === undefined) return null;
const importFullPath = path.resolve(path.dirname(importer), source);
const native = nativeByImport.get(importFullPath);
if(native === undefined) return null;
native.used = true;
return native.nodeFileFullPath;
},
load(id) {
// console.log('---vite.load---', id);
const native = nativeByNodeFile.get(id);
if(native !== undefined) return `
import { createRequire } from 'module';
const customRequire = createRequire(import.meta.url);
const content = customRequire(${JSON.stringify(native.requirePath)});
export default content;
`;
return null;
},
async generateBundle(options, bundle) {
// console.log('---vite.generateBundle---', options, bundle);
if(options.dir === undefined) return;
//https://rollupjs.org/plugin-development/#generatebundle
for(const output of Object.values(bundle)) {
if(output.type !== 'chunk') continue;
for(const native of natives) {
const include = native.used || native.config.alwaysInclude === true;
if(!include) continue;
const copyTo = path.resolve(options.dir, native.requirePath);
try {
await cp(native.nodeFileFullPath, copyTo, { mode: constants.COPYFILE_FICLONE });
}
catch(e) {
console.error(e);
}
}
}
},
}
};