Skip to content

Commit 0575ab5

Browse files
authored
fix(EntryFilesAnalyser): join non-absolute entry files when rootPath is provided (#345)
1 parent d38f809 commit 0575ab5

File tree

2 files changed

+51
-17
lines changed

2 files changed

+51
-17
lines changed

src/EntryFilesAnalyser.js

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,12 @@ export class EntryFilesAnalyser {
4141
this.dependencies = new DiGraph();
4242

4343
for (const entryFile of new Set(entryFiles)) {
44-
const normalizedEntryFile = path.normalize(
45-
fileURLToPathExtended(entryFile)
46-
);
44+
const normalizedEntryFile = this.#normalizeAndCleanEntryFile(entryFile);
4745

48-
if (this.ignoreENOENT && !await this.#fileExists(normalizedEntryFile)) {
46+
if (
47+
this.ignoreENOENT &&
48+
!await this.#fileExists(normalizedEntryFile)
49+
) {
4950
return;
5051
}
5152

@@ -57,8 +58,21 @@ export class EntryFilesAnalyser {
5758
}
5859
}
5960

61+
#normalizeAndCleanEntryFile(file) {
62+
let normalizedEntryFile = path.normalize(
63+
fileURLToPathExtended(file)
64+
);
65+
if (this.#rootPath !== null && !path.isAbsolute(normalizedEntryFile)) {
66+
normalizedEntryFile = path.join(this.#rootPath, normalizedEntryFile);
67+
}
68+
69+
return normalizedEntryFile;
70+
}
71+
6072
#getRelativeFilePath(file) {
61-
return this.#rootPath ? path.relative(this.#rootPath, file) : file;
73+
return this.#rootPath ?
74+
path.relative(this.#rootPath, file) :
75+
file;
6276
}
6377

6478
async* #analyseFile(

test/EntryFilesAnalyser.spec.js

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -181,23 +181,43 @@ describe("EntryFilesAnalyser", () => {
181181
assert.ok(to.startsWith(kFixtureURLPath));
182182
}
183183
});
184-
});
185184

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: kFixtureURL
185+
it("should automatically build absolute path for entryFiles when rootPath is provided", async() => {
186+
const entryFilesAnalyser = new EntryFilesAnalyser({
187+
rootPath: kFixtureURL
188+
});
189+
190+
const generator = entryFilesAnalyser.analyse(
191+
["recursive/A.js"]
192+
);
193+
const reports = await fromAsync(generator);
194+
195+
const files = reports.map((report) => path.normalize(report.file));
196+
assert.deepEqual(
197+
files,
198+
[
199+
"recursive/A.js",
200+
"recursive/B.js"
201+
].map((file) => path.normalize(file))
202+
);
190203
});
191204

192-
const entryUrl = new URL("does-not-exists.js", kFixtureURL);
205+
it("should ignore file that does not exist when option ignoreENOENT is provided", async() => {
206+
const entryFilesAnalyser = new EntryFilesAnalyser({
207+
ignoreENOENT: true,
208+
rootPath: kFixtureURL
209+
});
210+
211+
const entryUrl = new URL("does-not-exists.js", kFixtureURL);
193212

194-
const generator = entryFilesAnalyser.analyse(
195-
[entryUrl]
196-
);
213+
const generator = entryFilesAnalyser.analyse(
214+
[entryUrl]
215+
);
197216

198-
const reports = await fromAsync(generator);
199-
assert.strictEqual(reports.length, 0);
200-
assert.strictEqual(entryFilesAnalyser.dependencies.hasVertex("does-not-exists.js"), false);
217+
const reports = await fromAsync(generator);
218+
assert.strictEqual(reports.length, 0);
219+
assert.strictEqual(entryFilesAnalyser.dependencies.hasVertex("does-not-exists.js"), false);
220+
});
201221
});
202222

203223
// TODO: replace with Array.fromAsync when droping Node.js 20

0 commit comments

Comments
 (0)