-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.ts
62 lines (52 loc) · 2.13 KB
/
index.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
import type {ResolveOptions} from "webpack";
type Resolver = NonNullable<ResolveOptions["resolver"]>;
const pluginName = "ResolveTypescriptPlugin";
interface ResolveTypescriptPluginOptions {
includeNodeModules?: boolean;
}
class ResolveTypescriptPlugin {
/** @deprecated For backwards compatibility with versions < v1.1.2.
* Will be removed in v2.0. */
public static default = ResolveTypescriptPlugin;
private static readonly defaultOptions: ResolveTypescriptPluginOptions = {
includeNodeModules: false
};
private readonly options: ResolveTypescriptPluginOptions;
public constructor(options: ResolveTypescriptPluginOptions = {}) {
this.options = {...ResolveTypescriptPlugin.defaultOptions, ...options};
}
public apply(resolver: Resolver): void {
const target = resolver.ensureHook("file");
for (const extension of [".ts", ".tsx"]) {
resolver
.getHook("raw-file")
.tapAsync(pluginName, (request, resolveContext, callback) => {
if (
typeof request.path !== "string" ||
(!(this.options.includeNodeModules ?? false) &&
request.path.match(/(^|[\\/])node_modules($|[\\/])/u) != null)
) {
callback();
return;
}
const path = request.path.replace(/\.jsx?$/u, extension);
if (path === request.path) {
callback();
} else {
resolver.doResolve(
target,
{
...request,
path,
relativePath: request.relativePath?.replace(/\.jsx?$/u, extension)
},
`using path: ${path}`,
resolveContext,
callback
);
}
});
}
}
}
export = ResolveTypescriptPlugin;