-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
44 lines (42 loc) · 1.37 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
var glob = require("glob");
var path = require("path");
module.exports = function(source) {
this.cacheable();
var regex = /.?import + ?((\w+) +from )?([\'\"])(.*?)\3/gm;
var importModules = /import +(\w+) +from +([\'\"])(.*?)\2/gm;
var importFiles = /import +([\'\"])(.*?)\1/gm;
var importSass = /@import +([\'\"])(.*?)\1/gm;
var resourceDir = path.dirname(this.resourcePath);
function replacer(match, fromStatement, obj, quote, filename) {
var modules = [];
var withModules = false;
if (!glob.hasMagic(filename)) return match;
var result = glob
.sync(filename, {
cwd: resourceDir
})
.map(function(file, index) {
var fileName = quote + file + quote;
if (match.match(importSass)) {
return '@import ' + fileName;
} else if (match.match(importModules)) {
var moduleName = obj + index;
modules.push(moduleName);
withModules = true;
return 'import * as ' + moduleName + ' from ' + fileName;
} else if (match.match(importFiles)) {
return 'import ' + fileName;
}
})
.join('; ');
if (result && withModules) {
result += '; let ' + obj + ' = [' + modules.join(', ') + ']';
}
if(!result) {
result = 'const ' + obj + ' = []';
}
return result;
}
var res = source.replace(regex, replacer);
return res;
};