|
5 | 5 | * Use of this source code is governed by an MIT-style license that can be |
6 | 6 | * found in the LICENSE file at https://angular.io/license |
7 | 7 | */ |
8 | | -import { virtualFs } from '@angular-devkit/core'; |
9 | | -import { readFileSync, readdirSync } from 'fs'; |
10 | | -import { dirname, join } from 'path'; |
11 | 8 | import * as ts from 'typescript'; |
12 | | -import { WebpackCompilerHost } from '../compiler_host'; |
13 | 9 |
|
14 | 10 |
|
15 | 11 | // Find all nodes from the AST in the subtree of node of SyntaxKind kind. |
@@ -45,129 +41,3 @@ export function getLastNode(sourceFile: ts.SourceFile): ts.Node | null { |
45 | 41 |
|
46 | 42 | return null; |
47 | 43 | } |
48 | | - |
49 | | - |
50 | | -// Test transform helpers. |
51 | | -const basePath = '/project/src/'; |
52 | | -const fileName = basePath + 'test-file.ts'; |
53 | | -const typeScriptLibFiles = loadTypeScriptLibFiles(); |
54 | | -const tsLibFiles = loadTsLibFiles(); |
55 | | - |
56 | | -export function createTypescriptContext( |
57 | | - content: string, |
58 | | - additionalFiles?: Record<string, string>, |
59 | | - useLibs = false, |
60 | | - extraCompilerOptions: ts.CompilerOptions = {}, |
61 | | -) { |
62 | | - // Set compiler options. |
63 | | - const compilerOptions: ts.CompilerOptions = { |
64 | | - noEmitOnError: useLibs, |
65 | | - allowJs: true, |
66 | | - newLine: ts.NewLineKind.LineFeed, |
67 | | - moduleResolution: ts.ModuleResolutionKind.NodeJs, |
68 | | - module: ts.ModuleKind.ESNext, |
69 | | - target: ts.ScriptTarget.ESNext, |
70 | | - skipLibCheck: true, |
71 | | - sourceMap: false, |
72 | | - importHelpers: true, |
73 | | - experimentalDecorators: true, |
74 | | - ...extraCompilerOptions, |
75 | | - }; |
76 | | - |
77 | | - // Create compiler host. |
78 | | - const compilerHost = new WebpackCompilerHost( |
79 | | - compilerOptions, |
80 | | - basePath, |
81 | | - new virtualFs.SimpleMemoryHost(), |
82 | | - false, |
83 | | - ); |
84 | | - |
85 | | - // Add a dummy file to host content. |
86 | | - compilerHost.writeFile(fileName, content, false); |
87 | | - |
88 | | - if (useLibs) { |
89 | | - // Write the default libs. |
90 | | - // These are needed for tests that use import(), because it relies on a Promise being there. |
91 | | - const compilerLibFolder = dirname(compilerHost.getDefaultLibFileName(compilerOptions)); |
92 | | - for (const [k, v] of Object.entries(typeScriptLibFiles)) { |
93 | | - compilerHost.writeFile(join(compilerLibFolder, k), v, false); |
94 | | - } |
95 | | - } |
96 | | - |
97 | | - if (compilerOptions.importHelpers) { |
98 | | - for (const [k, v] of Object.entries(tsLibFiles)) { |
99 | | - compilerHost.writeFile(k, v, false); |
100 | | - } |
101 | | - } |
102 | | - |
103 | | - if (additionalFiles) { |
104 | | - for (const key in additionalFiles) { |
105 | | - compilerHost.writeFile(basePath + key, additionalFiles[key], false); |
106 | | - } |
107 | | - } |
108 | | - |
109 | | - // Create the TypeScript program. |
110 | | - const program = ts.createProgram([fileName], compilerOptions, compilerHost); |
111 | | - |
112 | | - return { compilerHost, program }; |
113 | | -} |
114 | | - |
115 | | -export function transformTypescript( |
116 | | - content: string | undefined, |
117 | | - transformers: ts.TransformerFactory<ts.SourceFile>[], |
118 | | - program?: ts.Program, |
119 | | - compilerHost?: WebpackCompilerHost, |
120 | | -): string | undefined { |
121 | | - // Use given context or create a new one. |
122 | | - if (content !== undefined) { |
123 | | - const typescriptContext = createTypescriptContext(content); |
124 | | - if (!program) { |
125 | | - program = typescriptContext.program; |
126 | | - } |
127 | | - if (!compilerHost) { |
128 | | - compilerHost = typescriptContext.compilerHost; |
129 | | - } |
130 | | - } else if (!program || !compilerHost) { |
131 | | - throw new Error('transformTypescript needs either `content` or a `program` and `compilerHost'); |
132 | | - } |
133 | | - |
134 | | - // Emit. |
135 | | - const { emitSkipped, diagnostics } = program.emit( |
136 | | - undefined, undefined, undefined, undefined, { before: transformers }, |
137 | | - ); |
138 | | - |
139 | | - // Throw error with diagnostics if emit wasn't successfull. |
140 | | - if (emitSkipped) { |
141 | | - throw new Error(ts.formatDiagnostics(diagnostics, compilerHost)); |
142 | | - } |
143 | | - |
144 | | - // Return the transpiled js. |
145 | | - return compilerHost.readFile(fileName.replace(/\.tsx?$/, '.js')); |
146 | | -} |
147 | | - |
148 | | -function loadTypeScriptLibFiles(): Record<string, string> { |
149 | | - const libFolderPath = dirname(require.resolve('typescript/lib/lib.d.ts')); |
150 | | - const libFolderFiles = readdirSync(libFolderPath); |
151 | | - const libFileNames = libFolderFiles.filter(f => f.startsWith('lib.') && f.endsWith('.d.ts')); |
152 | | - |
153 | | - // Return a map of the lib names to their content. |
154 | | - const libs: Record<string, string> = {}; |
155 | | - for (const f of libFileNames) { |
156 | | - libs[f] = readFileSync(join(libFolderPath, f), 'utf-8'); |
157 | | - } |
158 | | - |
159 | | - return libs; |
160 | | -} |
161 | | - |
162 | | -function loadTsLibFiles(): Record<string, string> { |
163 | | - const libFolderPath = dirname(require.resolve('tslib/package.json')); |
164 | | - const libFolderFiles = readdirSync(libFolderPath); |
165 | | - |
166 | | - // Return a map of the lib names to their content. |
167 | | - const libs: Record<string, string> = {}; |
168 | | - for (const f of libFolderFiles) { |
169 | | - libs[join('node_modules/tslib', f)] = readFileSync(join(libFolderPath, f), 'utf-8'); |
170 | | - } |
171 | | - |
172 | | - return libs; |
173 | | -} |
0 commit comments