Skip to content

Commit

Permalink
feat: extract data from mf generated code (#4799)
Browse files Browse the repository at this point in the history
* wip

* feat: integrating with 1.5 runtime

* fix

* cleanup data
  • Loading branch information
ahabhgk committed Jan 2, 2024
1 parent 81470a4 commit 1a893ef
Show file tree
Hide file tree
Showing 23 changed files with 378 additions and 134 deletions.
7 changes: 2 additions & 5 deletions crates/rspack_core/src/mf/container/remote_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use rspack_sources::{RawSource, Source, SourceExt};

use super::remote_to_external_dependency::RemoteToExternalDependency;
use crate::{
mf::share_runtime_module::{CodeGenerationDataShareInit, ShareInitData},
mf::share_runtime_module::{CodeGenerationDataShareInit, DataInitInfo, ShareInitData},
AsyncDependenciesBlockIdentifier, BoxDependency, BuildContext, BuildInfo, BuildResult,
CodeGenerationResult, Compilation, Context, DependenciesBlock, DependencyId, LibIdentOptions,
Module, ModuleIdentifier, ModuleType, RuntimeSpec, SourceType,
Expand Down Expand Up @@ -160,10 +160,7 @@ impl Module for RemoteModule {
items: vec![ShareInitData {
share_scope: self.share_scope.clone(),
init_stage: 20,
init: format!(
"initExternal({});",
serde_json::to_string(&id).expect("module_id should able to json to_string")
),
init: DataInitInfo::ExternalModuleId(id.map(|i| i.to_owned())),
}],
});
Ok(codegen)
Expand Down
7 changes: 3 additions & 4 deletions crates/rspack_core/src/mf/container/remote_runtime_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,15 @@ impl RuntimeModule for RemoteRuntimeModule {
}
RawSource::from(format!(
r#"
var chunkMapping = {chunk_mapping};
var idToExternalAndNameMapping = {id_to_external_and_name_mapping};
{ensure_chunk_handlers}.remotes = function(chunkId, promises) {{ return {remotes_fn}({{ chunkId: chunkId, promises: promises, chunkMapping: chunkMapping, idToExternalAndNameMapping: idToExternalAndNameMapping }}); }};
__webpack_require__.MF.remotesLoadingData = {{ chunkMapping: {chunk_mapping}, moduleIdToRemoteDataMapping: {id_to_external_and_name_mapping} }};
{ensure_chunk_handlers}.remotes = function(chunkId, promises) {{ return {remotes_fn}(chunkId, promises); }};
"#,
chunk_mapping = serde_json::to_string(&chunk_to_remotes_mapping)
.expect("chunk_to_remotes_mapping should able to json to_string"),
id_to_external_and_name_mapping = serde_json::to_string(&id_to_external_and_name_mapping)
.expect("id_to_external_and_name_mapping should able to json to_string"),
ensure_chunk_handlers = RuntimeGlobals::ENSURE_CHUNK_HANDLERS,
remotes_fn = "__webpack_require__.MF.remotes",
remotes_fn = "__webpack_require__.MF.remotesLoading",
))
.boxed()
}
Expand Down
10 changes: 10 additions & 0 deletions crates/rspack_core/src/mf/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,13 @@ mod container;
mod sharing;
pub use container::*;
pub use sharing::*;

mod utils {
use std::fmt;

use serde::Serialize;

pub fn json_stringify<T: ?Sized + Serialize + fmt::Debug>(v: &T) -> String {
serde_json::to_string(v).unwrap_or_else(|e| panic!("{e}: {v:?} should able to json stringify"))
}
}
32 changes: 19 additions & 13 deletions crates/rspack_core/src/mf/sharing/consume_shared_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ use async_trait::async_trait;
use rspack_error::{IntoTWithDiagnosticArray, Result, TWithDiagnosticArray};
use rspack_hash::RspackHash;
use rspack_identifier::{Identifiable, Identifier};
use rspack_sources::{RawSource, Source, SourceExt};
use rspack_sources::Source;

