2012-03-10 20:11:23 +08:00
|
|
|
// Polyfill for node.js
|
2012-03-21 19:41:03 +08:00
|
|
|
// - adds require.ensure
|
|
|
|
// - adds require.context
|
2012-04-03 22:26:08 +08:00
|
|
|
// call it like this:
|
2012-03-21 19:41:03 +08:00
|
|
|
// require = require("webpack/require-polyfill")(require.valueOf());
|
|
|
|
// This is only required when you want to use the special require.xxx methods
|
2012-03-20 02:59:38 +08:00
|
|
|
// in server-side code which should be so only in rar cases.
|
2012-03-10 20:11:23 +08:00
|
|
|
module.exports = function(req) {
|
2012-03-21 19:41:03 +08:00
|
|
|
if(!req.webpackPolyfill) {
|
|
|
|
var oldReq = req;
|
|
|
|
req = function(name) {
|
2012-03-27 06:00:32 +08:00
|
|
|
if(name.indexOf("!") !== -1) {
|
|
|
|
var items = name.split(/!/g);
|
|
|
|
var resource = oldReq.resolve(items.pop());
|
|
|
|
var resolved = [];
|
|
|
|
items.forEach(function(item, index) {
|
|
|
|
var relative = false;
|
|
|
|
if(item.length > 2 &&
|
|
|
|
item[0] === ".") {
|
|
|
|
if(item[1] === "/")
|
|
|
|
relative = true;
|
|
|
|
else if(item.length > 3 &&
|
|
|
|
item[1] === "." &&
|
|
|
|
item[2] === "/")
|
|
|
|
relative = true;
|
|
|
|
}
|
|
|
|
if(item.length > 3 &&
|
|
|
|
item[1] === ":" &&
|
|
|
|
item[2] === "\\")
|
|
|
|
relative = true;
|
|
|
|
var tries = [];
|
|
|
|
if(!relative) {
|
|
|
|
postfixes.forEach(function(postfix) {
|
|
|
|
if(item.indexOf("/") !== -1)
|
|
|
|
tries.push(item.replace("/", postfix+"/"));
|
|
|
|
else
|
|
|
|
tries.push(item + postfix);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
tries.push(item);
|
|
|
|
for(var i = 0; i < tries.length; i++) {
|
|
|
|
for(var ext = 0; ext < extensions.length; ext++) {
|
|
|
|
try {
|
|
|
|
var file = oldReq.resolve(tries[i] + extensions[ext]);
|
|
|
|
} catch(e) {}
|
|
|
|
if(file) {
|
|
|
|
resolved.push(file);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(ext !== extensions.length)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if(i === tries.length)
|
|
|
|
throw new Error("Cannot find loader module '"+item+"'");
|
|
|
|
});
|
|
|
|
resolved = resolved.reverse();
|
|
|
|
var cacheLine = resolved.join("!") + "!" + resource;
|
|
|
|
var cacheEntry = oldReq.cache[cacheLine];
|
|
|
|
if(cacheEntry)
|
|
|
|
return cacheEntry;
|
|
|
|
var content = [require("fs").readFileSync(resource, "utf-8")];
|
2012-04-03 22:26:08 +08:00
|
|
|
var values;
|
|
|
|
function exec(code, filename) {
|
|
|
|
var Module = require("module");
|
|
|
|
var m = new Module("exec in " + cacheLine, module);
|
|
|
|
m._compile(code, filename);
|
|
|
|
return m.exports;
|
|
|
|
}
|
2012-03-27 06:00:32 +08:00
|
|
|
resolved.forEach(function(loader) {
|
2012-04-03 22:26:08 +08:00
|
|
|
var set = false, err = null;
|
|
|
|
var context = {
|
2012-03-27 06:00:32 +08:00
|
|
|
request: cacheLine,
|
2012-04-03 22:26:08 +08:00
|
|
|
filenames: [resource],
|
|
|
|
exec: exec,
|
|
|
|
async: function() { return false; },
|
|
|
|
callback: function() {
|
|
|
|
set = true;
|
|
|
|
content = Array.prototype.slice.apply(arguments);
|
|
|
|
err = content.shift();
|
|
|
|
values = context.values;
|
|
|
|
},
|
|
|
|
inputValues: values,
|
|
|
|
values: undefined
|
|
|
|
};
|
|
|
|
var retVal = oldReq(loader).apply(context, content);
|
|
|
|
if(set) {
|
|
|
|
if(err) throw err;
|
|
|
|
} else {
|
|
|
|
content = [retVal];
|
|
|
|
values = context.values;
|
|
|
|
}
|
2012-03-27 06:00:32 +08:00
|
|
|
});
|
2012-04-03 22:26:08 +08:00
|
|
|
if(values !== undefined)
|
|
|
|
return values[0];
|
|
|
|
return exec(content[0], cacheLine);
|
2012-03-27 06:00:32 +08:00
|
|
|
} else
|
|
|
|
return oldReq(name);
|
2012-03-21 19:41:03 +08:00
|
|
|
};
|
|
|
|
req.__proto__ = oldReq;
|
|
|
|
req.webpackPolyfill = true;
|
|
|
|
}
|
2012-03-10 20:11:23 +08:00
|
|
|
if(!req.ensure) {
|
|
|
|
req.ensure = function(array, callback) {
|
|
|
|
callback(req);
|
|
|
|
};
|
|
|
|
}
|
2012-03-11 20:44:38 +08:00
|
|
|
if(!req.context) {
|
|
|
|
req.context = function(contextName) {
|
|
|
|
return function(name) {
|
|
|
|
return req(contextName + "/" + name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-03-21 19:41:03 +08:00
|
|
|
return req;
|
2012-03-27 06:00:32 +08:00
|
|
|
}
|
|
|
|
var extensions = [".webpack-loader.js", ".loader.js", ".js", ""];
|
|
|
|
var postfixes = ["-webpack-loader", "-loader", ""]
|