Created
January 1, 2025 06:40
-
-
Save SagnikPradhan/cadcf025b9e17c70f5fb107b4f6608cf to your computer and use it in GitHub Desktop.
Patch for tsc-alias to support yarn berry
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/dist/bin/index.js b/dist/bin/index.js | |
old mode 100755 | |
new mode 100644 | |
diff --git a/dist/helpers/config.d.ts b/dist/helpers/config.d.ts | |
index dca15b57f31eb935a08b4498f8b3289fc2848b3d..fc5e95887e4abfaa640fca88ea9e08ce9bcd6671 100644 | |
--- a/dist/helpers/config.d.ts | |
+++ b/dist/helpers/config.d.ts | |
@@ -1,5 +1,5 @@ | |
import { IConfig, IOutput, ITSConfig, ReplaceTscAliasPathsOptions } from '../interfaces'; | |
export declare function prepareConfig(options: ReplaceTscAliasPathsOptions): Promise<IConfig>; | |
export declare const loadConfig: (file: string, output: IOutput) => ITSConfig; | |
-export declare function normalizeTsConfigExtendsOption(ext: string | string[], file: string): string[]; | |
-export declare function resolveTsConfigExtendsPath(ext: string, file: string): string; | |
+export declare function normalizeTsConfigExtendsOption(ext: string | string[], file: string, output: IOutput): string[]; | |
+export declare function resolveTsConfigExtendsPath(ext: string, file: string, output: IOutput): string; | |
diff --git a/dist/helpers/config.js b/dist/helpers/config.js | |
index ddbe05c7161b109275bec98cb372acbae2d275f9..83fec4589665a3799f261c0fd61affaf8c7941ba 100644 | |
--- a/dist/helpers/config.js | |
+++ b/dist/helpers/config.js | |
@@ -16,6 +16,8 @@ const path_1 = require("path"); | |
const utils_1 = require("../utils"); | |
const replacers_1 = require("./replacers"); | |
const normalizePath = require("normalize-path"); | |
+const module_1 = require("module"); | |
+const path = require("path"); | |
function prepareConfig(options) { | |
var _a, _b, _c, _d, _e, _f; | |
return __awaiter(this, void 0, void 0, function* () { | |
@@ -108,49 +110,38 @@ const loadConfig = (file, output) => { | |
} | |
output.debug('loaded config (from file):', config); | |
if (ext) { | |
- return Object.assign(Object.assign({}, normalizeTsConfigExtendsOption(ext, file).reduce((pre, ext) => (Object.assign(Object.assign({}, pre), (0, exports.loadConfig)(ext, output))), {})), config); | |
+ return Object.assign(Object.assign({}, normalizeTsConfigExtendsOption(ext, file, output).reduce((pre, ext) => (Object.assign(Object.assign({}, pre), (0, exports.loadConfig)(ext, output))), {})), config); | |
} | |
return config; | |
}; | |
exports.loadConfig = loadConfig; | |
-function normalizeTsConfigExtendsOption(ext, file) { | |
+function normalizeTsConfigExtendsOption(ext, file, output) { | |
if (!ext) | |
return []; | |
const configDir = (0, path_1.dirname)(file); | |
const normExts = (Array.isArray(ext) ? ext : [ext]).map((e) => e.startsWith('.') | |
? (0, path_1.join)(configDir, e.endsWith('.json') ? e : `${e}.json`) | |
- : resolveTsConfigExtendsPath(e, file)); | |
+ : resolveTsConfigExtendsPath(e, file, output)); | |
return normExts; | |
} | |
exports.normalizeTsConfigExtendsOption = normalizeTsConfigExtendsOption; | |
-function resolveTsConfigExtendsPath(ext, file) { | |
- const tsConfigDir = (0, path_1.dirname)(file); | |
- const node_modules = mylas_1.Dir.nodeModules({ cwd: tsConfigDir }); | |
- const targetPaths = node_modules.map((v) => (0, path_1.join)(tsConfigDir, v, ext)); | |
- for (const targetPath of targetPaths) { | |
- if (ext.endsWith('.json')) { | |
- if ((0, fs_1.existsSync)(targetPath)) { | |
- return targetPath; | |
- } | |
- else { | |
- continue; | |
- } | |
- } | |
- let isDirectory = false; | |
- try { | |
- const stats = (0, fs_1.lstatSync)(targetPath); | |
- isDirectory = stats.isDirectory() || stats.isSymbolicLink(); | |
- } | |
- catch (err) { } | |
- if (isDirectory) { | |
- return (0, path_1.join)(targetPath, 'tsconfig.json'); | |
- } | |
- else { | |
- if ((0, fs_1.existsSync)(`${targetPath}.json`)) { | |
- return `${targetPath}.json`; | |
- } | |
- } | |
+function resolveTsConfigExtendsPath(ext, file, output) { | |
+ const userRequire = (0, module_1.createRequire)(file); | |
+ if (ext.endsWith('.json')) | |
+ return userRequire.resolve(ext); | |
+ try { | |
+ const extGuess = `${ext}.json`; | |
+ output.debug(`Trying to resolve ${extGuess} from ${file}`); | |
+ return userRequire.resolve(extGuess); | |
+ } | |
+ catch (_a) { } | |
+ try { | |
+ const extGuess = path.join(ext, 'tsconfig.json'); | |
+ output.debug(`Trying to resolve ${extGuess} from ${file}`); | |
+ return userRequire.resolve(extGuess); | |
} | |
+ catch (_b) { } | |
+ throw new Error(`Could not resolve ${ext} from ${file}`); | |
} | |
exports.resolveTsConfigExtendsPath = resolveTsConfigExtendsPath; | |
//# sourceMappingURL=config.js.map | |
\ No newline at end of file | |
diff --git a/dist/helpers/config.js.map b/dist/helpers/config.js.map | |
index a652fdf88565894220b788d00945ae7ef71afd86..260136ce8a06a9e338f289218a2d72dcdcc9dc9f 100644 | |
--- a/dist/helpers/config.js.map | |
+++ b/dist/helpers/config.js.map | |
@@ -1 +1 @@ | |
-{"version":3,"file":"config.js","sourceRoot":"","sources":["../../src/helpers/config.ts"],"names":[],"mappings":";;;;;;;;;;;;AAOA,2BAA2C;AAC3C,iCAAkC;AAClC,+BAAoE;AASpE,oCAAuD;AACvD,2CAA8C;AAC9C,gDAAiD;AAOjD,SAAsB,aAAa,CACjC,OAAoC;;;QAEpC,MAAM,MAAM,GAAG,MAAA,OAAO,CAAC,MAAM,mCAAI,IAAI,cAAM,CAAC,OAAO,CAAC,OAAO,EAAE,OAAO,CAAC,KAAK,CAAC,CAAC;QAE5E,MAAM,UAAU,GAAG,CAAC,OAAO,CAAC,UAAU;YACpC,CAAC,CAAC,IAAA,cAAO,EAAC,OAAO,CAAC,GAAG,EAAE,EAAE,eAAe,CAAC;YACzC,CAAC,CAAC,CAAC,IAAA,iBAAU,EAAC,OAAO,CAAC,UAAU,CAAC;gBACjC,CAAC,CAAC,IAAA,cAAO,EAAC,OAAO,CAAC,GAAG,EAAE,EAAE,OAAO,CAAC,UAAU,CAAC;gBAC5C,CAAC,CAAC,OAAO,CAAC,UAAU,CAAC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,CAAC;AAClB,CAAC;AAZD,wEAYC;AAQD,SAAgB,0BAA0B,CAAC,GAAW,EAAE,IAAY;IAClE,MAAM,WAAW,GAAG,IAAA,cAAO,EAAC,IAAI,CAAC,CAAC;IAClC,MAAM,YAAY,GAAa,WAAG,CAAC,WAAW,CAAC,EAAE,GAAG,EAAE,WAAW,EAAE,CAAC,CAAC;IACrE,MAAM,WAAW,GAAG,YAAY,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,IAAA,WAAI,EAAC,WAAW,EAAE,CAAC,EAAE,GAAG,CAAC,CAAC,CAAC;IAGvE,KAAK,MAAM,UAAU,IAAI,WAAW,EAAE;QACpC,IAAI,GAAG,CAAC,QAAQ,CAAC,OAAO,CAAC,EAAE;YAEzB,IAAI,IAAA,eAAU,EAAC,UAAU,CAAC,EAAE;gBAC1B,OAAO,UAAU,CAAC;aACnB;iBAAM;gBACL,SAAS;aACV;SACF;QACD,IAAI,WAAW,GAAG,KAAK,CAAC;QACxB,IAAI;YACF,MAAM,KAAK,GAAG,IAAA,cAAS,EAAC,UAAU,CAAC,CAAC;YACpC,WAAW,GAAG,KAAK,CAAC,WAAW,EAAE,IAAI,KAAK,CAAC,cAAc,EAAE,CAAC;SAC7D;QAAC,OAAO,GAAG,EAAE,GAAE;QAChB,IAAI,WAAW,EAAE;YACf,OAAO,IAAA,WAAI,EAAC,UAAU,EAAE,eAAe,CAAC,CAAC;SAC1C;aAAM;YAEL,IAAI,IAAA,eAAU,EAAC,GAAG,UAAU,OAAO,CAAC,EAAE;gBACpC,OAAO,GAAG,UAAU,OAAO,CAAC;aAC7B;SACF;KACF;AACH,CAAC;AA7BD,gEA6BC","sourcesContent":["/**\n * @file\n *\n * This file has all helperfunctions related to configuration.\n */\n\n/** */\nimport { existsSync, lstatSync } from 'fs';\nimport { Dir, Json } from 'mylas';\nimport { basename, dirname, isAbsolute, join, resolve } from 'path';\nimport {\n IConfig,\n IOutput,\n IProjectConfig,\n IRawTSConfig,\n ITSConfig,\n ReplaceTscAliasPathsOptions\n} from '../interfaces';\nimport { Output, PathCache, TrieNode } from '../utils';\nimport { importReplacers } from './replacers';\nimport normalizePath = require('normalize-path');\n\n/**\n * prepareConfig prepares a IConfig object for tsc-alias to be used.\n * @param {ReplaceTscAliasPathsOptions} options options that are used to prepare a config object.\n * @returns {Promise<IConfig>} a promise of a IConfig object.\n */\nexport async function prepareConfig(\n options: ReplaceTscAliasPathsOptions\n): Promise<IConfig> {\n const output = options.output ?? new Output(options.verbose, options.debug);\n\n const configFile = !options.configFile\n ? resolve(process.cwd(), 'tsconfig.json')\n : !isAbsolute(options.configFile)\n ? resolve(process.cwd(), options.configFile)\n : options.configFile;\n\n output.assert(existsSync(configFile), `Invalid file path => ${configFile}`);\n\n const {\n baseUrl = '',\n outDir,\n declarationDir,\n paths,\n replacers,\n resolveFullPaths,\n verbose,\n fileExtensions\n } = loadConfig(configFile, output);\n\n if (options?.fileExtensions?.inputGlob) {\n fileExtensions.inputGlob = options.fileExtensions.inputGlob;\n }\n if (options?.fileExtensions?.outputCheck) {\n fileExtensions.outputCheck = options.fileExtensions.outputCheck;\n }\n\n output.verbose = verbose;\n\n if (options.resolveFullPaths || resolveFullPaths) {\n output.debug('resolveFullPaths is active');\n options.resolveFullPaths = true;\n }\n\n const _outDir = options.outDir ?? outDir;\n if (declarationDir && _outDir !== declarationDir) {\n options.declarationDir ??= declarationDir;\n }\n\n output.assert(_outDir, 'compilerOptions.outDir is not set');\n\n const configDir: string = normalizePath(dirname(configFile));\n\n // config with project details and paths\n const projectConfig: IProjectConfig = {\n configFile: configFile,\n baseUrl: baseUrl,\n outDir: _outDir,\n configDir: configDir,\n outPath: _outDir,\n confDirParentFolderName: basename(configDir),\n hasExtraModule: false,\n configDirInOutPath: null,\n relConfDirPathInOutPath: null,\n pathCache: new PathCache(!options.watch, fileExtensions?.outputCheck),\n inputGlob:\n fileExtensions?.inputGlob || '{mjs,cjs,js,jsx,d.{mts,cts,ts,tsx}}'\n };\n output.debug('loaded project config:', projectConfig);\n\n const config: IConfig = {\n ...projectConfig,\n output: output,\n aliasTrie:\n options.aliasTrie ?? TrieNode.buildAliasTrie(projectConfig, paths),\n replacers: []\n };\n output.debug('loaded full config:', config);\n\n // Import replacers.\n await importReplacers(config, replacers, options.replacers);\n return config;\n}\n\n/**\n * loadConfig loads a config file from fs.\n * @param {string} file file path to the config file that will be loaded.\n * @param {IOutput} output the output instance to log error to.\n * @returns {ITSConfig} a ITSConfig object\n */\nexport const loadConfig = (file: string, output: IOutput): ITSConfig => {\n if (!existsSync(file)) {\n output.error(`File ${file} not found`, true);\n }\n output.debug('Loading config file:', file);\n const {\n extends: ext,\n compilerOptions: { baseUrl, outDir, declarationDir, paths } = {\n baseUrl: undefined,\n outDir: undefined,\n declarationDir: undefined,\n paths: undefined\n },\n 'tsc-alias': TSCAliasConfig\n } = Json.loadS<IRawTSConfig>(file, true);\n\n const configDir = dirname(file);\n output.debug('configDir', configDir);\n const config: ITSConfig = {};\n\n if (baseUrl) config.baseUrl = baseUrl;\n if (outDir) {\n config.outDir = isAbsolute(outDir) ? outDir : join(configDir, outDir);\n }\n if (paths) config.paths = paths;\n if (declarationDir) {\n config.declarationDir = isAbsolute(declarationDir)\n ? declarationDir\n : join(configDir, declarationDir);\n }\n if (TSCAliasConfig?.replacers) {\n config.replacers = TSCAliasConfig.replacers;\n }\n if (TSCAliasConfig?.resolveFullPaths) {\n config.resolveFullPaths = TSCAliasConfig.resolveFullPaths;\n }\n if (TSCAliasConfig?.verbose) {\n config.verbose = TSCAliasConfig.verbose;\n }\n config.fileExtensions = TSCAliasConfig?.fileExtensions ?? {};\n\n const replacerFile = config.replacers?.pathReplacer?.file;\n\n if (replacerFile) {\n config.replacers.pathReplacer.file = join(configDir, replacerFile);\n }\n\n output.debug('loaded config (from file):', config);\n if (ext) {\n return {\n ...normalizeTsConfigExtendsOption(ext, file).reduce<ITSConfig>(\n (pre, ext) => ({\n ...pre,\n ...loadConfig(ext, output)\n }),\n {}\n ),\n ...config\n };\n }\n\n return config;\n};\n\n/**\n * normalizeTsConfigExtendsOption normalizes tsconfig extends option to a directly loadable path array\n * @param { string|string[] } ext\n * @param { string } file\n * @returns {string[]}\n */\nexport function normalizeTsConfigExtendsOption(\n ext: string | string[],\n file: string\n): string[] {\n if (!ext) return [];\n const configDir = dirname(file);\n const normExts = (Array.isArray(ext) ? ext : [ext]).map((e) =>\n e.startsWith('.')\n ? join(configDir, e.endsWith('.json') ? e : `${e}.json`)\n : resolveTsConfigExtendsPath(e, file)\n );\n return normExts;\n}\n\n/**\n * resolveTsConfigExtendsPath resolves the path to the config file that is being inherited.\n * @param {string} ext the value of the extends field in the loaded config file.\n * @param {string} file file path to the config file that was loaded.\n * @returns {string} a file path to the config file that is being inherited.\n */\nexport function resolveTsConfigExtendsPath(ext: string, file: string): string {\n const tsConfigDir = dirname(file);\n const node_modules: string[] = Dir.nodeModules({ cwd: tsConfigDir }); // Getting all node_modules directories.\n const targetPaths = node_modules.map((v) => join(tsConfigDir, v, ext)); // Mapping node_modules to target paths.\n\n // Recursively checking ancestor directories for tsconfig.\n for (const targetPath of targetPaths) {\n if (ext.endsWith('.json')) {\n // Check if the file exists.\n if (existsSync(targetPath)) {\n return targetPath;\n } else {\n continue; // Continue checking when ext is a file but not yet found.\n }\n }\n let isDirectory = false;\n try {\n const stats = lstatSync(targetPath);\n isDirectory = stats.isDirectory() || stats.isSymbolicLink();\n } catch (err) {}\n if (isDirectory) {\n return join(targetPath, 'tsconfig.json');\n } else {\n // When target is not a file nor directory check with '.json' extension.\n if (existsSync(`${targetPath}.json`)) {\n return `${targetPath}.json`;\n }\n }\n }\n}\n"]} | |
\ No newline at end of file | |
+{"version":3,"file":"config.js","sourceRoot":"","sources":["../../src/helpers/config.ts"],"names":[],"mappings":";;;;;;;;;;;;AAOA,2BAA2C;AAC3C,iCAAkC;AAClC,+BAAoE;AASpE,oCAAuD;AACvD,2CAA8C;AAC9C,gDAAiD;AACjD,mCAAuC;AACv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;KACtC;IAAC,WAAM,GAAE;IAEV,MAAM,IAAI,KAAK,CAAC,qBAAqB,GAAG,SAAS,IAAI,EAAE,CAAC,CAAC;AAC3D,CAAC;AAtBD,gEAsBC","sourcesContent":["/**\n * @file\n *\n * This file has all helperfunctions related to configuration.\n */\n\n/** */\nimport { existsSync, lstatSync } from 'fs';\nimport { Dir, Json } from 'mylas';\nimport { basename, dirname, isAbsolute, join, resolve } from 'path';\nimport {\n IConfig,\n IOutput,\n IProjectConfig,\n IRawTSConfig,\n ITSConfig,\n ReplaceTscAliasPathsOptions\n} from '../interfaces';\nimport { Output, PathCache, TrieNode } from '../utils';\nimport { importReplacers } from './replacers';\nimport normalizePath = require('normalize-path');\nimport { createRequire } from 'module';\nimport path = require('path');\n\n/**\n * prepareConfig prepares a IConfig object for tsc-alias to be used.\n * @param {ReplaceTscAliasPathsOptions} options options that are used to prepare a config object.\n * @returns {Promise<IConfig>} a promise of a IConfig object.\n */\nexport async function prepareConfig(\n options: ReplaceTscAliasPathsOptions\n): Promise<IConfig> {\n const output = options.output ?? new Output(options.verbose, options.debug);\n\n const configFile = !options.configFile\n ? resolve(process.cwd(), 'tsconfig.json')\n : !isAbsolute(options.configFile)\n ? resolve(process.cwd(), options.configFile)\n : options.configFile;\n\n output.assert(existsSync(configFile), `Invalid file path => ${configFile}`);\n\n const {\n baseUrl = '',\n outDir,\n declarationDir,\n paths,\n replacers,\n resolveFullPaths,\n verbose,\n fileExtensions\n } = loadConfig(configFile, output);\n\n if (options?.fileExtensions?.inputGlob) {\n fileExtensions.inputGlob = options.fileExtensions.inputGlob;\n }\n if (options?.fileExtensions?.outputCheck) {\n fileExtensions.outputCheck = options.fileExtensions.outputCheck;\n }\n\n output.verbose = verbose;\n\n if (options.resolveFullPaths || resolveFullPaths) {\n output.debug('resolveFullPaths is active');\n options.resolveFullPaths = true;\n }\n\n const _outDir = options.outDir ?? outDir;\n if (declarationDir && _outDir !== declarationDir) {\n options.declarationDir ??= declarationDir;\n }\n\n output.assert(_outDir, 'compilerOptions.outDir is not set');\n\n const configDir: string = normalizePath(dirname(configFile));\n\n // config with project details and paths\n const projectConfig: IProjectConfig = {\n configFile: configFile,\n baseUrl: baseUrl,\n outDir: _outDir,\n configDir: configDir,\n outPath: _outDir,\n confDirParentFolderName: basename(configDir),\n hasExtraModule: false,\n configDirInOutPath: null,\n relConfDirPathInOutPath: null,\n pathCache: new PathCache(!options.watch, fileExtensions?.outputCheck),\n inputGlob:\n fileExtensions?.inputGlob || '{mjs,cjs,js,jsx,d.{mts,cts,ts,tsx}}'\n };\n output.debug('loaded project config:', projectConfig);\n\n const config: IConfig = {\n ...projectConfig,\n output: output,\n aliasTrie:\n options.aliasTrie ?? TrieNode.buildAliasTrie(projectConfig, paths),\n replacers: []\n };\n output.debug('loaded full config:', config);\n\n // Import replacers.\n await importReplacers(config, replacers, options.replacers);\n return config;\n}\n\n/**\n * loadConfig loads a config file from fs.\n * @param {string} file file path to the config file that will be loaded.\n * @param {IOutput} output the output instance to log error to.\n * @returns {ITSConfig} a ITSConfig object\n */\nexport const loadConfig = (file: string, output: IOutput): ITSConfig => {\n if (!existsSync(file)) {\n output.error(`File ${file} not found`, true);\n }\n output.debug('Loading config file:', file);\n const {\n extends: ext,\n compilerOptions: { baseUrl, outDir, declarationDir, paths } = {\n baseUrl: undefined,\n outDir: undefined,\n declarationDir: undefined,\n paths: undefined\n },\n 'tsc-alias': TSCAliasConfig\n } = Json.loadS<IRawTSConfig>(file, true);\n\n const configDir = dirname(file);\n output.debug('configDir', configDir);\n const config: ITSConfig = {};\n\n if (baseUrl) config.baseUrl = baseUrl;\n if (outDir) {\n config.outDir = isAbsolute(outDir) ? outDir : join(configDir, outDir);\n }\n if (paths) config.paths = paths;\n if (declarationDir) {\n config.declarationDir = isAbsolute(declarationDir)\n ? declarationDir\n : join(configDir, declarationDir);\n }\n if (TSCAliasConfig?.replacers) {\n config.replacers = TSCAliasConfig.replacers;\n }\n if (TSCAliasConfig?.resolveFullPaths) {\n config.resolveFullPaths = TSCAliasConfig.resolveFullPaths;\n }\n if (TSCAliasConfig?.verbose) {\n config.verbose = TSCAliasConfig.verbose;\n }\n config.fileExtensions = TSCAliasConfig?.fileExtensions ?? {};\n\n const replacerFile = config.replacers?.pathReplacer?.file;\n\n if (replacerFile) {\n config.replacers.pathReplacer.file = join(configDir, replacerFile);\n }\n\n output.debug('loaded config (from file):', config);\n if (ext) {\n return {\n ...normalizeTsConfigExtendsOption(ext, file, output).reduce<ITSConfig>(\n (pre, ext) => ({\n ...pre,\n ...loadConfig(ext, output)\n }),\n {}\n ),\n ...config\n };\n }\n\n return config;\n};\n\n/**\n * normalizeTsConfigExtendsOption normalizes tsconfig extends option to a directly loadable path array\n * @param { string|string[] } ext\n * @param { string } file\n * @param {IOutput} output the output instance to log error to.\n * @returns {string[]}\n */\nexport function normalizeTsConfigExtendsOption(\n ext: string | string[],\n file: string,\n output: IOutput\n): string[] {\n if (!ext) return [];\n const configDir = dirname(file);\n const normExts = (Array.isArray(ext) ? ext : [ext]).map((e) =>\n e.startsWith('.')\n ? join(configDir, e.endsWith('.json') ? e : `${e}.json`)\n : resolveTsConfigExtendsPath(e, file, output)\n );\n return normExts;\n}\n\n/**\n * resolveTsConfigExtendsPath resolves the path to the config file that is being inherited.\n * @param {string} ext the value of the extends field in the loaded config file.\n * @param {string} file file path to the config file that was loaded.\n * @param {IOutput} output the output instance to log error to.\n * @returns {string} a file path to the config file that is being inherited.\n */\nexport function resolveTsConfigExtendsPath(\n ext: string,\n file: string,\n output: IOutput\n): string {\n const userRequire = createRequire(file);\n\n if (ext.endsWith('.json')) return userRequire.resolve(ext);\n\n try {\n const extGuess = `${ext}.json`;\n output.debug(`Trying to resolve ${extGuess} from ${file}`);\n return userRequire.resolve(extGuess);\n } catch {}\n\n try {\n const extGuess = path.join(ext, 'tsconfig.json');\n output.debug(`Trying to resolve ${extGuess} from ${file}`);\n return userRequire.resolve(extGuess);\n } catch {}\n\n throw new Error(`Could not resolve ${ext} from ${file}`);\n}\n"]} | |
\ No newline at end of file |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment