Skip to content

Commit

Permalink
Only boot up Jarvis in watch mode by default, and preserve the Jarvis…
Browse files Browse the repository at this point in the history
… server even after webpack finishes, if the optional watchOnly flag is false (zouhir#125)
  • Loading branch information
stephencookdev authored and zouhir committed Mar 6, 2018
1 parent 4750e1d commit 6b0f8a1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 14 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,12 @@ Default: `localhost`

The Jarvis dashboard will attach to this host, e.g. `0.0.0.0`.

## `options.keepAlive`
## `options.watchOnly`

Type: `Boolean`<br>
Default: `false`
Default: `true`

If set to true, then Jarvis will keep running (rather than closing when the webpack build finishes).
If set to false, then Jarvis will also run for non-watch builds, and keep running after the build completes.

## Help & Contribute

Expand Down
6 changes: 3 additions & 3 deletions config/webpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ const dist = join(__dirname, "../dist");

module.exports = env => {
const isProd = env && env.production;

// Our style-loader chain
const cssGroup = styles(isProd);

// Our entry file
let entry = './src/index.js';

Expand Down Expand Up @@ -46,7 +46,7 @@ module.exports = env => {
new Jarvis()
)
}

return {
entry,
output: {
Expand Down
27 changes: 19 additions & 8 deletions lib/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Jarvis {
constructor(opts = {}) {
opts.host = opts.host || "localhost";
opts.port = parseInt(opts.port || 1337, 10);
opts.keepAlive = !!opts.keepAlive;
opts.watchOnly = opts.watchOnly !== false;

if (opts.port && isNaN(opts.port)) {
console.error(`[JARVIS] error: the specified port (${opts.port}) is invalid. Reverting to 1337`);
Expand Down Expand Up @@ -47,24 +47,36 @@ class Jarvis {
}

let jarvis;
let jarvisBooting;
let isDev = !this.env.production;
let { port, host } = this.options;

if (!this.env.running) {
const bootJarvis = () => {
if (jarvis || jarvisBooting) return;

jarvisBooting = true;
jarvis = this.server = server.init(compiler, isDev);
jarvis.http.listen(port, host, _ => {
console.log(`[JARVIS] Starting dashboard on: http://${host}:${port}`);
this.env.running = true;
jarvisBooting = false;
// if a new client is connected push current bundle info
jarvis.io.on("connection", socket => {
socket.emit("project", this.reports.project);
socket.emit("progress", this.reports.progress);
socket.emit("stats", this.reports.stats);
});
});
};

if (!this.options.watchOnly && !this.env.running) {
bootJarvis();
}

compiler.plugin("watch-run", (c, done) => {
if(this.options.watchOnly) {
bootJarvis();
}
this.env.watching = true;
done();
});
Expand All @@ -78,21 +90,20 @@ class Jarvis {
compiler.apply(
new webpack.ProgressPlugin((percentage, message) => {
this.reports.progress = { percentage, message };
jarvis.io.emit("progress", { percentage, message });
if(this.env.running) {
jarvis.io.emit("progress", { percentage, message });
}
})
);

// extract the final reports from the stats!
compiler.plugin("done", stats => {
if(!this.env.running) return;

const jsonStats = stats.toJson({ chunkModules: true });
jsonStats.isDev = isDev;
this.reports.stats = reporter.statsReporter(jsonStats);
jarvis.io.emit("stats", this.reports.stats);

if (!this.options.keepAlive && !this.env.watching) {
jarvis.http.close();
jarvis.io.close();
}
});
}
}
Expand Down

0 comments on commit 6b0f8a1

Please sign in to comment.