|
1 | 1 | import { Logger } from 'bs-logger'
|
2 | 2 | import { writeFileSync } from 'fs'
|
3 | 3 | import micromatch = require('micromatch')
|
4 |
| -import { dirname, join, normalize, relative, resolve } from 'path' |
| 4 | +import { join, normalize } from 'path' |
5 | 5 | import * as _ts from 'typescript'
|
6 | 6 |
|
7 |
| -import { ConfigSet } from '../config/config-set' |
8 |
| -import { EXTENSION_REGEX, JSON_REGEX, TS_TSX_REGEX } from '../constants' |
9 |
| -import { MemoryCache, SourceOutput, TSFiles } from '../types' |
| 7 | +import { MemoryCache } from '../types' |
10 | 8 | import { sha1 } from '../util/sha1'
|
11 | 9 |
|
12 | 10 | /**
|
@@ -60,151 +58,3 @@ export function isTestFile(testMatchPatterns: (string | RegExp)[], fileName: str
|
60 | 58 | typeof pattern === 'string' ? micromatch.isMatch(fileName, pattern) : pattern.test(fileName),
|
61 | 59 | )
|
62 | 60 | }
|
63 |
| - |
64 |
| -/* istanbul ignore next (we leave this for e2e) */ |
65 |
| -function isUsingProjectReferences( |
66 |
| - program: _ts.Program, |
67 |
| - projectReferences: readonly _ts.ProjectReference[] | undefined, |
68 |
| -) { |
69 |
| - if (projectReferences && !!program.getProjectReferences) { |
70 |
| - return Boolean(program && program.getProjectReferences()) |
71 |
| - } |
72 |
| - |
73 |
| - return false |
74 |
| -} |
75 |
| - |
76 |
| -/* istanbul ignore next (we leave this for e2e) */ |
77 |
| -function getResolvedProjectReferences( |
78 |
| - program: _ts.Program, |
79 |
| -): readonly (_ts.ResolvedProjectReference | undefined)[] | undefined { |
80 |
| - const getProjectReferences = program.getResolvedProjectReferences ?? program.getProjectReferences |
81 |
| - if (getProjectReferences) { |
82 |
| - return getProjectReferences() |
83 |
| - } |
84 |
| - |
85 |
| - return |
86 |
| -} |
87 |
| - |
88 |
| -/* istanbul ignore next (we leave this for e2e) */ |
89 |
| -function getProjectReferenceForFile( |
90 |
| - filePath: string, |
91 |
| - program: _ts.Program, |
92 |
| - projectReferences: readonly _ts.ProjectReference[] | undefined, |
93 |
| -) { |
94 |
| - if (isUsingProjectReferences(program, projectReferences)) { |
95 |
| - return ( |
96 |
| - program && |
97 |
| - getResolvedProjectReferences(program)!.find( |
98 |
| - ref => (ref && ref.commandLine.fileNames.some(file => normalize(file) === filePath)) || false, |
99 |
| - ) |
100 |
| - ) |
101 |
| - } |
102 |
| - |
103 |
| - return |
104 |
| -} |
105 |
| - |
106 |
| -/** |
107 |
| - * @internal |
108 |
| - */ |
109 |
| -/* istanbul ignore next (we leave this for e2e) */ |
110 |
| -export function getAndCacheProjectReference( |
111 |
| - filePath: string, |
112 |
| - program: _ts.Program, |
113 |
| - files: TSFiles, |
114 |
| - projectReferences: readonly _ts.ProjectReference[] | undefined, |
115 |
| -) { |
116 |
| - const file = files.get(filePath) |
117 |
| - if (file?.projectReference) { |
118 |
| - return file.projectReference.project |
119 |
| - } |
120 |
| - |
121 |
| - const projectReference = getProjectReferenceForFile(filePath, program, projectReferences) |
122 |
| - if (file !== undefined) { |
123 |
| - file.projectReference = { project: projectReference } |
124 |
| - } |
125 |
| - |
126 |
| - return projectReference |
127 |
| -} |
128 |
| - |
129 |
| -// Adapted from https://github.com/Microsoft/TypeScript/blob/45101491c0b077c509b25830ef0ee5f85b293754/src/compiler/tsbuild.ts#L305 |
130 |
| -/* istanbul ignore next (we leave this for e2e) */ |
131 |
| -function getOutputJavaScriptFileName(inputFileName: string, projectReference: _ts.ResolvedProjectReference) { |
132 |
| - const { options } = projectReference.commandLine |
133 |
| - const projectDirectory = options.rootDir || dirname(projectReference.sourceFile.fileName) |
134 |
| - const relativePath = relative(projectDirectory, inputFileName) |
135 |
| - const outputPath = resolve(options.outDir || projectDirectory, relativePath) |
136 |
| - const newExtension = JSON_REGEX.test(inputFileName) |
137 |
| - ? '.json' |
138 |
| - : TS_TSX_REGEX.test(inputFileName) && options.jsx === _ts.JsxEmit.Preserve |
139 |
| - ? '.jsx' |
140 |
| - : '.js' |
141 |
| - |
142 |
| - return outputPath.replace(EXTENSION_REGEX, newExtension) |
143 |
| -} |
144 |
| - |
145 |
| -/** |
146 |
| - * Gets the output JS file path for an input file governed by a composite project. |
147 |
| - * Pulls from the cache if it exists; computes and caches the result otherwise. |
148 |
| - */ |
149 |
| -/* istanbul ignore next (we leave this for e2e) */ |
150 |
| -function getAndCacheOutputJSFileName( |
151 |
| - inputFileName: string, |
152 |
| - projectReference: _ts.ResolvedProjectReference, |
153 |
| - files: TSFiles, |
154 |
| -) { |
155 |
| - const file = files.get(inputFileName) |
156 |
| - if (file?.projectReference?.outputFileName) { |
157 |
| - return file.projectReference.outputFileName |
158 |
| - } |
159 |
| - |
160 |
| - const outputFileName = getOutputJavaScriptFileName(inputFileName, projectReference) |
161 |
| - if (file !== undefined) { |
162 |
| - file.projectReference = file.projectReference ?? { |
163 |
| - project: projectReference, |
164 |
| - } |
165 |
| - file.projectReference.outputFileName = outputFileName |
166 |
| - } |
167 |
| - |
168 |
| - return outputFileName |
169 |
| -} |
170 |
| - |
171 |
| -/** |
172 |
| - * @internal |
173 |
| - */ |
174 |
| -/* istanbul ignore next (we leave this for e2e) */ |
175 |
| -export function getCompileResultFromReferencedProject( |
176 |
| - fileName: string, |
177 |
| - configs: ConfigSet, |
178 |
| - files: TSFiles, |
179 |
| - referencedProject: _ts.ResolvedProjectReference, |
180 |
| -): SourceOutput { |
181 |
| - const [relativeProjectConfigPath, relativeFilePath] = [ |
182 |
| - configs.resolvePath(referencedProject.sourceFile.fileName), |
183 |
| - configs.resolvePath(fileName), |
184 |
| - ] |
185 |
| - if (referencedProject.commandLine.options.outFile !== undefined) { |
186 |
| - throw new Error( |
187 |
| - `The referenced project at ${relativeProjectConfigPath} is using ` + |
188 |
| - "the outFile' option, which is not supported with ts-jest.", |
189 |
| - ) |
190 |
| - } |
191 |
| - |
192 |
| - const jsFileName = getAndCacheOutputJSFileName(fileName, referencedProject, files) |
193 |
| - const relativeJSFileName = configs.resolvePath(jsFileName) |
194 |
| - if (!configs.compilerModule.sys.fileExists(jsFileName)) { |
195 |
| - throw new Error( |
196 |
| - 'Could not find output JavaScript file for input ' + |
197 |
| - `${relativeFilePath} (looked at ${relativeJSFileName}).\n` + |
198 |
| - 'The input file is part of a project reference located at ' + |
199 |
| - `${relativeProjectConfigPath}, so ts-jest is looking for the ` + |
200 |
| - 'project’s pre-built output on disk. Try running `tsc --build` ' + |
201 |
| - 'to build project references.', |
202 |
| - ) |
203 |
| - } |
204 |
| - |
205 |
| - const mapFileName = `${jsFileName}.map` |
206 |
| - const outputText = configs.compilerModule.sys.readFile(jsFileName) |
207 |
| - const sourceMapText = configs.compilerModule.sys.readFile(mapFileName) |
208 |
| - |
209 |
| - return [outputText!, sourceMapText!] |
210 |
| -} |
0 commit comments