mirror of
https://github.com/microsoft/playwright.git
synced 2025-06-26 21:40:17 +00:00
chrome: cache tsconfig for folder (#32477)
Fixes https://github.com/microsoft/playwright/issues/32459
This commit is contained in:
parent
f0e13164d7
commit
16cef9901d
@ -52,49 +52,12 @@ export interface LoadedTsConfig {
|
|||||||
allowJs?: boolean;
|
allowJs?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function tsConfigLoader(tsconfigPathOrDirecotry: string): LoadedTsConfig[] {
|
export function loadTsConfig(configPath: string): LoadedTsConfig[] {
|
||||||
const configPath = resolveConfigPath(tsconfigPathOrDirecotry);
|
|
||||||
|
|
||||||
if (!configPath)
|
|
||||||
return [];
|
|
||||||
|
|
||||||
const references: LoadedTsConfig[] = [];
|
const references: LoadedTsConfig[] = [];
|
||||||
const config = loadTsConfig(configPath, references);
|
const config = innerLoadTsConfig(configPath, references);
|
||||||
return [config, ...references];
|
return [config, ...references];
|
||||||
}
|
}
|
||||||
|
|
||||||
function resolveConfigPath(tsconfigPathOrDirecotry: string): string | undefined {
|
|
||||||
if (fs.statSync(tsconfigPathOrDirecotry).isFile()) {
|
|
||||||
return path.resolve(tsconfigPathOrDirecotry);
|
|
||||||
}
|
|
||||||
|
|
||||||
const configAbsolutePath = walkForTsConfig(tsconfigPathOrDirecotry);
|
|
||||||
return configAbsolutePath ? path.resolve(configAbsolutePath) : undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function walkForTsConfig(
|
|
||||||
directory: string,
|
|
||||||
existsSync: (path: string) => boolean = fs.existsSync
|
|
||||||
): string | undefined {
|
|
||||||
const tsconfigPath = path.join(directory, "./tsconfig.json");
|
|
||||||
if (existsSync(tsconfigPath)) {
|
|
||||||
return tsconfigPath;
|
|
||||||
}
|
|
||||||
const jsconfigPath = path.join(directory, "./jsconfig.json");
|
|
||||||
if (existsSync(jsconfigPath)) {
|
|
||||||
return jsconfigPath;
|
|
||||||
}
|
|
||||||
|
|
||||||
const parentDirectory = path.join(directory, "../");
|
|
||||||
|
|
||||||
// If we reached the top
|
|
||||||
if (directory === parentDirectory) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
return walkForTsConfig(parentDirectory, existsSync);
|
|
||||||
}
|
|
||||||
|
|
||||||
function resolveConfigFile(baseConfigFile: string, referencedConfigFile: string) {
|
function resolveConfigFile(baseConfigFile: string, referencedConfigFile: string) {
|
||||||
if (!referencedConfigFile.endsWith('.json'))
|
if (!referencedConfigFile.endsWith('.json'))
|
||||||
referencedConfigFile += '.json';
|
referencedConfigFile += '.json';
|
||||||
@ -106,7 +69,7 @@ function resolveConfigFile(baseConfigFile: string, referencedConfigFile: string)
|
|||||||
return resolvedConfigFile;
|
return resolvedConfigFile;
|
||||||
}
|
}
|
||||||
|
|
||||||
function loadTsConfig(
|
function innerLoadTsConfig(
|
||||||
configFilePath: string,
|
configFilePath: string,
|
||||||
references: LoadedTsConfig[],
|
references: LoadedTsConfig[],
|
||||||
visited = new Map<string, LoadedTsConfig>(),
|
visited = new Map<string, LoadedTsConfig>(),
|
||||||
@ -130,7 +93,7 @@ function loadTsConfig(
|
|||||||
const extendsArray = Array.isArray(parsedConfig.extends) ? parsedConfig.extends : (parsedConfig.extends ? [parsedConfig.extends] : []);
|
const extendsArray = Array.isArray(parsedConfig.extends) ? parsedConfig.extends : (parsedConfig.extends ? [parsedConfig.extends] : []);
|
||||||
for (const extendedConfig of extendsArray) {
|
for (const extendedConfig of extendsArray) {
|
||||||
const extendedConfigPath = resolveConfigFile(configFilePath, extendedConfig);
|
const extendedConfigPath = resolveConfigFile(configFilePath, extendedConfig);
|
||||||
const base = loadTsConfig(extendedConfigPath, references, visited);
|
const base = innerLoadTsConfig(extendedConfigPath, references, visited);
|
||||||
// Retain result instance, so that caching works.
|
// Retain result instance, so that caching works.
|
||||||
Object.assign(result, base, { tsConfigPath: configFilePath });
|
Object.assign(result, base, { tsConfigPath: configFilePath });
|
||||||
}
|
}
|
||||||
@ -154,7 +117,7 @@ function loadTsConfig(
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (const ref of parsedConfig.references || [])
|
for (const ref of parsedConfig.references || [])
|
||||||
references.push(loadTsConfig(resolveConfigFile(configFilePath, ref.path), references, visited));
|
references.push(innerLoadTsConfig(resolveConfigFile(configFilePath, ref.path), references, visited));
|
||||||
|
|
||||||
if (path.basename(configFilePath) === 'jsconfig.json' && result.allowJs === undefined)
|
if (path.basename(configFilePath) === 'jsconfig.json' && result.allowJs === undefined)
|
||||||
result.allowJs = true;
|
result.allowJs = true;
|
||||||
|
@ -15,12 +15,13 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import crypto from 'crypto';
|
import crypto from 'crypto';
|
||||||
|
import fs from 'fs';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import url from 'url';
|
import url from 'url';
|
||||||
import { sourceMapSupport, pirates } from '../utilsBundle';
|
import { sourceMapSupport, pirates } from '../utilsBundle';
|
||||||
import type { Location } from '../../types/testReporter';
|
import type { Location } from '../../types/testReporter';
|
||||||
import type { LoadedTsConfig } from '../third_party/tsconfig-loader';
|
import type { LoadedTsConfig } from '../third_party/tsconfig-loader';
|
||||||
import { tsConfigLoader } from '../third_party/tsconfig-loader';
|
import { loadTsConfig } from '../third_party/tsconfig-loader';
|
||||||
import Module from 'module';
|
import Module from 'module';
|
||||||
import type { BabelPlugin, BabelTransformFunction } from './babelBundle';
|
import type { BabelPlugin, BabelTransformFunction } from './babelBundle';
|
||||||
import { createFileMatcher, fileIsModule, resolveImportSpecifierExtension } from '../util';
|
import { createFileMatcher, fileIsModule, resolveImportSpecifierExtension } from '../util';
|
||||||
@ -57,14 +58,15 @@ export function transformConfig(): TransformConfig {
|
|||||||
return _transformConfig;
|
return _transformConfig;
|
||||||
}
|
}
|
||||||
|
|
||||||
let _singleTSConfig: string | undefined;
|
let _singleTSConfigPath: string | undefined;
|
||||||
|
let _singleTSConfig: ParsedTsConfigData[] | undefined;
|
||||||
|
|
||||||
export function setSingleTSConfig(value: string | undefined) {
|
export function setSingleTSConfig(value: string | undefined) {
|
||||||
_singleTSConfig = value;
|
_singleTSConfigPath = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function singleTSConfig(): string | undefined {
|
export function singleTSConfig(): string | undefined {
|
||||||
return _singleTSConfig;
|
return _singleTSConfigPath;
|
||||||
}
|
}
|
||||||
|
|
||||||
function validateTsConfig(tsconfig: LoadedTsConfig): ParsedTsConfigData {
|
function validateTsConfig(tsconfig: LoadedTsConfig): ParsedTsConfigData {
|
||||||
@ -81,12 +83,47 @@ function validateTsConfig(tsconfig: LoadedTsConfig): ParsedTsConfigData {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function loadAndValidateTsconfigsForFile(file: string): ParsedTsConfigData[] {
|
function loadAndValidateTsconfigsForFile(file: string): ParsedTsConfigData[] {
|
||||||
const tsconfigPathOrDirecotry = _singleTSConfig || path.dirname(file);
|
if (_singleTSConfigPath && !_singleTSConfig)
|
||||||
if (!cachedTSConfigs.has(tsconfigPathOrDirecotry)) {
|
_singleTSConfig = loadTsConfig(_singleTSConfigPath).map(validateTsConfig);
|
||||||
const loaded = tsConfigLoader(tsconfigPathOrDirecotry);
|
if (_singleTSConfig)
|
||||||
cachedTSConfigs.set(tsconfigPathOrDirecotry, loaded.map(validateTsConfig));
|
return _singleTSConfig;
|
||||||
|
return loadAndValidateTsconfigsForFolder(path.dirname(file));
|
||||||
}
|
}
|
||||||
return cachedTSConfigs.get(tsconfigPathOrDirecotry)!;
|
|
||||||
|
function loadAndValidateTsconfigsForFolder(folder: string): ParsedTsConfigData[] {
|
||||||
|
const foldersWithConfig: string[] = [];
|
||||||
|
let currentFolder = path.resolve(folder);
|
||||||
|
let result: ParsedTsConfigData[] | undefined;
|
||||||
|
while (true) {
|
||||||
|
const cached = cachedTSConfigs.get(currentFolder);
|
||||||
|
if (cached) {
|
||||||
|
result = cached;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
foldersWithConfig.push(currentFolder);
|
||||||
|
|
||||||
|
for (const name of ['tsconfig.json', 'jsconfig.json']) {
|
||||||
|
const configPath = path.join(currentFolder, name);
|
||||||
|
if (fs.existsSync(configPath)) {
|
||||||
|
const loaded = loadTsConfig(configPath);
|
||||||
|
result = loaded.map(validateTsConfig);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (result)
|
||||||
|
break;
|
||||||
|
|
||||||
|
const parentFolder = path.resolve(currentFolder, '../');
|
||||||
|
if (currentFolder === parentFolder)
|
||||||
|
break;
|
||||||
|
currentFolder = parentFolder;
|
||||||
|
}
|
||||||
|
|
||||||
|
result = result || [];
|
||||||
|
for (const folder of foldersWithConfig)
|
||||||
|
cachedTSConfigs.set(folder, result);
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
const pathSeparator = process.platform === 'win32' ? ';' : ':';
|
const pathSeparator = process.platform === 'win32' ? ';' : ':';
|
||||||
|
Loading…
x
Reference in New Issue
Block a user