Skip to content

Commit 1302f10

Browse files
authored
Merge pull request #289 from noahnu/bugfix/288
2 parents f08fe3f + 20caa8a commit 1302f10

File tree

3 files changed

+39
-8
lines changed

3 files changed

+39
-8
lines changed

lib/ResolverFactory.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -399,13 +399,21 @@ exports.createResolver = function (options) {
399399
});
400400
modules.forEach(item => {
401401
if (Array.isArray(item)) {
402-
plugins.push(
403-
new ModulesInHierachicDirectoriesPlugin("raw-module", item, "module")
404-
);
405402
if (item.includes("node_modules") && pnpApi) {
403+
plugins.push(
404+
new ModulesInHierachicDirectoriesPlugin(
405+
"raw-module",
406+
item.filter(i => i !== "node_modules"),
407+
"module"
408+
)
409+
);
406410
plugins.push(
407411
new PnpPlugin("raw-module", pnpApi, "undescribed-resolve-in-package")
408412
);
413+
} else {
414+
plugins.push(
415+
new ModulesInHierachicDirectoriesPlugin("raw-module", item, "module")
416+
);
409417
}
410418
} else {
411419
plugins.push(new ModulesInRootPlugin("raw-module", item, "module"));

test/fixtures/prefer-pnp/alternative-modules/m1/b.js

Whitespace-only changes.

test/pnp.js

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,11 @@ describe("pnp", () => {
6868
alias: path.resolve(fixture, "pkg")
6969
},
7070
pnpApi,
71-
modules: ["node_modules", path.resolve(fixture, "../pnp-a")]
71+
modules: [
72+
"alternative-modules",
73+
"node_modules",
74+
path.resolve(fixture, "../pnp-a")
75+
]
7276
});
7377
});
7478
it("should resolve by going through the pnp api", done => {
@@ -172,16 +176,35 @@ describe("pnp", () => {
172176
}
173177
);
174178
});
175-
it("should prefer normal modules over pnp resolves", done => {
176-
pnpApi.mocks.set("m1", path.resolve(fixture, "pkg"));
179+
it("should prefer pnp resolves over normal modules", done => {
180+
pnpApi.mocks.set("m1", path.resolve(fixture, "../node_modules/m2"));
177181
resolver.resolve(
178182
{},
179183
path.resolve(__dirname, "fixtures"),
180-
"m1/a.js",
184+
"m1/b.js",
181185
{},
182186
(err, result) => {
183187
if (err) return done(err);
184-
result.should.equal(path.resolve(fixture, "../node_modules/m1/a.js"));
188+
result.should.equal(path.resolve(fixture, "../node_modules/m2/b.js"));
189+
done();
190+
}
191+
);
192+
});
193+
it("should prefer alternative module directories over pnp", done => {
194+
pnpApi.mocks.set("m1", path.resolve(fixture, "../node_modules/m2"));
195+
resolver.resolve(
196+
{},
197+
path.resolve(__dirname, "fixtures/prefer-pnp"),
198+
"m1/b.js",
199+
{},
200+
(err, result) => {
201+
if (err) return done(err);
202+
result.should.equal(
203+
path.resolve(
204+
__dirname,
205+
"fixtures/prefer-pnp/alternative-modules/m1/b.js"
206+
)
207+
);
185208
done();
186209
}
187210
);

0 commit comments

Comments
 (0)