forked from wasm-tool/wasm-pack-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.js
141 lines (114 loc) · 3.25 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
const {
spawn
} = require('child_process');
const path = require('path');
const commandExistsSync = require('command-exists').sync;
const chalk = require('chalk');
const Watchpack = require('watchpack');
const error = msg => console.log(chalk.bold.red(msg));
const info = msg => console.log(chalk.bold.blue(msg));
/**
* In some cases Webpack will require the pkg entrypoint before it actually
* exists. To mitigate that we are forcing a first compilation.
*
* See https://github.com/wasm-tool/wasm-pack-plugin/issues/15
*/
let ranInitialCompilation = false;
class WasmPackPlugin {
constructor(options) {
this.crateDirectory = options.crateDirectory;
this.forceWatch = options.forceWatch;
this.forceMode = options.forceMode;
this.extraArgs = (options.extraArgs || '').trim().split(' ').filter(x=> x);
this.watchDirectories = (options.watchDirectories || [])
.concat(path.resolve(this.crateDirectory, 'src'));
this.wp = new Watchpack();
this.isDebug = true;
}
apply(compiler) {
this.isDebug = this.forceMode ? this.forceMode === "development" : compiler.options.mode === "development";
// force first compilation
compiler.hooks.beforeCompile.tapPromise('WasmPackPlugin', () => {
if (ranInitialCompilation === true) {
return Promise.resolve();
}
ranInitialCompilation = true;
return this._checkWasmPack()
.then(() => {
if (this.forceWatch || (this.forceWatch === undefined && compiler.watchMode)) {
this.wp.watch([], this.watchDirectories, Date.now() - 10000);
this.wp.on('change', this._compile.bind(this));
}
return this._compile();
})
.catch(this._compilationFailure);
});
}
_checkWasmPack() {
info('🧐 Checking for wasm-pack...\n');
if (commandExistsSync('wasm-pack')) {
info('✅ wasm-pack is installed. \n');
return Promise.resolve();
} else {
info('ℹ️ Installing wasm-pack \n');
return runProcess('cargo', ['install', 'wasm-pack'], {});
}
}
_compile() {
info('ℹ️ Compiling your crate...\n');
return spawnWasmPack({
isDebug: this.isDebug,
cwd: this.crateDirectory,
extraArgs: this.extraArgs,
})
.then(this._compilationSuccess)
.catch(this._compilationFailure);
}
_compilationSuccess(detail) {
if (detail) {
info(detail);
}
info('✅ Your crate has been correctly compiled\n');
}
_compilationFailure(err) {
error('wasm-pack error: ' + err);
}
}
function spawnWasmPack({
isDebug,
cwd,
extraArgs
}) {
const bin = 'wasm-pack';
const args = [
'--verbose',
'build',
...(isDebug ? ['--debug'] : []),
...extraArgs,
];
const options = {
cwd
};
return runProcess(bin, args, options);
}
function runProcess(bin, args, options) {
return new Promise((resolve, reject) => {
const p = spawn(bin, args, options);
let stdout = '';
let stderr = '';
p.stdout.on('data', d => {
stdout += d;
});
p.stderr.on('data', d => {
stderr += d;
});
p.on('close', code => {
if (code === 0) {
resolve(stdout);
} else {
reject(stderr);
}
});
});
}
module.exports = WasmPackPlugin;