use super::{
consume_shared_fallback_dependency::ConsumeSharedFallbackDependency,
consume_shared_plugin::ConsumeOptions,
consume_shared_runtime_module::CodeGenerationDataConsumeShared,
};
use crate::{
async_module_factory, returning_function, sync_module_factory, AsyncDependenciesBlock,
async_module_factory, sync_module_factory, AsyncDependenciesBlock,
AsyncDependenciesBlockIdentifier, BoxDependency, BuildContext, BuildInfo, BuildResult,
CodeGenerationResult, Compilation, Context, DependenciesBlock, DependencyId, LibIdentOptions,
Module, ModuleIdentifier, ModuleType, RuntimeGlobals, RuntimeSpec, SourceType,
Expand Down Expand Up @@ -193,8 +194,8 @@ impl Module for ConsumeSharedModule {
} else if self.options.singleton {
function += "Singleton";
}
if let Some(fallback) = &self.options.import {
let code = if self.options.eager {
let factory = self.options.import.as_ref().map(|fallback| {
if self.options.eager {
sync_module_factory(
&self.get_dependencies()[0],
fallback,
Expand All @@ -208,15 +209,20 @@ impl Module for ConsumeSharedModule {
compilation,
&mut code_generation_result.runtime_requirements,
)
};
function += "Fallback";
args.push(code);
};
function += "(";
function += &args.join(", ");
function += ")";
let code = returning_function(&function, "loaders");
code_generation_result.add(SourceType::ConsumeShared, RawSource::from(code).boxed());
}
});
code_generation_result
.data
.insert(CodeGenerationDataConsumeShared {
share_scope: self.options.share_scope.clone(),
share_key: self.options.share_key.clone(),
import: self.options.import.clone(),
required_version: self.options.required_version.clone(),
strict_version: self.options.strict_version,
singleton: self.options.singleton,
eager: self.options.eager,
fallback: factory,
});
Ok(code_generation_result)
}
}
Expand Down
52 changes: 36 additions & 16 deletions crates/rspack_core/src/mf/sharing/consume_shared_runtime_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ use rspack_identifier::Identifier;
use rspack_sources::{BoxSource, RawSource, SourceExt};
use rustc_hash::FxHashMap;

use super::consume_shared_plugin::ConsumeVersion;
use crate::{
impl_runtime_module, Chunk, ChunkUkey, Compilation, ModuleIdentifier, RuntimeGlobals,
RuntimeModule, RuntimeModuleStage, SourceType,
impl_runtime_module, mf::utils::json_stringify, Chunk, ChunkUkey, Compilation, ModuleIdentifier,
RuntimeGlobals, RuntimeModule, RuntimeModuleStage, SourceType,
};

