-
Notifications
You must be signed in to change notification settings - Fork 1
/
importer.es5.js
56 lines (46 loc) · 1.57 KB
/
importer.es5.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
'use strict';
module.exports.getNewInstance = function () {
var Importer = function Importer() {
var modules = {};
var getModulesContext = function getModulesContext() {
return require.context('.', false, /override-me-in-webpack-config$/);
};
var getFileName = function getFileName(string) {
return string.match(/[^\\|/]*(?=[.][a-zA-Z]+$)/)[0];
};
var contextId = void 0;
this.getModules = function () {
var context = getModulesContext();
contextId = context.id;
context.keys().forEach(function (key) {
var fileName = getFileName(key);
modules[fileName] = context(key).default;
});
return modules;
};
this.setupHMR = function (hmrHandler) {
if (module.hot) {
module.hot.accept(contextId, function () {
var updatedModules = {};
var reloadedContext = getModulesContext();
var allModules = reloadedContext.keys().map(function (key) {
return {
name: getFileName(key),
object: reloadedContext(key).default
};
});
var newModules = allModules.filter(function (reloadedModule) {
return modules[reloadedModule.name] !== reloadedModule.object;
});
newModules.forEach(function (module) {
modules[module.name] = module.object;
updatedModules[module.name] = module.object;
console.info('[HMR] - Modules are replaced');
});
hmrHandler(updatedModules);
});
}
};
};
return new Importer();
};