Skip to content

Commit

Permalink
alternative modules directories should be preferred over pnp
Browse files Browse the repository at this point in the history
  • Loading branch information
sokra committed Apr 19, 2021
1 parent 5df4f05 commit 20caa8a
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 9 deletions.
14 changes: 11 additions & 3 deletions lib/ResolverFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -400,13 +400,21 @@ exports.createResolver = function (options) {
modules.forEach(item => {
if (Array.isArray(item)) {
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")
);
}
plugins.push(
new ModulesInHierachicDirectoriesPlugin("raw-module", item, "module")
);
} else {
plugins.push(new ModulesInRootPlugin("raw-module", item, "module"));
}
Expand Down
Empty file.
36 changes: 30 additions & 6 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 @@ -173,15 +177,35 @@ describe("pnp", () => {
);
});
it("should prefer pnp resolves over normal modules", done => {
pnpApi.mocks.set("m1", path.resolve(fixture, "pkg"));
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/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 => {
if (err) return done();
done(new Error("Resolved by normal modules"));
(err, result) => {
if (err) return done(err);
result.should.equal(
path.resolve(
__dirname,
"fixtures/prefer-pnp/alternative-modules/m1/b.js"
)
);
done();
}
);
});
Expand Down

0 comments on commit 20caa8a

Please sign in to comment.