开发者社区> 镜像站> NPM> hot-module-replacement
hot-module-replacement
Hot module replacement for node.js
Last updated 5 months ago by sidorares .
ISC · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install hot-module-replacement 
SYNC missed versions from official npm registry.

hot-module-replacement

Hot module replacement for node.js

This module tries to mimic webpack HMR API

Installation

  npm install --save-dev hot-module-replacement

Usage

Put this code somewhere in your code to initialise hot reload

require('hot-module-replacement')({
  // options are optional
  ignore: /node_modules/  // regexp to decide if module should be ignored; also can be a function accepting string and returning true/false
})

You need to explicitly mark any subtree as 'hot reloadable' by calling hot.accept()

  let foo = require('./util/foo.js');

  if (module.hot) { 
    module.hot.accept('./util/foo', () => {
      // if foo.js or any files that foo.js depend on are modified this callback is invoked
      foo = require('./util/foo.js'); // by this time module cache entry for 'foo' already cleaned and module reloaded, requiring again is the easiest way of geting reference to new module. We need to assign it to local foo variable to make our local code in this file aware of it.
    })
  }

Similar projects:

webpack hmr on the server

Current Tags

  • 3.0.4                                ...           latest (5 months ago)

7 Versions

  • 3.0.4                                ...           5 months ago
  • 3.0.3                                ...           2 years ago
  • 3.0.2                                ...           2 years ago
  • 3.0.1                                ...           2 years ago
  • 3.0.0                                ...           3 years ago
  • 2.0.1                                ...           3 years ago
  • 1.0.0                                ...           3 years ago
Maintainers (1)
Downloads
Today 0
This Week 2
This Month 2
Last Day 2
Last Week 1
Last Month 5
Dependencies (2)
Dev Dependencies (2)