Skip to content

Commit 05748d7

Browse files
committed
feat(build): add webpack hot reloading
closes #15
1 parent a383d0b commit 05748d7

File tree

6 files changed

+53
-12
lines changed

6 files changed

+53
-12
lines changed

Diff for: README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ You have to place the file in your [project root](https://nuxtjs.org/api/configu
6464

6565
:information_source: Note that each path in the file (`media_folder` and collections `folder` fields) will be rewritten to prepend nuxt.js [srcDir](https://nuxtjs.org/api/configuration-srcdir/), so please specify each path relative to this folder.
6666

67-
This file can be changed while `nuxt dev` is running, and Netlify CMS will be updated automatically. At the moment, you'll have to refresh your browser window manually after the build is complete.
67+
This file can be changed while `nuxt dev` is running, and Netlify CMS will be updated automatically.
6868

6969
## Options
7070
You can pass options using module options or `netlifyCms` section in `nuxt.config.js`.
@@ -103,7 +103,7 @@ These are of two kinds, [Custom Previews](https://www.netlifycms.org/docs/custom
103103

104104
:information_source: The global variable `CMS` is available to these javascript files to reference the CMS object.
105105

106-
This directory can be changed while `nuxt dev` is running, and Netlify CMS will be updated automatically. At the moment, you'll have to refresh your browser window manually after the build is complete.
106+
This directory can be changed while `nuxt dev` is running, and Netlify CMS will be updated automatically.
107107

108108
## CONTRIBUTING
109109

Diff for: lib/hmr.client.js

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
(function() {
2+
"use strict";
3+
const webpackHotMiddlewareClient = require(`webpack-hot-middleware/client?name=client&reload=true&timeout=3000&dynamicPublicPath=true&path=__webpack_hmr`);
4+
5+
webpackHotMiddlewareClient.subscribe(function(payload) {
6+
if (payload.action === "reload" || payload.reload === true) {
7+
window.location.reload();
8+
}
9+
});
10+
11+
module.exports = webpackHotMiddlewareClient;
12+
})();

Diff for: src/module.js

+20-5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import chokidar from "chokidar";
66
import pify from "pify";
77
import webpack from "webpack";
88
import webpackDevMiddleware from "webpack-dev-middleware";
9+
import webpackHotMiddleware from "webpack-hot-middleware";
910
import serveStatic from "serve-static";
1011
import Debug from "debug";
1112

@@ -86,10 +87,23 @@ export default function NetlifyCmsModule(moduleOptions) {
8687
})
8788
);
8889

90+
const netlifyWebpackHotMiddleware = pify(
91+
webpackHotMiddleware(netlifyCompiler, {
92+
log: false,
93+
heartbeat: 1000
94+
})
95+
);
96+
8997
// Inject to renderer instance
9098
if (builder.nuxt.renderer) {
9199
builder.nuxt.renderer.netlifyWebpackDevMiddleware = netlifyWebpackDevMiddleware;
100+
builder.nuxt.renderer.netlifyWebpackHotMiddleware = netlifyWebpackHotMiddleware;
92101
}
102+
103+
// Stop webpack middleware on nuxt.close()
104+
this.nuxt.plugin("close", async () => {
105+
await this.nuxt.renderer.netlifyWebpackDevMiddleware.close();
106+
});
93107
}
94108
});
95109

@@ -109,14 +123,12 @@ export default function NetlifyCmsModule(moduleOptions) {
109123
debug(`requesting url: ${Utils.urlJoin(ADMIN_PATH, req.url)}`);
110124
await this.nuxt.renderer.netlifyWebpackDevMiddleware(req, res);
111125
}
126+
if (this.nuxt.renderer.netlifyWebpackHotMiddleware) {
127+
await this.nuxt.renderer.netlifyWebpackHotMiddleware(req, res);
128+
}
112129
}
113130
});
114131

