Skip to content

Commit 4ff1640

Browse files
filipesilvaKeen Yee Liau
authored andcommitted
test(@ngtools/webpack): support libs on transformer test errors
1 parent 9867c95 commit 4ff1640

File tree

1 file changed

+37
-6
lines changed

1 file changed

+37
-6
lines changed

packages/ngtools/webpack/src/transformers/ast_helpers.ts

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88
import { virtualFs } from '@angular-devkit/core';
9+
import { readFileSync, readdirSync } from 'fs';
10+
import { dirname, join } from 'path';
911
import * as ts from 'typescript';
1012
import { WebpackCompilerHost } from '../compiler_host';
1113

@@ -48,14 +50,20 @@ export function getLastNode(sourceFile: ts.SourceFile): ts.Node | null {
4850
// Test transform helpers.
4951
const basePath = '/project/src/';
5052
const fileName = basePath + 'test-file.ts';
53+
const tsLibFiles = loadTsLibFiles();
5154

52-
export function createTypescriptContext(content: string, additionalFiles?: Record<string, string>) {
55+
export function createTypescriptContext(
56+
content: string,
57+
additionalFiles?: Record<string, string>,
58+
useLibs = false,
59+
) {
5360
// Set compiler options.
5461
const compilerOptions: ts.CompilerOptions = {
55-
noEmitOnError: false,
62+
noEmitOnError: useLibs,
5663
allowJs: true,
5764
newLine: ts.NewLineKind.LineFeed,
5865
moduleResolution: ts.ModuleResolutionKind.NodeJs,
66+
module: ts.ModuleKind.ESNext,
5967
target: ts.ScriptTarget.ESNext,
6068
skipLibCheck: true,
6169
sourceMap: false,
@@ -73,6 +81,15 @@ export function createTypescriptContext(content: string, additionalFiles?: Recor
7381
// Add a dummy file to host content.
7482
compilerHost.writeFile(fileName, content, false);
7583

84+
if (useLibs) {
85+
// Write the default libs.
86+
// These are needed for tests that use import(), because it relies on a Promise being there.
87+
const compilerLibFolder = dirname(compilerHost.getDefaultLibFileName(compilerOptions));
88+
for (const [k, v] of Object.entries(tsLibFiles)) {
89+
compilerHost.writeFile(join(compilerLibFolder, k), v, false);
90+
}
91+
}
92+
7693
if (additionalFiles) {
7794
for (const key in additionalFiles) {
7895
compilerHost.writeFile(basePath + key, additionalFiles[key], false);
@@ -106,13 +123,27 @@ export function transformTypescript(
106123
undefined, undefined, undefined, undefined, { before: transformers },
107124
);
108125

109-
// Log diagnostics if emit wasn't successfull.
126+
// Throw error with diagnostics if emit wasn't successfull.
110127
if (emitSkipped) {
111-
console.error(diagnostics);
112-
113-
return null;
128+
throw new Error(ts.formatDiagnostics(diagnostics, compilerHost));
114129
}
115130

116131
// Return the transpiled js.
117132
return compilerHost.readFile(fileName.replace(/\.tsx?$/, '.js'));
118133
}
134+
135+
function loadTsLibFiles() {
136+
const libFolderPath = dirname(require.resolve('typescript/lib/lib.d.ts'));
137+
const libFolderFiles = readdirSync(libFolderPath);
138+
const libFileNames = libFolderFiles.filter(f => f.startsWith('lib.') && f.endsWith('.d.ts'));
139+
140+
// Return a map of the lib names to their content.
141+
return libFileNames.reduce(
142+
(map, f) => {
143+
map[f] = readFileSync(join(libFolderPath, f), 'utf-8');
144+
145+
return map;
146+
},
147+
{} as { [k: string]: string },
148+
);
149+
}

0 commit comments

Comments
 (0)