08-Live Reload

Well, obviously in dev we'd quite like to not have to hit refresh all the time, because developers are, well, we're lazy. So we can use a live reload server to do the job for us.

1
yarn add --dev broccoli-livereload@^1.3.0
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
// Brocfile.js
const funnel = require('broccoli-funnel');
const merge = require('broccoli-merge-trees');
const compileSass = require('broccoli-sass-source-maps')(require('sass'));
const Rollup = require("broccoli-rollup");
const LiveReload = require('broccoli-livereload');
const babel = require("rollup-plugin-babel");
const nodeResolve = require('rollup-plugin-node-resolve');
const commonjs = require('rollup-plugin-commonjs');

const appRoot = "app";

// Copy HTML file from app root to destination
const html = funnel(appRoot, {
files: ["index.html"],
annotation: "Index file",
});

// Compile JS through rollup
let js = new Rollup(appRoot, {
inputFiles: ["**/*.js"],
annotation: "JS Transformation",
rollup: {
input: "app.js",
output: {
file: "assets/app.js",
format: "iife",
sourcemap: true,
},
plugins: [
nodeResolve({
jsnext: true,
browser: true,
}),
commonjs({
include: 'node_modules/**',
}),
babel({
exclude: "node_modules/**",
}),
],
}
});

// Copy CSS file into assets
const css = compileSass(
[appRoot],
'styles/app.scss',
'assets/app.css',
{
sourceMap: true,
sourceMapContents: true,
annotation: "Sass files"
}
);

// Copy public files into destination
const public = funnel('public', {
annotation: "Public files",
});

// Remove the existing module.exports and replace with:
let tree = merge([html, js, css, public], {annotation: "Final output"});

// Include live reaload server
tree = new LiveReload(tree, {
target: 'index.html',
});

module.exports = tree;

What we're doing here is assigning the final output of Merge to a mutable variable tree, then passing that into the LiveReload plugin, that will auto-inject the Live Reload javascript and setup the server file watcher.

Now yarn serve, try changing a scss file, notice how the css refreshes in place, no browser refresh. Change a .js or .html file and the page will refresh. This doesn't support fancy hot reloading like React and Webpack does, but that's a slightly different ballgame, and is very architecture dependent.

Completed Branch: 08-live-reload

Next: 09-debug