-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
57 lines (50 loc) · 1.34 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
/*
MIT License http://www.opensource.org/licenses/mit-license.php
Author vnot
Inspired by bundle-loader
*/
var loaderUtils = require("loader-utils");
module.exports = function() {};
module.exports.pitch = function(remainingRequest) {
this.cacheable && this.cacheable();
var query = loaderUtils.parseQuery(this.query);
var ext = query.ext // 需要动态加载的资源后缀
if (typeof ext !== 'string' || ext.trim().length === 0) {
throw new Error("file extension should be assigned via 'ext' param");
}
if (new RegExp(ext + '$', 'ig').test(remainingRequest)) {
return [
"var cbs = [], \n",
" data;\n",
"module.exports = function(cb) {\n",
" if(cbs) cbs.push(cb);\n",
" else cb(data);\n",
"}\n",
"require.ensure([], function(require) {\n",
" data = require(", loaderUtils.stringifyRequest(this, "!!" + remainingRequest), ");\n",
" var callbacks = cbs;\n",
" cbs = null;\n",
" for(var i = 0, l = callbacks.length; i < l; i++) {\n",
" callbacks[i](data);\n",
" }\n",
"});"].join("");
}
return "";
}
/*
Output format:
var cbs = [],
data;
module.exports = function(cb) {
if(cbs) cbs.push(cb);
else cb(data);
}
require.ensure([], function(require) {
data = require("xxx");
var callbacks = cbs;
cbs = null;
for(var i = 0, l = callbacks.length; i < l; i++) {
callbacks[i](data);
}
});
*/