source: frontend/node_modules/tsconfig-paths/src/register.ts

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

Fix frontend appearance

  • Property mode set to 100644
File size: 2.5 KB
Line 
1import { createMatchPath } from "./match-path-sync";
2import { configLoader, ExplicitParams } from "./config-loader";
3import { options } from "./options";
4
5const noOp = (): void => void 0;
6
7function getCoreModules(
8 builtinModules: string[] | undefined
9): { [key: string]: boolean } {
10 builtinModules = builtinModules || [
11 "assert",
12 "buffer",
13 "child_process",
14 "cluster",
15 "crypto",
16 "dgram",
17 "dns",
18 "domain",
19 "events",
20 "fs",
21 "http",
22 "https",
23 "net",
24 "os",
25 "path",
26 "punycode",
27 "querystring",
28 "readline",
29 "stream",
30 "string_decoder",
31 "tls",
32 "tty",
33 "url",
34 "util",
35 "v8",
36 "vm",
37 "zlib",
38 ];
39
40 const coreModules: { [key: string]: boolean } = {};
41 for (let module of builtinModules) {
42 coreModules[module] = true;
43 }
44
45 return coreModules;
46}
47
48/**
49 * Installs a custom module load function that can adhere to paths in tsconfig.
50 * Returns a function to undo paths registration.
51 */
52export function register(explicitParams: ExplicitParams): () => void {
53 const configLoaderResult = configLoader({
54 cwd: options.cwd,
55 explicitParams,
56 });
57
58 if (configLoaderResult.resultType === "failed") {
59 console.warn(
60 `${configLoaderResult.message}. tsconfig-paths will be skipped`
61 );
62
63 return noOp;
64 }
65
66 const matchPath = createMatchPath(
67 configLoaderResult.absoluteBaseUrl,
68 configLoaderResult.paths,
69 configLoaderResult.mainFields,
70 configLoaderResult.addMatchAll
71 );
72
73 // Patch node's module loading
74 // tslint:disable-next-line:no-require-imports variable-name
75 const Module = require("module");
76 const originalResolveFilename = Module._resolveFilename;
77 const coreModules = getCoreModules(Module.builtinModules);
78 // tslint:disable-next-line:no-any
79 Module._resolveFilename = function (request: string, _parent: any): string {
80 const isCoreModule = coreModules.hasOwnProperty(request);
81 if (!isCoreModule) {
82 const found = matchPath(request);
83 if (found) {
84 const modifiedArguments = [found, ...[].slice.call(arguments, 1)]; // Passes all arguments. Even those that is not specified above.
85 // tslint:disable-next-line:no-invalid-this
86 return originalResolveFilename.apply(this, modifiedArguments);
87 }
88 }
89 // tslint:disable-next-line:no-invalid-this
90 return originalResolveFilename.apply(this, arguments);
91 };
92
93 return () => {
94 // Return node's module loading to original state.
95 Module._resolveFilename = originalResolveFilename;
96 };
97}
Note: See TracBrowser for help on using the repository browser.