diff --git a/README.md b/README.md index d9ad7ba..94475e6 100644 --- a/README.md +++ b/README.md @@ -121,6 +121,11 @@ Here are all the available options: The output object tsc-alias will send logs to. new Output(options.verbose) + + fileExtensions + Overwrite file extensions tsc-alias will use to scan and resolve files. + undefined + @@ -143,6 +148,10 @@ Here are all the available options: "enabled": true, "file": "./otherReplacer.js" } + }, + "fileExtensions": { + "inputGlob": "js,jsx,mjs", + "outputCheck": ["js", "json", "jsx", "mjs"] } } } diff --git a/src/bin/index.ts b/src/bin/index.ts index 0d12862..3c56599 100755 --- a/src/bin/index.ts +++ b/src/bin/index.ts @@ -24,6 +24,11 @@ program .option('-v, --verbose', 'Additional information is send to the terminal') .option('--debug', 'Debug information is send to the terminal') .option('-r, --replacer ', 'path to optional extra replacer') + .option('--inputglob ', 'Overwrite glob used for file scanning') + .option( + '--outputcheck ', + 'Overwrite file extensions used for path resolution' + ) .parseAsync(process.argv); const options = program.opts(); @@ -35,5 +40,9 @@ replaceTscAliasPaths({ verbose: !!options.verbose, debug: !!options.debug, resolveFullPaths: !!options.resolveFullPaths, - replacers: options.replacer + replacers: options.replacer, + fileExtensions: { + inputGlob: options.inputglob, + outputCheck: options.outputcheck + } }); diff --git a/src/helpers/config.ts b/src/helpers/config.ts index 59145c7..c511a90 100644 --- a/src/helpers/config.ts +++ b/src/helpers/config.ts @@ -45,9 +45,12 @@ export async function prepareConfig( paths, replacers, resolveFullPaths, - verbose + verbose, + fileExtensions: fileExtensionsConfig } = loadConfig(configFile, output); + const fileExtensions = { ...fileExtensionsConfig, ...options.fileExtensions }; + output.verbose = verbose; if (options.resolveFullPaths || resolveFullPaths) { @@ -75,7 +78,9 @@ export async function prepareConfig( hasExtraModule: false, configDirInOutPath: null, relConfDirPathInOutPath: null, - pathCache: new PathCache(!options.watch) + pathCache: new PathCache(!options.watch, fileExtensions?.outputCheck), + inputGlob: + fileExtensions?.inputGlob || '{mjs,cjs,js,jsx,d.{mts,cts,ts,tsx}}' }; output.debug('loaded project config:', projectConfig); @@ -133,6 +138,8 @@ export const loadConfig = (file: string, output: IOutput): ITSConfig => { if (TSCAliasConfig?.resolveFullPaths) config.resolveFullPaths = TSCAliasConfig.resolveFullPaths; if (TSCAliasConfig?.verbose) config.verbose = TSCAliasConfig.verbose; + if (TSCAliasConfig?.fileExtensions) + config.fileExtensions = TSCAliasConfig.fileExtensions; const replacerFile = config.replacers?.pathReplacer?.file; diff --git a/src/index.ts b/src/index.ts index 7bdcbc3..eeaadc3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -45,7 +45,7 @@ export async function replaceTscAliasPaths( // Finding files and changing alias paths const posixOutput = config.outPath.replace(/\\/g, '/').replace(/\/+$/g, ''); const globPattern = [ - `${posixOutput}/**/*.{mjs,cjs,js,jsx,d.{mts,cts,ts,tsx}}`, + `${posixOutput}/**/*.${config.inputGlob}`, `!${posixOutput}/**/node_modules` ]; output.debug('Search pattern:', globPattern); diff --git a/src/interfaces.ts b/src/interfaces.ts index 7891dc9..10b3c08 100644 --- a/src/interfaces.ts +++ b/src/interfaces.ts @@ -7,6 +7,7 @@ export interface IRawTSConfig { replacers?: ReplacerOptions; resolveFullPaths?: boolean; verbose?: boolean; + fileExtensions?: Partial; }; } @@ -16,6 +17,11 @@ export type PathLike = { export type StringReplacer = (importStatement: string) => string; +export interface FileExtensions { + inputGlob: string; + outputCheck: string[]; +} + export interface ITSConfig { baseUrl?: string; outDir?: string; @@ -24,6 +30,7 @@ export interface ITSConfig { replacers?: ReplacerOptions; resolveFullPaths?: boolean; verbose?: boolean; + fileExtensions?: Partial; } export interface IProjectConfig { @@ -37,6 +44,7 @@ export interface IProjectConfig { configDirInOutPath: string; relConfDirPathInOutPath: string; pathCache: PathCache; + inputGlob: string; } export interface IConfig extends IProjectConfig { @@ -56,6 +64,7 @@ export interface ReplaceTscAliasPathsOptions { replacers?: string[]; output?: IOutput; aliasTrie?: TrieNode; + fileExtensions?: Partial; } export interface Alias { diff --git a/src/utils/path-cache.ts b/src/utils/path-cache.ts index 9f28014..8fd31f0 100644 --- a/src/utils/path-cache.ts +++ b/src/utils/path-cache.ts @@ -13,8 +13,20 @@ export class PathCache { useCache: boolean; existsCache: Map; absoluteCache: Map<{ basePath: string; aliasPath: string }, string>; + fileExtensions: string[]; - constructor(useCache: boolean) { + constructor(useCache: boolean, fileExtensions?: string[]) { + this.fileExtensions = fileExtensions || [ + 'js', + 'json', + 'jsx', + 'cjs', + 'mjs', + 'd.ts', + 'd.tsx', + 'd.cts', + 'd.mts' + ]; this.useCache = useCache; if (useCache) { this.existsCache = new Map(); @@ -29,16 +41,10 @@ export class PathCache { */ private exists(path: string): boolean { return ( - existsSync(`${path}`) || - existsSync(`${path}.js`) || - existsSync(`${path}.json`) || - existsSync(`${path}.jsx`) || - existsSync(`${path}.cjs`) || - existsSync(`${path}.mjs`) || - existsSync(`${path}.d.ts`) || - existsSync(`${path}.d.tsx`) || - existsSync(`${path}.d.cts`) || - existsSync(`${path}.d.mts`) + existsSync(path) || + this.fileExtensions.some((extension) => + existsSync(`${path}.${extension}`) + ) ); }