Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: give precedence to pnp over node_modules, close #288 #289

Merged
merged 2 commits into from
Apr 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions lib/ResolverFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -399,13 +399,21 @@ exports.createResolver = function (options) {
});
modules.forEach(item => {
if (Array.isArray(item)) {
plugins.push(
new ModulesInHierachicDirectoriesPlugin("raw-module", item, "module")
);
if (item.includes("node_modules") && pnpApi) {
plugins.push(
new ModulesInHierachicDirectoriesPlugin(
"raw-module",
item.filter(i => i !== "node_modules"),
"module"
)
);
plugins.push(
new PnpPlugin("raw-module", pnpApi, "undescribed-resolve-in-package")
);
} else {
plugins.push(
new ModulesInHierachicDirectoriesPlugin("raw-module", item, "module")
);
}
} else {
plugins.push(new ModulesInRootPlugin("raw-module", item, "module"));
Expand Down
Empty file.
33 changes: 28 additions & 5 deletions test/pnp.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,11 @@ describe("pnp", () => {
alias: path.resolve(fixture, "pkg")
},
pnpApi,
modules: ["node_modules", path.resolve(fixture, "../pnp-a")]
modules: [
"alternative-modules",
"node_modules",
path.resolve(fixture, "../pnp-a")
]
});
});
it("should resolve by going through the pnp api", done => {
Expand Down Expand Up @@ -172,16 +176,35 @@ describe("pnp", () => {
}
);
});
it("should prefer normal modules over pnp resolves", done => {
pnpApi.mocks.set("m1", path.resolve(fixture, "pkg"));
it("should prefer pnp resolves over normal modules", done => {
pnpApi.mocks.set("m1", path.resolve(fixture, "../node_modules/m2"));
resolver.resolve(
{},
path.resolve(__dirname, "fixtures"),
"m1/a.js",
"m1/b.js",
{},
(err, result) => {
if (err) return done(err);
result.should.equal(path.resolve(fixture, "../node_modules/m1/a.js"));
result.should.equal(path.resolve(fixture, "../node_modules/m2/b.js"));
done();
}
);
});
it("should prefer alternative module directories over pnp", done => {
pnpApi.mocks.set("m1", path.resolve(fixture, "../node_modules/m2"));
resolver.resolve(
{},
path.resolve(__dirname, "fixtures/prefer-pnp"),
"m1/b.js",
{},
(err, result) => {
if (err) return done(err);
result.should.equal(
path.resolve(
__dirname,
"fixtures/prefer-pnp/alternative-modules/m1/b.js"
)
);
done();
}
);
Expand Down