Stream actions with completion to the socket

This commit is contained in:
Tyler Thompson 2017-08-19 13:38:02 -07:00
parent 71534c161e
commit dd06bcd783
3 changed files with 97 additions and 87 deletions

View File

@ -41,6 +41,7 @@
"handlebars": "^4.0.6",
"inspectpack": "^1.2.0",
"lodash": "^4.17.4",
"most": "^1.6.1",
"socket.io": "^1.4.8",
"socket.io-client": "^1.4.8",
"webpack": "^2.3.2",

View File

@ -4,6 +4,7 @@
const _ = require("lodash/fp");
const os = require("os");
const path = require("path");
const most = require("most");
const webpack = require("webpack");
const SocketIOClient = require("socket.io-client");
const InspectpackDaemon = require("inspectpack").daemon;
@ -33,8 +34,8 @@ function getTimeMessage(timer) {
return ` (${time})`;
}
function getBundleMetrics(stats, inspectpack, handler, done) {
const bundles = Object.keys(stats.compilation.assets)
function observeBundleMetrics(stats, inspectpack) {
const bundlesToObserve = Object.keys(stats.compilation.assets)
.filter(
bundlePath =>
// Don't include hot reload assets, they break everything
@ -49,89 +50,66 @@ function getBundleMetrics(stats, inspectpack, handler, done) {
source: stats.compilation.assets[bundlePath].source()
}));
Promise.all([
Promise.all(
bundles.map(bundle =>
inspectpack
.sizes({
const getSizes = bundles => Promise.all(
bundles.map(bundle =>
inspectpack.sizes({
code: bundle.source,
root: bundle.context,
format: "object",
minified: true,
gzip: true
})
.then(metrics => ({
path: bundle.path,
metrics
}))
)
)
.then(bundle => ({
type: "sizes",
value: bundle
}))
.catch(err => ({
type: "sizes",
error: true,
value: serializeError(err)
}));
const getProblems = bundles => Promise.all(
INSPECTPACK_PROBLEM_ACTIONS.map(action =>
Promise.all(
bundles.map(bundle =>
inspectpack[action]({
code: bundle.source,
root: bundle.context,
duplicates: true,
format: "object",
minified: true,
gzip: true
})
.then(metrics => ({
path: bundle.path,
metrics
}))
)
)
.then(bundle => {
handler([
{
type: "sizes",
value: bundle
}
]);
return true;
})
.catch(err => {
handler([
{
type: "sizes",
error: true,
value: serializeError(err)
}
]);
}),
Promise.all(
INSPECTPACK_PROBLEM_ACTIONS.map(action =>
Promise.all(
bundles.map(bundle =>
inspectpack
[action]({
code: bundle.source,
root: bundle.context,
duplicates: true,
format: "object",
minified: true,
gzip: true
})
.then(metrics => ({
path: bundle.path,
[action]: metrics
}))
)
.then(metrics => ({
path: bundle.path,
[action]: metrics
}))
)
)
)
.then(bundle => {
handler([
{
type: INSPECTPACK_PROBLEM_TYPE,
error: true,
value: _.flatten(bundle)
}
]);
return true;
})
.catch(err => {
handler([
{
type: INSPECTPACK_PROBLEM_TYPE,
error: true,
value: serializeError(err)
}
]);
})
])
.then(() => {
done();
})
.catch(err => {
console.error("INSPECTPACK ERROR: ", err);
done();
});
)
.then(bundle => ({
type: INSPECTPACK_PROBLEM_TYPE,
value: _.flatten(bundle)
}))
.catch(err => ({
type: INSPECTPACK_PROBLEM_TYPE,
error: true,
value: serializeError(err)
}));
const sizesStream = most.of(bundlesToObserve).map(getSizes);
const problemsStream = most.of(bundlesToObserve).map(getProblems);
return most.mergeArray([sizesStream, problemsStream])
.chain(most.fromPromise);
}
class DashboardPlugin {
@ -143,16 +121,17 @@ class DashboardPlugin {
this.port = options.port || DEFAULT_PORT;
this.handler = options.handler || null;
}
this.done = this.done.bind(this);
this.cleanup = this.cleanup.bind(this);
this.watching = false;
}
done() {
if (this.watching === false) {
if (this.socket) {
this.handler = null;
this.socket.close();
this.inspectpack._pool.clear();
}
cleanup() {
if (!this.watching && this.socket) {
this.handler = null;
this.socket.close();
this.inspectpack.terminate();
}
}
@ -283,7 +262,15 @@ class DashboardPlugin {
}
]);
getBundleMetrics(stats, this.inspectpack, handler, this.done);
observeBundleMetrics(stats, this.inspectpack)
.subscribe({
next: message => handler([message]),
error: err => {
console.log("Error from inspectpack:", err);
this.cleanup();
},
complete: this.cleanup
});
});
}
}

View File

@ -2,6 +2,16 @@
# yarn lockfile v1
"@most/multicast@^1.2.5":
version "1.2.5"
resolved "https://registry.yarnpkg.com/@most/multicast/-/multicast-1.2.5.tgz#ba5abc997f9a6511094bec117914f4959720a8fb"
dependencies:
"@most/prelude" "^1.4.0"
"@most/prelude@^1.4.0":
version "1.6.2"
resolved "https://registry.yarnpkg.com/@most/prelude/-/prelude-1.6.2.tgz#e022db0a3522ea45a427f739570b99f6a6b49162"
abbrev@1:
version "1.1.0"
resolved "https://registry.yarnpkg.com/abbrev/-/abbrev-1.1.0.tgz#d0554c2256636e2f56e7c2e5ad183f859428d81f"
@ -2117,6 +2127,14 @@ mkdirp@0.5.x, "mkdirp@>=0.5 0", mkdirp@^0.5.0, mkdirp@^0.5.1, mkdirp@~0.5.0:
dependencies:
minimist "0.0.8"
most@^1.6.1:
version "1.6.1"
resolved "https://registry.yarnpkg.com/most/-/most-1.6.1.tgz#2a245adcc7f88617b0a63167cb76bc1fc08a9d0a"
dependencies:
"@most/multicast" "^1.2.5"
"@most/prelude" "^1.4.0"
symbol-observable "^1.0.2"
ms@0.7.1:
version "0.7.1"
resolved "https://registry.yarnpkg.com/ms/-/ms-0.7.1.tgz#9cd13c03adbff25b65effde7ce864ee952017098"
@ -3041,6 +3059,10 @@ supports-color@^3.1.0, supports-color@^3.1.1:
dependencies:
has-flag "^1.0.0"
symbol-observable@^1.0.2:
version "1.0.4"
resolved "https://registry.yarnpkg.com/symbol-observable/-/symbol-observable-1.0.4.tgz#29bf615d4aa7121bdd898b22d4b3f9bc4e2aa03d"
table@^3.7.8:
version "3.8.3"
resolved "https://registry.yarnpkg.com/table/-/table-3.8.3.tgz#2bbc542f0fda9861a755d3947fefd8b3f513855f"
@ -3368,9 +3390,9 @@ wordwrap@~1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/wordwrap/-/wordwrap-1.0.0.tgz#27584810891456a4171c8d0226441ade90cbcaeb"
"workerpool@git+https://github.com/FormidableLabs/workerpool.git#71d4e17":
"workerpool@https://github.com/FormidableLabs/workerpool#71d4e17":
version "2.2.0"
resolved "git+https://github.com/FormidableLabs/workerpool.git#71d4e17"
resolved "https://github.com/FormidableLabs/workerpool#71d4e17"
dependencies:
object-assign "^4.1.1"