-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.idea | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,31 @@ | ||
# hot-callback-loader | ||
Webpack loader that wraps a module in a callback that executes whenever the module hot updates | ||
|
||
## Usage | ||
```javascript | ||
// import using es6 syntax | ||
import hotMyModule from 'hot-callback!./path/to/my-module'; | ||
// OR use commonjs syntax | ||
const hotMyModule = require('hot-callback!./path/to/my-module'); | ||
|
||
let MyModule; | ||
hotMyModule(function(newMyModule) { | ||
// this callback will execute initially and every time the module updates | ||
myModule = newMyModule; | ||
}); | ||
|
||
// the above callback is executed synchronously, so we can use MyModule now | ||
MyModule.doStuff(); | ||
``` | ||
|
||
### Getting the correct export | ||
Often the object you want to load is exported as a default export of your module. To avoid | ||
having to manually type 'newMyModule.default' every time in the hot callback, you can | ||
set the loader to always pass a specific export. | ||
|
||
```javascript | ||
// import using es6 syntax | ||
import hotRoutes from 'hot-callback?export=default!./routes'; | ||
|
||
hotRoutes(server.updateRoutes.bind(server)); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
var loaderUtils = require("loader-utils"); | ||
|
||
module.exports = function() {}; | ||
module.exports.pitch = function(remainingRequest) { | ||
this.cacheable && this.cacheable(); | ||
|
||
var options = loaderUtils.parseQuery(this.query); | ||
var useExport = options.export ? '.' + options.export : ''; | ||
|
||
var moduleRequest = loaderUtils.stringifyRequest(this, remainingRequest); | ||
var modulePath = loaderUtils.stringifyRequest(this, this.resourcePath); | ||
var result = [ | ||
"let moduleInstance = require(" + moduleRequest + ");", | ||
"const moduleId = require.resolve(" + moduleRequest + ");", | ||
"module.exports = function(cb) {", | ||
" cb(moduleInstance" + useExport + ");", | ||
" if(module.hot) {", | ||
" module.hot.accept([moduleId], function() {", | ||
" console.log('Hot updating "+modulePath+"');", | ||
" moduleInstance = require(" + moduleRequest + ");", | ||
" cb(moduleInstance" + useExport + ");", | ||
" });", | ||
" }", | ||
"}" | ||
]; | ||
return result.join("\n"); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
{ | ||
"name": "hot-callback-loader", | ||
"version": "0.1.0", | ||
"description": "Webpack loader that wraps a module in a callback that executes whenever the module hot updates", | ||
"main": "index.js", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/flut1/hot-callback-loader.git" | ||
}, | ||
"keywords": [ | ||
"webpack", | ||
"loader", | ||
"hot", | ||
"callback", | ||
"hmr", | ||
"module", | ||
"update" | ||
], | ||
"author": "Floris Bernard", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/flut1/hot-callback-loader/issues" | ||
}, | ||
"homepage": "https://github.com/flut1/hot-callback-loader#readme", | ||
"dependencies": { | ||
"loader-utils": "^0.2.16" | ||
} | ||
} |