-
Notifications
You must be signed in to change notification settings - Fork 304
/
ng-jest-compiler.ts
288 lines (252 loc) · 11.8 KB
/
ng-jest-compiler.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
import { CompilerHost, CompilerOptions } from '@angular/compiler-cli';
import { createCompilerHost } from '@angular/compiler-cli/src/transformers/compiler_host';
import type { Logger } from 'bs-logger';
import { updateOutput } from 'ts-jest/dist/compiler/compiler-utils';
import type { CompilerInstance, TTypeScript, ResolvedModulesMap } from 'ts-jest/dist/types';
import type * as ts from 'typescript';
import type { NgJestConfig } from '../config/ng-jest-config';
import { constructorParametersDownlevelTransform } from '../transformers/downlevel-ctor';
import { replaceResources } from '../transformers/replace-resources';
import { NgJestCompilerHost } from './compiler-host';
interface PatchedTranspileOptions {
fileName: string;
compilerOptions: ts.CompilerOptions;
moduleName?: string;
renamedDependencies?: ts.MapLike<string>;
}
export class NgJestCompiler implements CompilerInstance {
private _compilerOptions!: CompilerOptions;
private _program: ts.Program | undefined;
private _compilerHost: CompilerHost | undefined;
private _tsHost: NgJestCompilerHost | undefined;
private _rootNames: string[] = [];
private readonly _logger: Logger;
private readonly _ts: TTypeScript;
private readonly isAppPath = (fileName: string) =>
!fileName.endsWith('.ngfactory.ts') && !fileName.endsWith('.ngstyle.ts');
constructor(readonly ngJestConfig: NgJestConfig, readonly jestCacheFS: Map<string, string>) {
this._logger = this.ngJestConfig.logger;
this._ts = this.ngJestConfig.compilerModule;
this._setupOptions(this.ngJestConfig);
this._logger.debug('created NgJestCompiler');
}
getResolvedModulesMap(fileContent: string, fileName: string): ResolvedModulesMap {
this._tsHost?.updateMemoryHost(fileName, fileContent);
// eslint-disable-next-line
return (this._program?.getSourceFile(fileName) as any)?.resolvedModules;
}
getCompiledOutput(fileName: string, fileContent: string, supportsStaticESM: boolean): string {
const customTransformers = this.ngJestConfig.customTransformers;
let moduleKind = this._compilerOptions.module;
let esModuleInterop = this._compilerOptions.esModuleInterop;
let allowSyntheticDefaultImports = this._compilerOptions.allowSyntheticDefaultImports;
if (supportsStaticESM && this.ngJestConfig.useESM) {
moduleKind =
!moduleKind ||
(moduleKind &&
![this._ts.ModuleKind.ES2015, this._ts.ModuleKind.ES2020, this._ts.ModuleKind.ESNext].includes(moduleKind))
? this._ts.ModuleKind.ESNext
: moduleKind;
// Make sure `esModuleInterop` and `allowSyntheticDefaultImports` true to support import CJS into ESM
esModuleInterop = true;
allowSyntheticDefaultImports = true;
} else {
moduleKind = this._ts.ModuleKind.CommonJS;
}
this._compilerOptions = {
...this._compilerOptions,
allowSyntheticDefaultImports,
esModuleInterop,
module: moduleKind,
};
if (this._program) {
const allDiagnostics: ts.Diagnostic[] = [];
if (!this._rootNames.includes(fileName)) {
this._logger.debug({ fileName }, 'getCompiledOutput: update memory host, rootFiles and Program');
this._rootNames = [...this._rootNames, fileName];
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
this._tsHost!.updateMemoryHost(fileName, fileContent);
this._createOrUpdateProgram();
}
this._logger.debug({ fileName }, 'getCompiledOutput: compiling using Program');
const sourceFile = this._program.getSourceFile(fileName);
const emitResult = this._program.emit(sourceFile, undefined, undefined, undefined, {
...customTransformers,
before: [
...(customTransformers.before as Array<ts.TransformerFactory<ts.SourceFile>>),
/**
* Downlevel constructor parameters for DI support. This is required to support forwardRef in ES2015 due to
* TDZ issues. This wrapper is needed here due to the program not being available until after
* the transformers are created. Also because program can be updated so we can't push this transformer in
* _createCompilerHost
*/
constructorParametersDownlevelTransform(this._program),
replaceResources(this.isAppPath, this._program.getTypeChecker),
],
});
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const compiledOutput: [string, string] = this._tsHost!.getEmittedResult();
if (this.ngJestConfig.shouldReportDiagnostics(fileName)) {
this._logger.debug({ fileName }, 'getCompiledOutput: getting diagnostics');
allDiagnostics.push(
...emitResult.diagnostics,
...this._program.getSyntacticDiagnostics(sourceFile),
...this._program.getSemanticDiagnostics(sourceFile),
);
}
if (!allDiagnostics.length) {
this._logger.debug({ fileName }, 'getCompiledOutput: update compiled output including inline source map');
return updateOutput(compiledOutput[0], fileName, compiledOutput[1]);
} else {
this.ngJestConfig.raiseDiagnostics(allDiagnostics, fileName);
return '';
}
} else {
this._logger.debug({ fileName }, 'getCompiledOutput: compiling as isolated module');
const result: ts.TranspileOutput = this._transpileModule(
fileContent,
{
fileName,
compilerOptions: this._compilerOptions,
},
customTransformers,
);
if (result.diagnostics && this.ngJestConfig.shouldReportDiagnostics(fileName)) {
this.ngJestConfig.raiseDiagnostics(result.diagnostics, fileName, this._logger);
}
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
return updateOutput(result.outputText, fileName, result.sourceMapText!);
}
}
private _setupOptions({ parsedTsConfig }: NgJestConfig): void {
this._logger.debug({ parsedTsConfig }, '_setupOptions: initializing compiler config');
this._compilerOptions = { ...parsedTsConfig.options };
this._rootNames = parsedTsConfig.rootNames.filter((rootName) => !this.ngJestConfig.isTestFile(rootName));
if (this._compilerOptions.strictMetadataEmit) {
this._logger.warn(
`Using Angular compiler option 'strictMetadataEmit' for applications might cause undefined behavior.`,
);
}
if (!this.ngJestConfig.isolatedModules) {
this._logger.debug(
{ compilerOptions: this._compilerOptions },
'_setupOptions: creating Compiler Host using @angular/compiler-cli createCompilerHost',
);
this._tsHost = new NgJestCompilerHost(this._logger, this.ngJestConfig, this.jestCacheFS);
this._compilerHost = createCompilerHost({
options: this._compilerOptions,
tsHost: this._tsHost,
});
this._createOrUpdateProgram();
}
}
private _createOrUpdateProgram(): void {
const oldTsProgram = this._program;
this._logger.debug(`_createOrUpdateProgram: ${oldTsProgram ? 'update' : 'create'} TypeScript Program`);
this._program = this._ts.createProgram(this._rootNames, this._compilerOptions, this._compilerHost, oldTsProgram);
}
/**
* Copy from https://github.com/microsoft/TypeScript/blob/master/src/services/transpile.ts
* This is required because the exposed function `transpileModule` from TypeScript doesn't allow to access `Program`
* and we need `Program` to be able to use Angular `replace-resources` transformer.
*/
private _transpileModule(
fileContent: string,
transpileOptions: PatchedTranspileOptions,
customTransformers: ts.CustomTransformers,
): ts.TranspileOutput {
const diagnostics: ts.Diagnostic[] = [];
const options: ts.CompilerOptions = transpileOptions.compilerOptions
? // @ts-expect-error internal TypeScript API
this._ts.fixupCompilerOptions(transpileOptions.compilerOptions, diagnostics)
: {};
// mix in default options
const defaultOptions = this._ts.getDefaultCompilerOptions();
for (const key in defaultOptions) {
// @ts-expect-error internal TypeScript API
if (this._ts.hasProperty(defaultOptions, key) && options[key] === undefined) {
options[key] = defaultOptions[key];
}
}
// @ts-expect-error internal TypeScript API
for (const option of this._ts.transpileOptionValueCompilerOptions) {
options[option.name] = option.transpileOptionValue;
}
/**
* transpileModule does not write anything to disk so there is no need to verify that there are no conflicts between
* input and output paths.
*/
options.suppressOutputPathCheck = true;
// Filename can be non-ts file.
options.allowNonTsExtensions = true;
// if jsx is specified then treat file as .tsx
const inputFileName =
transpileOptions.fileName ||
(transpileOptions.compilerOptions && transpileOptions.compilerOptions.jsx ? 'module.tsx' : 'module.ts');
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const sourceFile = this._ts.createSourceFile(inputFileName, fileContent, options.target!); // TODO: GH#18217
if (transpileOptions.moduleName) {
sourceFile.moduleName = transpileOptions.moduleName;
}
if (transpileOptions.renamedDependencies) {
// @ts-expect-error internal TypeScript API
sourceFile.renamedDependencies = new Map(getEntries(transpileOptions.renamedDependencies));
}
// @ts-expect-error internal TypeScript API
const newLine = this._ts.getNewLineCharacter(options);
// Output
let outputText: string | undefined;
let sourceMapText: string | undefined;
// Create a compilerHost object to allow the compiler to read and write files
const compilerHost: ts.CompilerHost = {
// @ts-expect-error internal TypeScript API
getSourceFile: (fileName) => (fileName === this._ts.normalizePath(inputFileName) ? sourceFile : undefined),
writeFile: (name, text) => {
// @ts-expect-error internal TypeScript API
if (this._ts.fileExtensionIs(name, '.map')) {
// @ts-expect-error internal TypeScript API
this._ts.Debug.assertEqual(sourceMapText, undefined, 'Unexpected multiple source map outputs, file:', name);
sourceMapText = text;
} else {
// @ts-expect-error internal TypeScript API
this._ts.Debug.assertEqual(outputText, undefined, 'Unexpected multiple outputs, file:', name);
outputText = text;
}
},
getDefaultLibFileName: () => 'lib.d.ts',
useCaseSensitiveFileNames: () => false,
getCanonicalFileName: (fileName) => fileName,
getCurrentDirectory: () => '',
getNewLine: () => newLine,
fileExists: (fileName): boolean => fileName === inputFileName,
readFile: () => '',
directoryExists: () => true,
getDirectories: () => [],
};
const program = this._ts.createProgram([inputFileName], options, compilerHost);
if (this.ngJestConfig.shouldReportDiagnostics(inputFileName)) {
// @ts-expect-error internal TypeScript API
this._ts.addRange(/*to*/ diagnostics, /*from*/ program.getSyntacticDiagnostics(sourceFile));
// @ts-expect-error internal TypeScript API
this._ts.addRange(/*to*/ diagnostics, /*from*/ program.getOptionsDiagnostics());
}
// Emit
program.emit(
/*targetSourceFile*/ undefined,
/*writeFile*/ undefined,
/*cancellationToken*/ undefined,
/*emitOnlyDtsFiles*/ undefined,
{
...customTransformers,
before: [
// hoisting from `ts-jest` or other before transformers
...(customTransformers.before as Array<ts.TransformerFactory<ts.SourceFile>>),
replaceResources(this.isAppPath, program.getTypeChecker),
],
},
);
// @ts-expect-error internal TypeScript API
if (outputText === undefined) return this._ts.Debug.fail('Output generation failed');
return { outputText, diagnostics, sourceMapText };
}
}