This repository has been archived by the owner on Jul 31, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
129 lines (98 loc) · 2.9 KB
/
index.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
const {extname} = require("path");
const tempy = require('tempy');
const {writeFileSync, readFileSync, unlinkSync} = require('fs');
const getPreset = n => require(`./presets/${n}.js`);
const isWasm = n => extname(n) === ".wasm";
const defaultOpts = {
debug: false,
wasmopt: {},
wasmsnip: false
};
function createRunner(compilation, options, bin /*: Buffer */) {
const filename = tempy.file({extension: 'wasm'});
writeFileSync(filename, new Buffer(bin));
return {
filename,
options,
debug(...msg /*: Array<string> */) {
if (options.debug === true) {
console.log("debug", ...msg);
}
},
warn(msg /*: string */) {
// warnings are not showned by Webpack is production, so we log directly instead
if (options.env === "production") {
console.warn(msg);
} else {
compilation.warnings.push(new Error(msg));
}
},
runPasses(passFn /*: Array<Function> */)/*: Promise */ {
const promises = passFn.reduce((acc, fn) => {
acc.push(fn(this));
return acc;
}, []);
return Promise.all(promises);
},
get() /*: Buffer */ {
const buff = readFileSync(filename, null);
unlinkSync(filename);
return buff;
}
}
}
module.exports = class {
constructor(options = {}) {
this._options = Object.assign({}, defaultOpts, options);
}
_configure(webpackMode, warn) {
let env = "production";
if (webpackMode === "development") {
env = "debug";
}
if (env === "production" && this._options.profiling === true) {
warn("You are profiling a production build, this might not be intended.");
}
if (this._options.profiling === true) {
env = "profiling";
}
const {configureOptions, configurePasses} = getPreset(env);
this._options.env = env;
this._options = configureOptions(this._options);
this._passes = configurePasses(this._options);
}
apply(compiler) {
compiler.plugin("emit", (compilation, ok) => {
this._configure(
compiler.options.mode,
msg => compilation.warnings.push(new Error(msg))
);
const processes = [];
for (const name in compilation.assets) {
if (isWasm(name) === false) {
continue;
}
const cachedSource = compilation.assets[name];
const runner = createRunner(
compilation,
this._options,
cachedSource.source()
);
const p = runner.runPasses(this._passes)
.then(runner.get)
.then(newBin => {
// Emit the new binary
compilation.assets[name] = {
source: () => newBin,
size: () => newBin.byteLength
};
})
.catch(err => {
compilation.errors.push(err);
});
processes.push(p);
}
Promise.all(processes).then(() => ok()).catch(() => ok());
});
}
};