Giter Site home page Giter Site logo

Comments (9)

glenjamin avatar glenjamin commented on May 2, 2024

Would this be the normal hot-reload signal, or a different one where you could whatever you want?

I think a hot-reload signal wouldn't work, because webpack would need to compare the hash - but I'm open to making both ends of the socket extensible to additional events.

from webpack-hot-middleware.

mcmire avatar mcmire commented on May 2, 2024

Yeah, it would be a more generic signal in this case -- I don't need to rebuild webpack or do any HMR stuff, I just need to tell the page to reload somehow.

from webpack-hot-middleware.

glenjamin avatar glenjamin commented on May 2, 2024

How about something like this?
https://github.com/glenjamin/webpack-hot-middleware/tree/custom-events

I'll have to write some docs for this I think, rather than messing with the simple example - you need to require the client yourself rather than including it in the webpack config to get a reference to module for attaching your custom handler.

from webpack-hot-middleware.

mcmire avatar mcmire commented on May 2, 2024

That would work, although instead of the client exporting a function, what about an object that has a subscribe method (which would be the opposite of the middleware's publish method)?

Here's how I'd probably envision it:

// backend/index.js

var server = require("./server");
var hotReloader = require("./hot-reloader");

hotReloader.activate(server);
server.start();
// backend/server.js

var webpack = require("webpack");
var buildWebpackDevMiddleware = require("webpack-dev-middleware");
var buildWebpackHotMiddleware = require("webpack-hot-middleware");

function createServer() {
  var server = express();
  var compiler = webpack(...);
  var webpackDevMiddleware = buildWebpackDevMiddleware(compiler, { ... });
  var webpackHotMiddleware = buildWebpackHotMiddleware(compiler);
  app.use(webpackDevMiddleware);
  app.use(webpackHotMiddleware);

  function start() {
    server.listen(3000, "localhost", function () {
      console.log("App server started on port 3000");
    });
  }

  function reloadClient() {
    webpackHotMiddleware.publish({ reload: true });
  }

  return {
    start: start,
    reloadClient: reloadClient
  };
}

module.exports = createServer();
// backend/hot-reloader.js

function activate(appServer) {
  var watcher = chokidar.watch("./backend/routes.js");
  watcher.on("ready", function () {
    watcher.on("all", function () {
      for (var id in require.cache) {
        if (/\/backend\//.test(id)) {
          console.log("Removing from require.cache: " + id);
          delete require.cache[id];
          appServer.reloadClient();
        }
      }
    });
  });
}

module.exports = {
  activate: activate
};
// frontend/index.js

var webpackHotMiddlewareClient = require("webpack-hot-middleware/client");

webpackHotMiddlewareClient.subscribe(function (message) {
  if (message.reload === true) {
    window.location.reload();
  }
});

from webpack-hot-middleware.

glenjamin avatar glenjamin commented on May 2, 2024

Yep, that's doable.

I'll try and tidy this up and publish in the next week or so.

from webpack-hot-middleware.

mcmire avatar mcmire commented on May 2, 2024

Cool, sounds good. Thanks for the quick reply!

from webpack-hot-middleware.

glenjamin avatar glenjamin commented on May 2, 2024

Released in v2.3.0 - ad98aeb

from webpack-hot-middleware.

mcmire avatar mcmire commented on May 2, 2024

Great, thanks!

from webpack-hot-middleware.

the-dijkstra avatar the-dijkstra commented on May 2, 2024

this is amazing thanks ;)

from webpack-hot-middleware.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.