Skip to content

Commit 179871e

Browse files
authored
feat(EntryFilesAnalyzer): add option to ignore entry file that doesn not exist (#320)
1 parent de2b7c0 commit 179871e

File tree

4 files changed

+26
-1
lines changed

4 files changed

+26
-1
lines changed

docs/api/EntryFilesAnalyser.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ interface EntryFilesAnalyserOptions {
2222
astAnalyzer?: AstAnalyser;
2323
loadExtensions?: (defaults: string[]) => string[];
2424
rootPath?: string | URL;
25+
ignoreENOENT?: boolean;
2526
}
2627
```
2728

src/EntryFilesAnalyser.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ export class EntryFilesAnalyser {
1919
const {
2020
astAnalyzer = new AstAnalyser(),
2121
loadExtensions,
22-
rootPath = null
22+
rootPath = null,
23+
ignoreENOENT = false
2324
} = options;
2425

2526
this.astAnalyzer = astAnalyzer;
@@ -30,6 +31,7 @@ export class EntryFilesAnalyser {
3031
this.allowedExtensions = new Set(rawAllowedExtensions);
3132
this.#rootPath = options.rootPath === null ?
3233
null : fileURLToPathExtended(rootPath);
34+
this.ignoreENOENT = ignoreENOENT;
3335
}
3436

3537
async* analyse(
@@ -43,6 +45,10 @@ export class EntryFilesAnalyser {
4345
fileURLToPathExtended(entryFile)
4446
);
4547

48+
if (this.ignoreENOENT && !await this.#fileExists(normalizedEntryFile)) {
49+
return;
50+
}
51+
4652
yield* this.#analyseFile(
4753
normalizedEntryFile,
4854
this.#getRelativeFilePath(normalizedEntryFile),

test/EntryFilesAnalyser.spec.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,23 @@ describe("EntryFilesAnalyser", () => {
183183
});
184184
});
185185

186+
it("should ignore file that does not exist when option ignoreENOENT is provided", async() => {
187+
const entryFilesAnalyser = new EntryFilesAnalyser({
188+
ignoreENOENT: true,
189+
rootPath: FIXTURE_URL
190+
});
191+
192+
const entryUrl = new URL("does-not-exists.js", FIXTURE_URL);
193+
194+
const generator = entryFilesAnalyser.analyse(
195+
[entryUrl]
196+
);
197+
198+
const reports = await fromAsync(generator);
199+
assert.strictEqual(reports.length, 0);
200+
assert.strictEqual(entryFilesAnalyser.dependencies.hasVertex("does-not-exists.js"), false);
201+
});
202+
186203
// TODO: replace with Array.fromAsync when droping Node.js 20
187204
async function fromAsync(asyncIter) {
188205
const items = [];

types/api.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ interface EntryFilesAnalyserOptions {
145145
astAnalyzer?: AstAnalyser;
146146
loadExtensions?: (defaults: string[]) => string[];
147147
rootPath?: string | URL;
148+
ignoreENOENT?: boolean;
148149
}
149150

150151
declare class EntryFilesAnalyser {

0 commit comments

Comments
 (0)