forked from cryptool-org/wasm-webterm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworker.loader.js
58 lines (46 loc) · 1.57 KB
/
worker.loader.js
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
/**
* Webpack loader to prebundle WasmWorker.
* This eliminates the need of a separate webworker js file.
*/
const path = require("path")
const webpack = require("webpack")
const memfs = require("memfs")
module.exports = function (source) {
const inputFilename = "./src/runners/WasmWorker.js"
const outputFilename = "worker.compiled.js"
// create webpack compiler
let compiler = webpack({
entry: inputFilename,
output: {
path: "/",
filename: outputFilename,
library: {
type: "umd",
name: "[name]"
}
},
plugins: [
new webpack.ProvidePlugin({
Buffer: ["buffer", "Buffer"],
process: "process/browser"
})
]
})
// make compiler use memfs as *output* file system
compiler.outputFileSystem = memfs.createFsFromVolume(new memfs.Volume())
compiler.outputFileSystem.join = path.join.bind(path)
return new Promise((resolve, reject) => {
// compile webworker
compiler.run((error, stats) => {
// exit on errors
if(error != null) reject(error)
if(stats?.hasErrors()) reject(stats.compilation.errors)
// read compiled bundle from file system and resolve
try {
const compiled = compiler.outputFileSystem.readFileSync("/" + outputFilename, "utf-8")
resolve(compiled)
}
catch(e) { console.error("Errors while compiling with worker.loader.js") }
})
})
}