-
Notifications
You must be signed in to change notification settings - Fork 16
/
index.js
57 lines (43 loc) · 1.5 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
var path = require('path'),
SourceNode = require('source-map').SourceNode,
SourceMapConsumer = require('source-map').SourceMapConsumer,
makeIdentitySourceMap = require('./makeIdentitySourceMap');
var angularModule= /angular[\.\n ]+module\(([\'\"\w\.\/\(\)\n\-\,\[\] ]+)\)/g;
module.exports = function (source, map) {
if (this.cacheable) {
this.cacheable();
}
if (!source.match(angularModule)) {
return this.callback(null, source, map);
}
console.log('[AHMR] Replacement Matched');
var separator = '\n\n';
var prependText;
var processedSource;
var node;
var result;
prependText = [
'module.hot.accept();',
'var hotAngular = require(' + JSON.stringify(require.resolve('./angular-hot-replacement')) + ');'
].join(' ');
var appendText = [
//'module.hot.dispose(function(data) {console.log(\'[SBOS] Reloaded\')})'
].join(' ');
processedSource = source.replace(angularModule, 'hotAngular.test(module).module($1)');
if (this.sourceMap === false) {
return this.callback(null, [
prependText,
processedSource,
appendText
].join(separator));
}
if (!map) {
map = makeIdentitySourceMap(source, this.resourcePath);
}
node = new SourceNode(null, null, null, [
new SourceNode(null, null, this.resourcePath, prependText),
SourceNode.fromStringWithSourceMap(processedSource, new SourceMapConsumer(map))
]).join(separator);
result = node.toStringWithSourceMap();
this.callback(null, result.code, result.map.toString());
};