-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
45 lines (38 loc) · 1.47 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
var loaderUtils = require('loader-utils');
module.exports = function(content) {
// console.log(content);
this.cacheable && this.cacheable();
var options = loaderUtils.getOptions(this) || {};
var ngModule = getAndInterpolateOption.call(this, 'module', 'ngMaterial'); // ngMaterial is the global angular module that does not need to explicitly required
var requireAngular = !!options.requireAngular || false;
var theme;
if (content.match(/^module\.exports/)) {
var firstQuote = findQuote(content, false);
var secondQuote = findQuote(content, true);
theme = content.substr(firstQuote, secondQuote - firstQuote + 1);
} else {
theme = JSON.stringify(content);
}
return 'var theme = ' + theme + ';\n' +
(requireAngular ? 'var angular = require(\'angular\');\n' : 'window.') +
'angular.module(\'' + ngModule + '\').config([\'$mdThemingProvider\', function(c) { c.registerStyles(theme) }]);';
function getAndInterpolateOption(optionKey, def) {
return options[optionKey]
? loaderUtils.interpolateName(this, options[optionKey], {
context: options.context,
content: content,
regExp: options[optionKey + 'RegExp'] || options['regExp']
})
: def;
}
function findQuote(content, backwards) {
var i = backwards ? content.length - 1 : 0;
while (i >= 0 && i < content.length) {
if (content[i] === '"' || content[i] === '\'') {
return i;
}
i += backwards ? -1 : 1;
}
return -1;
}
};