Skip to content

Commit 20caa8a

Browse files
committed
alternative modules directories should be preferred over pnp
1 parent 5df4f05 commit 20caa8a

File tree

3 files changed

+41
-9
lines changed

3 files changed

+41
-9
lines changed

lib/ResolverFactory.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -400,13 +400,21 @@ exports.createResolver = function (options) {
400400
modules.forEach(item => {
401401
if (Array.isArray(item)) {
402402
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+
);
403410
plugins.push(
404411
new PnpPlugin("raw-module", pnpApi, "undescribed-resolve-in-package")
405412
);
413+
} else {
414+
plugins.push(
415+
new ModulesInHierachicDirectoriesPlugin("raw-module", item, "module")
416+
);
406417
}
407-
plugins.push(
408-
new ModulesInHierachicDirectoriesPlugin("raw-module", item, "module")
409-
);
410418
} else {
411419
plugins.push(new ModulesInRootPlugin("raw-module", item, "module"));
412420
}

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

Whitespace-only changes.

test/pnp.js

Lines changed: 30 additions & 6 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 => {
@@ -173,15 +177,35 @@ describe("pnp", () => {
173177
);
174178
});
175179
it("should prefer pnp resolves over normal modules", done => {
176-
pnpApi.mocks.set("m1", path.resolve(fixture, "pkg"));
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",
185+
{},
186+
(err, result) => {
187+
if (err) return done(err);
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",
181199
{},
182-
err => {
183-
if (err) return done();
184-
done(new Error("Resolved by normal modules"));
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+
);
208+
done();
185209
}
186210
);
187211
});

0 commit comments

Comments
 (0)