Skip to content

Commit

Permalink
feat(mf): recursive search for versions of shared dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
inottn committed Nov 10, 2024
1 parent 2ac4a11 commit 6d30a0a
Show file tree
Hide file tree
Showing 20 changed files with 169 additions and 24 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/get-runner-labels.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ on:
jobs:
main:
name: Get Runner Labels
runs-on: [self-hosted, Linux, ci]
runs-on: ubuntu-latest
outputs:
LINUX_RUNNER_LABELS: ${{ steps.run.outputs.LINUX_RUNNER_LABELS }}
MACOS_RUNNER_LABELS: ${{ steps.run.outputs.MACOS_RUNNER_LABELS }}
Expand Down
86 changes: 63 additions & 23 deletions crates/rspack_plugin_mf/src/sharing/consume_shared_plugin.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::collections::HashSet;
use std::path::PathBuf;

Check failure on line 2 in crates/rspack_plugin_mf/src/sharing/consume_shared_plugin.rs

View workflow job for this annotation

GitHub Actions / Rust check

unused import: `std::path::PathBuf`
use std::sync::LazyLock;
use std::sync::Mutex;
Expand Down Expand Up @@ -98,19 +99,31 @@ fn resolve_matched_configs(
}
}

async fn get_description_file(mut dir: &Path) -> Option<(PathBuf, serde_json::Value)> {
async fn get_description_file(
mut dir: &Path,
satisfies_description_file_data: Option<impl Fn(Option<serde_json::Value>) -> bool>,
) -> (Option<serde_json::Value>, Option<Vec<String>>) {
let description_filename = "package.json";
let mut checked_file_paths = HashSet::new();

loop {
let description_file = dir.join(description_filename);
if let Ok(data) = tokio::fs::read(&description_file).await
&& let Ok(data) = serde_json::from_slice::<serde_json::Value>(&data)
{
return Some((description_file, data));
if satisfies_description_file_data
.as_ref()
.map_or(false, |f| f(Some(data.clone())))
{
checked_file_paths.insert(description_file.to_string_lossy().to_string());
} else {
return (Some(data), None);
}
}
if let Some(parent) = dir.parent() {
dir = parent;
} else {
return None;
return (None, Some(checked_file_paths.into_iter().collect()));
}
}
}
Expand Down Expand Up @@ -226,29 +239,56 @@ impl ConsumeSharedPlugin {
required_version_warning("Unable to extract the package name from request.");
return None;
};
if let Some(package_name) = package_name
&& let Some((description_path, data)) = get_description_file(context.as_ref()).await
{
if let Some(name) = data.get("name").and_then(|n| n.as_str())
&& name == package_name
{
// Package self-referencing

if let Some(package_name) = package_name {
let (data, checked_description_file_paths) = get_description_file(
context.as_ref(),
Some(|data: Option<serde_json::Value>| {
if let Some(data) = data {
let name_matches = data
.get("name")
.and_then(|n| n.as_str())
.map_or(false, |name| name == package_name);
let version_matches = get_required_version_from_description_file(data, package_name)
.map_or(false, |version| {
matches!(version, ConsumeVersion::Version(_))
});
name_matches || version_matches
} else {
false
}
}),
)
.await;

if let Some(data) = data {
if let Some(name) = data.get("name").and_then(|n| n.as_str())
&& name == package_name
{
// Package self-referencing
return None;
}
return get_required_version_from_description_file(data, package_name);
} else {
if let Some(file_paths) = checked_description_file_paths
&& !file_paths.is_empty()
{
required_version_warning(&format!(
"Unable to find required version for \"{}\" in description file/s\n{}\nIt need to be in dependencies, devDependencies or peerDependencies.",
package_name,
file_paths.join("\n")
));
} else {
required_version_warning(&format!(
"Unable to find description file in {}",
context.as_str()
));
}
return None;
}
get_required_version_from_description_file(data, package_name).or_else(|| {
required_version_warning(&format!(
"Unable to find required version for \"{package_name}\" in description file ({}). It need to be in dependencies, devDependencies or peerDependencies.",
description_path.display(),
));
None
})
} else {
required_version_warning(&format!(
"Unable to find description file in {}",
context.as_str()
));
None
}

None
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
it('should provide own dependency', async () => {
expect(await import('lib')).toEqual(
expect.objectContaining({
default: '[email protected] with [email protected]',
}),
);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "commonjs"
}

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

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

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

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"dependencies": {
"lib": "^1.0.0"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const { SharePlugin } = require("@rspack/core").sharing;

/** @type {import("../../../../").Configuration} */
module.exports = {
context: `${__dirname}/cjs`,
plugins: [
new SharePlugin({
shared: {
lib: {},
transitive_lib: {},
},
}),
],
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
it('should provide library from own package.json', async () => {
expect(await import('lib1')).toEqual(
expect.objectContaining({
default: '[email protected]',
}),
);
});

it('should provide library from parent package.json', async () => {
expect(await import('lib2')).toEqual(
expect.objectContaining({
default: '[email protected]',
}),
);
});

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

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"dependencies": {
"lib2": "^2.0.0"
}
}

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

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

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

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"dependencies": {
"lib1": "^1.0.0",
"lib2": "^1.0.0"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const { SharePlugin } = require("@rspack/core").sharing;

/** @type {import("../../../../").Configuration} */
module.exports = {
context: `${__dirname}/app1`,
plugins: [
new SharePlugin({
shared: {
lib1: {},
lib2: {
singleton: true,
},
},
}),
],
};

0 comments on commit 6d30a0a

Please sign in to comment.