Skip to content
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
80 changes: 22 additions & 58 deletions crates/rspack_plugin_rstest/src/mock_method_dependency.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
use rspack_cacheable::{cacheable, cacheable_dyn, with::Skip};
use rspack_core::{
AsContextDependency, AsModuleDependency, ConditionalInitFragment, DependencyCodeGeneration,
DependencyId, DependencyRange, DependencyTemplate, DependencyTemplateType, DependencyType,
InitFragmentExt, InitFragmentKey, InitFragmentStage, NormalInitFragment, RuntimeCondition,
TemplateContext, TemplateReplaceSource, import_statement,
AsContextDependency, AsModuleDependency, DependencyCodeGeneration, DependencyRange,
DependencyTemplate, DependencyTemplateType, DependencyType, InitFragmentExt, InitFragmentKey,
InitFragmentStage, NormalInitFragment, TemplateContext, TemplateReplaceSource,
};
use swc_core::common::Span;

#[cacheable]
#[derive(Debug, Clone)]
pub enum Position {
Before,
After,
}

#[cacheable]
#[derive(Debug, Clone)]
pub struct MockMethodDependency {
Expand All @@ -24,16 +16,18 @@ pub struct MockMethodDependency {
request: String,
hoist: bool,
method: MockMethod,
module_dep_id: Option<DependencyId>,
position: Position,
}

#[cacheable]
#[derive(Debug, Clone, PartialEq, Eq)]
#[allow(dead_code)]
pub enum MockMethod {
Mock,
DoMock,
MockRequire,
DoMockRequire,
Unmock,
DoUnmock,
Hoisted,
}

Expand All @@ -44,17 +38,13 @@ impl MockMethodDependency {
request: String,
hoist: bool,
method: MockMethod,
module_dep_id: Option<DependencyId>,
position: Position,
) -> Self {
Self {
call_expr_span,
callee_span,
request,
hoist,
method,
module_dep_id,
position,
}
}
}
Expand Down Expand Up @@ -86,14 +76,7 @@ impl DependencyTemplate for MockMethodDependencyTemplate {
source: &mut TemplateReplaceSource,
code_generatable_context: &mut TemplateContext,
) {
let TemplateContext {
module,
runtime_requirements,
compilation,
init_fragments,
runtime,
..
} = code_generatable_context;
let TemplateContext { init_fragments, .. } = code_generatable_context;
let dep = dep
.as_any()
.downcast_ref::<MockMethodDependency>()
Expand All @@ -103,52 +86,33 @@ impl DependencyTemplate for MockMethodDependencyTemplate {
let hoist_flag = match dep.method {
MockMethod::Mock => "MOCK",
MockMethod::DoMock => "", // won't be used.
MockMethod::MockRequire => "MOCKREQUIRE",
MockMethod::DoMockRequire => "", // won't be used.
MockMethod::Unmock => "UNMOCK",
MockMethod::Hoisted => "HOISTED",
MockMethod::DoUnmock => "", // won't be used.
};

let mock_method = match dep.method {
MockMethod::Mock => "rstest_mock",
MockMethod::DoMock => "rstest_do_mock",
MockMethod::MockRequire => "rstest_mock_require",
MockMethod::DoMockRequire => "rstest_do_mock_require",
MockMethod::Unmock => "rstest_unmock",
MockMethod::Hoisted => "rstest_hoisted",
MockMethod::DoUnmock => "rstest_do_unmock",
};

// Hoist placeholder init fragment.
let init = NormalInitFragment::new(
format!("/* RSTEST:{hoist_flag}_PLACEHOLDER:{request} */;"),
InitFragmentStage::StageESMImports,
match dep.position {
Position::Before => 0,
Position::After => i32::MAX - 1,
},
InitFragmentKey::Const(format!("rstest mock_hoist {request}")),
None,
);
init_fragments.push(init.boxed());

if dep.method == MockMethod::Mock
&& let Some(module_dep_id) = dep.module_dep_id
{
let content: (String, String) = import_statement(
*module,
*runtime,
compilation,
runtime_requirements,
&module_dep_id,
request,
false,
);

// Redeclaration init fragment.
init_fragments.push(Box::new(ConditionalInitFragment::new(
format!("{}{}", content.0, content.1),
InitFragmentStage::StageAsyncESMImports,
i32::MAX,
InitFragmentKey::ESMImport(format!("{} {}", request, "mock")),
if !hoist_flag.is_empty() {
let init = NormalInitFragment::new(
format!("/* RSTEST:{hoist_flag}_PLACEHOLDER:{request} */;"),
InitFragmentStage::StageESMImports,
0,
InitFragmentKey::Const(format!("rstest mock_hoist {request}")),
None,
RuntimeCondition::Boolean(true),
)));
);
init_fragments.push(init.boxed());
}

// Start before hoist.
Expand Down
27 changes: 3 additions & 24 deletions crates/rspack_plugin_rstest/src/mock_module_id_dependency.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use rspack_cacheable::{cacheable, cacheable_dyn};
use rspack_core::{
AsContextDependency, ConditionalInitFragment, Dependency, DependencyCategory,
DependencyCodeGeneration, DependencyId, DependencyRange, DependencyTemplate,
DependencyTemplateType, DependencyType, ExtendedReferencedExport, FactorizeInfo, InitFragmentKey,
InitFragmentStage, ModuleDependency, ModuleGraph, ModuleGraphCacheArtifact, RuntimeCondition,
AsContextDependency, Dependency, DependencyCategory, DependencyCodeGeneration, DependencyId,
DependencyRange, DependencyTemplate, DependencyTemplateType, DependencyType,
ExtendedReferencedExport, FactorizeInfo, ModuleDependency, ModuleGraph, ModuleGraphCacheArtifact,
RuntimeSpec, TemplateContext, TemplateReplaceSource,
};

Expand All @@ -20,8 +19,6 @@ pub struct MockModuleIdDependency {
factorize_info: FactorizeInfo,
category: DependencyCategory,
pub suffix: Option<String>,
hoist: bool,
await_factory: bool,
}

#[allow(clippy::too_many_arguments)]
Expand All @@ -33,8 +30,6 @@ impl MockModuleIdDependency {
optional: bool,
category: DependencyCategory,
suffix: Option<String>,
hoist: bool,
async_factory: bool,
) -> Self {
Self {
range,
Expand All @@ -45,8 +40,6 @@ impl MockModuleIdDependency {
factorize_info: Default::default(),
category,
suffix,
hoist,
await_factory: async_factory,
}
}
}
Expand Down Expand Up @@ -136,8 +129,6 @@ impl DependencyTemplate for MockModuleIdDependencyTemplate {
source: &mut TemplateReplaceSource,
code_generatable_context: &mut TemplateContext,
) {
let TemplateContext { init_fragments, .. } = code_generatable_context;

let dep = dep
.as_any()
.downcast_ref::<MockModuleIdDependency>()
Expand All @@ -150,18 +141,6 @@ impl DependencyTemplate for MockModuleIdDependencyTemplate {
dep.weak,
);

if dep.hoist && dep.await_factory {
// Await exec init fragment.
init_fragments.push(Box::new(ConditionalInitFragment::new(
format!("await __webpack_require__.rstest_exec({module_id})\n"),
InitFragmentStage::StageAsyncESMImports,
i32::MAX - 1,
InitFragmentKey::ESMImport(format!("{}_{}", module_id, "mock")),
None,
RuntimeCondition::Boolean(true),
)));
}

source.replace(
dep.range.start,
dep.range.end,
Expand Down
Loading
Loading