115-
// Stop webpack middleware on nuxt.close()
116-
this.nuxt.plugin("close", async () => {
117-
await this.nuxt.renderer.netlifyWebpackDevMiddleware.close();
118-
});
119-
120132
// Start watching config file
121133
const patterns = [Utils.r(configManager.cmsConfigFile.fileName)];
122134

@@ -128,6 +140,9 @@ export default function NetlifyCmsModule(moduleOptions) {
128140
const refreshFiles = _.debounce(() => {
129141
configManager.cmsConfigFile.readFile();
130142
this.nuxt.renderer.netlifyWebpackDevMiddleware.invalidate();
143+
this.nuxt.renderer.netlifyWebpackHotMiddleware.publish({
144+
action: "reload"
145+
});
131146
}, 200);
132147

133148
// Watch for src Files

Diff for: src/webpack.config.js

+17-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export default function webpackNetlifyCmsConfig(
1414
nuxtOptions,
1515
moduleConfig
1616
) {
17-
const ENTRY = resolve(__dirname, "../lib/entry.js");
17+
const ENTRY = resolve(__dirname, "../lib/entry");
1818
const BUILD_DIR = moduleConfig.buildDir;
1919
const CHUNK_FILENAME = nuxtOptions.build.filenames.chunk;
2020
const PUBLIC_PATH = Utils.urlJoin(
@@ -25,13 +25,16 @@ export default function webpackNetlifyCmsConfig(
2525
const PAGE_TITLE = moduleConfig.adminTitle;
2626
const PAGE_TEMPLATE = resolve(__dirname, "../lib/template", "index.html");
2727
const REQUIRE_EXTENSIONS = existsSync(EXTENSIONS_DIR) ? true : false;
28+
const HMR_CLIENT = resolve(__dirname, "../lib/hmr.client");
2829

2930
const config = {
3031
name,
31-
entry: ENTRY,
32+
entry: {
33+
app: ENTRY
34+
},
3235
output: {
3336
path: BUILD_DIR,
34-
filename: "bundle.[chunkhash].js",
37+
filename: "bundle.[hash].js",
3538
chunkFilename: CHUNK_FILENAME,
3639
publicPath: PUBLIC_PATH
3740
},
@@ -71,7 +74,18 @@ export default function webpackNetlifyCmsConfig(
7174
// Development specific config
7275
// --------------------------------------
7376
if (nuxtOptions.dev) {
77+
// Add friendly error plugin
7478
config.plugins.push(new FriendlyErrorsWebpackPlugin());
79+
80+
// https://webpack.js.org/plugins/named-modules-plugin
81+
config.plugins.push(new webpack.NamedModulesPlugin());
82+
83+
// Add HMR support
84+
config.entry.app = [HMR_CLIENT, config.entry.app];
85+
config.plugins.push(
86+
new webpack.HotModuleReplacementPlugin(),
87+
new webpack.NoEmitOnErrorsPlugin()
88+
);
7589
} else {
7690
// --------------------------------------
7791
// Production specific config

Diff for: test/__snapshots__/dev.test.js.snap

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ exports[`module dev mode admin 1`] = `
99
<title>Content Manager</title>
1010
</head>
1111
<body>
12-
<script type=\\"text/javascript\\" src=\\"/admin/bundle.26dd124e81a2a69ff656.js\\"></script></body>
12+
<script type=\\"text/javascript\\" src=\\"/admin/bundle.fddebd073208cdcc9e8b.js\\"></script></body>
1313
</html>
1414
"
1515
`;

Diff for: test/__snapshots__/module.test.js.snap

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ exports[`module admin 1`] = `
99
<title>Content Manager</title>
1010
</head>
1111
<body>
12-
<script type=\\"text/javascript\\" src=\\"/admin/bundle.e205c229ca2f50a63bd9.js\\"></script></body>
12+
<script type=\\"text/javascript\\" src=\\"/admin/bundle.721d966526a89f3e0347.js\\"></script></body>
1313
</html>
1414
"
1515
`;

0 commit comments

Comments
 (0)