├── .gitignore ├── LICENSE ├── README.md ├── filesToLoad ├── a.js ├── b.js └── c.js ├── hmr_example.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Jauco Noordzij 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Deprecated 2 | 3 | **Note that this is for an older version of webpack!!** (I wrote this in 2016) 4 | 5 | HMR is kinda finicky, and require.context is finicky as well. When debugging HMR/require.context code that doesn't work I usually start with a known working version and then turn that code step by step into the code that isn't working and then watch when it fails. Here's a working version for those of you that want to do the same thing, or just want to play around. 6 | 7 | This is what should work: run `npm install` and then `npm start`. Open [http://localhost:8080/bundle](http://localhost:8080/bundle) in your browser. Open the console. Now edit or add files in the filesToLoad directory. 8 | 9 | If you want to create a module that does the hot reloading and that your code can then subscribe to check out the [allow_import_of_hmr_module branch](https://github.com/jauco/webpack-hot-module-reload-with-context-example/tree/allow_import_of_hmr_module) it's a bit more complex, but arguably a better starting point for actual code. 10 | -------------------------------------------------------------------------------- /filesToLoad/a.js: -------------------------------------------------------------------------------- 1 | console.log("a"); 2 | -------------------------------------------------------------------------------- /filesToLoad/b.js: -------------------------------------------------------------------------------- 1 | console.log("b"); 2 | -------------------------------------------------------------------------------- /filesToLoad/c.js: -------------------------------------------------------------------------------- 1 | console.log("c"); 2 | -------------------------------------------------------------------------------- /hmr_example.js: -------------------------------------------------------------------------------- 1 | document.write("Open the javascript console!"); 2 | var context = require.context("./filesToLoad", false, /\.js$/); 3 | var modules = {}; 4 | context.keys().forEach(function (key) { 5 | var module = context(key); 6 | modules[key] = module; 7 | customReloadLogic(key, module, false); 8 | }) 9 | 10 | if (module.hot) { 11 | module.hot.accept(context.id, function () { 12 | //You can't use context here. You _need_ to call require.context again to 13 | //get the new version. Otherwise you might get errors about using disposed 14 | //modules 15 | var reloadedContext = require.context("./filesToLoad", false, /\.js$/); 16 | //To find out what module was changed you just compare the result of the 17 | //require call with the version stored in the modules hash using strict 18 | //equality. Equal means it is unchanged. 19 | var changedModules = reloadedContext.keys() 20 | .map(function (key) { 21 | return [key, reloadedContext(key)]; 22 | }) 23 | .filter(function (reloadedModule) { 24 | return modules[reloadedModule[0]] !== reloadedModule[1]; 25 | }); 26 | changedModules.forEach(function (module) { 27 | modules[module[0]] = module[1]; 28 | customReloadLogic(module[0], module[1], true); 29 | }); 30 | }); 31 | } 32 | 33 | function customReloadLogic(name, module, isReload) { 34 | console.log("module " + name + (isReload ? " re" : " ") + "loaded"); 35 | } 36 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "webpack-hot-module-reload-with-context-example", 3 | "version": "1.0.0", 4 | "description": "Shows how to use hmr with require.context", 5 | "main": "hmr_example.js", 6 | "dependencies": { 7 | "webpack": "^1.12.14", 8 | "webpack-dev-server": "^1.14.1" 9 | }, 10 | "devDependencies": { 11 | "webpack": "^1.12.14", 12 | "webpack-dev-server": "^1.14.1" 13 | }, 14 | "scripts": { 15 | "start": "webpack-dev-server hmr_example.js --hot --inline" 16 | }, 17 | "repository": { 18 | "type": "git", 19 | "url": "git+https://github.com/jauco/webpack-hot-module-reload-with-context-example.git" 20 | }, 21 | "author": "Jauco Noordzij", 22 | "license": "MIT", 23 | "bugs": { 24 | "url": "https://github.com/jauco/webpack-hot-module-reload-with-context-example/issues" 25 | }, 26 | "homepage": "https://github.com/jauco/webpack-hot-module-reload-with-context-example#readme" 27 | } 28 | --------------------------------------------------------------------------------