6
6
* found in the LICENSE file at https://angular.io/license
7
7
*/
8
8
import { virtualFs } from '@angular-devkit/core' ;
9
+ import { readFileSync , readdirSync } from 'fs' ;
10
+ import { dirname , join } from 'path' ;
9
11
import * as ts from 'typescript' ;
10
12
import { WebpackCompilerHost } from '../compiler_host' ;
11
13
@@ -48,14 +50,20 @@ export function getLastNode(sourceFile: ts.SourceFile): ts.Node | null {
48
50
// Test transform helpers.
49
51
const basePath = '/project/src/' ;
50
52
const fileName = basePath + 'test-file.ts' ;
53
+ const tsLibFiles = loadTsLibFiles ( ) ;
51
54
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
+ ) {
53
60
// Set compiler options.
54
61
const compilerOptions : ts . CompilerOptions = {
55
- noEmitOnError : false ,
62
+ noEmitOnError : useLibs ,
56
63
allowJs : true ,
57
64
newLine : ts . NewLineKind . LineFeed ,
58
65
moduleResolution : ts . ModuleResolutionKind . NodeJs ,
66
+ module : ts . ModuleKind . ESNext ,
59
67
target : ts . ScriptTarget . ESNext ,
60
68
skipLibCheck : true ,
61
69
sourceMap : false ,
@@ -73,6 +81,15 @@ export function createTypescriptContext(content: string, additionalFiles?: Recor
73
81
// Add a dummy file to host content.
74
82
compilerHost . writeFile ( fileName , content , false ) ;
75
83
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
+
76
93
if ( additionalFiles ) {
77
94
for ( const key in additionalFiles ) {
78
95
compilerHost . writeFile ( basePath + key , additionalFiles [ key ] , false ) ;
@@ -106,13 +123,27 @@ export function transformTypescript(
106
123
undefined , undefined , undefined , undefined , { before : transformers } ,
107
124
) ;
108
125
109
- // Log diagnostics if emit wasn't successfull.
126
+ // Throw error with diagnostics if emit wasn't successfull.
110
127
if ( emitSkipped ) {
111
- console . error ( diagnostics ) ;
112
-
113
- return null ;
128
+ throw new Error ( ts . formatDiagnostics ( diagnostics , compilerHost ) ) ;
114
129
}
115
130
116
131
// Return the transpiled js.
117
132
return compilerHost . readFile ( fileName . replace ( / \. t s x ? $ / , '.js' ) ) ;
118
133
}
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