-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
54 lines (41 loc) · 1.41 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
'use strict';
var path = require('path'),
SourceNode = require('source-map').SourceNode,
SourceMapConsumer = require('source-map').SourceMapConsumer,
makeIdentitySourceMap = require('./makeIdentitySourceMap');
var ANGULAR_DIRECTIVE_RE = /angular\.module\(\"([\w\.\-]+)\"\)[.\n]*\.directive\(/g;
module.exports = function (source, map) {
if (this.cacheable) {
this.cacheable();
}
if (!source.match(ANGULAR_DIRECTIVE_RE)) {
return this.callback(null, source, map);
}
var resourcePath = this.resourcePath,
filename = path.basename(resourcePath),
separator = '\n\n',
prependText,
processedSource,
node,
result;
prependText = [
'module.hot.accept();',
'var hotAngular = require("angular-hmr-loader/hot-angular/main.js");'
].join(' ');
processedSource = source.replace(ANGULAR_DIRECTIVE_RE, 'hotAngular.module("$1").directive(');
if (this.sourceMap === false) {
return this.callback(null, [
prependText,
processedSource
].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());
};