-
Notifications
You must be signed in to change notification settings - Fork 409
Expand file tree
/
Copy pathregister.ts
More file actions
138 lines (123 loc) · 3.69 KB
/
register.ts
File metadata and controls
138 lines (123 loc) · 3.69 KB
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
import path from 'node:path';
import {
fs,
type Alias,
getAliasConfig,
isDepExists,
loadFromProject,
readTsConfigByFile,
} from '@modern-js/utils';
import type { ConfigChain } from '@rsbuild/core';
type TsRuntimeRegisterMode = 'ts-node' | 'node-loader' | 'unsupported';
interface TsRuntimeSetupOptions {
moduleType?: string;
preferTsNodeForServerRuntime?: boolean;
}
// Describes final runtime selection policy.
// Prefer Node.js native TypeScript support when available, otherwise fall back to ts-node; skip setup when neither exists.
export const resolveTsRuntimeRegisterMode = (
hasTsNode: boolean,
): TsRuntimeRegisterMode => {
const hasNativeTypeScriptSupport = (process as any).features?.typescript;
const nodeMajorVersion = Number(process.versions.node.split('.')[0]);
const supportsNativeTypeScript =
hasNativeTypeScriptSupport === undefined
? nodeMajorVersion >= 22
: hasNativeTypeScriptSupport !== false;
if (supportsNativeTypeScript) {
return 'node-loader';
}
if (hasTsNode) {
return 'ts-node';
}
return 'unsupported';
};
/**
* Setup TypeScript runtime support.
* Register ts-node for compilation and tsconfig-paths for path alias resolution.
*/
export const setupTsRuntime = async (
appDir: string,
distDir: string,
alias?: ConfigChain<Alias>,
options: TsRuntimeSetupOptions = {},
) => {
const TS_CONFIG_FILENAME = `tsconfig.json`;
const tsconfigPath = path.resolve(appDir, TS_CONFIG_FILENAME);
const isTsProject = await fs.pathExists(tsconfigPath);
const hasTsNode = isDepExists(appDir, 'ts-node');
if (!isTsProject) {
return;
}
const registerMode = resolveTsRuntimeRegisterMode(hasTsNode);
const aliasConfig = getAliasConfig(alias, {
appDirectory: appDir,
tsconfigPath,
});
const { paths = {}, absoluteBaseUrl = './' } = aliasConfig;
const tsPaths = Object.keys(paths).reduce((o, key) => {
let tsPath = paths[key];
// Do some special handling for Modern.js's internal alias, we can drop it in the next version
if (
typeof tsPath === 'string' &&
key.startsWith('@') &&
tsPath.startsWith('@')
) {
try {
tsPath = require.resolve(tsPath, {
paths: [process.cwd(), ...module.paths],
});
} catch {}
}
if (typeof tsPath === 'string' && path.isAbsolute(tsPath)) {
tsPath = path.relative(absoluteBaseUrl, tsPath);
}
if (typeof tsPath === 'string') {
tsPath = [tsPath];
}
return {
...o,
[`${key}`]: tsPath,
};
}, {});
if (registerMode === 'unsupported') {
return;
}
if (registerMode === 'ts-node') {
if (options.moduleType === 'module') {
const { registerModuleHooks } = await import('../esm/register-esm.mjs');
await registerModuleHooks({
appDir,
distDir,
alias: alias || {},
});
}
const tsConfig = readTsConfigByFile(tsconfigPath);
const tsNode = await loadFromProject('ts-node', appDir);
const tsNodeOptions = tsConfig['ts-node'];
tsNode.register({
project: tsconfigPath,
scope: true,
// for env.d.ts, https://www.npmjs.com/package/ts-node#missing-types
files: true,
transpileOnly: true,
ignore: [
'(?:^|/)node_modules/',
`(?:^|/)${path.relative(appDir, distDir)}/`,
],
...tsNodeOptions,
});
} else if (registerMode === 'node-loader') {
const { registerPathsLoader } = await import('../esm/register-esm.mjs');
await registerPathsLoader({
appDir,
baseUrl: absoluteBaseUrl || './',
paths: tsPaths,
});
}
const { register } = await import('@modern-js/utils/tsconfig-paths');
register({
baseUrl: absoluteBaseUrl || './',
paths: tsPaths,
});
};