-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
58 lines (46 loc) · 1.55 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
'use strict';
var path = require('path');
var loaderUtils = require('loader-utils'),
migrate = require('nginject-migrate');
var PACKAGE_NAME = require('./package.json').name;
/**
* Webpack loader where explicit @ngInject comment creates pre-minification $inject property.
* @param {string} content JS content
* @param {object} sourceMap The source-map
* @returns {string|String}
*/
function loader(content, sourceMap) {
/* jshint validthis:true */
// loader result is cacheable
this.cacheable();
// path of the file being processed
var filename = path.relative(this.options.context || process.cwd(), this.resourcePath).replace(/\\/g, '/'),
options = loaderUtils.parseQuery(this.query),
useMap = loader.sourceMap || options.sourceMap;
// make sure the AST has the data from the original source map
var pending = migrate.processSync(content, {
filename : filename,
sourceMap: useMap && (sourceMap || true),
quoteChar: options.singleQuote ? '\'' : '"'
});
// emit deprecation warning
if ((pending.isChanged) && (options.deprecate)) {
var text = ' ' + PACKAGE_NAME + ': @ngInject doctag is deprecated, use "ngInject" string directive instead';
this.emitWarning(text);
}
// emit errors
if (pending.errors.length) {
var text = pending.errors.map(indent).join('\n');
this.emitError(text);
}
// complete
if (useMap) {
this.callback(null, pending.content, pending.sourceMap);
} else {
return pending.content;
}
}
module.exports = loader;
function indent(value) {
return ' ' + value;
}