This repository has been archived by the owner on Oct 18, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathindex.ts
201 lines (175 loc) · 4.67 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
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import path from 'path';
import fs from 'fs';
import Module from 'module';
import {
transformSync,
installSourceMapSupport,
resolveTsPath,
transformDynamicImport,
compareNodeVersion,
} from '@esbuild-kit/core-utils';
import {
getTsconfig,
parseTsconfig,
createPathsMatcher,
createFilesMatcher,
} from 'get-tsconfig';
import type { TransformOptions } from 'esbuild';
const isPathPattern = /^\.{0,2}\//;
const isTsFilePatten = /\.[cm]?tsx?$/;
const nodeModulesPath = `${path.sep}node_modules${path.sep}`;
const tsconfig = (
process.env.ESBK_TSCONFIG_PATH
? {
path: path.resolve(process.env.ESBK_TSCONFIG_PATH),
config: parseTsconfig(process.env.ESBK_TSCONFIG_PATH),
}
: getTsconfig()
);
const fileMatcher = tsconfig && createFilesMatcher(tsconfig);
const tsconfigPathsMatcher = tsconfig && createPathsMatcher(tsconfig);
const applySourceMap = installSourceMapSupport();
const nodeSupportsImport = (
// v13.2.0 and higher
compareNodeVersion([13, 2, 0]) >= 0
// 12.20.0 ~ 13.0.0
|| (
compareNodeVersion([12, 20, 0]) >= 0
&& compareNodeVersion([13, 0, 0]) < 0
)
);
function transformer(
module: Module,
filePath: string,
) {
/**
* For tracking dependencies in watch mode
*/
if (process.send) {
process.send({
type: 'dependency',
path: filePath,
});
}
let code = fs.readFileSync(filePath, 'utf8');
if (filePath.endsWith('.cjs') && nodeSupportsImport) {
const transformed = transformDynamicImport(filePath, code);
if (transformed) {
code = applySourceMap(transformed, filePath);
}
} else {
const transformed = transformSync(
code,
filePath,
{
tsconfigRaw: fileMatcher?.(filePath) as TransformOptions['tsconfigRaw'],
},
);
code = applySourceMap(transformed, filePath);
}
module._compile(code, filePath);
}
const extensions = Module._extensions;
/**
* Loaders for implicitly resolvable extensions
* https://github.com/nodejs/node/blob/v12.16.0/lib/internal/modules/cjs/loader.js#L1166
*/
[
'.js', // (Handles .cjs, .cts, .mts & any explicitly specified extension that doesn't match any loaders)
'.ts',
'.tsx',
'.jsx',
].forEach((extension) => {
extensions[extension] = transformer;
});
/**
* Loaders for explicitly resolvable extensions
* (basically just .mjs because CJS loader has a special handler for it)
*
* Loaders for extensions .cjs, .cts, & .mts don't need to be
* registered because they're explicitly specified and unknown
* extensions (incl .cjs) fallsback to using the '.js' loader:
* https://github.com/nodejs/node/blob/v18.4.0/lib/internal/modules/cjs/loader.js#L430
*
* That said, it's actually ".js" and ".mjs" that get special treatment
* rather than ".cjs" (it might as well be ".random-ext")
*/
Object.defineProperty(extensions, '.mjs', {
value: transformer,
// Prevent Object.keys from detecting these extensions
// when CJS loader iterates over the possible extensions
enumerable: false,
});
const supportsNodePrefix = (
compareNodeVersion([16, 0, 0]) >= 0
|| compareNodeVersion([14, 18, 0]) >= 0
);
// Add support for "node:" protocol
const resolveFilename = Module._resolveFilename;
Module._resolveFilename = function (request, parent, isMain, options) {
// Added in v12.20.0
// https://nodejs.org/api/esm.html#esm_node_imports
if (!supportsNodePrefix && request.startsWith('node:')) {
request = request.slice(5);
}
if (
tsconfigPathsMatcher
// bare specifier
&& !isPathPattern.test(request)
// Dependency paths should not be resolved using tsconfig.json
&& !parent?.filename?.includes(nodeModulesPath)
) {
const possiblePaths = tsconfigPathsMatcher(request);
for (const possiblePath of possiblePaths) {
const tsFilename = resolveTsFilename.call(this, possiblePath, parent, isMain, options);
if (tsFilename) {
return tsFilename;
}
try {
return resolveFilename.call(
this,
possiblePath,
parent,
isMain,
options,
);
} catch {}
}
}
const tsFilename = resolveTsFilename.call(this, request, parent, isMain, options);
if (tsFilename) {
return tsFilename;
}
return resolveFilename.call(this, request, parent, isMain, options);
};
/**
* Typescript gives .ts, .cts, or .mts priority over actual .js, .cjs, or .mjs extensions
*/
function resolveTsFilename(
this: ThisType<typeof resolveFilename>,
request: string,
parent: any,
isMain: boolean,
options?: any,
) {
const tsPath = resolveTsPath(request);
if (parent && isTsFilePatten.test(parent.filename) && tsPath) {
try {
return resolveFilename.call(
this,
tsPath,
parent,
isMain,
options,
);
} catch (error) {
const { code } = error as any;
if (
code !== 'MODULE_NOT_FOUND'
&& code !== 'ERR_PACKAGE_PATH_NOT_EXPORTED'
) {
throw error;
}
}
}
}