Skip to content

Commit

Permalink
feat(mf): ContainerPlugin
Browse files Browse the repository at this point in the history
  • Loading branch information
ahabhgk committed Nov 20, 2023
1 parent 3be5f16 commit 85e35ea
Show file tree
Hide file tree
Showing 29 changed files with 851 additions and 25 deletions.
16 changes: 16 additions & 0 deletions crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ export const enum BuiltinPluginName {
LimitChunkCountPlugin = 'LimitChunkCountPlugin',
WebWorkerTemplatePlugin = 'WebWorkerTemplatePlugin',
MergeDuplicateChunksPlugin = 'MergeDuplicateChunksPlugin',
ContainerPlugin = 'ContainerPlugin',
HttpExternalsRspackPlugin = 'HttpExternalsRspackPlugin',
CopyRspackPlugin = 'CopyRspackPlugin',
HtmlRspackPlugin = 'HtmlRspackPlugin',
Expand Down Expand Up @@ -581,6 +582,15 @@ export interface RawCacheOptions {
version: string
}

export interface RawContainerPluginOptions {
name: string
shareScope: string
library: RawLibraryOptions
runtime?: string
filename?: string
exposes: Array<RawExposeOptions>
}

export interface RawCopyGlobOptions {
caseSensitiveMatch?: boolean
dot?: boolean
Expand Down Expand Up @@ -655,6 +665,12 @@ export interface RawExperiments {
rspackFuture: RawRspackFuture
}

export interface RawExposeOptions {
key: string
name?: string
import: Array<string>
}

export interface RawExternalItemFnCtx {
request: string
context: string
Expand Down
14 changes: 12 additions & 2 deletions crates/rspack_binding_options/src/options/raw_builtins/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mod raw_banner;
mod raw_copy;
mod raw_html;
mod raw_limit_chunk_count;
mod raw_mf;
mod raw_progress;
mod raw_swc_js_minimizer;
mod raw_to_be_deprecated;
Expand All @@ -11,7 +12,9 @@ use napi::{
JsUnknown,
};
use napi_derive::napi;
use rspack_core::{BoxPlugin, Define, DefinePlugin, PluginExt, Provide, ProvidePlugin};
use rspack_core::{
mf::ContainerPlugin, BoxPlugin, Define, DefinePlugin, PluginExt, Provide, ProvidePlugin,
};
use rspack_error::Result;
use rspack_napi_shared::NapiResultExt;
use rspack_plugin_banner::BannerPlugin;
Expand All @@ -38,7 +41,7 @@ use rspack_plugin_web_worker_template::web_worker_template_plugin;
pub use self::{
raw_banner::RawBannerPluginOptions, raw_copy::RawCopyRspackPluginOptions,
raw_html::RawHtmlRspackPluginOptions, raw_limit_chunk_count::RawLimitChunkCountPluginOptions,
raw_progress::RawProgressPluginOptions,
raw_mf::RawContainerPluginOptions, raw_progress::RawProgressPluginOptions,
raw_swc_js_minimizer::RawSwcJsMinimizerRspackPluginOptions,
};
use crate::{
Expand Down Expand Up @@ -68,6 +71,7 @@ pub enum BuiltinPluginName {
LimitChunkCountPlugin,
WebWorkerTemplatePlugin,
MergeDuplicateChunksPlugin,
ContainerPlugin,

// rspack specific plugins
HttpExternalsRspackPlugin,
Expand Down Expand Up @@ -174,6 +178,12 @@ impl RawOptionsApply for BuiltinPlugin {
BuiltinPluginName::MergeDuplicateChunksPlugin => {
plugins.push(MergeDuplicateChunksPlugin.boxed());
}
BuiltinPluginName::ContainerPlugin => {
plugins.push(
ContainerPlugin::new(downcast_into::<RawContainerPluginOptions>(self.options)?.into())
.boxed(),
);
}

// rspack specific plugins
BuiltinPluginName::HttpExternalsRspackPlugin => {
Expand Down
48 changes: 48 additions & 0 deletions crates/rspack_binding_options/src/options/raw_builtins/raw_mf.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
use napi_derive::napi;
use rspack_core::mf::{ContainerPluginOptions, ExposeOptions};

use crate::RawLibraryOptions;

#[derive(Debug)]
#[napi(object)]
pub struct RawContainerPluginOptions {
pub name: String,
pub share_scope: String,
pub library: RawLibraryOptions,
pub runtime: Option<String>,
pub filename: Option<String>,
pub exposes: Vec<RawExposeOptions>,
}

impl From<RawContainerPluginOptions> for ContainerPluginOptions {
fn from(value: RawContainerPluginOptions) -> Self {
Self {
name: value.name,
share_scope: value.share_scope,
library: value.library.into(),
runtime: value.runtime,
filename: value.filename.map(|f| f.into()),
exposes: value.exposes.into_iter().map(|e| e.into()).collect(),
}
}
}

#[derive(Debug, Clone)]
#[napi(object)]
pub struct RawExposeOptions {
pub key: String,
pub name: Option<String>,
pub import: Vec<String>,
}

impl From<RawExposeOptions> for (String, ExposeOptions) {
fn from(value: RawExposeOptions) -> Self {
(
value.key,
ExposeOptions {
name: value.name,
import: value.import,
},
)
}
}
4 changes: 3 additions & 1 deletion crates/rspack_core/src/chunk_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,10 +284,12 @@ impl ChunkGroupKind {
}
}

pub type EntryRuntime = String;

#[derive(Debug, Default, Clone)]
pub struct EntryOptions {
pub name: Option<String>,
pub runtime: Option<String>,
pub runtime: Option<EntryRuntime>,
pub chunk_loading: Option<ChunkLoading>,
pub async_chunks: Option<bool>,
pub public_path: Option<PublicPath>,
Expand Down
10 changes: 6 additions & 4 deletions crates/rspack_core/src/compiler/compilation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,19 +172,21 @@ impl Compilation {
}
}

pub fn add_entry(&mut self, entry: DependencyId, options: EntryOptions) {
pub fn add_entry(&mut self, entry: BoxDependency, options: EntryOptions) {
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);
data.dependencies.push(entry_id);
} else {
let data = EntryData {
dependencies: vec![entry],
dependencies: vec![entry_id],
options,
};
self.entries.insert(name, data);
}
} else {
self.global_entry.dependencies.push(entry);
self.global_entry.dependencies.push(entry_id);
}
}

Expand Down
11 changes: 11 additions & 0 deletions crates/rspack_core/src/compiler/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,17 @@ impl WorkerTask for FactorizeTask {
.await?
.split_into_parts()
}
DependencyType::ContainerEntry => {
let factory = crate::mf::ContainerEntryModuleFactory;
factory
.create(ModuleFactoryCreateData {
resolve_options: self.resolve_options,
context,
dependency,
})
.await?
.split_into_parts()
}
_ => {
assert!(dependency.as_context_dependency().is_none());
let factory = NormalModuleFactory::new(
Expand Down
6 changes: 6 additions & 0 deletions crates/rspack_core/src/dependency/dependency_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ pub enum DependencyType {
WasmExportImported,
/// static exports
StaticExports,
/// container exposed
ContainerExposed,
/// container entry,
ContainerEntry,
Custom(Box<str>), // TODO it will increase large layout size
}

Expand Down Expand Up @@ -98,6 +102,8 @@ impl DependencyType {
DependencyType::ExportInfoApi => Cow::Borrowed("export info api"),
// TODO: mode
DependencyType::ImportMetaContext => Cow::Borrowed("import.meta context"),
DependencyType::ContainerExposed => Cow::Borrowed("container exposed"),
DependencyType::ContainerEntry => Cow::Borrowed("container entry"),
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions crates/rspack_core/src/dependency/runtime_template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,3 +346,11 @@ pub fn weak_error(request: &str) -> String {
let msg = format!("Module is not available (weak dependency), request is {request}.");
format!("var e = new Error('{msg}'); e.code = 'MODULE_NOT_FOUND'; throw e;")
}

pub fn returning_function(return_value: &str, args: &str) -> String {
format!("function({args}) {{ return {return_value}; }}")
}

pub fn basic_function(args: &str, body: &str) -> String {
format!("function({args}) {{\n{body}\n}}")
}
1 change: 1 addition & 0 deletions crates/rspack_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use std::sync::atomic::AtomicBool;
use std::{fmt, sync::Arc};
mod dependencies_block;
pub mod mf;
pub use dependencies_block::{
AsyncDependenciesBlock, AsyncDependenciesBlockIdentifier, DependenciesBlock,
};
Expand Down
64 changes: 64 additions & 0 deletions crates/rspack_core/src/mf/container/container_entry_dependency.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
use super::ExposeOptions;
use crate::{
AsContextDependency, AsDependencyTemplate, Dependency, DependencyCategory, DependencyId,
DependencyType, ModuleDependency,
};

#[derive(Debug, Clone)]
pub struct ContainerEntryDependency {
id: DependencyId,
pub name: String,
pub exposes: Vec<(String, ExposeOptions)>,
pub share_scope: String,
resource_identifier: String,
}

impl ContainerEntryDependency {
pub fn new(name: String, exposes: Vec<(String, ExposeOptions)>, share_scope: String) -> Self {
let resource_identifier = format!("container-entry-{}", &name);
Self {
id: DependencyId::new(),
name,
exposes,
share_scope,
resource_identifier,
}
}
}

impl Dependency for ContainerEntryDependency {
fn dependency_debug_name(&self) -> &'static str {
"ContainerEntryDependency"
}

fn id(&self) -> &DependencyId {
&self.id
}

fn category(&self) -> &DependencyCategory {
&DependencyCategory::Esm
}

fn dependency_type(&self) -> &DependencyType {
&DependencyType::ContainerEntry
}

fn resource_identifier(&self) -> Option<&str> {
Some(&self.resource_identifier)
}
}

impl ModuleDependency for ContainerEntryDependency {
fn request(&self) -> &str {
&self.resource_identifier
}

fn user_request(&self) -> &str {
&self.resource_identifier
}

fn set_request(&mut self, _request: String) {}
}

impl AsContextDependency for ContainerEntryDependency {}
impl AsDependencyTemplate for ContainerEntryDependency {}
Loading

0 comments on commit 85e35ea

Please sign in to comment.