Skip to content

Commit

Permalink
Exposes pnpapi through resolveToUnqualified (#6643)
Browse files Browse the repository at this point in the history
* Exposes pnpapi through resolveToUnqualified

* Update CHANGELOG.md
  • Loading branch information
arcanis committed Nov 7, 2018
1 parent 1ceabe8 commit 124a2ef
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Expand Up @@ -27,11 +27,15 @@ Please add one entry in this file for each change in Yarn's behavior. Use the sa
- Fixes an issue with how symlinks are setup into the cache on Windows

[#6621](https://github.com/yarnpkg/yarn/pull/6621) - [**Yoad Snapir**](https://github.com/yoadsn)

- Upgrades `inquirer`, fixing `upgrade-interactive` for users using both Node 10 and Windows

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

- Exposes the path to the PnP file using `require.resolve('pnpapi')`

[#6643](https://github.com/yarnpkg/yarn/pull/6643) - [**Maël Nison**](https://twitter.com/arcanis)

## 1.12.1

- Ensures the engine check is ran before showing the UI for `upgrade-interactive`
Expand Down
20 changes: 16 additions & 4 deletions packages/pkg-tests/pkg-tests-specs/sources/pnpapi-v1.js
@@ -1,9 +1,10 @@
const {normalize} = require('path');
const {fs: {writeFile, writeJson}} = require('pkg-tests-core');

module.exports = makeTemporaryEnv => {
describe(`Plug'n'Play API (v1)`, () => {
test(
`it should expost VERSIONS`,
`it should expose VERSIONS`,
makeTemporaryEnv({}, {plugNPlay: true}, async ({path, run, source}) => {
await run(`install`);

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

test(
`it should expost resolveToUnqualified`,
`it should expose resolveToUnqualified`,
makeTemporaryEnv({}, {plugNPlay: true}, async ({path, run, source}) => {
await run(`install`);

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

test(
`it should expost resolveToUnqualified`,
`it should expose resolveToUnqualified`,
makeTemporaryEnv({}, {plugNPlay: true}, async ({path, run, source}) => {
await run(`install`);

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

test(
`it should expost resolveToUnqualified`,
`it should expose resolveToUnqualified`,
makeTemporaryEnv({}, {plugNPlay: true}, async ({path, run, source}) => {
await run(`install`);

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

describe(`resolveRequest`, () => {
test(
`it should return the path to the PnP file for when 'pnpapi' is requested`,
makeTemporaryEnv({}, {plugNPlay: true}, async ({path, run, source}) => {
await run(`install`);

await expect(source(`require('pnpapi').resolveRequest('pnpapi', '${path}/')`)).resolves.toEqual(
normalize(`${path}/.pnp.js`),
);
}),
);

test(
`it should return null for builtins`,
makeTemporaryEnv({}, {plugNPlay: true}, async ({path, run, source}) => {
Expand Down
7 changes: 7 additions & 0 deletions src/util/generate-pnp-map-api.tpl.js
Expand Up @@ -13,6 +13,7 @@ const StringDecoder = require('string_decoder');

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

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

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

exports.resolveToUnqualified = function resolveToUnqualified(request, issuer, {considerBuiltins = true} = {}) {
// The 'pnpapi' request is reserved and will always return the path to the PnP file, from everywhere

if (request === `pnpapi`) {
return pnpFile;
}

// Bailout if the request is a native module

if (considerBuiltins && builtinModules.has(request)) {
Expand Down

0 comments on commit 124a2ef

Please sign in to comment.