Skip to content

Commit 124a2ef

Browse files
authored
Exposes pnpapi through resolveToUnqualified (#6643)
* Exposes pnpapi through resolveToUnqualified * Update CHANGELOG.md
1 parent 1ceabe8 commit 124a2ef

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,15 @@ Please add one entry in this file for each change in Yarn's behavior. Use the sa
2727
- Fixes an issue with how symlinks are setup into the cache on Windows
2828

2929
[#6621](https://github.com/yarnpkg/yarn/pull/6621) - [**Yoad Snapir**](https://github.com/yoadsn)
30-
30+
3131
- Upgrades `inquirer`, fixing `upgrade-interactive` for users using both Node 10 and Windows
3232

3333
[#6635](https://github.com/yarnpkg/yarn/pull/6635) - [**Philipp Feigl**](https://github.com/pfeigl)
3434

35+
- Exposes the path to the PnP file using `require.resolve('pnpapi')`
36+
37+
[#6643](https://github.com/yarnpkg/yarn/pull/6643) - [**Maël Nison**](https://twitter.com/arcanis)
38+
3539
## 1.12.1
3640

3741
- Ensures the engine check is ran before showing the UI for `upgrade-interactive`

packages/pkg-tests/pkg-tests-specs/sources/pnpapi-v1.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
const {normalize} = require('path');
12
const {fs: {writeFile, writeJson}} = require('pkg-tests-core');
23

34
module.exports = makeTemporaryEnv => {
45
describe(`Plug'n'Play API (v1)`, () => {
56
test(
6-
`it should expost VERSIONS`,
7+
`it should expose VERSIONS`,
78
makeTemporaryEnv({}, {plugNPlay: true}, async ({path, run, source}) => {
89
await run(`install`);
910

@@ -12,7 +13,7 @@ module.exports = makeTemporaryEnv => {
1213
);
1314

1415
test(
15-
`it should expost resolveToUnqualified`,
16+
`it should expose resolveToUnqualified`,
1617
makeTemporaryEnv({}, {plugNPlay: true}, async ({path, run, source}) => {
1718
await run(`install`);
1819

@@ -21,7 +22,7 @@ module.exports = makeTemporaryEnv => {
2122
);
2223

2324
test(
24-
`it should expost resolveToUnqualified`,
25+
`it should expose resolveToUnqualified`,
2526
makeTemporaryEnv({}, {plugNPlay: true}, async ({path, run, source}) => {
2627
await run(`install`);
2728

@@ -30,7 +31,7 @@ module.exports = makeTemporaryEnv => {
3031
);
3132

3233
test(
33-
`it should expost resolveToUnqualified`,
34+
`it should expose resolveToUnqualified`,
3435
makeTemporaryEnv({}, {plugNPlay: true}, async ({path, run, source}) => {
3536
await run(`install`);
3637

@@ -39,6 +40,17 @@ module.exports = makeTemporaryEnv => {
3940
);
4041

4142
describe(`resolveRequest`, () => {
43+
test(
44+
`it should return the path to the PnP file for when 'pnpapi' is requested`,
45+
makeTemporaryEnv({}, {plugNPlay: true}, async ({path, run, source}) => {
46+
await run(`install`);
47+
48+
await expect(source(`require('pnpapi').resolveRequest('pnpapi', '${path}/')`)).resolves.toEqual(
49+
normalize(`${path}/.pnp.js`),
50+
);
51+
}),
52+
);
53+
4254
test(
4355
`it should return null for builtins`,
4456
makeTemporaryEnv({}, {plugNPlay: true}, async ({path, run, source}) => {

src/util/generate-pnp-map-api.tpl.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const StringDecoder = require('string_decoder');
1313

1414
const ignorePattern = $$BLACKLIST ? new RegExp($$BLACKLIST) : null;
1515

16+
const pnpFile = path.resolve(__dirname, __filename);
1617
const builtinModules = new Set(Module.builtinModules || Object.keys(process.binding('natives')));
1718

1819
const topLevelLocator = {name: null, reference: null};
@@ -313,6 +314,12 @@ exports.getPackageInformation = function getPackageInformation({name, reference}
313314
*/
314315

315316
exports.resolveToUnqualified = function resolveToUnqualified(request, issuer, {considerBuiltins = true} = {}) {
317+
// The 'pnpapi' request is reserved and will always return the path to the PnP file, from everywhere
318+
319+
if (request === `pnpapi`) {
320+
return pnpFile;
321+
}
322+
316323
// Bailout if the request is a native module
317324

318325
if (considerBuiltins && builtinModules.has(request)) {

0 commit comments

Comments
 (0)