This repository has been archived by the owner on Oct 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
hookResolveFilename.ts
107 lines (84 loc) · 2.84 KB
/
hookResolveFilename.ts
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Copyright (c) 2018 Steven Enten. All rights reserved. Licensed under the MIT license.
import Module = require('module');
import debug = require('debug');
export interface IModuleStatic {
_resolveFilename: ModuleFilenameResolver;
}
export type ModuleFilenameResolver = (
request: string,
parent: Module,
isMain: boolean,
options?: ModuleFilenameResolverOptions | null,
) => string;
export type ModuleFilenameResolverHook = (
request: string,
parent: Module,
isMain: boolean,
options: ModuleFilenameResolverOptions | null | undefined,
previousResolver: ModuleFilenameResolver,
) => string;
export interface ModuleFilenameResolverOptions {
paths?: string[];
}
export interface ModuleRequestAlias {
fromFile: string;
replaceRequest: string;
withRequest: string;
}
export type RemoveModuleFilenameResolverHookFn = () => void;
export const ModuleStatic = Module as Partial<IModuleStatic> as IModuleStatic;
export const nativeModuleResolveFilename = ModuleStatic._resolveFilename;
export let moduleResolveFilename = nativeModuleResolveFilename;
const dbg = debug('udk:resolve');
export function aliasModuleResolveFilename(alias: ModuleRequestAlias) {
return hookModuleResolveFilename(resolveModuleRequestAlias.bind(null, alias));
}
export function disableModuleResolveFilenameHook() {
ModuleStatic._resolveFilename = nativeModuleResolveFilename;
}
export function enableModuleResolveFilenameHook() {
ModuleStatic._resolveFilename = (request, parent, isMain, options) => {
return moduleResolveFilename(request, parent, isMain, options);
};
}
export function hookModuleResolveFilename(
hook: ModuleFilenameResolverHook,
): RemoveModuleFilenameResolverHookFn {
const previousResolver = moduleResolveFilename;
let hookRemoved = false;
moduleResolveFilename = (request, parent, isMain, options) => {
if (hookRemoved) {
return previousResolver(request, parent, isMain, options);
}
return hook(request, parent, isMain, options, previousResolver);
};
return () => {
hookRemoved = true;
};
}
export function resolveModuleRequestAlias(
alias: ModuleRequestAlias,
request: string,
parent: Module,
isMain: boolean,
options: { paths: string[] } | null | undefined,
previousResolver: ModuleFilenameResolver,
) {
if (request === alias.replaceRequest) {
const parentId = previousResolver(parent.id, parent, isMain, options);
const fromFile = previousResolver(alias.fromFile, parent, isMain, options);
if (parentId === fromFile) {
const withRequest = previousResolver(alias.withRequest, parent, isMain, options);
dbg('alias hook %O', {
alias,
fromFile,
replaceRequest: alias.replaceRequest,
withRequest,
request,
parentId: parentId,
});
return withRequest;
}
}
return previousResolver(request, parent, isMain, options);
}