-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.js
114 lines (108 loc) · 3.31 KB
/
plugin.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
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
const path = require('path');
const fs = require('fs');
const { spawn } = require('child_process');
const Watchpack = require('watchpack');
const log = msg => console.log("[swift-webpack-plugin] ", msg);
function runProcess(bin, args, options) {
return new Promise((resolve, reject) => {
const p = spawn(bin, args, options);
let errorMessage = "";
let output = "";
p.on('close', code => {
if (code === 0) {
resolve(output);
} else {
const cmd = [bin].concat(args).join(" ")
reject(new Error(`Failed to execute: ${cmd}\n${errorMessage}\n${output}`));
}
});
p.stderr.on('data', (data) => {
errorMessage += data.toString();
});
p.stdout.on('data', (data) => {
output += data.toString();
});
p.on('error', reject);
});
}
class SwiftWebpackPlugin {
constructor(options) {
this.packageDirectory = options.packageDirectory
this.target = options.target
this.dist = options.dist
this.buildOptions = options.buildOptions
if (!this.buildOptions)
this.buildOptions = []
this.config = options.config
if (!this.config)
this.config = "debug"
this.swift_build = options.swift_build
if (!this.swift_build)
this.swift_build = "swift build"
this.buildDirectory = path.join(this.packageDirectory, ".build")
this.building = false;
this.ignoring = [this.buildDirectory, this.dist, ".git", "node_modules", ".xcodeproj"]
this.wp = new Watchpack({
ignored: this.ignoring,
});
}
apply(compiler) {
compiler.hooks.watchRun.tap("SwiftWebpackPlugin", () => {
log("Watching " + this.packageDirectory)
this.wp.watch([], [this.packageDirectory], Date.now() - 10000);
this.wp.on('change', (filePath, mtime, explanation) => {
if (!filePath || typeof filePath != 'string') return;
if (this.ignoring.some(i => filePath.includes(i)))
return;
this._compile()
})
})
compiler.hooks.compile.tap("SwiftWebpackPlugin", compilation => {
this._compile();
});
}
_compile() {
if (this.building) return;
this.building = true;
log(`Compiling '${this.target}'`)
try {
fs.mkdirSync(this.dist);
log(`Created dist directory '${this.dist}'`)
} catch (e) {
if (e.code !== "EEXIST") {
throw e;
}
}
const options = {
cwd: this.packageDirectory,
}
const buildCmds = this.swift_build.split(" ")
const mainBin = buildCmds[0]
const mainArgs = buildCmds.slice(1)
const buildArgs = mainArgs.concat([
"--triple", "wasm32-unknown-wasi",
"--configuration", this.config,
"--build-path", this.buildDirectory,
]).concat(this.buildOptions)
runProcess(mainBin, buildArgs, options)
.then(() => runProcess(mainBin, buildArgs.concat(["--show-bin-path"]), options))
.then((binPath) => {
return runProcess(
"cp",
[
path.join(binPath.slice(0, binPath.length - 1), this.target + ".wasm"),
path.join(this.dist, this.target + ".wasm"),
]
);
})
.then(() => {
log(`'${this.target}' has been compiled successfully`)
this.building = false;
})
.catch((error) => {
log(error)
this.building = false;
})
}
}
module.exports = SwiftWebpackPlugin