Skip to content

Commit

Permalink
resolve PnP at the location of node_modules in the modules option
Browse files Browse the repository at this point in the history
  • Loading branch information
sokra committed Sep 17, 2020
1 parent c200a08 commit 6ec84c2
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 9 deletions.
13 changes: 13 additions & 0 deletions lib/PnpPlugin.js
Expand Up @@ -51,6 +51,19 @@ module.exports = class PnpPlugin {
});
}
} catch (error) {
if (
error.code === "MODULE_NOT_FOUND" &&
error.pnpCode === "UNDECLARED_DEPENDENCY"
) {
// This is not a PnP managed dependency.
// Try to continue resolving with our alternatives
if (resolveContext.log) {
resolveContext.log(`request is not managed by the pnpapi`);
for (const line of error.message.split("\n").filter(Boolean))
resolveContext.log(` ${line}`);
}
return callback();
}
return callback(error);
}

Expand Down
12 changes: 7 additions & 5 deletions lib/ResolverFactory.js
Expand Up @@ -375,15 +375,17 @@ exports.createResolver = function(options) {
new SelfReferencePlugin("raw-module", exportsField, "resolve-as-module")
);
});
if (pnpApi) {
plugins.push(new PnpPlugin("raw-module", pnpApi, "relative"));
}
modules.forEach(item => {
if (Array.isArray(item))
if (Array.isArray(item)) {
plugins.push(
new ModulesInHierachicDirectoriesPlugin("raw-module", item, "module")
);
else plugins.push(new ModulesInRootPlugin("raw-module", item, "module"));
if (item.includes("node_modules") && pnpApi) {
plugins.push(new PnpPlugin("raw-module", pnpApi, "relative"));
}
} else {
plugins.push(new ModulesInRootPlugin("raw-module", item, "module"));
}
});

// module
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/pnp-a/m2/a.js
@@ -0,0 +1,3 @@
module.exports = function a() {
return "This is nested m1/a";
};
60 changes: 56 additions & 4 deletions test/pnp.js
Expand Up @@ -42,15 +42,22 @@ describe("pnp", () => {
if (pnpApi.mocks.has(request)) {
return pnpApi.mocks.get(request);
} else {
throw new Error(`No way`);
const err = /** @type {any} */ (new Error(`No way`));
err.code = "MODULE_NOT_FOUND";
err.pnpCode = "UNDECLARED_DEPENDENCY";
throw err;
}
}
});
resolver = ResolverFactory.createResolver({
extensions: [".ts", ".js"],
aliasFields: ["browser"],
fileSystem: nodeFileSystem,
pnpApi
alias: {
alias: path.resolve(fixture, "pkg")
},
pnpApi,
modules: ["node_modules", path.resolve(fixture, "../pnp-a")]
});
});
it("should resolve by going through the pnp api", done => {
Expand Down Expand Up @@ -145,15 +152,60 @@ describe("pnp", () => {
done();
});
});
it("should skip normal modules when pnp resolves", done => {
it("should prefer normal modules over pnp resolves", done => {
pnpApi.mocks.set("m1/a.js", path.resolve(fixture, "pkg/a.js"));
resolver.resolve(
{},
path.resolve(__dirname, "fixtures"),
"m1/a.js",
{},
(err, result) => {
if (!err) return done(new Error("Resolving should fail"));
if (err) return done(err);
result.should.equal(path.resolve(fixture, "../node_modules/m1/a.js"));
done();
}
);
});
it("should prefer alias over pnp resolves", done => {
pnpApi.mocks.set(
"alias/index.js",
path.resolve(fixture, "pkg/dir/index.js")
);
resolver.resolve(
{},
path.resolve(__dirname, "fixtures"),
"alias/index.js",
{},
(err, result) => {
if (err) return done(err);
result.should.equal(path.resolve(fixture, "pkg/index.js"));
done();
}
);
});
it("should prefer pnp over modules after node_modules", done => {
pnpApi.mocks.set("m2/a.js", path.resolve(fixture, "pkg/index.js"));
resolver.resolve(
{},
path.resolve(__dirname, "fixtures"),
"m2/a.js",
{},
(err, result) => {
if (err) return done(err);
result.should.equal(path.resolve(fixture, "pkg/index.js"));
done();
}
);
});
it("should fallback to alternatives when pnp resolving fails", done => {
resolver.resolve(
{},
path.resolve(__dirname, "fixtures"),
"m2/a.js",
{},
(err, result) => {
if (err) return done(err);
result.should.equal(path.resolve(fixture, "../pnp-a/m2/a.js"));
done();
}
);
Expand Down

0 comments on commit 6ec84c2

Please sign in to comment.