Skip to content

Commit

Permalink
fix: allow configuration for ignored folders
Browse files Browse the repository at this point in the history
  • Loading branch information
theKashey committed Oct 18, 2021
1 parent 3f157ef commit 4fc6519
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
12 changes: 10 additions & 2 deletions src/configuration/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,16 @@ type ImportOptions = KnownImportOptions | Record<string, string | boolean>;
*/
export interface ImportedConfiguration {
/**
* tests if this file should scanned by `imported-component`
* tests folder during scanning process. Can be used to optimize scanning process.
* @default ignores `node_modules` and `.*` directories
* @returns boolean flag
* - true, dive in
* - false, stop here
*/
testFolder?: (targetName: string) => boolean;
/**
* tests if this file should scanned by `imported-component`.
* Keep in mind that you might consider removing (unit)test files from the scan
* @param fileName - source file name
* @returns {Boolean} true - if should, false - is should not
* @example
Expand Down Expand Up @@ -47,7 +56,6 @@ export interface ImportedConfiguration {
* }
*/
testImport?: (targetFileName: string, sourceFileName: string) => boolean;

/**
* marks import with prefetch comment (if possible)
* @param {String} targetFile
Expand Down
14 changes: 11 additions & 3 deletions src/scanners/scanForImports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,14 @@ const mapImports = (file: string, imports: MappedImport[]) =>
};
});

const rejectSystemFiles = (file: string, stats: Stats) =>
(stats.isDirectory() && file.match(/node_modules/)) || file.match(/(\/\.\w+)/);
const rejectSystemFiles = (test: Required<ImportedConfiguration>['testFolder']) => (file: string, stats: Stats) => {
if (stats.isDirectory()) {
return test(file);
}
return false;
};

const rejectNodeModulesAndDotFolders = (file: string) => !(file.match(/node_modules/) || file.match(/(\/\.\w+)/));

export const remapImports = (
data: FileContent[],
Expand Down Expand Up @@ -129,13 +135,15 @@ export function scanTop(root: string, start: string, target: string) {
// try load configuration
const configurationFile = join(root, '.imported.js');
const {
testFolder = rejectNodeModulesAndDotFolders,
testFile = () => true,
testImport = () => true,
chunkName,
configuration,
}: ImportedConfiguration = existsSync(configurationFile) ? require(configurationFile) : {};

const files = ((await scanDirectory(join(root, start), undefined, rejectSystemFiles)) as string[])
const sourceDir = resolve(root, start);
const files = ((await scanDirectory(sourceDir, undefined, rejectSystemFiles(testFolder))) as string[])
.filter(name => normalizePath(name).indexOf(target) === -1)
.filter(name => RESOLVE_EXTENSIONS.indexOf(extname(name)) >= 0)
.filter(name => testFile(name))
Expand Down

0 comments on commit 4fc6519

Please sign in to comment.