forked from mogelbrod/apply-loader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
35 lines (29 loc) · 976 Bytes
/
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
var loaderUtils = require("loader-utils");
module.exports = function() {}
module.exports.pitch = function applyLoader(remainingRequest) {
var query = loaderUtils.parseQuery(this.query);
var args = [];
this.cacheable && this.cacheable();
// apply?config=key => sourceFn(require('webpack.config').key)
if (typeof query.config === "string") {
if (!(query.config in this.options))
throw new Error("apply-loader: '"+query.config+"' property not present in webpack config");
args.push(this.options[query.config]);
}
// apply?{obj: {a: 1, b: 2}} => sourceFn({a: 1, b:2})
if (query.obj) {
args.push(query.obj);
}
// apply?args[]=1&args[]=2 => sourceFn(1, 2)
if (Array.isArray(query.args)) {
args.push.apply(args, query.args);
}
return [
"var req = require(",
JSON.stringify("!!" + remainingRequest),
");\n",
"module.exports = (req['default'] || req).apply(req, ",
JSON.stringify(args),
")"
].join("")
};