Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File extensions settings #150

Merged
merged 2 commits into from
Dec 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ Here are all the available options:
<td>The output object tsc-alias will send logs to.</td>
<td><code>new Output(options.verbose)</code></td>
</tr>
<tr>
<td>fileExtensions</td>
<td>Overwrite file extensions tsc-alias will use to scan and resolve files.</td>
<td><code>undefined</code></td>
</tr>
</tbody>
</table>

Expand All @@ -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"]
}
}
}
Expand Down
11 changes: 10 additions & 1 deletion src/bin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 <replacers...>', 'path to optional extra replacer')
.option('--inputglob <glob>', 'Overwrite glob used for file scanning')
.option(
'--outputcheck <extensions...>',
'Overwrite file extensions used for path resolution'
)
.parseAsync(process.argv);

const options = program.opts();
Expand All @@ -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
}
});
11 changes: 9 additions & 2 deletions src/helpers/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
9 changes: 9 additions & 0 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export interface IRawTSConfig {
replacers?: ReplacerOptions;
resolveFullPaths?: boolean;
verbose?: boolean;
fileExtensions?: Partial<FileExtensions>;
};
}

Expand All @@ -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;
Expand All @@ -24,6 +30,7 @@ export interface ITSConfig {
replacers?: ReplacerOptions;
resolveFullPaths?: boolean;
verbose?: boolean;
fileExtensions?: Partial<FileExtensions>;
}

export interface IProjectConfig {
Expand All @@ -37,6 +44,7 @@ export interface IProjectConfig {
configDirInOutPath: string;
relConfDirPathInOutPath: string;
pathCache: PathCache;
inputGlob: string;
}

export interface IConfig extends IProjectConfig {
Expand All @@ -56,6 +64,7 @@ export interface ReplaceTscAliasPathsOptions {
replacers?: string[];
output?: IOutput;
aliasTrie?: TrieNode<Alias>;
fileExtensions?: Partial<FileExtensions>;
}

export interface Alias {
Expand Down
28 changes: 17 additions & 11 deletions src/utils/path-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,20 @@ export class PathCache {
useCache: boolean;
existsCache: Map<string, boolean>;
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();
Expand All @@ -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}`)
)
);
}

Expand Down