-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extracts the repository plugin hooks into their own file (#5622)
**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
Showing
7 changed files
with
86 additions
and
85 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
declined: | ||
- "@yarnpkg/cli" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters