[6a3a178] | 1 | var LoaderLoadingError = require("./LoaderLoadingError");
|
---|
| 2 | var url;
|
---|
| 3 |
|
---|
| 4 | module.exports = function loadLoader(loader, callback) {
|
---|
| 5 | if(loader.type === "module") {
|
---|
| 6 | try {
|
---|
| 7 | if(url === undefined) url = require("url");
|
---|
| 8 | var loaderUrl = url.pathToFileURL(loader.path);
|
---|
| 9 | var modulePromise = eval("import(" + JSON.stringify(loaderUrl.toString()) + ")");
|
---|
| 10 | modulePromise.then(function(module) {
|
---|
| 11 | handleResult(loader, module, callback);
|
---|
| 12 | }, callback);
|
---|
| 13 | return;
|
---|
| 14 | } catch(e) {
|
---|
| 15 | callback(e);
|
---|
| 16 | }
|
---|
| 17 | } else {
|
---|
| 18 | try {
|
---|
| 19 | var module = require(loader.path);
|
---|
| 20 | } catch(e) {
|
---|
| 21 | // it is possible for node to choke on a require if the FD descriptor
|
---|
| 22 | // limit has been reached. give it a chance to recover.
|
---|
| 23 | if(e instanceof Error && e.code === "EMFILE") {
|
---|
| 24 | var retry = loadLoader.bind(null, loader, callback);
|
---|
| 25 | if(typeof setImmediate === "function") {
|
---|
| 26 | // node >= 0.9.0
|
---|
| 27 | return setImmediate(retry);
|
---|
| 28 | } else {
|
---|
| 29 | // node < 0.9.0
|
---|
| 30 | return process.nextTick(retry);
|
---|
| 31 | }
|
---|
| 32 | }
|
---|
| 33 | return callback(e);
|
---|
| 34 | }
|
---|
| 35 | return handleResult(loader, module, callback);
|
---|
| 36 | }
|
---|
| 37 | };
|
---|
| 38 |
|
---|
| 39 | function handleResult(loader, module, callback) {
|
---|
| 40 | if(typeof module !== "function" && typeof module !== "object") {
|
---|
| 41 | return callback(new LoaderLoadingError(
|
---|
| 42 | "Module '" + loader.path + "' is not a loader (export function or es6 module)"
|
---|
| 43 | ));
|
---|
| 44 | }
|
---|
| 45 | loader.normal = typeof module === "function" ? module : module.default;
|
---|
| 46 | loader.pitch = module.pitch;
|
---|
| 47 | loader.raw = module.raw;
|
---|
| 48 | if(typeof loader.normal !== "function" && typeof loader.pitch !== "function") {
|
---|
| 49 | return callback(new LoaderLoadingError(
|
---|
| 50 | "Module '" + loader.path + "' is not a loader (must have normal or pitch function)"
|
---|
| 51 | ));
|
---|
| 52 | }
|
---|
| 53 | callback();
|
---|
| 54 | }
|
---|