Skip to content

Commit

Permalink
Extracts the repository plugin hooks into their own file (#5622)
Browse files Browse the repository at this point in the history
**What's the problem this PR addresses?**

The repository is configured to compile files dynamically, but this only
works for the CLI - we currently have no way to require the plugin
configuration without executing Yarn. I will need that for #5587, which
will use the CLI instance to tokenize the CLI commands but without
actually executing them.

**How did you fix it?**

The script hooking the repository into the plugin configuration is
extracted in a standalone script.

**Checklist**
<!--- Don't worry if you miss something, chores are automatically
tested. -->
<!--- This checklist exists to help you remember doing the chores when
you submit a PR. -->
<!--- Put an `x` in all the boxes that apply. -->
- [x] I have read the [Contributing
Guide](https://yarnpkg.com/advanced/contributing).

<!-- See
https://yarnpkg.com/advanced/contributing#preparing-your-pr-to-be-released
for more details. -->
<!-- Check with `yarn version check` and fix with `yarn version check
-i` -->
- [x] I have set the packages that need to be released for my changes to
be effective.

<!-- The "Testing chores" workflow validates that your PR follows our
guidelines. -->
<!-- If it doesn't pass, click on it to see details as to what your PR
might be missing. -->
- [x] I will check that all automated PR checks pass before the PR gets
reviewed.
  • Loading branch information
arcanis authored Jul 28, 2023
1 parent bbcce29 commit 0ff9d24
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 85 deletions.
27 changes: 8 additions & 19 deletions .pnp.cjs

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

2 changes: 2 additions & 0 deletions .yarn/versions/7043d2c3.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
declined:
- "@yarnpkg/cli"
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
"@babel/preset-react": "^7.18.6",
"@babel/preset-typescript": "^7.18.6",
"@types/jest": "^28.1.6",
"@types/micromatch": "^4.0.1",
"@types/node": "^18.15.11",
"@types/semver": "^7.1.0",
"@yarnpkg/cli": "workspace:^",
"@yarnpkg/core": "workspace:^",
"@yarnpkg/eslint-config": "workspace:^",
Expand Down Expand Up @@ -95,6 +97,8 @@
},
"dependencies": {
"@yarnpkg/types": "workspace:^",
"chalk": "^3.0.0"
"chalk": "^3.0.0",
"micromatch": "^4.0.2",
"semver": "^7.1.2"
}
}
3 changes: 1 addition & 2 deletions packages/yarnpkg-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@
"@types/semver": "^7.1.0",
"@yarnpkg/builder": "workspace:^",
"@yarnpkg/monorepo": "workspace:^",
"@yarnpkg/pnpify": "workspace:^",
"micromatch": "^4.0.2"
"@yarnpkg/pnpify": "workspace:^"
},
"peerDependencies": {
"@yarnpkg/core": "workspace:^"
Expand Down
64 changes: 2 additions & 62 deletions packages/yarnpkg-cli/sources/boot-cli-dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,67 +5,7 @@ const pnpFile = `${__dirname}/../../../.pnp.cjs`;
if (fs.existsSync(pnpFile))
require(pnpFile).setup();

// Adds TS support to Node
require(`@yarnpkg/monorepo/scripts/setup-ts-execution`);
require(`@yarnpkg/monorepo/scripts/setup-local-plugins`);

const semver = require(`semver`);

const {version} = require(`@yarnpkg/cli/package.json`);

// Exposes the CLI version as like for the bundle
global.YARN_VERSION = semver.prerelease(version) !== null
? `${version}.dev`
: `${version}-dev`;

// Inject the plugins in the runtime. With Webpack that would be through
// val-loader which would execute pluginConfiguration.raw.js, so in Node
// we need to do something similar and mutate the require cache.
const PLUGIN_CONFIG_MODULE = `./tools/getPluginConfiguration.ts`;
require.cache[require.resolve(PLUGIN_CONFIG_MODULE)] = {exports: {getPluginConfiguration}};

const micromatch = require(`micromatch`);

module.exports = require(`./cli`);

function getPluginConfiguration() {
const folders = fs.readdirSync(`${__dirname}/../../`);

const pluginFolders = folders.filter(folder => {
if (!folder.startsWith(`plugin-`))
return false;

if (process.env.BLACKLIST && micromatch.match([folder, folder.replace(`plugin-`, ``)], process.env.BLACKLIST).length > 0) {
console.warn(`Disabled blacklisted plugin ${folder}`);
return false;
}

let isRequirable;
try {
require(`${__dirname}/../../${folder}`);
isRequirable = true;
} catch (e) {
console.warn(`Disabled non-requirable plugin ${folder}: ${e.message}`);
isRequirable = false;
}
return isRequirable;
});

// Note that we don't need to populate the `modules` field, because the
// plugins will be loaded without being transformed by the builder wrapper,
// so they will simply access their own set of dependencies.
const pluginConfiguration = {
plugins: new Set(),
modules: new Map(),
};

for (const folder of pluginFolders) {
pluginConfiguration.plugins.add(`@yarnpkg/${folder}`);
pluginConfiguration.modules.set(`@yarnpkg/${folder}`, require(`../../${folder}`));
}

const {getDynamicLibs} = require(`./tools/getDynamicLibs`);
for (const [name, module] of getDynamicLibs())
pluginConfiguration.modules.set(name, module);

return pluginConfiguration;
}
require(`./cli`);
64 changes: 64 additions & 0 deletions scripts/setup-local-plugins.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
const fs = require(`fs`);
const micromatch = require(`micromatch`);
const path = require(`path`);
const semver = require(`semver`);

const {version} = require(`@yarnpkg/cli/package.json`);

// Exposes the CLI version as like for the bundle
global.YARN_VERSION = semver.prerelease(version) !== null
? `${version}.dev`
: `${version}-dev`;

const PACKAGES = path.normalize(`${__dirname}/../packages`);

const PLUGIN_CONFIGURATION_MODULE = require.resolve(`${PACKAGES}/yarnpkg-cli/sources/tools/getPluginConfiguration.ts`);
const DYNAMIC_LIBS_MODULE = require.resolve(`${PACKAGES}/yarnpkg-cli/sources/tools/getDynamicLibs.ts`);

// Inject the plugins in the runtime. With Webpack that would be through
// val-loader which would execute pluginConfiguration.raw.js, so in Node
// we need to do something similar and mutate the require cache.
require.cache[PLUGIN_CONFIGURATION_MODULE] = {exports: {getPluginConfiguration}};

function getPluginConfiguration() {
const folders = fs.readdirSync(PACKAGES);

const pluginFolders = folders.filter(folder => {
if (!folder.startsWith(`plugin-`))
return false;

if (process.env.BLACKLIST && micromatch.match([folder, folder.replace(`plugin-`, ``)], process.env.BLACKLIST).length > 0) {
console.warn(`Disabled blacklisted plugin ${folder}`);
return false;
}

let isRequirable;
try {
require(`${PACKAGES}/${folder}`);
isRequirable = true;
} catch (e) {
console.warn(`Disabled non-requirable plugin ${folder}: ${e.message}`);
isRequirable = false;
}
return isRequirable;
});

// Note that we don't need to populate the `modules` field, because the
// plugins will be loaded without being transformed by the builder wrapper,
// so they will simply access their own set of dependencies.
const pluginConfiguration = {
plugins: new Set(),
modules: new Map(),
};

for (const folder of pluginFolders) {
pluginConfiguration.plugins.add(`@yarnpkg/${folder}`);
pluginConfiguration.modules.set(`@yarnpkg/${folder}`, require(`${PACKAGES}/${folder}`));
}

const {getDynamicLibs} = require(DYNAMIC_LIBS_MODULE);
for (const [name, module] of getDynamicLibs())
pluginConfiguration.modules.set(name, module);

return pluginConfiguration;
}
5 changes: 4 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7099,7 +7099,6 @@ __metadata:
"@yarnpkg/shell": "workspace:^"
ci-info: "npm:^3.2.0"
clipanion: "npm:^3.2.1"
micromatch: "npm:^4.0.2"
semver: "npm:^7.1.2"
tslib: "npm:^2.4.0"
typanion: "npm:^3.12.1"
Expand Down Expand Up @@ -7406,7 +7405,9 @@ __metadata:
"@babel/preset-react": "npm:^7.18.6"
"@babel/preset-typescript": "npm:^7.18.6"
"@types/jest": "npm:^28.1.6"
"@types/micromatch": "npm:^4.0.1"
"@types/node": "npm:^18.15.11"
"@types/semver": "npm:^7.1.0"
"@yarnpkg/cli": "workspace:^"
"@yarnpkg/core": "workspace:^"
"@yarnpkg/eslint-config": "workspace:^"
Expand All @@ -7419,7 +7420,9 @@ __metadata:
esbuild-wasm: "npm:0.17.5"
eslint: "npm:^8.45.0"
jest: "npm:^29.2.1"
micromatch: "npm:^4.0.2"
pirates: "npm:^4.0.5"
semver: "npm:^7.1.2"
tslib: "npm:^2.4.0"
typescript: "npm:5.2.0-beta"
dependenciesMeta:
Expand Down

0 comments on commit 0ff9d24

Please sign in to comment.