source: frontend/node_modules/loader-runner/lib/loadLoader.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 11 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 1.8 KB
Line 
1"use strict";
2
3const LoaderLoadingError = require("./LoaderLoadingError");
4
5let url;
6
7function handleResult(loader, module, callback) {
8 if (typeof module !== "function" && typeof module !== "object") {
9 return callback(
10 new LoaderLoadingError(
11 `Module '${loader.path}' is not a loader (export function or es6 module)`
12 )
13 );
14 }
15
16 loader.normal = typeof module === "function" ? module : module.default;
17 loader.pitch = module.pitch;
18 loader.raw = module.raw;
19
20 if (
21 typeof loader.normal !== "function" &&
22 typeof loader.pitch !== "function"
23 ) {
24 return callback(
25 new LoaderLoadingError(
26 `Module '${loader.path}' is not a loader (must have normal or pitch function)`
27 )
28 );
29 }
30 callback();
31}
32
33function loadLoader(loader, callback) {
34 if (loader.type === "module") {
35 try {
36 if (url === undefined) url = require("url");
37
38 // eslint-disable-next-line n/no-unsupported-features/node-builtins
39 const loaderUrl = url.pathToFileURL(loader.path);
40 // Use `eval` so older parsers (and the main module resolver) don't
41 // need to recognize the dynamic `import()` syntax at load time.
42 // eslint-disable-next-line no-eval
43 const modulePromise = eval(
44 `import(${JSON.stringify(loaderUrl.toString())})`
45 );
46
47 modulePromise.then((module) => {
48 handleResult(loader, module, callback);
49 }, callback);
50 } catch (err) {
51 callback(err);
52 }
53 return;
54 }
55
56 let loadedModule;
57 try {
58 loadedModule = require(loader.path);
59 } catch (err) {
60 // It is possible for node to choke on a require if the FD descriptor
61 // limit has been reached. Give it a chance to recover by deferring.
62 if (err instanceof Error && err.code === "EMFILE") {
63 return setImmediate(loadLoader, loader, callback);
64 }
65 return callback(err);
66 }
67
68 return handleResult(loader, loadedModule, callback);
69}
70
71module.exports = loadLoader;
Note: See TracBrowser for help on using the repository browser.