Skip to content

Commit

Permalink
refactor!: remove pnpDataPath (#4773)
Browse files Browse the repository at this point in the history
* refactor!: remove pnpDataPath

* fix: fix required path
  • Loading branch information
paul-soporan authored Aug 18, 2022
1 parent f6886cc commit 8963fc0
Show file tree
Hide file tree
Showing 12 changed files with 62 additions and 62 deletions.
16 changes: 4 additions & 12 deletions .pnp.cjs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 37 additions & 0 deletions .yarn/versions/a1792ae0.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
releases:
"@yarnpkg/cli": major
"@yarnpkg/fslib": minor
"@yarnpkg/plugin-pnp": major
"@yarnpkg/pnp": major

declined:
- "@yarnpkg/esbuild-plugin-pnp"
- "@yarnpkg/plugin-compat"
- "@yarnpkg/plugin-constraints"
- "@yarnpkg/plugin-dlx"
- "@yarnpkg/plugin-essentials"
- "@yarnpkg/plugin-exec"
- "@yarnpkg/plugin-file"
- "@yarnpkg/plugin-git"
- "@yarnpkg/plugin-github"
- "@yarnpkg/plugin-init"
- "@yarnpkg/plugin-interactive-tools"
- "@yarnpkg/plugin-link"
- "@yarnpkg/plugin-nm"
- "@yarnpkg/plugin-npm"
- "@yarnpkg/plugin-npm-cli"
- "@yarnpkg/plugin-pack"
- "@yarnpkg/plugin-patch"
- "@yarnpkg/plugin-pnpm"
- "@yarnpkg/plugin-stage"
- "@yarnpkg/plugin-typescript"
- "@yarnpkg/plugin-version"
- "@yarnpkg/plugin-workspace-tools"
- vscode-zipfs
- "@yarnpkg/builder"
- "@yarnpkg/core"
- "@yarnpkg/doctor"
- "@yarnpkg/nm"
- "@yarnpkg/pnpify"
- "@yarnpkg/sdks"
- "@yarnpkg/shell"
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Yarn now accepts sponsorships! Please give a look at our [OpenCollective](https:
- `yarn npm audit` no longer takes into account publish registries. Use [`npmAuditRegistry`](https://yarnpkg.com/configuration/yarnrc#npmAuditRegistry) instead.
- The `--assume-fresh-project` flag of `yarn init` has been removed. Should only affect people initializing Yarn 4+ projects using a Yarn 2 binary.
- Yarn will no longer remove the old Yarn 2.x `.pnp.js` file when migrating.
- The `pnpDataPath` option has been removed to adhere to our new [PnP specification](https://yarnpkg.com/advanced/pnp-spec). For consistency, all PnP files will now be hardcoded to a single value so that third-party tools can implement the PnP specification without relying on the Yarn configuration.

### **API Changes**

Expand Down
20 changes: 1 addition & 19 deletions packages/acceptance-tests/pkg-tests-specs/sources/pnp.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1651,25 +1651,7 @@ describe(`Plug'n'Play`, () => {
}, async ({path, run, source}) => {
await run(`add`, `no-deps`);

expect(xfs.existsSync(`${path}/.pnp.data.json`)).toBeTruthy();

await writeFile(`${path}/file.js`, `
console.log(require.resolve('no-deps'));
`);

await expect(run(`node`, `file.js`)).resolves.toBeTruthy();
}),
);

test(
`it should work with pnpEnableInlining set to false and with a custom pnpDataPath`,
makeTemporaryEnv({}, {
pnpEnableInlining: false,
pnpDataPath: `./.pnp.meta.json`,
}, async ({path, run, source}) => {
await run(`add`, `no-deps`);

expect(xfs.existsSync(`${path}/.pnp.meta.json`)).toBeTruthy();
expect(xfs.existsSync(`${path}/${Filename.pnpData}`)).toBeTruthy();

await writeFile(`${path}/file.js`, `
console.log(require.resolve('no-deps'));
Expand Down
7 changes: 0 additions & 7 deletions packages/gatsby/static/configuration/yarnrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -618,13 +618,6 @@
"format": "uri-reference",
"default": "./.yarn/patches"
},
"pnpDataPath": {
"_package": "@yarnpkg/plugin-pnp",
"description": "The location where Yarn will read and write the `.pnp.meta.json` file.",
"type": "string",
"format": "uri-reference",
"default": "./.pnp.data.json"
},
"pnpEnableEsmLoader": {
"_package": "@yarnpkg/plugin-pnp",
"description": "If true, Yarn will generate an experimental ESM loader (`.pnp.loader.mjs`). Yarn tries to automatically detect whether ESM support is required.",
Expand Down
10 changes: 4 additions & 6 deletions packages/plugin-pnp/sources/PnpLinker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ export class PnpInstaller implements Installer {

if (this.opts.project.configuration.get(`nodeLinker`) !== `pnp`) {
await xfs.removePromise(pnpPath.cjs);
await xfs.removePromise(this.opts.project.configuration.get(`pnpDataPath`));
await xfs.removePromise(pnpPath.data);
await xfs.removePromise(pnpPath.esmLoader);
await xfs.removePromise(this.opts.project.configuration.get(`pnpUnpluggedFolder`));

Expand Down Expand Up @@ -323,7 +323,6 @@ export class PnpInstaller implements Installer {

async finalizeInstallWithPnp(pnpSettings: PnpSettings) {
const pnpPath = getPnpPath(this.opts.project);
const pnpDataPath = this.opts.project.configuration.get(`pnpDataPath`);

const nodeModules = await this.locateNodeModules(pnpSettings.ignorePattern);
if (nodeModules.length > 0) {
Expand All @@ -343,17 +342,16 @@ export class PnpInstaller implements Installer {
mode: 0o755,
});

await xfs.removePromise(pnpDataPath);
await xfs.removePromise(pnpPath.data);
} else {
const dataLocation = ppath.relative(ppath.dirname(pnpPath.cjs), pnpDataPath);
const {dataFile, loaderFile} = generateSplitScript({...pnpSettings, dataLocation});
const {dataFile, loaderFile} = generateSplitScript(pnpSettings);

await xfs.changeFilePromise(pnpPath.cjs, loaderFile, {
automaticNewlines: true,
mode: 0o755,
});

await xfs.changeFilePromise(pnpDataPath, dataFile, {
await xfs.changeFilePromise(pnpPath.data, dataFile, {
automaticNewlines: true,
mode: 0o644,
});
Expand Down
11 changes: 3 additions & 8 deletions packages/plugin-pnp/sources/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ export {pnpUtils};
export const getPnpPath = (project: Project) => {
return {
cjs: ppath.join(project.cwd, Filename.pnpCjs),
esmLoader: ppath.join(project.cwd, `.pnp.loader.mjs` as Filename),
data: ppath.join(project.cwd, Filename.pnpData),
esmLoader: ppath.join(project.cwd, Filename.pnpEsmLoader),
};
};

Expand Down Expand Up @@ -51,9 +52,9 @@ async function setupScriptEnvironment(project: Project, env: {[key: string]: str
async function populateYarnPaths(project: Project, definePath: (path: PortablePath | null) => void) {
const pnpPath = getPnpPath(project);
definePath(pnpPath.cjs);
definePath(pnpPath.data);
definePath(pnpPath.esmLoader);

definePath(project.configuration.get(`pnpDataPath`));
definePath(project.configuration.get(`pnpUnpluggedFolder`));
}

Expand All @@ -67,7 +68,6 @@ declare module '@yarnpkg/core' {
pnpEnableInlining: boolean;
pnpFallbackMode: string;
pnpUnpluggedFolder: PortablePath;
pnpDataPath: PortablePath;
}
}

Expand Down Expand Up @@ -118,11 +118,6 @@ const plugin: Plugin<CoreHooks & StageHooks> = {
type: SettingsType.ABSOLUTE_PATH,
default: `./.yarn/unplugged`,
},
pnpDataPath: {
description: `Path of the file where the PnP data (used by the loader) must be written`,
type: SettingsType.ABSOLUTE_PATH,
default: `./.pnp.data.json`,
},
},
linkers: [
PnpLinker,
Expand Down
2 changes: 2 additions & 0 deletions packages/yarnpkg-fslib/sources/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ export const Filename = {
*/
pnpJs: `.pnp.js` as Filename,
pnpCjs: `.pnp.cjs` as Filename,
pnpData: `.pnp.data.json` as Filename,
pnpEsmLoader: `.pnp.loader.mjs` as Filename,
rc: `.yarnrc.yml` as Filename,
};

Expand Down
12 changes: 6 additions & 6 deletions packages/yarnpkg-pnp/sources/generatePnpScript.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import {Filename} from '@yarnpkg/fslib';

import {generatePrettyJson} from './generatePrettyJson';
import {generateSerializedState} from './generateSerializedState';
// @ts-expect-error
Expand Down Expand Up @@ -44,12 +46,10 @@ function generateInlinedSetup(data: SerializedState) {
].join(``);
}

function generateSplitSetup(dataLocation: string) {
function generateSplitSetup() {
return [
`function $$SETUP_STATE(hydrateRuntimeState, basePath) {\n`,
` const path = require('path');\n`,
` const dataLocation = path.resolve(__dirname, ${JSON.stringify(dataLocation)});\n`,
` return hydrateRuntimeState(require(dataLocation), {basePath: basePath || path.dirname(dataLocation)});\n`,
` return hydrateRuntimeState(require(${JSON.stringify(`./${Filename.pnpData}`)}), {basePath: basePath || __dirname});\n`,
`}\n`,
].join(``);
}
Expand All @@ -63,10 +63,10 @@ export function generateInlinedScript(settings: PnpSettings): string {
return loaderFile;
}

export function generateSplitScript(settings: PnpSettings & {dataLocation: string}): {dataFile: string, loaderFile: string} {
export function generateSplitScript(settings: PnpSettings): {dataFile: string, loaderFile: string} {
const data = generateSerializedState(settings);

const setup = generateSplitSetup(settings.dataLocation);
const setup = generateSplitSetup();
const loaderFile = generateLoader(settings.shebang, setup);

return {dataFile: generateJsonString(data), loaderFile};
Expand Down
2 changes: 1 addition & 1 deletion packages/yarnpkg-pnp/sources/hook.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/yarnpkg-pnp/sources/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export type PackageRegistryData = Array<[string | null, PackageStoreData]>;

export type LocationLengthData = Array<number>;

// This is what is stored within the .pnp.meta.json file
// This is what is stored within the .pnp.data.json file
export type SerializedState = {
// @eslint-ignore-next-line @typescript-eslint/naming-convention
__info: Array<string>;
Expand Down
4 changes: 2 additions & 2 deletions packages/yarnpkg-pnp/tests/pnpStandardRunner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ for (const {manifest, tests} of expectations) {

fakeFs.mkdirSync(projectRoot, {recursive: true});

const pnpApiFile = ppath.join(projectRoot, `.pnp.cjs` as Filename);
const pnpApiFile = ppath.join(projectRoot, Filename.pnpCjs);
fakeFs.writeFileSync(pnpApiFile, `/* something */`);

const pnpDataFile = ppath.join(projectRoot, `.pnp.data.json` as Filename);
const pnpDataFile = ppath.join(projectRoot, Filename.pnpData);
fakeFs.writeJsonSync(pnpDataFile, manifest);

for (const test of tests) {
Expand Down

0 comments on commit 8963fc0

Please sign in to comment.