forked from PatrickJS/angular-hmr-loader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
81 lines (71 loc) · 2.83 KB
/
index.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
var utils = require('loader-utils');
var bootstrapModule = /(\.bootstrapModule|\.bootstrapModuleFactory)\((.+)\)/gm;
var bootLoader = /((hmr_1|r)\.bootloader)\((.+)\)/gm;
function Angular2HMRLoader(source, sourcemap) {
var self = this;
// Not cacheable during unit tests;
self.cacheable && self.cacheable();
var query = {};
if (typeof self.query === 'string') {
query = utils.parseQuery(self.query)
} else {
query = self.query
}
function done(src, srcmap) {
// Support for tests
if (self.callback) {
self.callback(null, src, srcmap);
} else {
return src;
}
}
if (query.prod) {
source = source.replace(bootLoader, function (match, boot, ngmodule, main, offset, src) {
// return updated metadata
return '(document.readyState === "complete") ? ' + main +
'() : document.addEventListener("DOMContentLoaded", function() { ' + main + '()' + ' })';
});
return done(source, sourcemap);
}
source = source.replace(bootstrapModule, function (match, boot, ngmodule, offset, src) {
// return updated metadata
var newLine = ' ';
if (query.pretty) {
newLine = '\n';
}
return boot + '(' + ngmodule + ')' +
'.then(function(MODULE_REF) {'+ newLine +
' if (module["hot"]) {'+ newLine +
' module["hot"]["accept"]();'+ newLine +
' '+ newLine +
' if (MODULE_REF.instance["hmrOnInit"]) {'+ newLine +
' module["hot"]["data"] && MODULE_REF.instance["hmrOnInit"](module["hot"]["data"]);'+ newLine +
' }'+ newLine +
' if (MODULE_REF.instance["hmrOnStatus"]) {'+ newLine +
' module["hot"]["apply"](function(status) {'+ newLine +
' MODULE_REF.instance["hmrOnStatus"](status);'+ newLine +
' });'+ newLine +
' }'+ newLine +
' if (MODULE_REF.instance["hmrOnCheck"]) {'+ newLine +
' module["hot"]["check"](function(err, outdatedModules) {'+ newLine +
' MODULE_REF.instance["hmrOnCheck"](err, outdatedModules);'+ newLine +
' });'+ newLine +
' }'+ newLine +
' if (MODULE_REF.instance["hmrOnDecline"]) {'+ newLine +
' module["hot"]["decline"](function(dependencies) {'+ newLine +
' MODULE_REF.instance["hmrOnDecline"](dependencies);'+ newLine +
' });'+ newLine +
' }'+ newLine +
' module["hot"]["dispose"](function(store) {'+ newLine +
' MODULE_REF.instance["hmrOnDestroy"] && MODULE_REF.instance["hmrOnDestroy"](store);'+ newLine +
' MODULE_REF.destroy();'+ newLine +
' MODULE_REF.instance["hmrAfterDestroy"] && MODULE_REF.instance["hmrAfterDestroy"](store);'+ newLine +
' });'+ newLine +
' }'+ newLine +
' return MODULE_REF;'+ newLine +
'})'
});
return done(source, sourcemap)
};
Angular2HMRLoader.default = Angular2HMRLoader;
module.exports = Angular2HMRLoader