#[derive(Debug, Eq)]
Expand All @@ -16,7 +17,7 @@ pub struct ConsumeSharedRuntimeModule {
impl Default for ConsumeSharedRuntimeModule {
fn default() -> Self {
Self {
id: Identifier::from("webpack/runtime/consumes"),
id: Identifier::from("webpack/runtime/consumes_loading"),
chunk: None,
}
}
Expand All @@ -37,7 +38,7 @@ impl RuntimeModule for ConsumeSharedRuntimeModule {
.expect("should have chunk in <ConsumeSharedRuntimeModule as RuntimeModule>::generate");
let chunk = compilation.chunk_by_ukey.expect_get(&chunk_ukey);
let mut chunk_to_module_mapping = FxHashMap::default();
let mut module_id_to_source_mapping = FxHashMap::default();
let mut module_id_to_consume_data_mapping = FxHashMap::default();
let mut initial_consumes = Vec::new();
let mut add_module = |module: ModuleIdentifier, chunk: &Chunk, ids: &mut Vec<String>| {
let id = compilation
Expand All @@ -49,9 +50,19 @@ impl RuntimeModule for ConsumeSharedRuntimeModule {
if let Ok(code_gen) = compilation
.code_generation_results
.get(&module, Some(&chunk.runtime))
&& let Some(source) = code_gen.get(&SourceType::ConsumeShared)
&& let Some(data) = code_gen.data.get::<CodeGenerationDataConsumeShared>()
{
module_id_to_source_mapping.insert(id, source.clone());
module_id_to_consume_data_mapping.insert(id, format!(
"{{ shareScope: {}, shareKey: {}, import: {}, requiredVersion: {}, strictVersion: {}, singleton: {}, eager: {}, fallback: {} }}",
json_stringify(&data.share_scope),
json_stringify(&data.share_key),
json_stringify(&data.import),
json_stringify(&data.required_version.as_ref().map(|v| v.to_string()).unwrap_or_else(|| "*".to_string())),
json_stringify(&data.strict_version),
json_stringify(&data.singleton),
json_stringify(&data.eager),
data.fallback.as_deref().unwrap_or("undefined"),
));
}
};
for chunk in chunk.get_all_async_chunks(&compilation.chunk_group_by_ukey) {
Expand Down Expand Up @@ -88,31 +99,28 @@ impl RuntimeModule for ConsumeSharedRuntimeModule {
add_module(module.identifier(), chunk, &mut initial_consumes);
}
}
if module_id_to_source_mapping.is_empty() {
if module_id_to_consume_data_mapping.is_empty() {
return RawSource::from("").boxed();
}
let module_to_handler_mapping = module_id_to_source_mapping
let module_id_to_consume_data_mapping = module_id_to_consume_data_mapping
.into_iter()
.map(|(k, v)| {
format!(
"{}: {}",
serde_json::to_string(&k)
.expect("module_id_to_source_mapping key should able to json to_string"),
v.source()
v
)
})
.collect::<Vec<_>>()
.join(", ");
let mut source = format!(
r#"
var chunkMapping = {chunk_mapping};
var moduleToHandlerMapping = {{ {module_to_handler_mapping} }};
var initialConsumes = {initial_consumes};
__webpack_require__.MF.initialConsumesData = {{ initialConsumes: initialConsumes, moduleToHandlerMapping: moduleToHandlerMapping }};
__webpack_require__.MF.consumesLoadingData = {{ chunkMapping: {chunk_mapping}, moduleIdToConsumeDataMapping: {{ {module_to_consume_data_mapping} }}, initialConsumeModuleIds: {initial_consumes} }};
"#,
chunk_mapping = serde_json::to_string(&chunk_to_module_mapping)
.expect("chunk_to_module_mapping should able to json to_string"),
module_to_handler_mapping = module_to_handler_mapping,
module_to_consume_data_mapping = module_id_to_consume_data_mapping,
initial_consumes = serde_json::to_string(&initial_consumes)
.expect("initial_consumes should able to json to_string"),
);
Expand All @@ -122,9 +130,9 @@ __webpack_require__.MF.initialConsumesData = {{ initialConsumes: initialConsumes
.runtime_requirements
.contains(RuntimeGlobals::ENSURE_CHUNK_HANDLERS)
{
source += &format!("{ensure_chunk_handlers}.consumes = function(chunkId, promises) {{ return {consumes_loading_fn}({{ chunkId: chunkId, promises: promises, chunkMapping: chunkMapping, moduleToHandlerMapping: moduleToHandlerMapping }}); }};",
source += &format!("{ensure_chunk_handlers}.consumes = function(chunkId, promises) {{ return {consumes_loading_fn}(chunkId, promises); }};",
ensure_chunk_handlers = RuntimeGlobals::ENSURE_CHUNK_HANDLERS,
consumes_loading_fn = "__webpack_require__.MF.consumes",
consumes_loading_fn = "__webpack_require__.MF.consumesLoading",
);
}
RawSource::from(source).boxed()
Expand All @@ -136,3 +144,15 @@ __webpack_require__.MF.initialConsumesData = {{ initialConsumes: initialConsumes
}

impl_runtime_module!(ConsumeSharedRuntimeModule);

#[derive(Debug, Clone)]
pub struct CodeGenerationDataConsumeShared {
pub share_scope: String,
pub share_key: String,
pub import: Option<String>,
pub required_version: Option<ConsumeVersion>,
pub strict_version: bool,
pub singleton: bool,
pub eager: bool,
pub fallback: Option<String>,
}
47 changes: 24 additions & 23 deletions crates/rspack_core/src/mf/sharing/provide_shared_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ use rspack_sources::Source;
use super::{
provide_for_shared_dependency::ProvideForSharedDependency,
provide_shared_plugin::ProvideVersion,
share_runtime_module::{CodeGenerationDataShareInit, ShareInitData},
share_runtime_module::{
CodeGenerationDataShareInit, DataInitInfo, ProvideSharedInfo, ShareInitData,
},
};
use crate::{
async_module_factory, sync_module_factory, AsyncDependenciesBlock,
Expand Down Expand Up @@ -154,34 +156,33 @@ impl Module for ProvideSharedModule {
code_generation_result
.runtime_requirements
.insert(RuntimeGlobals::INITIALIZE_SHARING);
let init = format!(
"register({}, {}, {}{})",
serde_json::to_string(&self.name).expect("ProvideSharedModule name should able to json to_string"),
serde_json::to_string(&self.version.to_string()).expect("ProvideVersion::Version should able to json to_string in ProvideSharedModule::code_generation"),
if self.eager {
sync_module_factory(
&self.get_dependencies()[0],
&self.request,
compilation,
&mut code_generation_result.runtime_requirements,
)
} else {
async_module_factory(
&self.get_blocks()[0],
&self.request,
compilation,
&mut code_generation_result.runtime_requirements,
)
},
if self.eager { ", 1" } else { "" },
);
let factory = if self.eager {
sync_module_factory(
&self.get_dependencies()[0],
&self.request,
compilation,
&mut code_generation_result.runtime_requirements,
)
} else {
async_module_factory(
&self.get_blocks()[0],
&self.request,
compilation,
&mut code_generation_result.runtime_requirements,
)
};
code_generation_result
.data
.insert(CodeGenerationDataShareInit {
items: vec![ShareInitData {
share_scope: self.share_scope.clone(),
init_stage: 10,
init,
init: DataInitInfo::ProvideSharedInfo(ProvideSharedInfo {
name: self.name.clone(),
version: self.version.clone(),
factory,
eager: self.eager,
}),
}],
});
Ok(code_generation_result)
Expand Down
2 changes: 1 addition & 1 deletion crates/rspack_core/src/mf/sharing/provide_shared_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl ProvideOptions {
}
}

#[derive(Debug, Default, Clone)]
#[derive(Debug, Default, Clone, PartialEq, Eq, Hash)]
pub enum ProvideVersion {
Version(String),
#[default]
Expand Down
Loading

0 comments on commit 1a893ef

Please sign in to comment.