-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwasm.compile.js
More file actions
53 lines (44 loc) · 1.36 KB
/
wasm.compile.js
File metadata and controls
53 lines (44 loc) · 1.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
import childProcess from 'child_process';
import fs from 'fs';
const SourceDir = "./src/wasm/";
// if you have multiple source files, add them to here
const SourceFiles = [
"main.cpp"
];
const Compiler = "em++";
const CompileSettings = [
"-Os", // zip the output js file
"-sDISABLE_EXCEPTION_CATCHING=0", // allow exceptions to be caught
"-sALLOW_MEMORY_GROWTH=1", // allow memory growth
"-lembind", // link the embind library
];
let finalArgs = [];
if(!fs.existsSync("WasmTemp")){
fs.mkdirSync("WasmTemp");
}
finalArgs = finalArgs.concat(CompileSettings);
finalArgs.push('-o', './WasmTemp/webassembly.js');
SourceFiles.forEach(srcFile=>{
finalArgs.push(SourceDir + srcFile);
});
console.log(finalArgs);
let ret = childProcess.spawnSync(Compiler, finalArgs,{
stdio: 'inherit'
});
if(ret.status == 0){
console.log("Compile wasm files successfully!");
// copy the wasm file
fs.copyFileSync("./WasmTemp/webassembly.wasm", "./src/assets/webassembly.wasm");
// modify the wasm js file
let originJs = fs.readFileSync("./WasmTemp/webassembly.js");
let extraCode = `
import wasmURL from './assets/webassembly.wasm?url';
wasmBinaryFile = wasmURL;
export default Module;
`;
fs.writeFileSync("./src/webassembly.js", originJs + extraCode);
}else{
console.log("Compile wasm files failed!");
console.log(ret);
throw new Error("Compile wasm files failed!");
}