Skip to content

Commit

Permalink
First version
Browse files Browse the repository at this point in the history
  • Loading branch information
flut1 committed Sep 22, 2016
1 parent 49efaf0 commit 127a197
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.idea
node_modules
29 changes: 29 additions & 0 deletions README.md
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));
```
27 changes: 27 additions & 0 deletions index.js
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");
};
28 changes: 28 additions & 0 deletions package.json
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"
}
}

0 comments on commit 127a197

Please sign in to comment.