Skip to content

Commit

Permalink
Added file extension options
Browse files Browse the repository at this point in the history
  • Loading branch information
raouldeheer committed Dec 1, 2022
1 parent 39e76bf commit d26254c
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 14 deletions.
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[];
}

This comment has been minimized.

Copy link
@DerHannes85

DerHannes85 Dec 1, 2022

I think this can be reduced to an simple string[]. The needed inputGlob can be generated by a simple join.

This comment has been minimized.

Copy link
@raouldeheer

raouldeheer Dec 1, 2022

Author Contributor

The inputGlob and outputCheck are separate because the files to scan could be different from the files to resolve to. For example, with importing .css, this file shouldn't be scanned but should be resolved to.

This comment has been minimized.

Copy link
@DerHannes85

DerHannes85 Dec 1, 2022

Got it!


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

0 comments on commit d26254c

Please sign in to comment.