diff --git a/packages/remix-dev/compiler/utils/tsconfig/configLoader.ts b/packages/remix-dev/compiler/utils/tsconfig/configLoader.ts deleted file mode 100644 index ec3b5cce008..00000000000 --- a/packages/remix-dev/compiler/utils/tsconfig/configLoader.ts +++ /dev/null @@ -1,63 +0,0 @@ -import * as path from "path"; - -import { tsConfigLoader } from "./tsConfigLoader"; - -export interface ConfigLoaderParams { - cwd: string; -} - -export interface ConfigLoaderSuccessResult { - resultType: "success"; - configFileAbsolutePath: string; - baseUrl: string; - absoluteBaseUrl: string; - paths: { [key: string]: Array }; - mainFields?: Array; - addMatchAll?: boolean; -} - -export interface ConfigLoaderFailResult { - resultType: "failed"; - message: string; -} - -export type ConfigLoaderResult = - | ConfigLoaderSuccessResult - | ConfigLoaderFailResult; - -export function loadTsConfig(cwd: string = process.cwd()): ConfigLoaderResult { - return configLoader({ cwd: cwd }); -} - -export function configLoader({ cwd }: ConfigLoaderParams): ConfigLoaderResult { - // Load tsconfig and create path matching function - let loadResult = tsConfigLoader({ - cwd, - getEnv: (key: string) => process.env[key], - }); - - if (!loadResult.tsConfigPath) { - return { - resultType: "failed", - message: "Couldn't find tsconfig.json", - }; - } - - if (!loadResult.baseUrl) { - return { - resultType: "failed", - message: "Missing baseUrl in compilerOptions", - }; - } - - let tsConfigDir = path.dirname(loadResult.tsConfigPath); - let absoluteBaseUrl = path.join(tsConfigDir, loadResult.baseUrl); - - return { - resultType: "success", - configFileAbsolutePath: loadResult.tsConfigPath, - baseUrl: loadResult.baseUrl, - absoluteBaseUrl, - paths: loadResult.paths || {}, - }; -} diff --git a/packages/remix-dev/compiler/utils/tsconfig/index.ts b/packages/remix-dev/compiler/utils/tsconfig/index.ts index c7d4ffd6246..8836e7a472f 100644 --- a/packages/remix-dev/compiler/utils/tsconfig/index.ts +++ b/packages/remix-dev/compiler/utils/tsconfig/index.ts @@ -1,11 +1,9 @@ import tsConfigPaths from "tsconfig-paths"; -import { loadTsConfig } from "./configLoader"; import { writeConfigDefaults } from "./write-config-defaults"; -export { loadTsConfig } from "./configLoader"; export function createMatchPath() { - let configLoaderResult = loadTsConfig(); + let configLoaderResult = tsConfigPaths.loadConfig(); if (configLoaderResult.resultType === "failed") { if (configLoaderResult.message === "Missing baseUrl in compilerOptions") { throw new Error( diff --git a/packages/remix-dev/compiler/utils/tsconfig/tsConfigLoader.ts b/packages/remix-dev/compiler/utils/tsconfig/tsConfigLoader.ts deleted file mode 100644 index a5ae6f546d8..00000000000 --- a/packages/remix-dev/compiler/utils/tsconfig/tsConfigLoader.ts +++ /dev/null @@ -1,153 +0,0 @@ -import * as path from "path"; -import * as fs from "fs"; -import JSON5 from "json5"; -import stripBom from "strip-bom"; - -/** - * Typing for the parts of tsconfig that we care about - */ -export interface TsConfig { - extends?: string; - compilerOptions?: { - baseUrl?: string; - paths?: { [key: string]: Array }; - strict?: boolean; - }; -} - -export interface TsConfigLoaderResult { - tsConfigPath: string | undefined; - baseUrl: string | undefined; - paths: { [key: string]: Array } | undefined; -} - -export interface TsConfigLoaderParams { - getEnv: (key: string) => string | undefined; - cwd: string; - loadSync?( - cwd: string, - filename?: string, - baseUrl?: string - ): TsConfigLoaderResult; -} - -export function tsConfigLoader({ - cwd, -}: TsConfigLoaderParams): TsConfigLoaderResult { - let loadResult = loadSync(cwd); - return loadResult; -} - -function loadSync(cwd: string): TsConfigLoaderResult { - // Tsconfig.loadSync uses path.resolve. This is why we can use an absolute path as filename - let configPath = resolveConfigPath(cwd); - - if (!configPath) { - return { - tsConfigPath: undefined, - baseUrl: undefined, - paths: undefined, - }; - } - let config = parseTsConfig(configPath); - - return { - tsConfigPath: configPath, - baseUrl: config && config.compilerOptions && config.compilerOptions.baseUrl, - paths: config && config.compilerOptions && config.compilerOptions.paths, - }; -} - -function resolveConfigPath(cwd: string): string | undefined { - if (fs.statSync(cwd).isFile()) { - return path.resolve(cwd); - } - - let configAbsolutePath = walkForTsConfig(cwd); - return configAbsolutePath ? path.resolve(configAbsolutePath) : undefined; -} - -function walkForTsConfig( - directory: string, - existsSync: (path: string) => boolean = fs.existsSync -): string | undefined { - let configPath = path.join(directory, "./tsconfig.json"); - if (existsSync(configPath)) { - return configPath; - } - - configPath = path.join(directory, "./jsconfig.json"); - if (existsSync(configPath)) { - return configPath; - } - - let parentDirectory = path.join(directory, "../"); - - // If we reached the top - if (directory === parentDirectory) { - return undefined; - } - - return walkForTsConfig(parentDirectory, existsSync); -} - -function parseTsConfig( - configFilePath: string, - existsSync: (path: string) => boolean = fs.existsSync, - readFileSync: (filename: string) => string = (filename: string) => - fs.readFileSync(filename, "utf8") -): TsConfig | undefined { - if (!existsSync(configFilePath)) { - return undefined; - } - - let configString = readFileSync(configFilePath); - let cleanedJson = stripBom(configString); - let config = JSON5.parse(cleanedJson); - let extendedConfig = config.extends; - - if (extendedConfig) { - if ( - typeof extendedConfig === "string" && - extendedConfig.indexOf(".json") === -1 - ) { - extendedConfig += ".json"; - } - let currentDir = path.dirname(configFilePath); - let extendedConfigPath = path.join(currentDir, extendedConfig); - if ( - extendedConfig.indexOf("/") !== -1 && - extendedConfig.indexOf(".") !== -1 && - !existsSync(extendedConfigPath) - ) { - extendedConfigPath = path.join( - currentDir, - "node_modules", - extendedConfig - ); - } - - let base = - parseTsConfig(extendedConfigPath, existsSync, readFileSync) || {}; - - // baseUrl should be interpreted as relative to the base tsconfig, - // but we need to update it so it is relative to the original tsconfig being loaded - if (base.compilerOptions && base.compilerOptions.baseUrl) { - let extendsDir = path.dirname(extendedConfig); - base.compilerOptions.baseUrl = path.join( - extendsDir, - base.compilerOptions.baseUrl - ); - } - - return { - ...base, - ...config, - compilerOptions: { - ...base.compilerOptions, - ...config.compilerOptions, - }, - }; - } - return config; -} diff --git a/packages/remix-dev/package.json b/packages/remix-dev/package.json index dfc7ab8884b..0671b11749b 100644 --- a/packages/remix-dev/package.json +++ b/packages/remix-dev/package.json @@ -50,9 +50,8 @@ "remark-mdx-frontmatter": "^1.0.1", "semver": "^7.3.5", "sort-package-json": "^1.55.0", - "strip-bom": "^4.0.0", "tar-fs": "^2.1.1", - "tsconfig-paths": "^3.14.0", + "tsconfig-paths": "^4.0.0", "ws": "^7.4.5", "xdm": "^2.0.0" }, diff --git a/yarn.lock b/yarn.lock index 699fa43911b..9b3683dbaeb 100644 --- a/yarn.lock +++ b/yarn.lock @@ -10319,7 +10319,7 @@ ts-node@8.9.1: source-map-support "^0.5.17" yn "3.1.1" -tsconfig-paths@^3.12.0, tsconfig-paths@^3.14.0, tsconfig-paths@^3.9.0: +tsconfig-paths@^3.12.0, tsconfig-paths@^3.9.0: version "3.14.1" resolved "https://registry.npmjs.org/tsconfig-paths/-/tsconfig-paths-3.14.1.tgz" integrity sha512-fxDhWnFSLt3VuTwtvJt5fpwxBHg5AdKWMsgcPOOIilyjymcYVZoCQF8fvFRezCNfblEXmi+PcM1eYHeOAgXCOQ== @@ -10329,6 +10329,15 @@ tsconfig-paths@^3.12.0, tsconfig-paths@^3.14.0, tsconfig-paths@^3.9.0: minimist "^1.2.6" strip-bom "^3.0.0" +tsconfig-paths@^4.0.0: + version "4.0.0" + resolved "https://registry.yarnpkg.com/tsconfig-paths/-/tsconfig-paths-4.0.0.tgz#1082f5d99fd127b72397eef4809e4dd06d229b64" + integrity sha512-SLBg2GBKlR6bVtMgJJlud/o3waplKtL7skmLkExomIiaAtLGtVsoXIqP3SYdjbcH9lq/KVv7pMZeCBpLYOit6Q== + dependencies: + json5 "^2.2.1" + minimist "^1.2.6" + strip-bom "^3.0.0" + tslib@^1.8.1, tslib@^1.9.0: version "1.14.1" resolved "https://registry.npmjs.org/tslib/-/tslib-1.14.1.tgz"