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

resolve PnP at the location of node_modules in the modules option #245

Merged
merged 1 commit into from
Sep 17, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions lib/PnpPlugin.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = function a() {
return "This is nested m1/a";
};
60 changes: 56 additions & 4 deletions test/pnp.js
Original file line number Diff line number Diff line change
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