-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtype-script-support.ts
More file actions
133 lines (114 loc) · 3.92 KB
/
Copy pathtype-script-support.ts
File metadata and controls
133 lines (114 loc) · 3.92 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
import os from 'node:os';
import fs from 'node:fs';
import path from 'node:path';
import type { TypeScriptWorkerConfig } from './type-script-worker-config';
import {
getTsgoPackage,
readTsgoPackageJson,
} from './type-script-go-package';
import {
TYPESCRIPT_PACKAGE,
TYPESCRIPT_PACKAGE_JSON,
TYPESCRIPT_PREVIEW_PACKAGE,
TYPESCRIPT_PREVIEW_PACKAGE_JSON,
} from './type-script-go-constants';
import {
resolveTypeScriptGoBinPath,
resolveTypeScriptGoPackageJsonPath,
} from './type-script-go-runner';
function isDefaultTypeScriptGoPath(typescriptPath: string): boolean {
if (typescriptPath === TYPESCRIPT_PREVIEW_PACKAGE_JSON) {
return true;
}
try {
if (typescriptPath === require.resolve(TYPESCRIPT_PREVIEW_PACKAGE_JSON)) {
return true;
}
} catch {
// silent catch
}
if (
!path.isAbsolute(typescriptPath) ||
path.basename(typescriptPath) !== 'package.json'
) {
return false;
}
try {
return Boolean(getTsgoPackage(readTsgoPackageJson(typescriptPath)));
} catch {
return false;
}
}
function createTypeScriptGoSupportError(config: TypeScriptWorkerConfig, error?: unknown) {
const message = [
`When you enable TsCheckerRspackPlugin with \`typescript.tsgo\`, you must install \`${TYPESCRIPT_PACKAGE}@rc\` or \`${TYPESCRIPT_PREVIEW_PACKAGE}\` package.`,
];
if (!isDefaultTypeScriptGoPath(config.typescriptPath)) {
message.push(
`If you set \`typescript.typescriptPath\`, it must be an absolute path to \`${TYPESCRIPT_PACKAGE_JSON}\` from \`${TYPESCRIPT_PACKAGE}@rc\` or \`${TYPESCRIPT_PREVIEW_PACKAGE_JSON}\`.`,
);
}
if (error instanceof Error && error.message) {
message.push(`Failed to resolve the tsgo executable: ${error.message}`);
}
message.push(
`You can install it with \`npm add ${TYPESCRIPT_PACKAGE}@rc -D\` or \`npm add ${TYPESCRIPT_PREVIEW_PACKAGE} -D\`.`,
);
return new Error(message.join(os.EOL));
}
function assertTypeScriptGoSupport(config: TypeScriptWorkerConfig) {
try {
const tsgoPackageJsonPath = resolveTypeScriptGoPackageJsonPath(config);
const getExePathPath = path.resolve(
path.dirname(tsgoPackageJsonPath),
'./lib/getExePath.js',
);
if (!fs.existsSync(getExePathPath)) {
throw new Error();
}
} catch (error) {
throw createTypeScriptGoSupportError(config, error);
}
}
async function assertTypeScriptGoExecutable(config: TypeScriptWorkerConfig) {
try {
await resolveTypeScriptGoBinPath(config);
} catch (error) {
throw createTypeScriptGoSupportError(config, error);
}
}
function assertTypeScriptSupport(config: TypeScriptWorkerConfig) {
if (config.tsgo) {
assertTypeScriptGoSupport(config);
} else {
if (path.basename(config.typescriptPath) === 'package.json') {
throw new Error(
"When you use TsCheckerRspackPlugin without `typescript.tsgo`, `typescript.typescriptPath` should point to a path like `require.resolve('typescript')`.",
);
}
let typescriptVersion: string | undefined;
try {
// eslint-disable-next-line @typescript-eslint/no-var-requires
typescriptVersion = require(config.typescriptPath).version;
} catch {
// silent catch
}
if (!typescriptVersion) {
throw new Error(
'When you use TsCheckerRspackPlugin with typescript reporter enabled, you must install `typescript` package.',
);
}
}
if (!fs.existsSync(config.configFile)) {
throw new Error(
[
`Cannot find the "${config.configFile}" file.`,
`Please check Rspack and TsCheckerRspackPlugin configuration.`,
`Possible errors:`,
' - wrong `context` directory in Rspack configuration (if `configFile` is not set or is a relative path in the fork plugin configuration)',
' - wrong `typescript.configFile` path in the plugin configuration (should be a relative or absolute path)',
].join(os.EOL),
);
}
}
export { assertTypeScriptGoExecutable, assertTypeScriptSupport };