Repository navigation
Expand file tree
/
Copy pathts-format.ts
More file actions
104 lines (87 loc) · 3.61 KB
/
Copy pathts-format.ts
File metadata and controls
104 lines (87 loc) · 3.61 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
namespace tsp {
/* ********************************************************* */
// region: Types
/* ********************************************************* */
export type TsFileFormat = 'commonjs' | 'module' | 'unsupported';
/** @internal */
interface ImpliedNodeFormatInfo {
impliedNodeFormat?: tsShim.ModuleKind;
packageJsonLocations?: string[];
packageJsonScope?: {
contents?: {
packageJsonContent?: {
type?: string
}
}
}
}
// endregion
/* ********************************************************* */
// region: Helpers
/* ********************************************************* */
function getCreateSourceFileOptions(
filePath: string,
compilerOptions: tsShim.CompilerOptions
): tsShim.CreateSourceFileOptions & Partial<ImpliedNodeFormatInfo> {
const implied = tsShim.getImpliedNodeFormatForFileWorker(
filePath,
undefined,
tsShim.sys,
compilerOptions
);
return {
...(typeof implied === 'object' ? implied : { impliedNodeFormat: implied }),
languageVersion: tsShim.getEmitScriptTarget(compilerOptions),
setExternalModuleIndicator: tsShim.getSetExternalModuleIndicator(compilerOptions)
};
}
function createFormatSourceFile(filePath: string, sourceText: string, compilerOptions: tsShim.CompilerOptions) {
const sourceFileOptions = getCreateSourceFileOptions(filePath, compilerOptions);
const sourceFile = tsShim.createSourceFile(filePath, sourceText, sourceFileOptions, false);
const internalSourceFile = sourceFile as tsShim.SourceFile & Partial<ImpliedNodeFormatInfo>;
internalSourceFile.packageJsonLocations = sourceFileOptions.packageJsonLocations;
internalSourceFile.packageJsonScope = sourceFileOptions.packageJsonScope;
return sourceFile;
}
// endregion
/* ********************************************************* */
// region: Utils
/* ********************************************************* */
export function getEmitFormat(
filePath: string,
sourceText: string,
compilerOptions: tsShim.CompilerOptions
): TsFileFormat {
const sourceFile = createFormatSourceFile(filePath, sourceText, compilerOptions);
const emitKind = tsShim.getEmitModuleFormatOfFileWorker(sourceFile, compilerOptions);
/*
* This switch names the Node load format, not the user's exact TypeScript emit target.
* Transformer source must be compiled to something Node can synchronously load. Known
* non-Node module wrappers are rejected deliberately, while unknown future module kinds
* are treated as ESM because new TypeScript module kinds are expected to extend modern
* ES/Node behavior rather than add another AMD/System-like wrapper.
*/
switch (emitKind) {
case undefined:
// getCompilerOptions() supplies NodeNext by default, so this should be unreachable.
// If TypeScript still cannot decide, preserve the old non-ESM transformer fallback.
case tsShim.ModuleKind.None:
// Historically ts-patch forced TS transformer loading to CommonJS unless ESM was active.
// Treat an explicit "None" module setting as that legacy CJS runtime fallback.
case tsShim.ModuleKind.CommonJS:
return 'commonjs';
case tsShim.ModuleKind.AMD:
case tsShim.ModuleKind.UMD:
case tsShim.ModuleKind.System:
case tsShim.ModuleKind.Preserve:
return 'unsupported';
case tsShim.ModuleKind.ES2015:
case tsShim.ModuleKind.ES2020:
case tsShim.ModuleKind.ES2022:
case tsShim.ModuleKind.ESNext:
default:
return 'module';
}
}
// endregion
}