Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: entryOptions should able to merge #5134

Merged
merged 2 commits into from
Dec 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions crates/rspack_core/src/chunk_group.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use std::fmt;

use itertools::Itertools;
use rspack_database::DatabaseItem;
use rspack_error::{internal_error, Result};
use rspack_identifier::IdentifierMap;
use rustc_hash::FxHashSet as HashSet;

Expand Down Expand Up @@ -316,6 +319,45 @@ pub struct EntryOptions {
pub library: Option<LibraryOptions>,
}

impl EntryOptions {
pub fn merge(&mut self, other: EntryOptions) -> Result<()> {
macro_rules! merge_field {
($field:ident) => {
if Self::should_merge_field(
self.$field.as_ref(),
other.$field.as_ref(),
stringify!($field),
)? {
self.$field = other.$field;
}
};
}
merge_field!(name);
merge_field!(runtime);
merge_field!(chunk_loading);
merge_field!(async_chunks);
merge_field!(public_path);
merge_field!(base_uri);
merge_field!(filename);
merge_field!(library);
Ok(())
}

fn should_merge_field<T: Eq + fmt::Debug>(
a: Option<&T>,
b: Option<&T>,
key: &str,
) -> Result<bool> {
match (a, b) {
(Some(a), Some(b)) if a != b => Err(internal_error!(
"Conflicting entry option {key} = ${a:?} vs ${b:?}"
)),
(None, Some(_)) => Ok(true),
_ => Ok(false),
}
}
}

#[derive(Debug, Hash, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum ChunkGroupOrderKey {
Preload,
Expand Down
4 changes: 3 additions & 1 deletion crates/rspack_core/src/compiler/compilation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,12 +190,13 @@ impl Compilation {
.unwrap_or_default()
}

pub fn add_entry(&mut self, entry: BoxDependency, options: EntryOptions) {
pub fn add_entry(&mut self, entry: BoxDependency, options: EntryOptions) -> Result<()> {
let entry_id = *entry.id();
self.module_graph.add_dependency(entry);
if let Some(name) = options.name.clone() {
if let Some(data) = self.entries.get_mut(&name) {
data.dependencies.push(entry_id);
data.options.merge(options)?;
} else {
let data = EntryData {
dependencies: vec![entry_id],
Expand All @@ -207,6 +208,7 @@ impl Compilation {
} else {
self.global_entry.dependencies.push(entry_id);
}
Ok(())
}

pub async fn add_include(&mut self, entry: BoxDependency, options: EntryOptions) -> Result<()> {
Expand Down
2 changes: 1 addition & 1 deletion crates/rspack_plugin_entry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl Plugin for EntryPlugin {
self.context.clone(),
));
let dependency_id = *dependency.id();
compilation.add_entry(dependency, self.options.clone());
compilation.add_entry(dependency, self.options.clone())?;
param.add_force_build_dependency(dependency_id, None);
Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion crates/rspack_plugin_mf/src/container/container_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ impl Plugin for ContainerPlugin {
library: Some(self.options.library.clone()),
..Default::default()
},
);
)?;
param.add_force_build_dependency(dependency_id, None);
Ok(())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ module.exports = {
name: "main",
library: { type: "commonjs-module" },
remotes: {
containerB: "../1-container-full/container.js",
containerB: "../../1-container-full/dist/container.js",
self: [
"var undefined",
"var (() => { throw new Error(); })()",
"var { then: (a, b) => b(new Error()) }",
"./bundle0.js"
"./dist/main.js"
]
},
exposes: ["./Self"],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,27 +35,30 @@ module.exports = [
...common
})
]
},
{
experiments: {
outputModule: true,
rspackFuture: {
newTreeshaking: true
}
},
output: {
filename: "module/[name].mjs",
uniqueName: "0-container-full-mjs"
},
plugins: [
new ModuleFederationPlugin({
library: { type: "module" },
filename: "module/container.mjs",
remotes: {
containerA: {
external: "./container.mjs"
}
},
...common
})
],
target: "node14"
}
// {
// experiments: {
// outputModule: true
// },
// output: {
// filename: "module/[name].mjs",
// uniqueName: "0-container-full-mjs"
// },
// plugins: [
// new ModuleFederationPlugin({
// library: { type: "module" },
// filename: "module/container.mjs",
// remotes: {
// containerA: {
// external: "./container.mjs"
// }
// },
// ...common
// })
// ],
// target: "node14"
// }
];
Original file line number Diff line number Diff line change
Expand Up @@ -40,28 +40,31 @@ module.exports = [
...commonMF
})
]
},
{
...common,
experiments: {
outputModule: true,
rspackFuture: {
newTreeshaking: true
}
},
output: {
filename: "module/[name].mjs",
uniqueName: "1-container-full-mjs"
},
plugins: [
new ModuleFederationPlugin({
name: "container",
library: { type: "module" },
filename: "module/container.mjs",
remotes: {
containerA: "../../../0-container-full/dist/module/container.mjs",
containerB: "./container.mjs"
},
...commonMF
})
],
target: "node14"
}
// {
// ...common,
// experiments: {
// outputModule: true
// },
// output: {
// filename: "module/[name].mjs",
// uniqueName: "1-container-full-mjs"
// },
// plugins: [
// new ModuleFederationPlugin({
// name: "container",
// library: { type: "module" },
// filename: "module/container.mjs",
// remotes: {
// containerA: "../../0-container-full/module/container.mjs",
// containerB: "./container.mjs"
// },
// ...commonMF
// })
// ],
// target: "node14"
// }
];
3 changes: 2 additions & 1 deletion webpack-test/ConfigTestCases.template.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ const describeCases = config => {
}
if (!options.experiments.rspackFuture) {
options.experiments.rspackFuture = {
newTreeshaking: true
newTreeshaking: true,
disableApplyEntryLazily: true,
}
}
// if (options.optimization.minimizer === undefined) {
Expand Down

This file was deleted.

Loading