forked from tzusman/glob-loader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
121 lines (90 loc) · 3.09 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
"use strict";
var glob = require("glob");
var globArray = require('./glob-array');
var path = require("path");
var loaderUtils = require("loader-utils");
module.exports = function (content, sourceMap) {
this.cacheable && this.cacheable();
var query = loaderUtils.parseQuery(this.query);
if (query.pattern) {
return content;
}
var resourceDir = path.dirname(this.resourcePath);
content = require(this.resourcePath);
var getObject = function(key) {
var config = content[key];
var files = globArray.sync(config.glob, {
cwd: resourceDir
});
if (!files.length) {
this.emitWarning('Did not find anything for glob key ["' + key + '"] in directory "' + resourceDir + '"');
}
return generateObject.call(this, files, resourceDir, config);
}.bind(this);
if (query.section) {
return 'module.exports = ' + getObject(query.section);
}
var objects = Object.keys(content).map(function(key) {
var object = getObject(key);
return JSON.stringify(key) + ': ' + object;
}, this);
var result = 'module.exports = {\n' + objects.join(',\n') + '\n};'
return result;
};
// var request = loaderUtils.stringifyRequest(this, remainingRequest);
module.exports.pitch = function(remainingRequest, precedingRequest, data) {
this.cacheable && this.cacheable();
var query = loaderUtils.parseQuery(this.query);
var resourceDir;
if (query.pattern) {
var issuer;
if (typeof this._module.issuer === 'string') {
issuer = this._module.issuer;
} else {
issuer = this._module.issuer.resource;
}
var issuerIndex = issuer.lastIndexOf('!');
if (issuerIndex !== -1) {
issuer = issuer.slice(issuerIndex + 1);
}
return handle.call(this, query, issuer);
}
};
function handle(query, issuer) {
var resourceDir = path.dirname(issuer);
var files = glob.sync(query.pattern, {
cwd: resourceDir
});
if (!files.length) {
this.emitWarning('Did not find anything for glob "' + query.pattern + '" in directory "' + resourceDir + '"');
}
var result = 'module.exports = {\n' + files.map(function (file) {
var dep = path.resolve(resourceDir, file);
this.addDependency(dep);
var stringifiedFile = JSON.stringify(file);
var stringifiedDep = JSON.stringify(dep);
return '\t' + stringifiedFile + ': require(' + stringifiedDep + ')';
}, this).join(',\n') + '\n};';
return result;
}
function generateObject(files, resourceDir, config) {
var rewrite = config.rewrite;
var needExport = config.export === false ? false : true;
var result = files.map(function (file) {
var dependency = path.resolve(resourceDir, file);
this.addDependency(dependency);
var stringifiedDep = JSON.stringify(dependency);
var requireString = 'require(' + stringifiedDep + ')';
if (needExport) {
var stringifiedFile = JSON.stringify(rewrite ? rewrite(file) : file);
return '\t' + stringifiedFile + ': ' + requireString;
} else {
return requireString;
}
}, this);
if (needExport) {
return '({\n' + result.join(',\n') + '\n})';
} else {
return '(' + result.join(', ') + ', null)';
}
}