Skip to content

Commit

Permalink
feat: use devServer watchFiles for html/ejs
Browse files Browse the repository at this point in the history
  • Loading branch information
dmnsgn committed Apr 9, 2021
1 parent 69150b2 commit dfe5bd0
Show file tree
Hide file tree
Showing 5 changed files with 692 additions and 819 deletions.
3 changes: 1 addition & 2 deletions config/plugins.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { NODE_ENV } from "./config.js";
import { htmlPages, htmlProcessing, htmlReload } from "./plugins/html.js";
import { htmlPages, htmlProcessing } from "./plugins/html.js";
import { cssExtract, cssUnused, cssCritical } from "./plugins/css.js";
import {
spritesheet,
Expand All @@ -21,7 +21,6 @@ export default [
obsolete,
define,
...htmlPages,
NODE_ENV === "development" ? htmlReload : 0,
htmlProcessing,
cssExtract,
NODE_ENV === "production" ? cssUnused : 0,
Expand Down
5 changes: 1 addition & 4 deletions config/plugins/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import validator from "html-validator";
import posthtml from "posthtml";
import posthtmlImageSources from "./posthtml-image-sources.js";

import { CustomHtmlReloadPlugin } from "../server.js";
import { NODE_ENV, PACKAGE, GIT_INFO } from "../config.js";

const minifyOptions = {
Expand Down Expand Up @@ -139,6 +138,4 @@ getPageInstanceRecursively(PACKAGE.config);

const htmlProcessing = new HtmlWebpackProcessingPlugin();

const htmlReload = new CustomHtmlReloadPlugin();

export { htmlPages, htmlProcessing, htmlReload };
export { htmlPages, htmlProcessing };
34 changes: 5 additions & 29 deletions config/server.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,6 @@
import HtmlWebpackPlugin from "html-webpack-plugin";
import { join } from "path";

import { PATHS } from "./config.js";

let devServer;
const cache = {};

export class CustomHtmlReloadPlugin {
apply(compiler) {
compiler.hooks.compilation.tap("CustomHtmlReloadPlugin", (compilation) => {
HtmlWebpackPlugin.getHooks(compilation).beforeEmit.tapAsync(
"CustomHtmlReloadPlugin",
(data, cb) => {
const orig = cache[data.outputName];
const html = data.html;
if (orig && orig !== html) {
devServer.sockWrite(devServer.sockets, "content-changed");
}
cache[data.outputName] = html;
cb();
}
);
});
}
}
import { PATHS, ROOT } from "./config.js";

export default {
// bonjour: false,
Expand All @@ -45,9 +23,7 @@ export default {
// injectHot: false,
liveReload: true,
// onAfterSetupMiddleware: () => {},
onBeforeSetupMiddleware(server) {
devServer = server;
},
// onBeforeSetupMiddleware(server) {},
// onListening: () => {},
open: true,
// openPage: ['/different/page1', '/different/page2'],
Expand All @@ -58,6 +34,6 @@ export default {
setupExitSignals: true,
// static: path.join(ROOT, PATHS.get("dist")),
static: PATHS.get("dist"),
transportMode: "ws", // "sockjs"
useLocalIp: true,
transportMode: "ws", // "sockjs",
watchFiles: [join(ROOT, PATHS.get("src"), "**/*.{ejs,html}")],
};

0 comments on commit dfe5bd0

Please sign in to comment.