From 4b17353970a95b680c2c1cad3349b5b0e1f32b9c Mon Sep 17 00:00:00 2001 From: Cong-Cong Date: Sat, 11 Jan 2025 15:11:40 +0800 Subject: [PATCH] chore: merge rspack_binding_values to node_binding --- Cargo.lock | 136 ++-- Cargo.toml | 1 - crates/node_binding/Cargo.toml | 65 +- .../src/asset.rs | 0 .../src/asset_condition.rs | 0 .../src/chunk.rs | 0 .../src/chunk_graph.rs | 27 +- .../src/chunk_group.rs | 7 +- .../src/clean_options.rs | 0 .../src/codegen_result.rs | 0 .../src/compilation/dependencies.rs | 0 .../src/compilation/entries.rs | 0 .../src/compilation/mod.rs | 35 +- .../src/context_module_factory.rs | 0 .../src/dependency.rs | 0 .../src/dependency_block.rs | 0 .../src/error.rs} | 0 .../src/exports_info.rs | 0 .../src/filename.rs | 0 .../src/html.rs | 0 .../src/identifier.rs | 0 crates/node_binding/src/lib.rs | 66 +- .../src/module.rs | 22 +- .../src/module_graph.rs | 42 +- .../src/module_graph_connection.rs | 27 +- .../src/normal_module_factory.rs | 0 .../src/options/entry.rs | 0 .../src/options/library.rs | 0 .../src/options/mod.rs | 0 .../src/options/raw_resolve.rs | 0 .../src/path_data.rs | 0 .../src/plugins/buildtime_plugins.rs | 0 .../src/plugins/context_replacement.rs | 0 .../node_binding/src/plugins/interceptor.rs | 71 +- .../src/plugins/js_hooks_plugin.rs | 642 +++++++++++++++ .../src/plugins/js_loader/context.rs | 0 .../src/plugins/js_loader/mod.rs | 0 .../src/plugins/js_loader/resolver.rs | 0 .../src/plugins/js_loader/scheduler.rs | 0 crates/node_binding/src/plugins/mod.rs | 769 +----------------- .../src/raw_options/mod.rs | 0 .../src/raw_options/raw_builtins/mod.rs | 0 .../raw_options/raw_builtins/raw_banner.rs | 0 .../raw_builtins/raw_bundle_info.rs | 0 .../src/raw_options/raw_builtins/raw_copy.rs | 0 .../raw_builtins/raw_css_extract.rs | 0 .../src/raw_options/raw_builtins/raw_dll.rs | 0 .../src/raw_options/raw_builtins/raw_html.rs | 0 .../src/raw_options/raw_builtins/raw_ids.rs | 0 .../raw_options/raw_builtins/raw_ignore.rs | 0 .../raw_builtins/raw_lazy_compilation.rs | 11 +- .../raw_lightning_css_minimizer.rs | 0 .../raw_builtins/raw_limit_chunk_count.rs | 0 .../src/raw_options/raw_builtins/raw_mf.rs | 0 .../raw_options/raw_builtins/raw_progress.rs | 0 .../raw_builtins/raw_runtime_chunk.rs | 0 .../raw_builtins/raw_size_limits.rs | 0 .../raw_builtins/raw_swc_js_minimizer.rs | 0 .../src/raw_options/raw_cache.rs | 0 .../src/raw_options/raw_devtool.rs | 0 .../src/raw_options/raw_dynamic_entry.rs | 0 .../src/raw_options/raw_experiments/mod.rs | 0 .../raw_experiments/raw_cache/mod.rs | 0 .../raw_experiments/raw_cache/raw_snapshot.rs | 0 .../raw_experiments/raw_cache/raw_storage.rs | 0 .../raw_experiments/raw_incremental.rs | 0 .../raw_experiments/raw_rspack_future.rs | 0 .../src/raw_options/raw_external.rs | 0 .../src/raw_options/raw_mode.rs | 0 .../src/raw_options/raw_module/mod.rs | 1 + .../src/raw_options/raw_node.rs | 0 .../src/raw_options/raw_optimization.rs | 0 .../src/raw_options/raw_output.rs | 0 .../src/raw_options/raw_split_chunks/mod.rs | 0 .../raw_split_chunk_cache_group_test.rs | 1 + .../raw_split_chunk_chunks.rs | 0 .../raw_split_chunks/raw_split_chunk_name.rs | 1 + .../raw_split_chunks/raw_split_chunk_size.rs | 0 .../src/raw_options/raw_stats.rs | 0 .../src/resolver.rs | 0 .../src/resource_data.rs | 0 .../src/rsdoctor.rs | 0 crates/node_binding/src/rspack_error.rs | 80 ++ .../src/runtime.rs | 0 .../src/source.rs | 0 .../src/stats.rs | 0 .../src/utils.rs | 0 crates/rspack_binding_values/Cargo.toml | 93 --- crates/rspack_binding_values/LICENSE | 22 - crates/rspack_binding_values/build.rs | 11 - crates/rspack_binding_values/src/lib.rs | 66 -- .../rspack_binding_values/src/plugins/mod.rs | 6 - .../rspack_core/src/compiler/compilation.rs | 14 +- crates/rspack_core/src/compiler/hmr.rs | 1 + .../src/compiler/make/repair/factorize.rs | 8 +- .../src/compiler/make/repair/mod.rs | 8 +- .../make/repair/process_dependencies.rs | 1 + crates/rspack_core/src/compiler/mod.rs | 30 + .../src/compiler/module_executor/entry.rs | 1 + crates/rspack_core/src/module_factory.rs | 5 +- .../src/factory.rs | 1 + .../src/plugin.rs | 33 +- crates/rspack_tracing/src/lib.rs | 93 ++- 103 files changed, 1271 insertions(+), 1126 deletions(-) rename crates/{rspack_binding_values => node_binding}/src/asset.rs (100%) rename crates/{rspack_binding_values => node_binding}/src/asset_condition.rs (100%) rename crates/{rspack_binding_values => node_binding}/src/chunk.rs (100%) rename crates/{rspack_binding_values => node_binding}/src/chunk_graph.rs (88%) rename crates/{rspack_binding_values => node_binding}/src/chunk_group.rs (97%) rename crates/{rspack_binding_values => node_binding}/src/clean_options.rs (100%) rename crates/{rspack_binding_values => node_binding}/src/codegen_result.rs (100%) rename crates/{rspack_binding_values => node_binding}/src/compilation/dependencies.rs (100%) rename crates/{rspack_binding_values => node_binding}/src/compilation/entries.rs (100%) rename crates/{rspack_binding_values => node_binding}/src/compilation/mod.rs (95%) rename crates/{rspack_binding_values => node_binding}/src/context_module_factory.rs (100%) rename crates/{rspack_binding_values => node_binding}/src/dependency.rs (100%) rename crates/{rspack_binding_values => node_binding}/src/dependency_block.rs (100%) rename crates/{rspack_binding_values/src/rspack_error.rs => node_binding/src/error.rs} (100%) rename crates/{rspack_binding_values => node_binding}/src/exports_info.rs (100%) rename crates/{rspack_binding_values => node_binding}/src/filename.rs (100%) rename crates/{rspack_binding_values => node_binding}/src/html.rs (100%) rename crates/{rspack_binding_values => node_binding}/src/identifier.rs (100%) rename crates/{rspack_binding_values => node_binding}/src/module.rs (96%) rename crates/{rspack_binding_values => node_binding}/src/module_graph.rs (86%) rename crates/{rspack_binding_values => node_binding}/src/module_graph_connection.rs (90%) rename crates/{rspack_binding_values => node_binding}/src/normal_module_factory.rs (100%) rename crates/{rspack_binding_values => node_binding}/src/options/entry.rs (100%) rename crates/{rspack_binding_values => node_binding}/src/options/library.rs (100%) rename crates/{rspack_binding_values => node_binding}/src/options/mod.rs (100%) rename crates/{rspack_binding_values => node_binding}/src/options/raw_resolve.rs (100%) rename crates/{rspack_binding_values => node_binding}/src/path_data.rs (100%) rename crates/{rspack_binding_values => node_binding}/src/plugins/buildtime_plugins.rs (100%) rename crates/{rspack_binding_values => node_binding}/src/plugins/context_replacement.rs (100%) create mode 100644 crates/node_binding/src/plugins/js_hooks_plugin.rs rename crates/{rspack_binding_values => node_binding}/src/plugins/js_loader/context.rs (100%) rename crates/{rspack_binding_values => node_binding}/src/plugins/js_loader/mod.rs (100%) rename crates/{rspack_binding_values => node_binding}/src/plugins/js_loader/resolver.rs (100%) rename crates/{rspack_binding_values => node_binding}/src/plugins/js_loader/scheduler.rs (100%) rename crates/{rspack_binding_values => node_binding}/src/raw_options/mod.rs (100%) rename crates/{rspack_binding_values => node_binding}/src/raw_options/raw_builtins/mod.rs (100%) rename crates/{rspack_binding_values => node_binding}/src/raw_options/raw_builtins/raw_banner.rs (100%) rename crates/{rspack_binding_values => node_binding}/src/raw_options/raw_builtins/raw_bundle_info.rs (100%) rename crates/{rspack_binding_values => node_binding}/src/raw_options/raw_builtins/raw_copy.rs (100%) rename crates/{rspack_binding_values => node_binding}/src/raw_options/raw_builtins/raw_css_extract.rs (100%) rename crates/{rspack_binding_values => node_binding}/src/raw_options/raw_builtins/raw_dll.rs (100%) rename crates/{rspack_binding_values => node_binding}/src/raw_options/raw_builtins/raw_html.rs (100%) rename crates/{rspack_binding_values => node_binding}/src/raw_options/raw_builtins/raw_ids.rs (100%) rename crates/{rspack_binding_values => node_binding}/src/raw_options/raw_builtins/raw_ignore.rs (100%) rename crates/{rspack_binding_values => node_binding}/src/raw_options/raw_builtins/raw_lazy_compilation.rs (91%) rename crates/{rspack_binding_values => node_binding}/src/raw_options/raw_builtins/raw_lightning_css_minimizer.rs (100%) rename crates/{rspack_binding_values => node_binding}/src/raw_options/raw_builtins/raw_limit_chunk_count.rs (100%) rename crates/{rspack_binding_values => node_binding}/src/raw_options/raw_builtins/raw_mf.rs (100%) rename crates/{rspack_binding_values => node_binding}/src/raw_options/raw_builtins/raw_progress.rs (100%) rename crates/{rspack_binding_values => node_binding}/src/raw_options/raw_builtins/raw_runtime_chunk.rs (100%) rename crates/{rspack_binding_values => node_binding}/src/raw_options/raw_builtins/raw_size_limits.rs (100%) rename crates/{rspack_binding_values => node_binding}/src/raw_options/raw_builtins/raw_swc_js_minimizer.rs (100%) rename crates/{rspack_binding_values => node_binding}/src/raw_options/raw_cache.rs (100%) rename crates/{rspack_binding_values => node_binding}/src/raw_options/raw_devtool.rs (100%) rename crates/{rspack_binding_values => node_binding}/src/raw_options/raw_dynamic_entry.rs (100%) rename crates/{rspack_binding_values => node_binding}/src/raw_options/raw_experiments/mod.rs (100%) rename crates/{rspack_binding_values => node_binding}/src/raw_options/raw_experiments/raw_cache/mod.rs (100%) rename crates/{rspack_binding_values => node_binding}/src/raw_options/raw_experiments/raw_cache/raw_snapshot.rs (100%) rename crates/{rspack_binding_values => node_binding}/src/raw_options/raw_experiments/raw_cache/raw_storage.rs (100%) rename crates/{rspack_binding_values => node_binding}/src/raw_options/raw_experiments/raw_incremental.rs (100%) rename crates/{rspack_binding_values => node_binding}/src/raw_options/raw_experiments/raw_rspack_future.rs (100%) rename crates/{rspack_binding_values => node_binding}/src/raw_options/raw_external.rs (100%) rename crates/{rspack_binding_values => node_binding}/src/raw_options/raw_mode.rs (100%) rename crates/{rspack_binding_values => node_binding}/src/raw_options/raw_module/mod.rs (99%) rename crates/{rspack_binding_values => node_binding}/src/raw_options/raw_node.rs (100%) rename crates/{rspack_binding_values => node_binding}/src/raw_options/raw_optimization.rs (100%) rename crates/{rspack_binding_values => node_binding}/src/raw_options/raw_output.rs (100%) rename crates/{rspack_binding_values => node_binding}/src/raw_options/raw_split_chunks/mod.rs (100%) rename crates/{rspack_binding_values => node_binding}/src/raw_options/raw_split_chunks/raw_split_chunk_cache_group_test.rs (96%) rename crates/{rspack_binding_values => node_binding}/src/raw_options/raw_split_chunks/raw_split_chunk_chunks.rs (100%) rename crates/{rspack_binding_values => node_binding}/src/raw_options/raw_split_chunks/raw_split_chunk_name.rs (97%) rename crates/{rspack_binding_values => node_binding}/src/raw_options/raw_split_chunks/raw_split_chunk_size.rs (100%) rename crates/{rspack_binding_values => node_binding}/src/raw_options/raw_stats.rs (100%) rename crates/{rspack_binding_values => node_binding}/src/resolver.rs (100%) rename crates/{rspack_binding_values => node_binding}/src/resource_data.rs (100%) rename crates/{rspack_binding_values => node_binding}/src/rsdoctor.rs (100%) create mode 100644 crates/node_binding/src/rspack_error.rs rename crates/{rspack_binding_values => node_binding}/src/runtime.rs (100%) rename crates/{rspack_binding_values => node_binding}/src/source.rs (100%) rename crates/{rspack_binding_values => node_binding}/src/stats.rs (100%) rename crates/{rspack_binding_values => node_binding}/src/utils.rs (100%) delete mode 100644 crates/rspack_binding_values/Cargo.toml delete mode 100644 crates/rspack_binding_values/LICENSE delete mode 100644 crates/rspack_binding_values/build.rs delete mode 100644 crates/rspack_binding_values/src/lib.rs delete mode 100644 crates/rspack_binding_values/src/plugins/mod.rs diff --git a/Cargo.lock b/Cargo.lock index e3e19b60bb3f..f74de85c1ceb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4217,85 +4217,6 @@ dependencies = [ "tokio", ] -[[package]] -name = "rspack_binding_values" -version = "0.2.0" -dependencies = [ - "async-trait", - "cow-utils", - "derive_more 1.0.0", - "futures", - "glob", - "heck 0.5.0", - "napi", - "napi-build", - "napi-derive", - "pollster", - "rspack_cacheable", - "rspack_collections", - "rspack_core", - "rspack_error", - "rspack_hook", - "rspack_ids", - "rspack_loader_lightningcss", - "rspack_loader_preact_refresh", - "rspack_loader_react_refresh", - "rspack_loader_runner", - "rspack_loader_swc", - "rspack_loader_testing", - "rspack_napi", - "rspack_napi_macros", - "rspack_paths", - "rspack_plugin_asset", - "rspack_plugin_banner", - "rspack_plugin_context_replacement", - "rspack_plugin_copy", - "rspack_plugin_css", - "rspack_plugin_devtool", - "rspack_plugin_dll", - "rspack_plugin_dynamic_entry", - "rspack_plugin_ensure_chunk_conditions", - "rspack_plugin_entry", - "rspack_plugin_externals", - "rspack_plugin_extract_css", - "rspack_plugin_hmr", - "rspack_plugin_html", - "rspack_plugin_ignore", - "rspack_plugin_javascript", - "rspack_plugin_json", - "rspack_plugin_lazy_compilation", - "rspack_plugin_library", - "rspack_plugin_lightning_css_minimizer", - "rspack_plugin_limit_chunk_count", - "rspack_plugin_merge_duplicate_chunks", - "rspack_plugin_mf", - "rspack_plugin_no_emit_on_errors", - "rspack_plugin_progress", - "rspack_plugin_real_content_hash", - "rspack_plugin_remove_duplicate_modules", - "rspack_plugin_remove_empty_chunks", - "rspack_plugin_rsdoctor", - "rspack_plugin_runtime", - "rspack_plugin_runtime_chunk", - "rspack_plugin_schemes", - "rspack_plugin_size_limits", - "rspack_plugin_split_chunks", - "rspack_plugin_swc_js_minimizer", - "rspack_plugin_warn_sensitive_module", - "rspack_plugin_wasm", - "rspack_plugin_web_worker_template", - "rspack_plugin_worker", - "rspack_regex", - "rspack_tracing", - "rspack_util", - "rustc-hash 2.1.0", - "serde", - "serde_json", - "swc_core", - "tokio", - "tracing", -] - [[package]] name = "rspack_cacheable" version = "0.2.0" @@ -4671,12 +4592,17 @@ dependencies = [ "async-trait", "color-backtrace", "cow-utils", + "derive_more", + "futures", + "glob", + "heck 0.5.0", "napi", "napi-build", "napi-derive", + "pollster", "ropey", "rspack_allocator", - "rspack_binding_values", + "rspack_cacheable", "rspack_collections", "rspack_core", "rspack_error", @@ -4684,14 +4610,62 @@ dependencies = [ "rspack_fs_node", "rspack_hash", "rspack_hook", + "rspack_ids", + "rspack_loader_lightningcss", + "rspack_loader_preact_refresh", + "rspack_loader_react_refresh", + "rspack_loader_runner", + "rspack_loader_swc", + "rspack_loader_testing", "rspack_napi", + "rspack_napi_macros", "rspack_paths", + "rspack_plugin_asset", + "rspack_plugin_banner", + "rspack_plugin_context_replacement", + "rspack_plugin_copy", + "rspack_plugin_css", + "rspack_plugin_devtool", + "rspack_plugin_dll", + "rspack_plugin_dynamic_entry", + "rspack_plugin_ensure_chunk_conditions", + "rspack_plugin_entry", + "rspack_plugin_externals", + "rspack_plugin_extract_css", + "rspack_plugin_hmr", "rspack_plugin_html", + "rspack_plugin_ignore", "rspack_plugin_javascript", - "rspack_plugin_rsdoctor", + "rspack_plugin_json", + "rspack_plugin_lazy_compilation", + "rspack_plugin_library", + "rspack_plugin_lightning_css_minimizer", + "rspack_plugin_limit_chunk_count", + "rspack_plugin_merge_duplicate_chunks", + "rspack_plugin_mf", + "rspack_plugin_no_emit_on_errors", + "rspack_plugin_progress", + "rspack_plugin_real_content_hash", + "rspack_plugin_remove_duplicate_modules", + "rspack_plugin_remove_empty_chunks", "rspack_plugin_runtime", + "rspack_plugin_runtime_chunk", + "rspack_plugin_schemes", + "rspack_plugin_size_limits", + "rspack_plugin_split_chunks", + "rspack_plugin_swc_js_minimizer", + "rspack_plugin_warn_sensitive_module", + "rspack_plugin_wasm", + "rspack_plugin_web_worker_template", + "rspack_plugin_worker", + "rspack_regex", "rspack_tracing", "rspack_util", + "rustc-hash 2.1.0", + "serde", + "serde_json", + "swc_core", + "tokio", "tracing", "tracing-subscriber", ] diff --git a/Cargo.toml b/Cargo.toml index 1791d82445ca..6691bfc2eb90 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -116,7 +116,6 @@ rspack_allocator = { version = "0.2.0", path = "crates/rsp rspack_ast = { version = "0.2.0", path = "crates/rspack_ast" } rspack_base64 = { version = "0.2.0", path = "crates/rspack_base64" } rspack_binding = { version = "0.2.0", path = "crates/rspack_binding" } -rspack_binding_values = { version = "0.2.0", path = "crates/rspack_binding_values" } rspack_builtin = { version = "0.2.0", path = "crates/rspack_builtin" } rspack_cacheable = { version = "0.2.0", path = "crates/rspack_cacheable" } rspack_collection = { version = "0.2.0", path = "crates/rspack_collection" } diff --git a/crates/node_binding/Cargo.toml b/crates/node_binding/Cargo.toml index 04ede57eb6b5..183b5278d119 100644 --- a/crates/node_binding/Cargo.toml +++ b/crates/node_binding/Cargo.toml @@ -9,14 +9,10 @@ version = "0.2.0" [lib] crate-type = ["cdylib"] -[features] -plugin = ["rspack_binding_values/plugin"] - [dependencies] anyhow = { workspace = true } ropey = { workspace = true } rspack_allocator = { workspace = true } -rspack_binding_values = { workspace = true } rspack_collections = { workspace = true } rspack_core = { workspace = true } rspack_error = { workspace = true } @@ -39,11 +35,70 @@ cow-utils = { workspace = true } tracing = { workspace = true } tracing-subscriber = { workspace = true } -napi = { workspace = true } +napi = { workspace = true, features = ["async", "tokio_rt", "serde-json", "anyhow"] } napi-derive = { workspace = true } color-backtrace = "0.6.1" +derive_more = { workspace = true, features = ["debug"] } +futures = { workspace = true } +glob = { workspace = true } +heck = { workspace = true } +pollster = { workspace = true } +rspack_cacheable = { workspace = true } +rspack_ids = { workspace = true } +rspack_napi_macros = { workspace = true } +rspack_regex = { workspace = true } +rustc-hash = { workspace = true } +serde = { workspace = true } +serde_json = { workspace = true } +swc_core = { workspace = true, default-features = false, features = ["ecma_transforms_react"] } +tokio = { workspace = true, features = ["rt", "rt-multi-thread", "macros", "test-util", "parking_lot"] } + + +rspack_loader_lightningcss = { workspace = true } +rspack_loader_preact_refresh = { workspace = true } +rspack_loader_react_refresh = { workspace = true } +rspack_loader_runner = { workspace = true } +rspack_loader_swc = { workspace = true } +rspack_loader_testing = { workspace = true } +rspack_plugin_asset = { workspace = true } +rspack_plugin_banner = { workspace = true } +rspack_plugin_context_replacement = { workspace = true } +rspack_plugin_copy = { workspace = true } +rspack_plugin_css = { workspace = true } +rspack_plugin_devtool = { workspace = true } +rspack_plugin_dll = { workspace = true } +rspack_plugin_dynamic_entry = { workspace = true } +rspack_plugin_ensure_chunk_conditions = { workspace = true } +rspack_plugin_entry = { workspace = true } +rspack_plugin_externals = { workspace = true } +rspack_plugin_extract_css = { workspace = true } +rspack_plugin_hmr = { workspace = true } +rspack_plugin_ignore = { workspace = true } +rspack_plugin_json = { workspace = true } +rspack_plugin_lazy_compilation = { workspace = true } +rspack_plugin_library = { workspace = true } +rspack_plugin_lightning_css_minimizer = { workspace = true } +rspack_plugin_limit_chunk_count = { workspace = true } +rspack_plugin_merge_duplicate_chunks = { workspace = true } +rspack_plugin_mf = { workspace = true } +rspack_plugin_no_emit_on_errors = { workspace = true } +rspack_plugin_progress = { workspace = true } +rspack_plugin_real_content_hash = { workspace = true } +rspack_plugin_remove_duplicate_modules = { workspace = true } +rspack_plugin_remove_empty_chunks = { workspace = true } +rspack_plugin_runtime = { workspace = true } +rspack_plugin_runtime_chunk = { workspace = true } +rspack_plugin_schemes = { workspace = true } +rspack_plugin_size_limits = { workspace = true } +rspack_plugin_split_chunks = { workspace = true } +rspack_plugin_swc_js_minimizer = { workspace = true } +rspack_plugin_warn_sensitive_module = { workspace = true } +rspack_plugin_wasm = { workspace = true } +rspack_plugin_web_worker_template = { workspace = true } +rspack_plugin_worker = { workspace = true } + [build-dependencies] napi-build = { workspace = true } diff --git a/crates/rspack_binding_values/src/asset.rs b/crates/node_binding/src/asset.rs similarity index 100% rename from crates/rspack_binding_values/src/asset.rs rename to crates/node_binding/src/asset.rs diff --git a/crates/rspack_binding_values/src/asset_condition.rs b/crates/node_binding/src/asset_condition.rs similarity index 100% rename from crates/rspack_binding_values/src/asset_condition.rs rename to crates/node_binding/src/asset_condition.rs diff --git a/crates/rspack_binding_values/src/chunk.rs b/crates/node_binding/src/chunk.rs similarity index 100% rename from crates/rspack_binding_values/src/chunk.rs rename to crates/node_binding/src/chunk.rs diff --git a/crates/rspack_binding_values/src/chunk_graph.rs b/crates/node_binding/src/chunk_graph.rs similarity index 88% rename from crates/rspack_binding_values/src/chunk_graph.rs rename to crates/node_binding/src/chunk_graph.rs index 282fb35a163c..dde8d7290928 100644 --- a/crates/rspack_binding_values/src/chunk_graph.rs +++ b/crates/node_binding/src/chunk_graph.rs @@ -42,7 +42,14 @@ impl JsChunkGraph { Ok( modules .iter() - .map(|module| JsModuleWrapper::new(module.as_ref(), compilation.id(), Some(compilation))) + .map(|module| { + JsModuleWrapper::new( + module.as_ref(), + compilation.id(), + compilation.compiler_id(), + Some(compilation), + ) + }) .collect::>(), ) } @@ -59,7 +66,14 @@ impl JsChunkGraph { modules .iter() .filter_map(|module| module_graph.module_by_identifier(module)) - .map(|module| JsModuleWrapper::new(module.as_ref(), compilation.id(), Some(compilation))) + .map(|module| { + JsModuleWrapper::new( + module.as_ref(), + compilation.id(), + compilation.compiler_id(), + Some(compilation), + ) + }) .collect::>(), ) } @@ -114,7 +128,14 @@ impl JsChunkGraph { SourceType::from(source_type.as_str()), &compilation.get_module_graph(), ) - .map(|module| JsModuleWrapper::new(module, compilation.id(), Some(compilation))) + .map(|module| { + JsModuleWrapper::new( + module, + compilation.id(), + compilation.compiler_id(), + Some(compilation), + ) + }) .collect(), ) } diff --git a/crates/rspack_binding_values/src/chunk_group.rs b/crates/node_binding/src/chunk_group.rs similarity index 97% rename from crates/rspack_binding_values/src/chunk_group.rs rename to crates/node_binding/src/chunk_group.rs index 7650599a7f9a..edb1db2b0f01 100644 --- a/crates/rspack_binding_values/src/chunk_group.rs +++ b/crates/node_binding/src/chunk_group.rs @@ -71,7 +71,12 @@ impl JsChunkGroup { js_origins.push(JsChunkGroupOrigin { module: origin.module.and_then(|module_id| { compilation.module_by_identifier(&module_id).map(|module| { - JsModuleWrapper::new(module.as_ref(), self.compilation_id, Some(compilation)) + JsModuleWrapper::new( + module.as_ref(), + self.compilation_id, + compilation.compiler_id(), + Some(compilation), + ) }) }), request: match &origin.request { diff --git a/crates/rspack_binding_values/src/clean_options.rs b/crates/node_binding/src/clean_options.rs similarity index 100% rename from crates/rspack_binding_values/src/clean_options.rs rename to crates/node_binding/src/clean_options.rs diff --git a/crates/rspack_binding_values/src/codegen_result.rs b/crates/node_binding/src/codegen_result.rs similarity index 100% rename from crates/rspack_binding_values/src/codegen_result.rs rename to crates/node_binding/src/codegen_result.rs diff --git a/crates/rspack_binding_values/src/compilation/dependencies.rs b/crates/node_binding/src/compilation/dependencies.rs similarity index 100% rename from crates/rspack_binding_values/src/compilation/dependencies.rs rename to crates/node_binding/src/compilation/dependencies.rs diff --git a/crates/rspack_binding_values/src/compilation/entries.rs b/crates/node_binding/src/compilation/entries.rs similarity index 100% rename from crates/rspack_binding_values/src/compilation/entries.rs rename to crates/node_binding/src/compilation/entries.rs diff --git a/crates/rspack_binding_values/src/compilation/mod.rs b/crates/node_binding/src/compilation/mod.rs similarity index 95% rename from crates/rspack_binding_values/src/compilation/mod.rs rename to crates/node_binding/src/compilation/mod.rs index 21d22c559476..a83a95389650 100644 --- a/crates/rspack_binding_values/src/compilation/mod.rs +++ b/crates/node_binding/src/compilation/mod.rs @@ -180,7 +180,12 @@ impl JsCompilation { .keys() .filter_map(|module_id| { compilation.module_by_identifier(module_id).map(|module| { - JsModuleWrapper::new(module.as_ref(), compilation.id(), Some(compilation)) + JsModuleWrapper::new( + module.as_ref(), + compilation.id(), + compilation.compiler_id(), + Some(compilation), + ) }) }) .collect::>(), @@ -197,7 +202,12 @@ impl JsCompilation { .iter() .filter_map(|module_id| { compilation.module_by_identifier(module_id).map(|module| { - JsModuleWrapper::new(module.as_ref(), compilation.id(), Some(compilation)) + JsModuleWrapper::new( + module.as_ref(), + compilation.id(), + compilation.compiler_id(), + Some(compilation), + ) }) }) .collect::>(), @@ -577,6 +587,7 @@ impl JsCompilation { callbackify(env, f, async { let compilation_id = compilation.id(); + let compiler_id = compilation.compiler_id(); let mut modules = compilation .rebuild_module( @@ -584,7 +595,9 @@ impl JsCompilation { |modules| { modules .into_iter() - .map(|module| JsModuleWrapper::new(module.as_ref(), compilation_id, None)) + .map(|module| { + JsModuleWrapper::new(module.as_ref(), compilation_id, compiler_id, None) + }) .collect::>() }, ) @@ -743,9 +756,19 @@ impl JsCompilation { match module_graph.module_graph_module_by_dependency_id(&dependency_id) { Some(module) => match module_graph.module_by_identifier(&module.module_identifier) { Some(module) => { - let js_module = - JsModuleWrapper::new(module.as_ref(), compilation.id(), Some(compilation)); - (Either::B(()), Either::B(js_module)) + let dependency = module_graph.dependency_by_id(&dependency_id).unwrap(); + let js_dependency = JsDependencyWrapper::new( + dependency.as_ref(), + compilation.id(), + Some(&compilation), + ); + let js_module = JsModuleWrapper::new( + module.as_ref(), + compilation.id(), + compilation.compiler_id(), + Some(compilation), + ); + Either::B((js_dependency, js_module)) } None => ( Either::A(format!( diff --git a/crates/rspack_binding_values/src/context_module_factory.rs b/crates/node_binding/src/context_module_factory.rs similarity index 100% rename from crates/rspack_binding_values/src/context_module_factory.rs rename to crates/node_binding/src/context_module_factory.rs diff --git a/crates/rspack_binding_values/src/dependency.rs b/crates/node_binding/src/dependency.rs similarity index 100% rename from crates/rspack_binding_values/src/dependency.rs rename to crates/node_binding/src/dependency.rs diff --git a/crates/rspack_binding_values/src/dependency_block.rs b/crates/node_binding/src/dependency_block.rs similarity index 100% rename from crates/rspack_binding_values/src/dependency_block.rs rename to crates/node_binding/src/dependency_block.rs diff --git a/crates/rspack_binding_values/src/rspack_error.rs b/crates/node_binding/src/error.rs similarity index 100% rename from crates/rspack_binding_values/src/rspack_error.rs rename to crates/node_binding/src/error.rs diff --git a/crates/rspack_binding_values/src/exports_info.rs b/crates/node_binding/src/exports_info.rs similarity index 100% rename from crates/rspack_binding_values/src/exports_info.rs rename to crates/node_binding/src/exports_info.rs diff --git a/crates/rspack_binding_values/src/filename.rs b/crates/node_binding/src/filename.rs similarity index 100% rename from crates/rspack_binding_values/src/filename.rs rename to crates/node_binding/src/filename.rs diff --git a/crates/rspack_binding_values/src/html.rs b/crates/node_binding/src/html.rs similarity index 100% rename from crates/rspack_binding_values/src/html.rs rename to crates/node_binding/src/html.rs diff --git a/crates/rspack_binding_values/src/identifier.rs b/crates/node_binding/src/identifier.rs similarity index 100% rename from crates/rspack_binding_values/src/identifier.rs rename to crates/node_binding/src/identifier.rs diff --git a/crates/node_binding/src/lib.rs b/crates/node_binding/src/lib.rs index 4bed566b0709..8ec661156719 100644 --- a/crates/node_binding/src/lib.rs +++ b/crates/node_binding/src/lib.rs @@ -16,20 +16,74 @@ use rspack_fs::IntermediateFileSystem; use rspack_fs_node::{NodeFileSystem, ThreadsafeNodeFS}; use rspack_napi::napi::bindgen_prelude::within_runtime_if_available; +mod asset; +mod asset_condition; +mod chunk; +mod chunk_graph; +mod chunk_group; +mod clean_options; +mod codegen_result; +mod compilation; mod compiler; +mod context_module_factory; +mod dependency; +mod dependency_block; mod diagnostic; +mod error; +mod exports_info; +mod filename; +mod html; +mod identifier; +mod module; +mod module_graph; +mod module_graph_connection; +mod normal_module_factory; +mod options; mod panic; +mod path_data; mod plugins; +mod raw_options; +mod resolver; mod resolver_factory; +mod resource_data; +mod runtime; +mod source; +mod stats; +mod utils; +pub use asset::*; +pub use asset_condition::*; +pub use chunk::*; +pub use chunk_graph::*; +pub use chunk_group::*; +pub use clean_options::*; +pub use codegen_result::*; +pub use compilation::*; +pub use context_module_factory::*; +pub use dependency::*; +pub use dependency_block::*; pub use diagnostic::*; +pub use error::*; +pub use exports_info::*; +pub use filename::*; +pub use html::*; +pub use module::*; +pub use module_graph::*; +pub use module_graph_connection::*; +pub use normal_module_factory::*; +pub use options::*; +pub use path_data::*; +pub use plugins::buildtime_plugins; use plugins::*; +pub use raw_options::*; +pub use resolver::*; use resolver_factory::*; -use rspack_binding_values::*; -use rspack_tracing::{ChromeTracer, OtelTracer, StdoutTracer, Tracer}; -use tracing::Level; -use tracing_subscriber::EnvFilter; -use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt as _, Layer, Registry}; +pub use resource_data::*; +use rspack_tracing::chrome::FlushGuard; +pub use runtime::*; +pub use source::*; +pub use stats::*; +pub use utils::*; #[napi] pub struct Rspack { @@ -86,7 +140,7 @@ impl Rspack { compiler_path, compiler_options, plugins, - rspack_binding_values::buildtime_plugins::buildtime_plugins(), + buildtime_plugins::buildtime_plugins(), Some(Arc::new(NodeFileSystem::new(output_filesystem).map_err( |e| Error::from_reason(format!("Failed to create writable filesystem: {e}",)), )?)), diff --git a/crates/rspack_binding_values/src/module.rs b/crates/node_binding/src/module.rs similarity index 96% rename from crates/rspack_binding_values/src/module.rs rename to crates/node_binding/src/module.rs index dad2c8c956af..2e4a38471a97 100644 --- a/crates/rspack_binding_values/src/module.rs +++ b/crates/node_binding/src/module.rs @@ -4,9 +4,12 @@ use napi::JsString; use napi_derive::napi; use rspack_collections::IdentifierMap; use rspack_core::{ - BuildMeta, BuildMetaDefaultObject, BuildMetaExportsType, Compilation, CompilationAsset, - CompilationId, ExportsArgument, LibIdentOptions, Module, ModuleArgument, ModuleIdentifier, - RuntimeModuleStage, SourceType, + BuildMeta, BuildMetaDefaultObject, BuildMetaExportsType, Compilation, CompilationId, CompilerId, + ExportsArgument, LibIdentOptions, Module, ModuleArgument, ModuleIdentifier, RuntimeModuleStage, + SourceType, +}; +use rspack_napi::{ + napi::bindgen_prelude::*, threadsafe_function::ThreadsafeFunction, OneShotInstanceRef, }; use rspack_napi::{napi::bindgen_prelude::*, threadsafe_function::ThreadsafeFunction, OneShotRef}; use rspack_plugin_runtime::RuntimeModuleFromJs; @@ -35,6 +38,7 @@ pub struct JsModule { pub(crate) identifier: ModuleIdentifier, module: NonNull, compilation_id: CompilationId, + compiler_id: CompilerId, compilation: Option>, } @@ -270,7 +274,12 @@ impl JsModule { compilation .module_by_identifier(&inner_module_info.id) .map(|module| { - JsModuleWrapper::new(module.as_ref(), compilation.id(), Some(compilation)) + JsModuleWrapper::new( + module.as_ref(), + compilation.id(), + compilation.compiler_id(), + Some(compilation), + ) }) }) .collect::>(); @@ -359,6 +368,7 @@ pub struct JsModuleWrapper { identifier: ModuleIdentifier, module: NonNull, compilation_id: CompilationId, + compiler_id: CompilerId, compilation: Option>, } @@ -368,6 +378,7 @@ impl JsModuleWrapper { pub fn new( module: &dyn Module, compilation_id: CompilationId, + compiler_id: CompilerId, compilation: Option<&Compilation>, ) -> Self { #[allow(clippy::not_unsafe_ptr_arg_deref)] @@ -378,6 +389,7 @@ impl JsModuleWrapper { identifier, module: NonNull::new(module as *const dyn Module as *mut dyn Module).unwrap(), compilation_id, + compiler_id, compilation: compilation .map(|c| NonNull::new(c as *const Compilation as *mut Compilation).unwrap()), } @@ -428,6 +440,7 @@ impl ToNapiValue for JsModuleWrapper { identifier: val.identifier, module: val.module, compilation_id: val.compilation_id, + compiler_id: val.compiler_id, compilation: val.compilation, }; let r = entry.insert(OneShotRef::new(env, js_module)?); @@ -447,6 +460,7 @@ impl FromNapiValue for JsModuleWrapper { #[allow(clippy::unwrap_used)] module: instance.module, compilation_id: instance.compilation_id, + compiler_id: instance.compiler_id, compilation: instance.compilation, }) } diff --git a/crates/rspack_binding_values/src/module_graph.rs b/crates/node_binding/src/module_graph.rs similarity index 86% rename from crates/rspack_binding_values/src/module_graph.rs rename to crates/node_binding/src/module_graph.rs index 132b2f2ffea6..34f55ea4196c 100644 --- a/crates/rspack_binding_values/src/module_graph.rs +++ b/crates/node_binding/src/module_graph.rs @@ -36,8 +36,14 @@ impl JsModuleGraph { pub fn get_module(&self, js_dependency: &JsDependency) -> napi::Result> { let (compilation, module_graph) = self.as_ref()?; let module = module_graph.get_module_by_dependency_id(&js_dependency.dependency_id); - let js_module = module - .map(|module| JsModuleWrapper::new(module.as_ref(), compilation.id(), Some(compilation))); + let js_module = module.map(|module| { + JsModuleWrapper::new( + module.as_ref(), + compilation.id(), + compilation.compiler_id(), + Some(compilation), + ) + }); Ok(js_module) } @@ -51,7 +57,14 @@ impl JsModuleGraph { match module_graph.connection_by_dependency_id(&js_dependency.dependency_id) { Some(connection) => module_graph .module_by_identifier(&connection.resolved_module) - .map(|module| JsModuleWrapper::new(module.as_ref(), compilation.id(), Some(compilation))), + .map(|module| { + JsModuleWrapper::new( + module.as_ref(), + compilation.id(), + compilation.compiler_id(), + Some(compilation), + ) + }), None => None, }, ) @@ -93,10 +106,14 @@ impl JsModuleGraph { pub fn get_issuer(&self, module: &JsModule) -> napi::Result> { let (compilation, module_graph) = self.as_ref()?; let issuer = module_graph.get_issuer(&module.identifier); - Ok( - issuer - .map(|module| JsModuleWrapper::new(module.as_ref(), compilation.id(), Some(compilation))), - ) + Ok(issuer.map(|module| { + JsModuleWrapper::new( + module.as_ref(), + compilation.id(), + compilation.compiler_id(), + Some(compilation), + ) + })) } #[napi] @@ -161,9 +178,14 @@ impl JsModuleGraph { let (compilation, module_graph) = self.as_ref()?; Ok( match module_graph.get_parent_module(&js_dependency.dependency_id) { - Some(identifier) => compilation - .module_by_identifier(identifier) - .map(|module| JsModuleWrapper::new(module.as_ref(), compilation.id(), Some(compilation))), + Some(identifier) => compilation.module_by_identifier(identifier).map(|module| { + JsModuleWrapper::new( + module.as_ref(), + compilation.id(), + compilation.compiler_id(), + Some(compilation), + ) + }), None => None, }, ) diff --git a/crates/rspack_binding_values/src/module_graph_connection.rs b/crates/node_binding/src/module_graph_connection.rs similarity index 90% rename from crates/rspack_binding_values/src/module_graph_connection.rs rename to crates/node_binding/src/module_graph_connection.rs index 1be29f758ee1..315e9fa640a8 100644 --- a/crates/rspack_binding_values/src/module_graph_connection.rs +++ b/crates/node_binding/src/module_graph_connection.rs @@ -47,7 +47,14 @@ impl JsModuleGraphConnection { let (compilation, module_graph) = self.as_ref()?; if let Some(connection) = module_graph.connection_by_dependency_id(&self.dependency_id) { let module = module_graph.module_by_identifier(connection.module_identifier()); - Ok(module.map(|m| JsModuleWrapper::new(m.as_ref(), compilation.id(), Some(compilation)))) + Ok(module.map(|m| { + JsModuleWrapper::new( + m.as_ref(), + compilation.id(), + compilation.compiler_id(), + Some(compilation), + ) + })) } else { Err(napi::Error::from_reason(format!( "Unable to access ModuleGraphConnection with id = {:#?} now. The ModuleGraphConnection have been removed on the Rust side.", @@ -61,7 +68,14 @@ impl JsModuleGraphConnection { let (compilation, module_graph) = self.as_ref()?; if let Some(connection) = module_graph.connection_by_dependency_id(&self.dependency_id) { let module = module_graph.module_by_identifier(&connection.resolved_module); - Ok(module.map(|m| JsModuleWrapper::new(m.as_ref(), compilation.id(), Some(compilation)))) + Ok(module.map(|m| { + JsModuleWrapper::new( + m.as_ref(), + compilation.id(), + compilation.compiler_id(), + Some(compilation), + ) + })) } else { Err(napi::Error::from_reason(format!( "Unable to access ModuleGraphConnection with id = {:#?} now. The ModuleGraphConnection have been removed on the Rust side.", @@ -77,7 +91,14 @@ impl JsModuleGraphConnection { Ok(match connection.original_module_identifier { Some(original_module_identifier) => module_graph .module_by_identifier(&original_module_identifier) - .map(|m| JsModuleWrapper::new(m.as_ref(), compilation.id(), Some(compilation))), + .map(|m| { + JsModuleWrapper::new( + m.as_ref(), + compilation.id(), + compilation.compiler_id(), + Some(compilation), + ) + }), None => None, }) } else { diff --git a/crates/rspack_binding_values/src/normal_module_factory.rs b/crates/node_binding/src/normal_module_factory.rs similarity index 100% rename from crates/rspack_binding_values/src/normal_module_factory.rs rename to crates/node_binding/src/normal_module_factory.rs diff --git a/crates/rspack_binding_values/src/options/entry.rs b/crates/node_binding/src/options/entry.rs similarity index 100% rename from crates/rspack_binding_values/src/options/entry.rs rename to crates/node_binding/src/options/entry.rs diff --git a/crates/rspack_binding_values/src/options/library.rs b/crates/node_binding/src/options/library.rs similarity index 100% rename from crates/rspack_binding_values/src/options/library.rs rename to crates/node_binding/src/options/library.rs diff --git a/crates/rspack_binding_values/src/options/mod.rs b/crates/node_binding/src/options/mod.rs similarity index 100% rename from crates/rspack_binding_values/src/options/mod.rs rename to crates/node_binding/src/options/mod.rs diff --git a/crates/rspack_binding_values/src/options/raw_resolve.rs b/crates/node_binding/src/options/raw_resolve.rs similarity index 100% rename from crates/rspack_binding_values/src/options/raw_resolve.rs rename to crates/node_binding/src/options/raw_resolve.rs diff --git a/crates/rspack_binding_values/src/path_data.rs b/crates/node_binding/src/path_data.rs similarity index 100% rename from crates/rspack_binding_values/src/path_data.rs rename to crates/node_binding/src/path_data.rs diff --git a/crates/rspack_binding_values/src/plugins/buildtime_plugins.rs b/crates/node_binding/src/plugins/buildtime_plugins.rs similarity index 100% rename from crates/rspack_binding_values/src/plugins/buildtime_plugins.rs rename to crates/node_binding/src/plugins/buildtime_plugins.rs diff --git a/crates/rspack_binding_values/src/plugins/context_replacement.rs b/crates/node_binding/src/plugins/context_replacement.rs similarity index 100% rename from crates/rspack_binding_values/src/plugins/context_replacement.rs rename to crates/node_binding/src/plugins/context_replacement.rs diff --git a/crates/node_binding/src/plugins/interceptor.rs b/crates/node_binding/src/plugins/interceptor.rs index 6271aa38f3fd..b6c0c95ab0ca 100644 --- a/crates/node_binding/src/plugins/interceptor.rs +++ b/crates/node_binding/src/plugins/interceptor.rs @@ -9,21 +9,6 @@ use napi::{ bindgen_prelude::{Buffer, FromNapiValue, Function, JsValuesTupleIntoVec, Promise, ToNapiValue}, Env, NapiRaw, }; -use rspack_binding_values::{ - JsAdditionalTreeRuntimeRequirementsArg, JsAdditionalTreeRuntimeRequirementsResult, - JsAfterEmitData, JsAfterResolveData, JsAfterResolveOutput, JsAfterTemplateExecutionData, - JsAlterAssetTagGroupsData, JsAlterAssetTagsData, JsAssetEmittedArgs, - JsBeforeAssetTagGenerationData, JsBeforeEmitData, JsBeforeResolveArgs, JsBeforeResolveOutput, - JsChunkAssetArgs, JsChunkWrapper, JsCompilationWrapper, - JsContextModuleFactoryAfterResolveDataWrapper, JsContextModuleFactoryAfterResolveResult, - JsContextModuleFactoryBeforeResolveDataWrapper, JsContextModuleFactoryBeforeResolveResult, - JsCreateData, JsCreateScriptData, JsExecuteModuleArg, JsFactorizeArgs, JsFactorizeOutput, - JsLinkPrefetchData, JsLinkPreloadData, JsModuleWrapper, JsNormalModuleFactoryCreateModuleArgs, - JsResolveArgs, JsResolveForSchemeArgs, JsResolveForSchemeOutput, JsResolveOutput, - JsRsdoctorAssetPatch, JsRsdoctorChunkGraph, JsRsdoctorModuleGraph, JsRsdoctorModuleIdsPatch, - JsRsdoctorModuleSourcesPatch, JsRuntimeGlobals, JsRuntimeModule, JsRuntimeModuleArg, - JsRuntimeRequirementInTreeArg, JsRuntimeRequirementInTreeResult, ToJsCompatSourceOwned, -}; use rspack_collections::IdentifierSet; use rspack_core::{ parse_resource, AfterResolveResult, AssetEmittedInfo, BeforeResolveResult, BoxModule, ChunkUkey, @@ -43,15 +28,16 @@ use rspack_core::{ CompilationStillValidModule, CompilationStillValidModuleHook, CompilationSucceedModule, CompilationSucceedModuleHook, CompilerAfterEmit, CompilerAfterEmitHook, CompilerAssetEmitted, CompilerAssetEmittedHook, CompilerCompilation, CompilerCompilationHook, CompilerEmit, - CompilerEmitHook, CompilerFinishMake, CompilerFinishMakeHook, CompilerMake, CompilerMakeHook, - CompilerShouldEmit, CompilerShouldEmitHook, CompilerThisCompilation, CompilerThisCompilationHook, - ContextModuleFactoryAfterResolve, ContextModuleFactoryAfterResolveHook, - ContextModuleFactoryBeforeResolve, ContextModuleFactoryBeforeResolveHook, ExecuteModuleId, - ModuleFactoryCreateData, ModuleIdentifier, NormalModuleCreateData, - NormalModuleFactoryAfterResolve, NormalModuleFactoryAfterResolveHook, - NormalModuleFactoryBeforeResolve, NormalModuleFactoryBeforeResolveHook, - NormalModuleFactoryCreateModule, NormalModuleFactoryCreateModuleHook, - NormalModuleFactoryFactorize, NormalModuleFactoryFactorizeHook, NormalModuleFactoryResolve, + CompilerEmitHook, CompilerFinishMake, CompilerFinishMakeHook, CompilerId, CompilerMake, + CompilerMakeHook, CompilerShouldEmit, CompilerShouldEmitHook, CompilerThisCompilation, + CompilerThisCompilationHook, ContextModuleFactoryAfterResolve, + ContextModuleFactoryAfterResolveHook, ContextModuleFactoryBeforeResolve, + ContextModuleFactoryBeforeResolveHook, ExecuteModuleId, ModuleFactoryCreateData, + ModuleIdentifier, NormalModuleCreateData, NormalModuleFactoryAfterResolve, + NormalModuleFactoryAfterResolveHook, NormalModuleFactoryBeforeResolve, + NormalModuleFactoryBeforeResolveHook, NormalModuleFactoryCreateModule, + NormalModuleFactoryCreateModuleHook, NormalModuleFactoryFactorize, + NormalModuleFactoryFactorizeHook, NormalModuleFactoryResolve, NormalModuleFactoryResolveForScheme, NormalModuleFactoryResolveForSchemeHook, NormalModuleFactoryResolveHook, NormalModuleFactoryResolveResult, ResourceData, RuntimeGlobals, Scheme, @@ -82,6 +68,20 @@ use rspack_plugin_runtime::{ RuntimePluginLinkPreload, RuntimePluginLinkPreloadHook, }; +use crate::{ + JsAdditionalTreeRuntimeRequirementsArg, JsAdditionalTreeRuntimeRequirementsResult, + JsAfterEmitData, JsAfterResolveData, JsAfterResolveOutput, JsAfterTemplateExecutionData, + JsAlterAssetTagGroupsData, JsAlterAssetTagsData, JsAssetEmittedArgs, + JsBeforeAssetTagGenerationData, JsBeforeEmitData, JsBeforeResolveArgs, JsBeforeResolveOutput, + JsChunkAssetArgs, JsChunkWrapper, JsCompilationWrapper, + JsContextModuleFactoryAfterResolveDataWrapper, JsContextModuleFactoryAfterResolveResult, + JsContextModuleFactoryBeforeResolveDataWrapper, JsContextModuleFactoryBeforeResolveResult, + JsCreateData, JsExecuteModuleArg, JsFactorizeArgs, JsFactorizeOutput, JsModuleWrapper, + JsNormalModuleFactoryCreateModuleArgs, JsResolveArgs, JsResolveForSchemeArgs, + JsResolveForSchemeOutput, JsResolveOutput, JsRuntimeGlobals, JsRuntimeModule, JsRuntimeModuleArg, + JsRuntimeRequirementInTreeArg, JsRuntimeRequirementInTreeResult, ToJsCompatSourceOwned, +}; + #[napi(object)] pub struct JsTap<'f> { pub function: Function<'f>, @@ -1188,12 +1188,18 @@ impl CompilerAssetEmitted for CompilerAssetEmittedTap { impl CompilationBuildModule for CompilationBuildModuleTap { async fn run( &self, + compiler_id: CompilerId, compilation_id: CompilationId, module: &mut BoxModule, ) -> rspack_error::Result<()> { self .function - .call_with_sync(JsModuleWrapper::new(module.as_ref(), compilation_id, None)) + .call_with_sync(JsModuleWrapper::new( + module.as_ref(), + compilation_id, + compiler_id, + None, + )) .await } @@ -1206,12 +1212,18 @@ impl CompilationBuildModule for CompilationBuildModuleTap { impl CompilationStillValidModule for CompilationStillValidModuleTap { async fn run( &self, + compiler_id: CompilerId, compilation_id: CompilationId, module: &mut BoxModule, ) -> rspack_error::Result<()> { self .function - .call_with_sync(JsModuleWrapper::new(module.as_ref(), compilation_id, None)) + .call_with_sync(JsModuleWrapper::new( + module.as_ref(), + compilation_id, + compiler_id, + None, + )) .await } @@ -1229,7 +1241,12 @@ impl CompilationSucceedModule for CompilationSucceedModuleTap { ) -> rspack_error::Result<()> { self .function - .call_with_sync(JsModuleWrapper::new(module.as_ref(), compilation_id, None)) + .call_with_sync(JsModuleWrapper::new( + module.as_ref(), + compilation_id, + compilation.compiler_id(), + None, + )) .await } diff --git a/crates/node_binding/src/plugins/js_hooks_plugin.rs b/crates/node_binding/src/plugins/js_hooks_plugin.rs new file mode 100644 index 000000000000..c91ae4f30a39 --- /dev/null +++ b/crates/node_binding/src/plugins/js_hooks_plugin.rs @@ -0,0 +1,642 @@ +use std::fmt; + +use async_trait::async_trait; +use napi::{Env, Result}; +use rspack_core::Compilation; +use rspack_core::CompilationParams; +use rspack_core::CompilerCompilation; +use rspack_core::{ApplyContext, CompilerOptions, PluginContext}; +use rspack_hook::plugin; +use rspack_hook::plugin_hook; +use rspack_hook::Hook; +use rspack_plugin_html::HtmlRspackPlugin; +use rspack_plugin_javascript::JsPlugin; + +use super::interceptor::*; + +#[plugin] +#[derive(Clone)] +pub struct JsHooksAdapterPlugin { + non_skippable_registers: NonSkippableRegisters, + register_compiler_this_compilation_taps: RegisterCompilerThisCompilationTaps, + register_compiler_compilation_taps: RegisterCompilerCompilationTaps, + register_compiler_make_taps: RegisterCompilerMakeTaps, + register_compiler_finish_make_taps: RegisterCompilerFinishMakeTaps, + register_compiler_should_emit_taps: RegisterCompilerShouldEmitTaps, + register_compiler_emit_taps: RegisterCompilerEmitTaps, + register_compiler_after_emit_taps: RegisterCompilerAfterEmitTaps, + register_compiler_asset_emitted_taps: RegisterCompilerAssetEmittedTaps, + register_compilation_build_module_taps: RegisterCompilationBuildModuleTaps, + register_compilation_still_valid_module_taps: RegisterCompilationStillValidModuleTaps, + register_compilation_succeed_module_taps: RegisterCompilationSucceedModuleTaps, + register_compilation_execute_module_taps: RegisterCompilationExecuteModuleTaps, + register_compilation_finish_modules_taps: RegisterCompilationFinishModulesTaps, + register_compilation_optimize_modules_taps: RegisterCompilationOptimizeModulesTaps, + register_compilation_after_optimize_modules_taps: RegisterCompilationAfterOptimizeModulesTaps, + register_compilation_optimize_tree_taps: RegisterCompilationOptimizeTreeTaps, + register_compilation_optimize_chunk_modules_taps: RegisterCompilationOptimizeChunkModulesTaps, + register_compilation_additional_tree_runtime_requirements: + RegisterCompilationAdditionalTreeRuntimeRequirementsTaps, + register_compilation_runtime_requirement_in_tree: RegisterCompilationRuntimeRequirementInTreeTaps, + register_compilation_runtime_module_taps: RegisterCompilationRuntimeModuleTaps, + register_compilation_chunk_hash_taps: RegisterCompilationChunkHashTaps, + register_compilation_chunk_asset_taps: RegisterCompilationChunkAssetTaps, + register_compilation_process_assets_taps: RegisterCompilationProcessAssetsTaps, + register_compilation_after_process_assets_taps: RegisterCompilationAfterProcessAssetsTaps, + register_compilation_seal_taps: RegisterCompilationSealTaps, + register_compilation_after_seal_taps: RegisterCompilationAfterSealTaps, + register_normal_module_factory_before_resolve_taps: RegisterNormalModuleFactoryBeforeResolveTaps, + register_normal_module_factory_factorize_taps: RegisterNormalModuleFactoryFactorizeTaps, + register_normal_module_factory_resolve_taps: RegisterNormalModuleFactoryResolveTaps, + register_normal_module_factory_resolve_for_scheme_taps: + RegisterNormalModuleFactoryResolveForSchemeTaps, + register_normal_module_factory_after_resolve_taps: RegisterNormalModuleFactoryAfterResolveTaps, + register_normal_module_factory_create_module_taps: RegisterNormalModuleFactoryCreateModuleTaps, + register_context_module_factory_before_resolve_taps: + RegisterContextModuleFactoryBeforeResolveTaps, + register_context_module_factory_after_resolve_taps: RegisterContextModuleFactoryAfterResolveTaps, + register_javascript_modules_chunk_hash_taps: RegisterJavascriptModulesChunkHashTaps, + register_html_plugin_before_asset_tag_generation_taps: + RegisterHtmlPluginBeforeAssetTagGenerationTaps, + register_html_plugin_alter_asset_tags_taps: RegisterHtmlPluginAlterAssetTagsTaps, + register_html_plugin_alter_asset_tag_groups_taps: RegisterHtmlPluginAlterAssetTagGroupsTaps, + register_html_plugin_after_template_execution_taps: RegisterHtmlPluginAfterTemplateExecutionTaps, + register_html_plugin_before_emit_taps: RegisterHtmlPluginBeforeEmitTaps, + register_html_plugin_after_emit_taps: RegisterHtmlPluginAfterEmitTaps, +} + +impl fmt::Debug for JsHooksAdapterPlugin { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "rspack_plugin_js_hooks_adapter") + } +} + +#[async_trait] +impl rspack_core::Plugin for JsHooksAdapterPlugin { + fn name(&self) -> &'static str { + "rspack.JsHooksAdapterPlugin" + } + + #[tracing::instrument(name = "js_hooks_adapter::apply", skip_all)] + fn apply( + &self, + ctx: PluginContext<&mut ApplyContext>, + _options: &CompilerOptions, + ) -> rspack_error::Result<()> { + ctx + .context + .compiler_hooks + .this_compilation + .intercept(self.register_compiler_this_compilation_taps.clone()); + ctx + .context + .compiler_hooks + .compilation + .intercept(self.register_compiler_compilation_taps.clone()); + ctx + .context + .compiler_hooks + .make + .intercept(self.register_compiler_make_taps.clone()); + ctx + .context + .compiler_hooks + .finish_make + .intercept(self.register_compiler_finish_make_taps.clone()); + ctx + .context + .compiler_hooks + .should_emit + .intercept(self.register_compiler_should_emit_taps.clone()); + ctx + .context + .compiler_hooks + .emit + .intercept(self.register_compiler_emit_taps.clone()); + ctx + .context + .compiler_hooks + .after_emit + .intercept(self.register_compiler_after_emit_taps.clone()); + ctx + .context + .compiler_hooks + .asset_emitted + .intercept(self.register_compiler_asset_emitted_taps.clone()); + ctx + .context + .compilation_hooks + .build_module + .intercept(self.register_compilation_build_module_taps.clone()); + ctx + .context + .compilation_hooks + .still_valid_module + .intercept(self.register_compilation_still_valid_module_taps.clone()); + ctx + .context + .compilation_hooks + .succeed_module + .intercept(self.register_compilation_succeed_module_taps.clone()); + ctx + .context + .compilation_hooks + .execute_module + .intercept(self.register_compilation_execute_module_taps.clone()); + ctx + .context + .compilation_hooks + .finish_modules + .intercept(self.register_compilation_finish_modules_taps.clone()); + ctx + .context + .compilation_hooks + .optimize_modules + .intercept(self.register_compilation_optimize_modules_taps.clone()); + ctx + .context + .compilation_hooks + .after_optimize_modules + .intercept( + self + .register_compilation_after_optimize_modules_taps + .clone(), + ); + ctx + .context + .compilation_hooks + .optimize_tree + .intercept(self.register_compilation_optimize_tree_taps.clone()); + ctx + .context + .compilation_hooks + .optimize_chunk_modules + .intercept( + self + .register_compilation_optimize_chunk_modules_taps + .clone(), + ); + ctx + .context + .compilation_hooks + .additional_tree_runtime_requirements + .intercept( + self + .register_compilation_additional_tree_runtime_requirements + .clone(), + ); + ctx + .context + .compilation_hooks + .runtime_requirement_in_tree + .intercept( + self + .register_compilation_runtime_requirement_in_tree + .clone(), + ); + ctx + .context + .compilation_hooks + .runtime_module + .intercept(self.register_compilation_runtime_module_taps.clone()); + ctx + .context + .compilation_hooks + .chunk_hash + .intercept(self.register_compilation_chunk_hash_taps.clone()); + ctx + .context + .compilation_hooks + .chunk_asset + .intercept(self.register_compilation_chunk_asset_taps.clone()); + ctx + .context + .compilation_hooks + .process_assets + .intercept(self.register_compilation_process_assets_taps.clone()); + ctx + .context + .compilation_hooks + .after_process_assets + .intercept(self.register_compilation_after_process_assets_taps.clone()); + ctx + .context + .compilation_hooks + .seal + .intercept(self.register_compilation_seal_taps.clone()); + ctx + .context + .compilation_hooks + .after_seal + .intercept(self.register_compilation_after_seal_taps.clone()); + + ctx + .context + .normal_module_factory_hooks + .before_resolve + .intercept( + self + .register_normal_module_factory_before_resolve_taps + .clone(), + ); + ctx + .context + .normal_module_factory_hooks + .factorize + .intercept(self.register_normal_module_factory_factorize_taps.clone()); + ctx + .context + .normal_module_factory_hooks + .resolve + .intercept(self.register_normal_module_factory_resolve_taps.clone()); + ctx + .context + .normal_module_factory_hooks + .resolve_for_scheme + .intercept( + self + .register_normal_module_factory_resolve_for_scheme_taps + .clone(), + ); + ctx + .context + .normal_module_factory_hooks + .after_resolve + .intercept( + self + .register_normal_module_factory_after_resolve_taps + .clone(), + ); + ctx + .context + .normal_module_factory_hooks + .create_module + .intercept( + self + .register_normal_module_factory_create_module_taps + .clone(), + ); + ctx + .context + .context_module_factory_hooks + .before_resolve + .intercept( + self + .register_context_module_factory_before_resolve_taps + .clone(), + ); + ctx + .context + .context_module_factory_hooks + .after_resolve + .intercept( + self + .register_context_module_factory_after_resolve_taps + .clone(), + ); + + ctx + .context + .compiler_hooks + .compilation + .tap(js_hooks_adapter_compilation::new(self)); + + ctx + .context + .compiler_hooks + .compilation + .tap(html_hooks_adapter_compilation::new(self)); + + Ok(()) + } + + fn clear_cache(&self) { + self.register_compiler_this_compilation_taps.clear_cache(); + self.register_compiler_compilation_taps.clear_cache(); + self.register_compiler_make_taps.clear_cache(); + self.register_compiler_finish_make_taps.clear_cache(); + self.register_compiler_should_emit_taps.clear_cache(); + self.register_compiler_emit_taps.clear_cache(); + self.register_compiler_after_emit_taps.clear_cache(); + self.register_compiler_asset_emitted_taps.clear_cache(); + self.register_compilation_build_module_taps.clear_cache(); + self + .register_compilation_still_valid_module_taps + .clear_cache(); + self.register_compilation_succeed_module_taps.clear_cache(); + self.register_compilation_execute_module_taps.clear_cache(); + self.register_compilation_finish_modules_taps.clear_cache(); + self + .register_compilation_optimize_modules_taps + .clear_cache(); + self + .register_compilation_after_optimize_modules_taps + .clear_cache(); + self.register_compilation_optimize_tree_taps.clear_cache(); + self + .register_compilation_optimize_chunk_modules_taps + .clear_cache(); + self + .register_compilation_additional_tree_runtime_requirements + .clear_cache(); + self + .register_compilation_runtime_requirement_in_tree + .clear_cache(); + self.register_compilation_runtime_module_taps.clear_cache(); + self.register_compilation_chunk_hash_taps.clear_cache(); + self.register_compilation_chunk_asset_taps.clear_cache(); + self.register_compilation_process_assets_taps.clear_cache(); + self + .register_compilation_after_process_assets_taps + .clear_cache(); + self.register_compilation_seal_taps.clear_cache(); + self.register_compilation_after_seal_taps.clear_cache(); + self + .register_normal_module_factory_before_resolve_taps + .clear_cache(); + self + .register_normal_module_factory_factorize_taps + .clear_cache(); + self + .register_normal_module_factory_resolve_taps + .clear_cache(); + self + .register_normal_module_factory_resolve_for_scheme_taps + .clear_cache(); + self + .register_normal_module_factory_after_resolve_taps + .clear_cache(); + self + .register_normal_module_factory_create_module_taps + .clear_cache(); + self + .register_context_module_factory_before_resolve_taps + .clear_cache(); + self + .register_context_module_factory_after_resolve_taps + .clear_cache(); + self + .register_javascript_modules_chunk_hash_taps + .clear_cache(); + self + .register_html_plugin_before_asset_tag_generation_taps + .clear_cache(); + self + .register_html_plugin_alter_asset_tags_taps + .clear_cache(); + self + .register_html_plugin_alter_asset_tag_groups_taps + .clear_cache(); + self + .register_html_plugin_after_template_execution_taps + .clear_cache(); + self.register_html_plugin_before_emit_taps.clear_cache(); + self.register_html_plugin_after_emit_taps.clear_cache(); + } +} + +#[plugin_hook(CompilerCompilation for JsHooksAdapterPlugin)] +async fn js_hooks_adapter_compilation( + &self, + compilation: &mut Compilation, + _params: &mut CompilationParams, +) -> rspack_error::Result<()> { + let mut hooks = JsPlugin::get_compilation_hooks_mut(compilation); + hooks + .chunk_hash + .intercept(self.register_javascript_modules_chunk_hash_taps.clone()); + + Ok(()) +} + +#[plugin_hook(CompilerCompilation for JsHooksAdapterPlugin)] +async fn html_hooks_adapter_compilation( + &self, + compilation: &mut Compilation, + _params: &mut CompilationParams, +) -> rspack_error::Result<()> { + let mut hooks = HtmlRspackPlugin::get_compilation_hooks_mut(compilation); + hooks.before_asset_tag_generation.intercept( + self + .register_html_plugin_before_asset_tag_generation_taps + .clone(), + ); + hooks + .alter_asset_tags + .intercept(self.register_html_plugin_alter_asset_tags_taps.clone()); + hooks.alter_asset_tag_groups.intercept( + self + .register_html_plugin_alter_asset_tag_groups_taps + .clone(), + ); + hooks.after_template_execution.intercept( + self + .register_html_plugin_after_template_execution_taps + .clone(), + ); + hooks + .before_emit + .intercept(self.register_html_plugin_before_emit_taps.clone()); + hooks + .after_emit + .intercept(self.register_html_plugin_after_emit_taps.clone()); + + Ok(()) +} + +impl JsHooksAdapterPlugin { + pub fn from_js_hooks(_env: Env, register_js_taps: RegisterJsTaps) -> Result { + let non_skippable_registers = NonSkippableRegisters::default(); + Ok(JsHooksAdapterPlugin { + inner: JsHooksAdapterPluginInner { + register_compiler_this_compilation_taps: RegisterCompilerThisCompilationTaps::new( + register_js_taps.register_compiler_this_compilation_taps, + non_skippable_registers.clone(), + ), + register_compiler_compilation_taps: RegisterCompilerCompilationTaps::new( + register_js_taps.register_compiler_compilation_taps, + non_skippable_registers.clone(), + ), + register_compiler_make_taps: RegisterCompilerMakeTaps::new( + register_js_taps.register_compiler_make_taps, + non_skippable_registers.clone(), + ), + register_compiler_finish_make_taps: RegisterCompilerFinishMakeTaps::new( + register_js_taps.register_compiler_finish_make_taps, + non_skippable_registers.clone(), + ), + register_compiler_should_emit_taps: RegisterCompilerShouldEmitTaps::new( + register_js_taps.register_compiler_should_emit_taps, + non_skippable_registers.clone(), + ), + register_compiler_emit_taps: RegisterCompilerEmitTaps::new( + register_js_taps.register_compiler_emit_taps, + non_skippable_registers.clone(), + ), + register_compiler_after_emit_taps: RegisterCompilerAfterEmitTaps::new( + register_js_taps.register_compiler_after_emit_taps, + non_skippable_registers.clone(), + ), + register_compiler_asset_emitted_taps: RegisterCompilerAssetEmittedTaps::new( + register_js_taps.register_compiler_asset_emitted_taps, + non_skippable_registers.clone(), + ), + register_compilation_build_module_taps: RegisterCompilationBuildModuleTaps::new( + register_js_taps.register_compilation_build_module_taps, + non_skippable_registers.clone(), + ), + register_compilation_still_valid_module_taps: RegisterCompilationStillValidModuleTaps::new( + register_js_taps.register_compilation_still_valid_module_taps, + non_skippable_registers.clone(), + ), + register_compilation_succeed_module_taps: RegisterCompilationSucceedModuleTaps::new( + register_js_taps.register_compilation_succeed_module_taps, + non_skippable_registers.clone(), + ), + register_compilation_execute_module_taps: RegisterCompilationExecuteModuleTaps::new( + register_js_taps.register_compilation_execute_module_taps, + non_skippable_registers.clone(), + ), + register_compilation_finish_modules_taps: RegisterCompilationFinishModulesTaps::new( + register_js_taps.register_compilation_finish_modules_taps, + non_skippable_registers.clone(), + ), + register_compilation_optimize_modules_taps: RegisterCompilationOptimizeModulesTaps::new( + register_js_taps.register_compilation_optimize_modules_taps, + non_skippable_registers.clone(), + ), + register_compilation_after_optimize_modules_taps: + RegisterCompilationAfterOptimizeModulesTaps::new( + register_js_taps.register_compilation_after_optimize_modules_taps, + non_skippable_registers.clone(), + ), + register_compilation_optimize_tree_taps: RegisterCompilationOptimizeTreeTaps::new( + register_js_taps.register_compilation_optimize_tree_taps, + non_skippable_registers.clone(), + ), + register_compilation_optimize_chunk_modules_taps: + RegisterCompilationOptimizeChunkModulesTaps::new( + register_js_taps.register_compilation_optimize_chunk_modules_taps, + non_skippable_registers.clone(), + ), + register_compilation_additional_tree_runtime_requirements: + RegisterCompilationAdditionalTreeRuntimeRequirementsTaps::new( + register_js_taps.register_compilation_additional_tree_runtime_requirements, + non_skippable_registers.clone(), + ), + register_compilation_runtime_requirement_in_tree: + RegisterCompilationRuntimeRequirementInTreeTaps::new( + register_js_taps.register_compilation_runtime_requirement_in_tree, + non_skippable_registers.clone(), + ), + register_compilation_runtime_module_taps: RegisterCompilationRuntimeModuleTaps::new( + register_js_taps.register_compilation_runtime_module_taps, + non_skippable_registers.clone(), + ), + register_compilation_chunk_hash_taps: RegisterCompilationChunkHashTaps::new( + register_js_taps.register_compilation_chunk_hash_taps, + non_skippable_registers.clone(), + ), + register_compilation_chunk_asset_taps: RegisterCompilationChunkAssetTaps::new( + register_js_taps.register_compilation_chunk_asset_taps, + non_skippable_registers.clone(), + ), + register_compilation_process_assets_taps: RegisterCompilationProcessAssetsTaps::new( + register_js_taps.register_compilation_process_assets_taps, + non_skippable_registers.clone(), + ), + register_compilation_after_process_assets_taps: + RegisterCompilationAfterProcessAssetsTaps::new( + register_js_taps.register_compilation_after_process_assets_taps, + non_skippable_registers.clone(), + ), + register_compilation_seal_taps: RegisterCompilationSealTaps::new( + register_js_taps.register_compilation_seal_taps, + non_skippable_registers.clone(), + ), + register_compilation_after_seal_taps: RegisterCompilationAfterSealTaps::new( + register_js_taps.register_compilation_after_seal_taps, + non_skippable_registers.clone(), + ), + register_normal_module_factory_before_resolve_taps: + RegisterNormalModuleFactoryBeforeResolveTaps::new( + register_js_taps.register_normal_module_factory_before_resolve_taps, + non_skippable_registers.clone(), + ), + register_normal_module_factory_factorize_taps: + RegisterNormalModuleFactoryFactorizeTaps::new( + register_js_taps.register_normal_module_factory_factorize_taps, + non_skippable_registers.clone(), + ), + register_normal_module_factory_resolve_taps: RegisterNormalModuleFactoryResolveTaps::new( + register_js_taps.register_normal_module_factory_resolve_taps, + non_skippable_registers.clone(), + ), + register_normal_module_factory_resolve_for_scheme_taps: + RegisterNormalModuleFactoryResolveForSchemeTaps::new( + register_js_taps.register_normal_module_factory_resolve_for_scheme_taps, + non_skippable_registers.clone(), + ), + register_normal_module_factory_after_resolve_taps: + RegisterNormalModuleFactoryAfterResolveTaps::new( + register_js_taps.register_normal_module_factory_after_resolve_taps, + non_skippable_registers.clone(), + ), + register_normal_module_factory_create_module_taps: + RegisterNormalModuleFactoryCreateModuleTaps::new( + register_js_taps.register_normal_module_factory_create_module_taps, + non_skippable_registers.clone(), + ), + register_context_module_factory_before_resolve_taps: + RegisterContextModuleFactoryBeforeResolveTaps::new( + register_js_taps.register_context_module_factory_before_resolve_taps, + non_skippable_registers.clone(), + ), + register_context_module_factory_after_resolve_taps: + RegisterContextModuleFactoryAfterResolveTaps::new( + register_js_taps.register_context_module_factory_after_resolve_taps, + non_skippable_registers.clone(), + ), + register_javascript_modules_chunk_hash_taps: RegisterJavascriptModulesChunkHashTaps::new( + register_js_taps.register_javascript_modules_chunk_hash_taps, + non_skippable_registers.clone(), + ), + register_html_plugin_before_asset_tag_generation_taps: + RegisterHtmlPluginBeforeAssetTagGenerationTaps::new( + register_js_taps.register_html_plugin_before_asset_tag_generation_taps, + non_skippable_registers.clone(), + ), + register_html_plugin_alter_asset_tags_taps: RegisterHtmlPluginAlterAssetTagsTaps::new( + register_js_taps.register_html_plugin_alter_asset_tags_taps, + non_skippable_registers.clone(), + ), + register_html_plugin_alter_asset_tag_groups_taps: + RegisterHtmlPluginAlterAssetTagGroupsTaps::new( + register_js_taps.register_html_plugin_alter_asset_tag_groups_taps, + non_skippable_registers.clone(), + ), + register_html_plugin_after_template_execution_taps: + RegisterHtmlPluginAfterTemplateExecutionTaps::new( + register_js_taps.register_html_plugin_after_template_execution_taps, + non_skippable_registers.clone(), + ), + register_html_plugin_before_emit_taps: RegisterHtmlPluginBeforeEmitTaps::new( + register_js_taps.register_html_plugin_before_emit_taps, + non_skippable_registers.clone(), + ), + register_html_plugin_after_emit_taps: RegisterHtmlPluginAfterEmitTaps::new( + register_js_taps.register_html_plugin_after_emit_taps, + non_skippable_registers.clone(), + ), + non_skippable_registers, + } + .into(), + }) + } + + pub fn set_non_skippable_registers(&self, kinds: Vec) { + self + .non_skippable_registers + .set_non_skippable_registers(kinds); + } +} diff --git a/crates/rspack_binding_values/src/plugins/js_loader/context.rs b/crates/node_binding/src/plugins/js_loader/context.rs similarity index 100% rename from crates/rspack_binding_values/src/plugins/js_loader/context.rs rename to crates/node_binding/src/plugins/js_loader/context.rs diff --git a/crates/rspack_binding_values/src/plugins/js_loader/mod.rs b/crates/node_binding/src/plugins/js_loader/mod.rs similarity index 100% rename from crates/rspack_binding_values/src/plugins/js_loader/mod.rs rename to crates/node_binding/src/plugins/js_loader/mod.rs diff --git a/crates/rspack_binding_values/src/plugins/js_loader/resolver.rs b/crates/node_binding/src/plugins/js_loader/resolver.rs similarity index 100% rename from crates/rspack_binding_values/src/plugins/js_loader/resolver.rs rename to crates/node_binding/src/plugins/js_loader/resolver.rs diff --git a/crates/rspack_binding_values/src/plugins/js_loader/scheduler.rs b/crates/node_binding/src/plugins/js_loader/scheduler.rs similarity index 100% rename from crates/rspack_binding_values/src/plugins/js_loader/scheduler.rs rename to crates/node_binding/src/plugins/js_loader/scheduler.rs diff --git a/crates/node_binding/src/plugins/mod.rs b/crates/node_binding/src/plugins/mod.rs index de417d9eded6..ea24604ef71a 100644 --- a/crates/node_binding/src/plugins/mod.rs +++ b/crates/node_binding/src/plugins/mod.rs @@ -1,761 +1,10 @@ +mod context_replacement; mod interceptor; -use std::fmt; - -use async_trait::async_trait; -pub use interceptor::RegisterJsTapKind; -pub use interceptor::RegisterJsTaps; -use napi::{Env, Result}; -use rspack_core::Compilation; -use rspack_core::CompilationParams; -use rspack_core::CompilerCompilation; -use rspack_core::{ApplyContext, CompilerOptions, PluginContext}; -use rspack_hook::plugin; -use rspack_hook::plugin_hook; -use rspack_hook::Hook as _; -use rspack_plugin_html::HtmlRspackPlugin; -use rspack_plugin_javascript::JsPlugin; -use rspack_plugin_rsdoctor::RsdoctorPlugin; -use rspack_plugin_runtime::RuntimePlugin; - -use self::interceptor::*; - -#[plugin] -#[derive(Clone)] -pub struct JsHooksAdapterPlugin { - non_skippable_registers: NonSkippableRegisters, - register_compiler_this_compilation_taps: RegisterCompilerThisCompilationTaps, - register_compiler_compilation_taps: RegisterCompilerCompilationTaps, - register_compiler_make_taps: RegisterCompilerMakeTaps, - register_compiler_finish_make_taps: RegisterCompilerFinishMakeTaps, - register_compiler_should_emit_taps: RegisterCompilerShouldEmitTaps, - register_compiler_emit_taps: RegisterCompilerEmitTaps, - register_compiler_after_emit_taps: RegisterCompilerAfterEmitTaps, - register_compiler_asset_emitted_taps: RegisterCompilerAssetEmittedTaps, - register_compilation_build_module_taps: RegisterCompilationBuildModuleTaps, - register_compilation_still_valid_module_taps: RegisterCompilationStillValidModuleTaps, - register_compilation_succeed_module_taps: RegisterCompilationSucceedModuleTaps, - register_compilation_execute_module_taps: RegisterCompilationExecuteModuleTaps, - register_compilation_finish_modules_taps: RegisterCompilationFinishModulesTaps, - register_compilation_optimize_modules_taps: RegisterCompilationOptimizeModulesTaps, - register_compilation_after_optimize_modules_taps: RegisterCompilationAfterOptimizeModulesTaps, - register_compilation_optimize_tree_taps: RegisterCompilationOptimizeTreeTaps, - register_compilation_optimize_chunk_modules_taps: RegisterCompilationOptimizeChunkModulesTaps, - register_compilation_additional_tree_runtime_requirements_taps: - RegisterCompilationAdditionalTreeRuntimeRequirementsTaps, - register_compilation_runtime_requirement_in_tree_taps: - RegisterCompilationRuntimeRequirementInTreeTaps, - register_compilation_runtime_module_taps: RegisterCompilationRuntimeModuleTaps, - register_compilation_chunk_hash_taps: RegisterCompilationChunkHashTaps, - register_compilation_chunk_asset_taps: RegisterCompilationChunkAssetTaps, - register_compilation_process_assets_taps: RegisterCompilationProcessAssetsTaps, - register_compilation_after_process_assets_taps: RegisterCompilationAfterProcessAssetsTaps, - register_compilation_seal_taps: RegisterCompilationSealTaps, - register_compilation_after_seal_taps: RegisterCompilationAfterSealTaps, - register_normal_module_factory_before_resolve_taps: RegisterNormalModuleFactoryBeforeResolveTaps, - register_normal_module_factory_factorize_taps: RegisterNormalModuleFactoryFactorizeTaps, - register_normal_module_factory_resolve_taps: RegisterNormalModuleFactoryResolveTaps, - register_normal_module_factory_resolve_for_scheme_taps: - RegisterNormalModuleFactoryResolveForSchemeTaps, - register_normal_module_factory_after_resolve_taps: RegisterNormalModuleFactoryAfterResolveTaps, - register_normal_module_factory_create_module_taps: RegisterNormalModuleFactoryCreateModuleTaps, - register_context_module_factory_before_resolve_taps: - RegisterContextModuleFactoryBeforeResolveTaps, - register_context_module_factory_after_resolve_taps: RegisterContextModuleFactoryAfterResolveTaps, - register_javascript_modules_chunk_hash_taps: RegisterJavascriptModulesChunkHashTaps, - register_html_plugin_before_asset_tag_generation_taps: - RegisterHtmlPluginBeforeAssetTagGenerationTaps, - register_html_plugin_alter_asset_tags_taps: RegisterHtmlPluginAlterAssetTagsTaps, - register_html_plugin_alter_asset_tag_groups_taps: RegisterHtmlPluginAlterAssetTagGroupsTaps, - register_html_plugin_after_template_execution_taps: RegisterHtmlPluginAfterTemplateExecutionTaps, - register_html_plugin_before_emit_taps: RegisterHtmlPluginBeforeEmitTaps, - register_html_plugin_after_emit_taps: RegisterHtmlPluginAfterEmitTaps, - register_runtime_plugin_create_script_taps: RegisterRuntimePluginCreateScriptTaps, - register_runtime_plugin_link_preload_taps: RegisterRuntimePluginLinkPreloadTaps, - register_runtime_plugin_link_prefetch_taps: RegisterRuntimePluginLinkPrefetchTaps, - register_rsdoctor_plugin_module_graph_taps: RegisterRsdoctorPluginModuleGraphTaps, - register_rsdoctor_plugin_chunk_graph_taps: RegisterRsdoctorPluginChunkGraphTaps, - register_rsdoctor_plugin_assets_taps: RegisterRsdoctorPluginAssetsTaps, - register_rsdoctor_plugin_module_ids_taps: RegisterRsdoctorPluginModuleIdsTaps, - register_rsdoctor_plugin_module_sources_taps: RegisterRsdoctorPluginModuleSourcesTaps, -} - -impl fmt::Debug for JsHooksAdapterPlugin { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "rspack_plugin_js_hooks_adapter") - } -} - -#[async_trait] -impl rspack_core::Plugin for JsHooksAdapterPlugin { - fn name(&self) -> &'static str { - "rspack.JsHooksAdapterPlugin" - } - - // #[tracing::instrument("js_hooks_adapter::apply", skip_all)] - fn apply( - &self, - ctx: PluginContext<&mut ApplyContext>, - _options: &CompilerOptions, - ) -> rspack_error::Result<()> { - ctx - .context - .compiler_hooks - .this_compilation - .intercept(self.register_compiler_this_compilation_taps.clone()); - ctx - .context - .compiler_hooks - .compilation - .intercept(self.register_compiler_compilation_taps.clone()); - ctx - .context - .compiler_hooks - .make - .intercept(self.register_compiler_make_taps.clone()); - ctx - .context - .compiler_hooks - .finish_make - .intercept(self.register_compiler_finish_make_taps.clone()); - ctx - .context - .compiler_hooks - .should_emit - .intercept(self.register_compiler_should_emit_taps.clone()); - ctx - .context - .compiler_hooks - .emit - .intercept(self.register_compiler_emit_taps.clone()); - ctx - .context - .compiler_hooks - .after_emit - .intercept(self.register_compiler_after_emit_taps.clone()); - ctx - .context - .compiler_hooks - .asset_emitted - .intercept(self.register_compiler_asset_emitted_taps.clone()); - ctx - .context - .compilation_hooks - .build_module - .intercept(self.register_compilation_build_module_taps.clone()); - ctx - .context - .compilation_hooks - .still_valid_module - .intercept(self.register_compilation_still_valid_module_taps.clone()); - ctx - .context - .compilation_hooks - .succeed_module - .intercept(self.register_compilation_succeed_module_taps.clone()); - ctx - .context - .compilation_hooks - .execute_module - .intercept(self.register_compilation_execute_module_taps.clone()); - ctx - .context - .compilation_hooks - .finish_modules - .intercept(self.register_compilation_finish_modules_taps.clone()); - ctx - .context - .compilation_hooks - .optimize_modules - .intercept(self.register_compilation_optimize_modules_taps.clone()); - ctx - .context - .compilation_hooks - .after_optimize_modules - .intercept( - self - .register_compilation_after_optimize_modules_taps - .clone(), - ); - ctx - .context - .compilation_hooks - .optimize_tree - .intercept(self.register_compilation_optimize_tree_taps.clone()); - ctx - .context - .compilation_hooks - .optimize_chunk_modules - .intercept( - self - .register_compilation_optimize_chunk_modules_taps - .clone(), - ); - ctx - .context - .compilation_hooks - .additional_tree_runtime_requirements - .intercept( - self - .register_compilation_additional_tree_runtime_requirements_taps - .clone(), - ); - ctx - .context - .compilation_hooks - .runtime_requirement_in_tree - .intercept( - self - .register_compilation_runtime_requirement_in_tree_taps - .clone(), - ); - ctx - .context - .compilation_hooks - .runtime_module - .intercept(self.register_compilation_runtime_module_taps.clone()); - ctx - .context - .compilation_hooks - .chunk_hash - .intercept(self.register_compilation_chunk_hash_taps.clone()); - ctx - .context - .compilation_hooks - .chunk_asset - .intercept(self.register_compilation_chunk_asset_taps.clone()); - ctx - .context - .compilation_hooks - .process_assets - .intercept(self.register_compilation_process_assets_taps.clone()); - ctx - .context - .compilation_hooks - .after_process_assets - .intercept(self.register_compilation_after_process_assets_taps.clone()); - ctx - .context - .compilation_hooks - .seal - .intercept(self.register_compilation_seal_taps.clone()); - ctx - .context - .compilation_hooks - .after_seal - .intercept(self.register_compilation_after_seal_taps.clone()); - - ctx - .context - .normal_module_factory_hooks - .before_resolve - .intercept( - self - .register_normal_module_factory_before_resolve_taps - .clone(), - ); - ctx - .context - .normal_module_factory_hooks - .factorize - .intercept(self.register_normal_module_factory_factorize_taps.clone()); - ctx - .context - .normal_module_factory_hooks - .resolve - .intercept(self.register_normal_module_factory_resolve_taps.clone()); - ctx - .context - .normal_module_factory_hooks - .resolve_for_scheme - .intercept( - self - .register_normal_module_factory_resolve_for_scheme_taps - .clone(), - ); - ctx - .context - .normal_module_factory_hooks - .after_resolve - .intercept( - self - .register_normal_module_factory_after_resolve_taps - .clone(), - ); - ctx - .context - .normal_module_factory_hooks - .create_module - .intercept( - self - .register_normal_module_factory_create_module_taps - .clone(), - ); - ctx - .context - .context_module_factory_hooks - .before_resolve - .intercept( - self - .register_context_module_factory_before_resolve_taps - .clone(), - ); - ctx - .context - .context_module_factory_hooks - .after_resolve - .intercept( - self - .register_context_module_factory_after_resolve_taps - .clone(), - ); - - ctx - .context - .compiler_hooks - .compilation - .tap(js_hooks_adapter_compilation::new(self)); - - ctx - .context - .compiler_hooks - .compilation - .tap(html_hooks_adapter_compilation::new(self)); - - ctx - .context - .compiler_hooks - .compilation - .tap(runtime_hooks_adapter_compilation::new(self)); - - ctx - .context - .compiler_hooks - .compilation - .tap(rsdoctor_hooks_adapter_compilation::new(self)); - - Ok(()) - } - - fn clear_cache(&self) { - self.register_compiler_this_compilation_taps.clear_cache(); - self.register_compiler_compilation_taps.clear_cache(); - self.register_compiler_make_taps.clear_cache(); - self.register_compiler_finish_make_taps.clear_cache(); - self.register_compiler_should_emit_taps.clear_cache(); - self.register_compiler_emit_taps.clear_cache(); - self.register_compiler_after_emit_taps.clear_cache(); - self.register_compiler_asset_emitted_taps.clear_cache(); - self.register_compilation_build_module_taps.clear_cache(); - self - .register_compilation_still_valid_module_taps - .clear_cache(); - self.register_compilation_succeed_module_taps.clear_cache(); - self.register_compilation_execute_module_taps.clear_cache(); - self.register_compilation_finish_modules_taps.clear_cache(); - self - .register_compilation_optimize_modules_taps - .clear_cache(); - self - .register_compilation_after_optimize_modules_taps - .clear_cache(); - self.register_compilation_optimize_tree_taps.clear_cache(); - self - .register_compilation_optimize_chunk_modules_taps - .clear_cache(); - self - .register_compilation_additional_tree_runtime_requirements_taps - .clear_cache(); - self - .register_compilation_runtime_requirement_in_tree_taps - .clear_cache(); - self.register_compilation_runtime_module_taps.clear_cache(); - self.register_compilation_chunk_hash_taps.clear_cache(); - self.register_compilation_chunk_asset_taps.clear_cache(); - self.register_compilation_process_assets_taps.clear_cache(); - self - .register_compilation_after_process_assets_taps - .clear_cache(); - self.register_compilation_seal_taps.clear_cache(); - self.register_compilation_after_seal_taps.clear_cache(); - self - .register_normal_module_factory_before_resolve_taps - .clear_cache(); - self - .register_normal_module_factory_factorize_taps - .clear_cache(); - self - .register_normal_module_factory_resolve_taps - .clear_cache(); - self - .register_normal_module_factory_resolve_for_scheme_taps - .clear_cache(); - self - .register_normal_module_factory_after_resolve_taps - .clear_cache(); - self - .register_normal_module_factory_create_module_taps - .clear_cache(); - self - .register_context_module_factory_before_resolve_taps - .clear_cache(); - self - .register_context_module_factory_after_resolve_taps - .clear_cache(); - self - .register_javascript_modules_chunk_hash_taps - .clear_cache(); - self - .register_html_plugin_before_asset_tag_generation_taps - .clear_cache(); - self - .register_html_plugin_alter_asset_tags_taps - .clear_cache(); - self - .register_html_plugin_alter_asset_tag_groups_taps - .clear_cache(); - self - .register_html_plugin_after_template_execution_taps - .clear_cache(); - self.register_html_plugin_before_emit_taps.clear_cache(); - self.register_html_plugin_after_emit_taps.clear_cache(); - self - .register_runtime_plugin_create_script_taps - .clear_cache(); - self.register_runtime_plugin_link_preload_taps.clear_cache(); - self - .register_runtime_plugin_link_prefetch_taps - .clear_cache(); - self - .register_rsdoctor_plugin_module_graph_taps - .clear_cache(); - self.register_rsdoctor_plugin_chunk_graph_taps.clear_cache(); - self.register_rsdoctor_plugin_assets_taps.clear_cache(); - self.register_rsdoctor_plugin_module_ids_taps.clear_cache(); - self - .register_rsdoctor_plugin_module_sources_taps - .clear_cache(); - } -} - -#[plugin_hook(CompilerCompilation for JsHooksAdapterPlugin)] -async fn js_hooks_adapter_compilation( - &self, - compilation: &mut Compilation, - _params: &mut CompilationParams, -) -> rspack_error::Result<()> { - let mut hooks = JsPlugin::get_compilation_hooks_mut(compilation.id()); - hooks - .chunk_hash - .intercept(self.register_javascript_modules_chunk_hash_taps.clone()); - - Ok(()) -} - -#[plugin_hook(CompilerCompilation for JsHooksAdapterPlugin)] -async fn html_hooks_adapter_compilation( - &self, - compilation: &mut Compilation, - _params: &mut CompilationParams, -) -> rspack_error::Result<()> { - let mut hooks = HtmlRspackPlugin::get_compilation_hooks_mut(compilation.id()); - hooks.before_asset_tag_generation.intercept( - self - .register_html_plugin_before_asset_tag_generation_taps - .clone(), - ); - hooks - .alter_asset_tags - .intercept(self.register_html_plugin_alter_asset_tags_taps.clone()); - hooks.alter_asset_tag_groups.intercept( - self - .register_html_plugin_alter_asset_tag_groups_taps - .clone(), - ); - hooks.after_template_execution.intercept( - self - .register_html_plugin_after_template_execution_taps - .clone(), - ); - hooks - .before_emit - .intercept(self.register_html_plugin_before_emit_taps.clone()); - hooks - .after_emit - .intercept(self.register_html_plugin_after_emit_taps.clone()); - - Ok(()) -} - -#[plugin_hook(CompilerCompilation for JsHooksAdapterPlugin)] -async fn runtime_hooks_adapter_compilation( - &self, - compilation: &mut Compilation, - _params: &mut CompilationParams, -) -> rspack_error::Result<()> { - let mut hooks = RuntimePlugin::get_compilation_hooks_mut(compilation.id()); - hooks - .create_script - .intercept(self.register_runtime_plugin_create_script_taps.clone()); - hooks - .link_preload - .intercept(self.register_runtime_plugin_link_preload_taps.clone()); - hooks - .link_prefetch - .intercept(self.register_runtime_plugin_link_prefetch_taps.clone()); - Ok(()) -} - -#[plugin_hook(CompilerCompilation for JsHooksAdapterPlugin)] -async fn rsdoctor_hooks_adapter_compilation( - &self, - compilation: &mut Compilation, - _params: &mut CompilationParams, -) -> rspack_error::Result<()> { - let mut hooks = RsdoctorPlugin::get_compilation_hooks_mut(compilation.id()); - hooks - .module_graph - .intercept(self.register_rsdoctor_plugin_module_graph_taps.clone()); - hooks - .chunk_graph - .intercept(self.register_rsdoctor_plugin_chunk_graph_taps.clone()); - hooks - .assets - .intercept(self.register_rsdoctor_plugin_assets_taps.clone()); - hooks - .module_ids - .intercept(self.register_rsdoctor_plugin_module_ids_taps.clone()); - hooks - .module_sources - .intercept(self.register_rsdoctor_plugin_module_sources_taps.clone()); - - Ok(()) -} - -impl JsHooksAdapterPlugin { - pub fn from_js_hooks(_env: Env, register_js_taps: RegisterJsTaps) -> Result { - let non_skippable_registers = NonSkippableRegisters::default(); - Ok(JsHooksAdapterPlugin { - inner: JsHooksAdapterPluginInner { - register_compiler_this_compilation_taps: RegisterCompilerThisCompilationTaps::new( - register_js_taps.register_compiler_this_compilation_taps, - non_skippable_registers.clone(), - ), - register_compiler_compilation_taps: RegisterCompilerCompilationTaps::new( - register_js_taps.register_compiler_compilation_taps, - non_skippable_registers.clone(), - ), - register_compiler_make_taps: RegisterCompilerMakeTaps::new( - register_js_taps.register_compiler_make_taps, - non_skippable_registers.clone(), - ), - register_compiler_finish_make_taps: RegisterCompilerFinishMakeTaps::new( - register_js_taps.register_compiler_finish_make_taps, - non_skippable_registers.clone(), - ), - register_compiler_should_emit_taps: RegisterCompilerShouldEmitTaps::new( - register_js_taps.register_compiler_should_emit_taps, - non_skippable_registers.clone(), - ), - register_compiler_emit_taps: RegisterCompilerEmitTaps::new( - register_js_taps.register_compiler_emit_taps, - non_skippable_registers.clone(), - ), - register_compiler_after_emit_taps: RegisterCompilerAfterEmitTaps::new( - register_js_taps.register_compiler_after_emit_taps, - non_skippable_registers.clone(), - ), - register_compiler_asset_emitted_taps: RegisterCompilerAssetEmittedTaps::new( - register_js_taps.register_compiler_asset_emitted_taps, - non_skippable_registers.clone(), - ), - register_compilation_build_module_taps: RegisterCompilationBuildModuleTaps::new( - register_js_taps.register_compilation_build_module_taps, - non_skippable_registers.clone(), - ), - register_compilation_still_valid_module_taps: RegisterCompilationStillValidModuleTaps::new( - register_js_taps.register_compilation_still_valid_module_taps, - non_skippable_registers.clone(), - ), - register_compilation_succeed_module_taps: RegisterCompilationSucceedModuleTaps::new( - register_js_taps.register_compilation_succeed_module_taps, - non_skippable_registers.clone(), - ), - register_compilation_execute_module_taps: RegisterCompilationExecuteModuleTaps::new( - register_js_taps.register_compilation_execute_module_taps, - non_skippable_registers.clone(), - ), - register_compilation_finish_modules_taps: RegisterCompilationFinishModulesTaps::new( - register_js_taps.register_compilation_finish_modules_taps, - non_skippable_registers.clone(), - ), - register_compilation_optimize_modules_taps: RegisterCompilationOptimizeModulesTaps::new( - register_js_taps.register_compilation_optimize_modules_taps, - non_skippable_registers.clone(), - ), - register_compilation_after_optimize_modules_taps: - RegisterCompilationAfterOptimizeModulesTaps::new( - register_js_taps.register_compilation_after_optimize_modules_taps, - non_skippable_registers.clone(), - ), - register_compilation_optimize_tree_taps: RegisterCompilationOptimizeTreeTaps::new( - register_js_taps.register_compilation_optimize_tree_taps, - non_skippable_registers.clone(), - ), - register_compilation_optimize_chunk_modules_taps: - RegisterCompilationOptimizeChunkModulesTaps::new( - register_js_taps.register_compilation_optimize_chunk_modules_taps, - non_skippable_registers.clone(), - ), - register_compilation_additional_tree_runtime_requirements_taps: - RegisterCompilationAdditionalTreeRuntimeRequirementsTaps::new( - register_js_taps.register_compilation_additional_tree_runtime_requirements_taps, - non_skippable_registers.clone(), - ), - register_compilation_runtime_requirement_in_tree_taps: - RegisterCompilationRuntimeRequirementInTreeTaps::new( - register_js_taps.register_compilation_runtime_requirement_in_tree_taps, - non_skippable_registers.clone(), - ), - register_compilation_runtime_module_taps: RegisterCompilationRuntimeModuleTaps::new( - register_js_taps.register_compilation_runtime_module_taps, - non_skippable_registers.clone(), - ), - register_compilation_chunk_hash_taps: RegisterCompilationChunkHashTaps::new( - register_js_taps.register_compilation_chunk_hash_taps, - non_skippable_registers.clone(), - ), - register_compilation_chunk_asset_taps: RegisterCompilationChunkAssetTaps::new( - register_js_taps.register_compilation_chunk_asset_taps, - non_skippable_registers.clone(), - ), - register_compilation_process_assets_taps: RegisterCompilationProcessAssetsTaps::new( - register_js_taps.register_compilation_process_assets_taps, - non_skippable_registers.clone(), - ), - register_compilation_after_process_assets_taps: - RegisterCompilationAfterProcessAssetsTaps::new( - register_js_taps.register_compilation_after_process_assets_taps, - non_skippable_registers.clone(), - ), - register_compilation_seal_taps: RegisterCompilationSealTaps::new( - register_js_taps.register_compilation_seal_taps, - non_skippable_registers.clone(), - ), - register_compilation_after_seal_taps: RegisterCompilationAfterSealTaps::new( - register_js_taps.register_compilation_after_seal_taps, - non_skippable_registers.clone(), - ), - register_normal_module_factory_before_resolve_taps: - RegisterNormalModuleFactoryBeforeResolveTaps::new( - register_js_taps.register_normal_module_factory_before_resolve_taps, - non_skippable_registers.clone(), - ), - register_normal_module_factory_factorize_taps: - RegisterNormalModuleFactoryFactorizeTaps::new( - register_js_taps.register_normal_module_factory_factorize_taps, - non_skippable_registers.clone(), - ), - register_normal_module_factory_resolve_taps: RegisterNormalModuleFactoryResolveTaps::new( - register_js_taps.register_normal_module_factory_resolve_taps, - non_skippable_registers.clone(), - ), - register_normal_module_factory_resolve_for_scheme_taps: - RegisterNormalModuleFactoryResolveForSchemeTaps::new( - register_js_taps.register_normal_module_factory_resolve_for_scheme_taps, - non_skippable_registers.clone(), - ), - register_normal_module_factory_after_resolve_taps: - RegisterNormalModuleFactoryAfterResolveTaps::new( - register_js_taps.register_normal_module_factory_after_resolve_taps, - non_skippable_registers.clone(), - ), - register_normal_module_factory_create_module_taps: - RegisterNormalModuleFactoryCreateModuleTaps::new( - register_js_taps.register_normal_module_factory_create_module_taps, - non_skippable_registers.clone(), - ), - register_context_module_factory_before_resolve_taps: - RegisterContextModuleFactoryBeforeResolveTaps::new( - register_js_taps.register_context_module_factory_before_resolve_taps, - non_skippable_registers.clone(), - ), - register_context_module_factory_after_resolve_taps: - RegisterContextModuleFactoryAfterResolveTaps::new( - register_js_taps.register_context_module_factory_after_resolve_taps, - non_skippable_registers.clone(), - ), - register_javascript_modules_chunk_hash_taps: RegisterJavascriptModulesChunkHashTaps::new( - register_js_taps.register_javascript_modules_chunk_hash_taps, - non_skippable_registers.clone(), - ), - register_html_plugin_before_asset_tag_generation_taps: - RegisterHtmlPluginBeforeAssetTagGenerationTaps::new( - register_js_taps.register_html_plugin_before_asset_tag_generation_taps, - non_skippable_registers.clone(), - ), - register_html_plugin_alter_asset_tags_taps: RegisterHtmlPluginAlterAssetTagsTaps::new( - register_js_taps.register_html_plugin_alter_asset_tags_taps, - non_skippable_registers.clone(), - ), - register_html_plugin_alter_asset_tag_groups_taps: - RegisterHtmlPluginAlterAssetTagGroupsTaps::new( - register_js_taps.register_html_plugin_alter_asset_tag_groups_taps, - non_skippable_registers.clone(), - ), - register_html_plugin_after_template_execution_taps: - RegisterHtmlPluginAfterTemplateExecutionTaps::new( - register_js_taps.register_html_plugin_after_template_execution_taps, - non_skippable_registers.clone(), - ), - register_html_plugin_before_emit_taps: RegisterHtmlPluginBeforeEmitTaps::new( - register_js_taps.register_html_plugin_before_emit_taps, - non_skippable_registers.clone(), - ), - register_html_plugin_after_emit_taps: RegisterHtmlPluginAfterEmitTaps::new( - register_js_taps.register_html_plugin_after_emit_taps, - non_skippable_registers.clone(), - ), - register_runtime_plugin_create_script_taps: RegisterRuntimePluginCreateScriptTaps::new( - register_js_taps.register_runtime_plugin_create_script_taps, - non_skippable_registers.clone(), - ), - register_runtime_plugin_link_preload_taps: RegisterRuntimePluginLinkPreloadTaps::new( - register_js_taps.register_runtime_plugin_link_preload_taps, - non_skippable_registers.clone(), - ), - register_runtime_plugin_link_prefetch_taps: RegisterRuntimePluginLinkPrefetchTaps::new( - register_js_taps.register_runtime_plugin_link_prefetch_taps, - non_skippable_registers.clone(), - ), - register_rsdoctor_plugin_module_graph_taps: RegisterRsdoctorPluginModuleGraphTaps::new( - register_js_taps.register_rsdoctor_plugin_module_graph_taps, - non_skippable_registers.clone(), - ), - register_rsdoctor_plugin_chunk_graph_taps: RegisterRsdoctorPluginChunkGraphTaps::new( - register_js_taps.register_rsdoctor_plugin_chunk_graph_taps, - non_skippable_registers.clone(), - ), - register_rsdoctor_plugin_assets_taps: RegisterRsdoctorPluginAssetsTaps::new( - register_js_taps.register_rsdoctor_plugin_assets_taps, - non_skippable_registers.clone(), - ), - register_rsdoctor_plugin_module_ids_taps: RegisterRsdoctorPluginModuleIdsTaps::new( - register_js_taps.register_rsdoctor_plugin_module_ids_taps, - non_skippable_registers.clone(), - ), - register_rsdoctor_plugin_module_sources_taps: RegisterRsdoctorPluginModuleSourcesTaps::new( - register_js_taps.register_rsdoctor_plugin_module_sources_taps, - non_skippable_registers.clone(), - ), - non_skippable_registers, - } - .into(), - }) - } - - pub fn set_non_skippable_registers(&self, kinds: Vec) { - self - .non_skippable_registers - .set_non_skippable_registers(kinds); - } -} +mod js_hooks_plugin; +mod js_loader; + +pub use context_replacement::*; +pub use js_hooks_plugin::*; +pub(super) use js_loader::{JsLoaderRspackPlugin, JsLoaderRunner}; +pub mod buildtime_plugins; +pub use interceptor::*; diff --git a/crates/rspack_binding_values/src/raw_options/mod.rs b/crates/node_binding/src/raw_options/mod.rs similarity index 100% rename from crates/rspack_binding_values/src/raw_options/mod.rs rename to crates/node_binding/src/raw_options/mod.rs diff --git a/crates/rspack_binding_values/src/raw_options/raw_builtins/mod.rs b/crates/node_binding/src/raw_options/raw_builtins/mod.rs similarity index 100% rename from crates/rspack_binding_values/src/raw_options/raw_builtins/mod.rs rename to crates/node_binding/src/raw_options/raw_builtins/mod.rs diff --git a/crates/rspack_binding_values/src/raw_options/raw_builtins/raw_banner.rs b/crates/node_binding/src/raw_options/raw_builtins/raw_banner.rs similarity index 100% rename from crates/rspack_binding_values/src/raw_options/raw_builtins/raw_banner.rs rename to crates/node_binding/src/raw_options/raw_builtins/raw_banner.rs diff --git a/crates/rspack_binding_values/src/raw_options/raw_builtins/raw_bundle_info.rs b/crates/node_binding/src/raw_options/raw_builtins/raw_bundle_info.rs similarity index 100% rename from crates/rspack_binding_values/src/raw_options/raw_builtins/raw_bundle_info.rs rename to crates/node_binding/src/raw_options/raw_builtins/raw_bundle_info.rs diff --git a/crates/rspack_binding_values/src/raw_options/raw_builtins/raw_copy.rs b/crates/node_binding/src/raw_options/raw_builtins/raw_copy.rs similarity index 100% rename from crates/rspack_binding_values/src/raw_options/raw_builtins/raw_copy.rs rename to crates/node_binding/src/raw_options/raw_builtins/raw_copy.rs diff --git a/crates/rspack_binding_values/src/raw_options/raw_builtins/raw_css_extract.rs b/crates/node_binding/src/raw_options/raw_builtins/raw_css_extract.rs similarity index 100% rename from crates/rspack_binding_values/src/raw_options/raw_builtins/raw_css_extract.rs rename to crates/node_binding/src/raw_options/raw_builtins/raw_css_extract.rs diff --git a/crates/rspack_binding_values/src/raw_options/raw_builtins/raw_dll.rs b/crates/node_binding/src/raw_options/raw_builtins/raw_dll.rs similarity index 100% rename from crates/rspack_binding_values/src/raw_options/raw_builtins/raw_dll.rs rename to crates/node_binding/src/raw_options/raw_builtins/raw_dll.rs diff --git a/crates/rspack_binding_values/src/raw_options/raw_builtins/raw_html.rs b/crates/node_binding/src/raw_options/raw_builtins/raw_html.rs similarity index 100% rename from crates/rspack_binding_values/src/raw_options/raw_builtins/raw_html.rs rename to crates/node_binding/src/raw_options/raw_builtins/raw_html.rs diff --git a/crates/rspack_binding_values/src/raw_options/raw_builtins/raw_ids.rs b/crates/node_binding/src/raw_options/raw_builtins/raw_ids.rs similarity index 100% rename from crates/rspack_binding_values/src/raw_options/raw_builtins/raw_ids.rs rename to crates/node_binding/src/raw_options/raw_builtins/raw_ids.rs diff --git a/crates/rspack_binding_values/src/raw_options/raw_builtins/raw_ignore.rs b/crates/node_binding/src/raw_options/raw_builtins/raw_ignore.rs similarity index 100% rename from crates/rspack_binding_values/src/raw_options/raw_builtins/raw_ignore.rs rename to crates/node_binding/src/raw_options/raw_builtins/raw_ignore.rs diff --git a/crates/rspack_binding_values/src/raw_options/raw_builtins/raw_lazy_compilation.rs b/crates/node_binding/src/raw_options/raw_builtins/raw_lazy_compilation.rs similarity index 91% rename from crates/rspack_binding_values/src/raw_options/raw_builtins/raw_lazy_compilation.rs rename to crates/node_binding/src/raw_options/raw_builtins/raw_lazy_compilation.rs index a42cbdd22435..6611e87b0bde 100644 --- a/crates/rspack_binding_values/src/raw_options/raw_builtins/raw_lazy_compilation.rs +++ b/crates/node_binding/src/raw_options/raw_builtins/raw_lazy_compilation.rs @@ -3,7 +3,7 @@ use napi::{ Either, }; use napi_derive::napi; -use rspack_core::{CompilationId, ModuleIdentifier}; +use rspack_core::{CompilationId, CompilerId, ModuleIdentifier}; use rspack_napi::threadsafe_function::ThreadsafeFunction; use rspack_plugin_lazy_compilation::{ backend::{Backend, ModuleInfo}, @@ -42,10 +42,15 @@ pub struct LazyCompilationTestFn { } impl LazyCompilationTestCheck for LazyCompilationTestFn { - fn test(&self, compilation_id: CompilationId, m: &dyn rspack_core::Module) -> bool { + fn test( + &self, + compiler_id: CompilerId, + compilation_id: CompilationId, + m: &dyn rspack_core::Module, + ) -> bool { let res = self .tsfn - .blocking_call_with_sync(JsModuleWrapper::new(m, compilation_id, None)) + .blocking_call_with_sync(JsModuleWrapper::new(m, compiler_id, compilation_id, None)) .expect("failed to invoke lazyCompilation.test"); res.unwrap_or(false) diff --git a/crates/rspack_binding_values/src/raw_options/raw_builtins/raw_lightning_css_minimizer.rs b/crates/node_binding/src/raw_options/raw_builtins/raw_lightning_css_minimizer.rs similarity index 100% rename from crates/rspack_binding_values/src/raw_options/raw_builtins/raw_lightning_css_minimizer.rs rename to crates/node_binding/src/raw_options/raw_builtins/raw_lightning_css_minimizer.rs diff --git a/crates/rspack_binding_values/src/raw_options/raw_builtins/raw_limit_chunk_count.rs b/crates/node_binding/src/raw_options/raw_builtins/raw_limit_chunk_count.rs similarity index 100% rename from crates/rspack_binding_values/src/raw_options/raw_builtins/raw_limit_chunk_count.rs rename to crates/node_binding/src/raw_options/raw_builtins/raw_limit_chunk_count.rs diff --git a/crates/rspack_binding_values/src/raw_options/raw_builtins/raw_mf.rs b/crates/node_binding/src/raw_options/raw_builtins/raw_mf.rs similarity index 100% rename from crates/rspack_binding_values/src/raw_options/raw_builtins/raw_mf.rs rename to crates/node_binding/src/raw_options/raw_builtins/raw_mf.rs diff --git a/crates/rspack_binding_values/src/raw_options/raw_builtins/raw_progress.rs b/crates/node_binding/src/raw_options/raw_builtins/raw_progress.rs similarity index 100% rename from crates/rspack_binding_values/src/raw_options/raw_builtins/raw_progress.rs rename to crates/node_binding/src/raw_options/raw_builtins/raw_progress.rs diff --git a/crates/rspack_binding_values/src/raw_options/raw_builtins/raw_runtime_chunk.rs b/crates/node_binding/src/raw_options/raw_builtins/raw_runtime_chunk.rs similarity index 100% rename from crates/rspack_binding_values/src/raw_options/raw_builtins/raw_runtime_chunk.rs rename to crates/node_binding/src/raw_options/raw_builtins/raw_runtime_chunk.rs diff --git a/crates/rspack_binding_values/src/raw_options/raw_builtins/raw_size_limits.rs b/crates/node_binding/src/raw_options/raw_builtins/raw_size_limits.rs similarity index 100% rename from crates/rspack_binding_values/src/raw_options/raw_builtins/raw_size_limits.rs rename to crates/node_binding/src/raw_options/raw_builtins/raw_size_limits.rs diff --git a/crates/rspack_binding_values/src/raw_options/raw_builtins/raw_swc_js_minimizer.rs b/crates/node_binding/src/raw_options/raw_builtins/raw_swc_js_minimizer.rs similarity index 100% rename from crates/rspack_binding_values/src/raw_options/raw_builtins/raw_swc_js_minimizer.rs rename to crates/node_binding/src/raw_options/raw_builtins/raw_swc_js_minimizer.rs diff --git a/crates/rspack_binding_values/src/raw_options/raw_cache.rs b/crates/node_binding/src/raw_options/raw_cache.rs similarity index 100% rename from crates/rspack_binding_values/src/raw_options/raw_cache.rs rename to crates/node_binding/src/raw_options/raw_cache.rs diff --git a/crates/rspack_binding_values/src/raw_options/raw_devtool.rs b/crates/node_binding/src/raw_options/raw_devtool.rs similarity index 100% rename from crates/rspack_binding_values/src/raw_options/raw_devtool.rs rename to crates/node_binding/src/raw_options/raw_devtool.rs diff --git a/crates/rspack_binding_values/src/raw_options/raw_dynamic_entry.rs b/crates/node_binding/src/raw_options/raw_dynamic_entry.rs similarity index 100% rename from crates/rspack_binding_values/src/raw_options/raw_dynamic_entry.rs rename to crates/node_binding/src/raw_options/raw_dynamic_entry.rs diff --git a/crates/rspack_binding_values/src/raw_options/raw_experiments/mod.rs b/crates/node_binding/src/raw_options/raw_experiments/mod.rs similarity index 100% rename from crates/rspack_binding_values/src/raw_options/raw_experiments/mod.rs rename to crates/node_binding/src/raw_options/raw_experiments/mod.rs diff --git a/crates/rspack_binding_values/src/raw_options/raw_experiments/raw_cache/mod.rs b/crates/node_binding/src/raw_options/raw_experiments/raw_cache/mod.rs similarity index 100% rename from crates/rspack_binding_values/src/raw_options/raw_experiments/raw_cache/mod.rs rename to crates/node_binding/src/raw_options/raw_experiments/raw_cache/mod.rs diff --git a/crates/rspack_binding_values/src/raw_options/raw_experiments/raw_cache/raw_snapshot.rs b/crates/node_binding/src/raw_options/raw_experiments/raw_cache/raw_snapshot.rs similarity index 100% rename from crates/rspack_binding_values/src/raw_options/raw_experiments/raw_cache/raw_snapshot.rs rename to crates/node_binding/src/raw_options/raw_experiments/raw_cache/raw_snapshot.rs diff --git a/crates/rspack_binding_values/src/raw_options/raw_experiments/raw_cache/raw_storage.rs b/crates/node_binding/src/raw_options/raw_experiments/raw_cache/raw_storage.rs similarity index 100% rename from crates/rspack_binding_values/src/raw_options/raw_experiments/raw_cache/raw_storage.rs rename to crates/node_binding/src/raw_options/raw_experiments/raw_cache/raw_storage.rs diff --git a/crates/rspack_binding_values/src/raw_options/raw_experiments/raw_incremental.rs b/crates/node_binding/src/raw_options/raw_experiments/raw_incremental.rs similarity index 100% rename from crates/rspack_binding_values/src/raw_options/raw_experiments/raw_incremental.rs rename to crates/node_binding/src/raw_options/raw_experiments/raw_incremental.rs diff --git a/crates/rspack_binding_values/src/raw_options/raw_experiments/raw_rspack_future.rs b/crates/node_binding/src/raw_options/raw_experiments/raw_rspack_future.rs similarity index 100% rename from crates/rspack_binding_values/src/raw_options/raw_experiments/raw_rspack_future.rs rename to crates/node_binding/src/raw_options/raw_experiments/raw_rspack_future.rs diff --git a/crates/rspack_binding_values/src/raw_options/raw_external.rs b/crates/node_binding/src/raw_options/raw_external.rs similarity index 100% rename from crates/rspack_binding_values/src/raw_options/raw_external.rs rename to crates/node_binding/src/raw_options/raw_external.rs diff --git a/crates/rspack_binding_values/src/raw_options/raw_mode.rs b/crates/node_binding/src/raw_options/raw_mode.rs similarity index 100% rename from crates/rspack_binding_values/src/raw_options/raw_mode.rs rename to crates/node_binding/src/raw_options/raw_mode.rs diff --git a/crates/rspack_binding_values/src/raw_options/raw_module/mod.rs b/crates/node_binding/src/raw_options/raw_module/mod.rs similarity index 99% rename from crates/rspack_binding_values/src/raw_options/raw_module/mod.rs rename to crates/node_binding/src/raw_options/raw_module/mod.rs index 4f74360b3f7e..89f22e812069 100644 --- a/crates/rspack_binding_values/src/raw_options/raw_module/mod.rs +++ b/crates/node_binding/src/raw_options/raw_module/mod.rs @@ -625,6 +625,7 @@ impl From> for RawAssetGeneratorDataUrlFnCtx { module: JsModuleWrapper::new( value.module, value.compilation.id(), + value.compilation.compiler_id(), Some(value.compilation), ), } diff --git a/crates/rspack_binding_values/src/raw_options/raw_node.rs b/crates/node_binding/src/raw_options/raw_node.rs similarity index 100% rename from crates/rspack_binding_values/src/raw_options/raw_node.rs rename to crates/node_binding/src/raw_options/raw_node.rs diff --git a/crates/rspack_binding_values/src/raw_options/raw_optimization.rs b/crates/node_binding/src/raw_options/raw_optimization.rs similarity index 100% rename from crates/rspack_binding_values/src/raw_options/raw_optimization.rs rename to crates/node_binding/src/raw_options/raw_optimization.rs diff --git a/crates/rspack_binding_values/src/raw_options/raw_output.rs b/crates/node_binding/src/raw_options/raw_output.rs similarity index 100% rename from crates/rspack_binding_values/src/raw_options/raw_output.rs rename to crates/node_binding/src/raw_options/raw_output.rs diff --git a/crates/rspack_binding_values/src/raw_options/raw_split_chunks/mod.rs b/crates/node_binding/src/raw_options/raw_split_chunks/mod.rs similarity index 100% rename from crates/rspack_binding_values/src/raw_options/raw_split_chunks/mod.rs rename to crates/node_binding/src/raw_options/raw_split_chunks/mod.rs diff --git a/crates/rspack_binding_values/src/raw_options/raw_split_chunks/raw_split_chunk_cache_group_test.rs b/crates/node_binding/src/raw_options/raw_split_chunks/raw_split_chunk_cache_group_test.rs similarity index 96% rename from crates/rspack_binding_values/src/raw_options/raw_split_chunks/raw_split_chunk_cache_group_test.rs rename to crates/node_binding/src/raw_options/raw_split_chunks/raw_split_chunk_cache_group_test.rs index 51a6fca32b4a..193fb2c9b79e 100644 --- a/crates/rspack_binding_values/src/raw_options/raw_split_chunks/raw_split_chunk_cache_group_test.rs +++ b/crates/node_binding/src/raw_options/raw_split_chunks/raw_split_chunk_cache_group_test.rs @@ -23,6 +23,7 @@ impl<'a> From> for JsCacheGroupTestCtx { module: JsModuleWrapper::new( value.module, value.compilation.id(), + value.compilation.compiler_id(), Some(value.compilation), ), } diff --git a/crates/rspack_binding_values/src/raw_options/raw_split_chunks/raw_split_chunk_chunks.rs b/crates/node_binding/src/raw_options/raw_split_chunks/raw_split_chunk_chunks.rs similarity index 100% rename from crates/rspack_binding_values/src/raw_options/raw_split_chunks/raw_split_chunk_chunks.rs rename to crates/node_binding/src/raw_options/raw_split_chunks/raw_split_chunk_chunks.rs diff --git a/crates/rspack_binding_values/src/raw_options/raw_split_chunks/raw_split_chunk_name.rs b/crates/node_binding/src/raw_options/raw_split_chunks/raw_split_chunk_name.rs similarity index 97% rename from crates/rspack_binding_values/src/raw_options/raw_split_chunks/raw_split_chunk_name.rs rename to crates/node_binding/src/raw_options/raw_split_chunks/raw_split_chunk_name.rs index de4001ad01f3..baf6b3975fed 100644 --- a/crates/rspack_binding_values/src/raw_options/raw_split_chunks/raw_split_chunk_name.rs +++ b/crates/node_binding/src/raw_options/raw_split_chunks/raw_split_chunk_name.rs @@ -31,6 +31,7 @@ impl<'a> From> for JsChunkOptionNameCtx { module: JsModuleWrapper::new( value.module, value.compilation.id(), + value.compilation.compiler_id(), Some(value.compilation), ), chunks: value diff --git a/crates/rspack_binding_values/src/raw_options/raw_split_chunks/raw_split_chunk_size.rs b/crates/node_binding/src/raw_options/raw_split_chunks/raw_split_chunk_size.rs similarity index 100% rename from crates/rspack_binding_values/src/raw_options/raw_split_chunks/raw_split_chunk_size.rs rename to crates/node_binding/src/raw_options/raw_split_chunks/raw_split_chunk_size.rs diff --git a/crates/rspack_binding_values/src/raw_options/raw_stats.rs b/crates/node_binding/src/raw_options/raw_stats.rs similarity index 100% rename from crates/rspack_binding_values/src/raw_options/raw_stats.rs rename to crates/node_binding/src/raw_options/raw_stats.rs diff --git a/crates/rspack_binding_values/src/resolver.rs b/crates/node_binding/src/resolver.rs similarity index 100% rename from crates/rspack_binding_values/src/resolver.rs rename to crates/node_binding/src/resolver.rs diff --git a/crates/rspack_binding_values/src/resource_data.rs b/crates/node_binding/src/resource_data.rs similarity index 100% rename from crates/rspack_binding_values/src/resource_data.rs rename to crates/node_binding/src/resource_data.rs diff --git a/crates/rspack_binding_values/src/rsdoctor.rs b/crates/node_binding/src/rsdoctor.rs similarity index 100% rename from crates/rspack_binding_values/src/rsdoctor.rs rename to crates/node_binding/src/rsdoctor.rs diff --git a/crates/node_binding/src/rspack_error.rs b/crates/node_binding/src/rspack_error.rs new file mode 100644 index 000000000000..5e2b4f761c50 --- /dev/null +++ b/crates/node_binding/src/rspack_error.rs @@ -0,0 +1,80 @@ +use napi_derive::napi; +use rspack_error::{miette, Diagnostic, Result, RspackSeverity}; + +#[napi(object)] +pub struct JsRspackDiagnostic { + pub severity: JsRspackSeverity, + pub error: JsRspackError, +} + +impl From for Diagnostic { + fn from(value: JsRspackDiagnostic) -> Self { + value.error.into_diagnostic(value.severity.into()) + } +} + +#[napi(string_enum)] +pub enum JsRspackSeverity { + Error, + Warn, +} + +impl From for RspackSeverity { + fn from(value: JsRspackSeverity) -> Self { + match value { + JsRspackSeverity::Error => RspackSeverity::Error, + JsRspackSeverity::Warn => RspackSeverity::Warn, + } + } +} + +impl From for miette::Severity { + fn from(value: JsRspackSeverity) -> Self { + match value { + JsRspackSeverity::Error => miette::Severity::Error, + JsRspackSeverity::Warn => miette::Severity::Warning, + } + } +} + +#[napi(object)] +#[derive(Debug)] +pub struct JsRspackError { + pub name: String, + pub message: String, + pub module_identifier: Option, + pub loc: Option, + pub file: Option, + pub stack: Option, + pub hide_stack: Option, +} + +impl JsRspackError { + pub fn try_from_diagnostic(diagnostic: &Diagnostic, colored: bool) -> Result { + Ok(Self { + name: diagnostic.code().map(|n| n.to_string()).unwrap_or_else(|| { + match diagnostic.severity() { + rspack_error::RspackSeverity::Error => "Error".to_string(), + rspack_error::RspackSeverity::Warn => "Warn".to_string(), + } + }), + message: diagnostic.render_report(colored)?, + module_identifier: diagnostic.module_identifier().map(|d| d.to_string()), + loc: diagnostic.loc(), + file: diagnostic.file().map(|f| f.as_str().to_string()), + stack: diagnostic.stack(), + hide_stack: diagnostic.hide_stack(), + }) + } + + pub fn into_diagnostic(self, severity: RspackSeverity) -> Diagnostic { + (match severity { + RspackSeverity::Error => Diagnostic::error, + RspackSeverity::Warn => Diagnostic::warn, + })(self.name, self.message) + .with_file(self.file.map(Into::into)) + .with_module_identifier(self.module_identifier.map(Into::into)) + .with_stack(self.stack) + .with_hide_stack(self.hide_stack) + } +} diff --git a/crates/rspack_binding_values/src/runtime.rs b/crates/node_binding/src/runtime.rs similarity index 100% rename from crates/rspack_binding_values/src/runtime.rs rename to crates/node_binding/src/runtime.rs diff --git a/crates/rspack_binding_values/src/source.rs b/crates/node_binding/src/source.rs similarity index 100% rename from crates/rspack_binding_values/src/source.rs rename to crates/node_binding/src/source.rs diff --git a/crates/rspack_binding_values/src/stats.rs b/crates/node_binding/src/stats.rs similarity index 100% rename from crates/rspack_binding_values/src/stats.rs rename to crates/node_binding/src/stats.rs diff --git a/crates/rspack_binding_values/src/utils.rs b/crates/node_binding/src/utils.rs similarity index 100% rename from crates/rspack_binding_values/src/utils.rs rename to crates/node_binding/src/utils.rs diff --git a/crates/rspack_binding_values/Cargo.toml b/crates/rspack_binding_values/Cargo.toml deleted file mode 100644 index 0b1933b98c64..000000000000 --- a/crates/rspack_binding_values/Cargo.toml +++ /dev/null @@ -1,93 +0,0 @@ -[package] -description = "rspack binding values" -edition = "2021" -license = "MIT" -name = "rspack_binding_values" -repository = "https://github.com/web-infra-dev/rspack" -version = "0.2.0" -[lib] -crate-type = ["cdylib", "rlib"] -[features] -plugin = ["rspack_loader_swc/plugin"] - -[package.metadata.cargo-shear] -ignored = ["tracing"] - -[dependencies] -async-trait = { workspace = true } -cow-utils = { workspace = true } -derive_more = { workspace = true, features = ["debug"] } -futures = { workspace = true } -glob = { workspace = true } -heck = { workspace = true } -napi = { workspace = true, features = ["async", "tokio_rt", "serde-json", "anyhow"] } -napi-derive = { workspace = true } -pollster = { workspace = true } -rspack_cacheable = { workspace = true } -rspack_collections = { workspace = true } -rspack_core = { workspace = true } -rspack_error = { workspace = true } -rspack_hook = { workspace = true } -rspack_ids = { workspace = true } -rspack_napi = { workspace = true } -rspack_napi_macros = { workspace = true } -rspack_paths = { workspace = true } -rspack_regex = { workspace = true } -rspack_util = { workspace = true } -rustc-hash = { workspace = true } -serde = { workspace = true } -serde_json = { workspace = true } -swc_core = { workspace = true, default-features = false, features = ["ecma_transforms_react"] } -tokio = { workspace = true, features = ["rt", "rt-multi-thread", "macros", "test-util", "parking_lot"] } -tracing = { workspace = true } - - -rspack_loader_lightningcss = { workspace = true } -rspack_loader_preact_refresh = { workspace = true } -rspack_loader_react_refresh = { workspace = true } -rspack_loader_runner = { workspace = true } -rspack_loader_swc = { workspace = true } -rspack_loader_testing = { workspace = true } -rspack_plugin_asset = { workspace = true } -rspack_plugin_banner = { workspace = true } -rspack_plugin_context_replacement = { workspace = true } -rspack_plugin_copy = { workspace = true } -rspack_plugin_css = { workspace = true } -rspack_plugin_devtool = { workspace = true } -rspack_plugin_dll = { workspace = true } -rspack_plugin_dynamic_entry = { workspace = true } -rspack_plugin_ensure_chunk_conditions = { workspace = true } -rspack_plugin_entry = { workspace = true } -rspack_plugin_externals = { workspace = true } -rspack_plugin_extract_css = { workspace = true } -rspack_plugin_hmr = { workspace = true } -rspack_plugin_html = { workspace = true } -rspack_plugin_ignore = { workspace = true } -rspack_plugin_javascript = { workspace = true } -rspack_plugin_json = { workspace = true } -rspack_plugin_lazy_compilation = { workspace = true } -rspack_plugin_library = { workspace = true } -rspack_plugin_lightning_css_minimizer = { workspace = true } -rspack_plugin_limit_chunk_count = { workspace = true } -rspack_plugin_merge_duplicate_chunks = { workspace = true } -rspack_plugin_mf = { workspace = true } -rspack_plugin_no_emit_on_errors = { workspace = true } -rspack_plugin_progress = { workspace = true } -rspack_plugin_real_content_hash = { workspace = true } -rspack_plugin_remove_duplicate_modules = { workspace = true } -rspack_plugin_remove_empty_chunks = { workspace = true } -rspack_plugin_rsdoctor = { workspace = true } -rspack_plugin_runtime = { workspace = true } -rspack_plugin_runtime_chunk = { workspace = true } -rspack_plugin_schemes = { workspace = true } -rspack_plugin_size_limits = { workspace = true } -rspack_plugin_split_chunks = { workspace = true } -rspack_plugin_swc_js_minimizer = { workspace = true } -rspack_plugin_warn_sensitive_module = { workspace = true } -rspack_plugin_wasm = { workspace = true } -rspack_plugin_web_worker_template = { workspace = true } -rspack_plugin_worker = { workspace = true } -rspack_tracing = { workspace = true } - -[build-dependencies] -napi-build = { workspace = true } diff --git a/crates/rspack_binding_values/LICENSE b/crates/rspack_binding_values/LICENSE deleted file mode 100644 index 46310101ad8a..000000000000 --- a/crates/rspack_binding_values/LICENSE +++ /dev/null @@ -1,22 +0,0 @@ -MIT License - -Copyright (c) 2022-present Bytedance, Inc. and its affiliates. - - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. diff --git a/crates/rspack_binding_values/build.rs b/crates/rspack_binding_values/build.rs deleted file mode 100644 index 525b97263b16..000000000000 --- a/crates/rspack_binding_values/build.rs +++ /dev/null @@ -1,11 +0,0 @@ -fn main() { - napi_build::setup(); - - // Rebuild binding options if and only if it's built for crate `node_binding` - if std::env::var("OUT_DIR") - .expect("should exist") - .contains("node_binding") - { - println!("cargo:rerun-if-changed=../node_binding"); - } -} diff --git a/crates/rspack_binding_values/src/lib.rs b/crates/rspack_binding_values/src/lib.rs deleted file mode 100644 index dd09e8cc25ca..000000000000 --- a/crates/rspack_binding_values/src/lib.rs +++ /dev/null @@ -1,66 +0,0 @@ -#![feature(let_chains)] -#![feature(try_blocks)] - -mod asset; -mod asset_condition; -mod chunk; -mod chunk_graph; -mod chunk_group; -mod clean_options; -mod codegen_result; -mod compilation; -mod context_module_factory; -mod dependency; -mod dependency_block; -mod exports_info; -mod filename; -mod html; -mod identifier; -mod module; -mod module_graph; -mod module_graph_connection; -mod normal_module_factory; -mod options; -mod path_data; -mod plugins; -mod raw_options; -mod resolver; -mod resource_data; -mod rsdoctor; -mod rspack_error; -mod runtime; -mod source; -mod stats; -mod utils; - -pub use asset::*; -pub use asset_condition::*; -pub use chunk::*; -pub use chunk_graph::*; -pub use chunk_group::*; -pub use clean_options::*; -pub use codegen_result::*; -pub use compilation::*; -pub use context_module_factory::*; -pub use dependency::*; -pub use dependency_block::*; -pub use exports_info::*; -pub use filename::*; -pub use html::*; -pub use module::*; -pub use module_graph::*; -pub use module_graph_connection::*; -pub use normal_module_factory::*; -pub use options::*; -pub use path_data::*; -pub use plugins::buildtime_plugins; -pub(crate) use plugins::*; -pub use raw_options::*; -pub use resolver::*; -pub use resource_data::*; -pub use rsdoctor::*; -pub use rspack_error::*; -pub use runtime::*; -pub use source::*; -pub use stats::*; -pub use utils::*; diff --git a/crates/rspack_binding_values/src/plugins/mod.rs b/crates/rspack_binding_values/src/plugins/mod.rs deleted file mode 100644 index b8015e2e9c41..000000000000 --- a/crates/rspack_binding_values/src/plugins/mod.rs +++ /dev/null @@ -1,6 +0,0 @@ -mod context_replacement; -mod js_loader; - -pub use context_replacement::*; -pub(super) use js_loader::{JsLoaderRspackPlugin, JsLoaderRunner}; -pub mod buildtime_plugins; diff --git a/crates/rspack_core/src/compiler/compilation.rs b/crates/rspack_core/src/compiler/compilation.rs index 17c490794729..a723fd9a5241 100644 --- a/crates/rspack_core/src/compiler/compilation.rs +++ b/crates/rspack_core/src/compiler/compilation.rs @@ -37,6 +37,7 @@ use super::{ hmr::CompilationRecords, make::{make_module_graph, update_module_graph, MakeArtifact, MakeParam}, module_executor::ModuleExecutor, + CompilerId, }; use crate::{ build_chunk_graph::{build_chunk_graph, build_chunk_graph_new}, @@ -64,9 +65,9 @@ pub type BuildDependency = ( ); define_hook!(CompilationAddEntry: AsyncSeries(compilation: &mut Compilation, entry_name: Option<&str>)); -define_hook!(CompilationBuildModule: AsyncSeries(compilation_id: CompilationId, module: &mut BoxModule)); -define_hook!(CompilationStillValidModule: AsyncSeries(compilation_id: CompilationId, module: &mut BoxModule)); -define_hook!(CompilationSucceedModule: AsyncSeries(compilation_id: CompilationId, module: &mut BoxModule)); +define_hook!(CompilationBuildModule: AsyncSeries(compiler_id: CompilerId, compilation_id: CompilationId, module: &mut BoxModule)); +define_hook!(CompilationStillValidModule: AsyncSeries(compiler_id: CompilerId, compilation_id: CompilationId, module: &mut BoxModule)); +define_hook!(CompilationSucceedModule: AsyncSeries(compiler_id: CompilerId, compilation_id: CompilationId, module: &mut BoxModule)); define_hook!(CompilationExecuteModule: SyncSeries(module: &ModuleIdentifier, runtime_modules: &IdentifierSet, codegen_results: &CodeGenerationResults, execute_module_id: &ExecuteModuleId)); define_hook!(CompilationFinishModules: AsyncSeries(compilation: &mut Compilation)); @@ -155,6 +156,7 @@ static COMPILATION_ID: AtomicU32 = AtomicU32::new(0); pub struct Compilation { /// get_compilation_hooks(compilation.id) id: CompilationId, + compiler_id: CompilerId, // Mark compilation status, because the hash of `[hash].hot-update.js/json` is previous compilation hash. // Status A(hash: A) -> Status B(hash: B) will generate `A.hot-update.js` // Status A(hash: A) -> Status C(hash: C) will generate `A.hot-update.js` @@ -263,6 +265,7 @@ impl Compilation { #[allow(clippy::too_many_arguments)] pub fn new( + compiler_id: CompilerId, options: Arc, plugin_driver: SharedPluginDriver, buildtime_plugin_driver: SharedPluginDriver, @@ -281,6 +284,7 @@ impl Compilation { let incremental = Incremental::new(options.experiments.incremental); Self { id: CompilationId::new(), + compiler_id, hot_index: 0, runtime_template: RuntimeTemplate::new(options.output.environment), records, @@ -354,6 +358,10 @@ impl Compilation { self.id } + pub fn compiler_id(&self) -> CompilerId { + self.compiler_id + } + pub fn swap_make_artifact_with_compilation(&mut self, other: &mut Compilation) { std::mem::swap(&mut self.make_artifact, &mut other.make_artifact); } diff --git a/crates/rspack_core/src/compiler/hmr.rs b/crates/rspack_core/src/compiler/hmr.rs index 2b8d1096ac69..517f5c3a30e6 100644 --- a/crates/rspack_core/src/compiler/hmr.rs +++ b/crates/rspack_core/src/compiler/hmr.rs @@ -73,6 +73,7 @@ impl Compiler { self.plugin_driver.clear_cache(); let mut new_compilation = Compilation::new( + self.id, self.options.clone(), self.plugin_driver.clone(), self.buildtime_plugin_driver.clone(), diff --git a/crates/rspack_core/src/compiler/make/repair/factorize.rs b/crates/rspack_core/src/compiler/make/repair/factorize.rs index 872d5e1ab0f8..4ee0e3dad550 100644 --- a/crates/rspack_core/src/compiler/make/repair/factorize.rs +++ b/crates/rspack_core/src/compiler/make/repair/factorize.rs @@ -9,13 +9,14 @@ use super::{add::AddTask, MakeTaskContext}; use crate::{ module_graph::ModuleGraphModule, utils::task_loop::{Task, TaskResult, TaskType}, - BoxDependency, CompilationId, CompilerOptions, Context, ExportInfoData, ExportsInfoData, - ModuleFactory, ModuleFactoryCreateData, ModuleFactoryResult, ModuleIdentifier, ModuleLayer, - ModuleProfile, Resolve, ResolverFactory, + BoxDependency, CompilationId, CompilerId, CompilerOptions, Context, ExportInfoData, + ExportsInfoData, ModuleFactory, ModuleFactoryCreateData, ModuleFactoryResult, ModuleIdentifier, + ModuleLayer, ModuleProfile, Resolve, ResolverFactory, }; #[derive(Debug)] pub struct FactorizeTask { + pub compiler_id: CompilerId, pub compilation_id: CompilationId, pub module_factory: Arc, pub original_module_identifier: Option, @@ -82,6 +83,7 @@ impl Task for FactorizeTask { // Error and result are not mutually exclusive in webpack module factorization. // Rspack puts results that need to be shared in both error and ok in [ModuleFactoryCreateData]. let mut create_data = ModuleFactoryCreateData { + compiler_id: self.compiler_id, compilation_id: self.compilation_id, resolve_options: self.resolve_options, options: self.options.clone(), diff --git a/crates/rspack_core/src/compiler/make/repair/mod.rs b/crates/rspack_core/src/compiler/make/repair/mod.rs index e4cf8802f1b1..33ad6a9ee7f2 100644 --- a/crates/rspack_core/src/compiler/make/repair/mod.rs +++ b/crates/rspack_core/src/compiler/make/repair/mod.rs @@ -15,11 +15,12 @@ use crate::{ module_graph::{ModuleGraph, ModuleGraphPartial}, old_cache::Cache as OldCache, utils::task_loop::{run_task_loop, Task}, - BuildDependency, Compilation, CompilationId, CompilerOptions, DependencyType, Module, + BuildDependency, Compilation, CompilationId, CompilerId, CompilerOptions, DependencyType, Module, ModuleFactory, ModuleProfile, NormalModuleSource, ResolverFactory, SharedPluginDriver, }; pub struct MakeTaskContext { + pub compiler_id: CompilerId, // compilation info pub compilation_id: CompilationId, pub plugin_driver: SharedPluginDriver, @@ -40,6 +41,7 @@ pub struct MakeTaskContext { impl MakeTaskContext { pub fn new(compilation: &Compilation, artifact: MakeArtifact, cache: Arc) -> Self { Self { + compiler_id: compilation.compiler_id(), compilation_id: compilation.id(), plugin_driver: compilation.plugin_driver.clone(), buildtime_plugin_driver: compilation.buildtime_plugin_driver.clone(), @@ -69,6 +71,7 @@ impl MakeTaskContext { // TODO remove it after incremental rebuild cover all stage pub fn transform_to_temp_compilation(&mut self) -> Compilation { let mut compilation = Compilation::new( + self.compiler_id, self.compiler_options.clone(), self.plugin_driver.clone(), self.buildtime_plugin_driver.clone(), @@ -125,7 +128,8 @@ pub async fn repair( None } }); - Box::new(factorize::FactorizeTask { + Some(Box::new(factorize::FactorizeTask { + compiler_id: compilation.compiler_id(), compilation_id: compilation.id(), module_factory: compilation.get_dependency_factory(dependency), original_module_identifier: parent_module_identifier, diff --git a/crates/rspack_core/src/compiler/make/repair/process_dependencies.rs b/crates/rspack_core/src/compiler/make/repair/process_dependencies.rs index 57236fcbe128..682ae6f8dd6f 100644 --- a/crates/rspack_core/src/compiler/make/repair/process_dependencies.rs +++ b/crates/rspack_core/src/compiler/make/repair/process_dependencies.rs @@ -96,6 +96,7 @@ impl Task for ProcessDependenciesTask { }) .clone(); res.push(Box::new(FactorizeTask { + compiler_id: context.compiler_id, compilation_id: context.compilation_id, module_factory, original_module_identifier: Some(module.identifier()), diff --git a/crates/rspack_core/src/compiler/mod.rs b/crates/rspack_core/src/compiler/mod.rs index b07dc96a4429..4e98fbf0a709 100644 --- a/crates/rspack_core/src/compiler/mod.rs +++ b/crates/rspack_core/src/compiler/mod.rs @@ -2,12 +2,14 @@ mod compilation; mod hmr; pub mod make; mod module_executor; +use std::sync::atomic::AtomicU32; use std::sync::Arc; use rspack_error::Result; use rspack_fs::{IntermediateFileSystem, NativeFileSystem, ReadableFileSystem, WritableFileSystem}; use rspack_futures::FuturesResults; use rspack_hook::define_hook; +use rspack_macros::cacheable; use rspack_paths::{Utf8Path, Utf8PathBuf}; use rspack_sources::BoxSource; use rspack_util::node_path::NodePath; @@ -51,8 +53,27 @@ pub struct CompilerHooks { pub asset_emitted: CompilerAssetEmittedHook, } +static COMPILER_ID: AtomicU32 = AtomicU32::new(0); + +#[cacheable] +#[derive(Debug, Clone, Copy, Hash, Eq, PartialEq, Ord, PartialOrd)] +pub struct CompilerId(u32); + +impl CompilerId { + pub fn new() -> Self { + Self(COMPILER_ID.fetch_add(1, std::sync::atomic::Ordering::Relaxed)) + } +} + +impl Default for CompilerId { + fn default() -> Self { + Self::new() + } +} + #[derive(Debug)] pub struct Compiler { + id: CompilerId, pub compiler_path: String, pub options: Arc, pub output_filesystem: Arc, @@ -127,10 +148,14 @@ impl Compiler { let old_cache = Arc::new(OldCache::new(options.clone())); let module_executor = ModuleExecutor::default(); + let id = CompilerId::new(); + Self { + id, compiler_path, options: options.clone(), compilation: Compilation::new( + id, options, plugin_driver.clone(), buildtime_plugin_driver.clone(), @@ -159,6 +184,10 @@ impl Compiler { } } + pub fn id(&self) -> CompilerId { + self.id + } + pub async fn run(&mut self) -> Result<()> { self.build().await?; Ok(()) @@ -174,6 +203,7 @@ impl Compiler { fast_set( &mut self.compilation, Compilation::new( + self.id, self.options.clone(), self.plugin_driver.clone(), self.buildtime_plugin_driver.clone(), diff --git a/crates/rspack_core/src/compiler/module_executor/entry.rs b/crates/rspack_core/src/compiler/module_executor/entry.rs index 80e3c3c79585..825af5b58fdd 100644 --- a/crates/rspack_core/src/compiler/module_executor/entry.rs +++ b/crates/rspack_core/src/compiler/module_executor/entry.rs @@ -22,6 +22,7 @@ impl Task for EntryTask { module_graph.add_dependency(dep.clone()); Ok(vec![Box::new(FactorizeTask { + compiler_id: context.compiler_id, compilation_id: context.compilation_id, module_factory: context .dependency_factories diff --git a/crates/rspack_core/src/module_factory.rs b/crates/rspack_core/src/module_factory.rs index 862da8f848ee..febd2499029e 100644 --- a/crates/rspack_core/src/module_factory.rs +++ b/crates/rspack_core/src/module_factory.rs @@ -9,13 +9,14 @@ use rspack_paths::ArcPath; use rustc_hash::FxHashSet as HashSet; use crate::{ - cache::persistent::FromContext, BoxDependency, BoxModule, CompilationId, CompilerOptions, - Context, ModuleIdentifier, ModuleLayer, Resolve, ResolverFactory, + cache::persistent::FromContext, BoxDependency, BoxModule, CompilationId, CompilerId, + CompilerOptions, Context, ModuleIdentifier, ModuleLayer, Resolve, ResolverFactory, }; #[cacheable] #[derive(Debug, Clone)] pub struct ModuleFactoryCreateData { + pub compiler_id: CompilerId, pub compilation_id: CompilationId, pub resolve_options: Option>, #[cacheable(with=As)] diff --git a/crates/rspack_plugin_lazy_compilation/src/factory.rs b/crates/rspack_plugin_lazy_compilation/src/factory.rs index 69c045c4daed..09e39097f602 100644 --- a/crates/rspack_plugin_lazy_compilation/src/factory.rs +++ b/crates/rspack_plugin_lazy_compilation/src/factory.rs @@ -33,6 +33,7 @@ impl ModuleFactory for LazyCompilationDependencyFactory { let dep = dep.clone(); let mut create_data = ModuleFactoryCreateData { + compiler_id: data.compiler_id, compilation_id: data.compilation_id, resolve_options: proxy_data.resolve_options.clone(), options: data.options.clone(), diff --git a/crates/rspack_plugin_lazy_compilation/src/plugin.rs b/crates/rspack_plugin_lazy_compilation/src/plugin.rs index 80c5edea21ba..b1b9d7bea274 100644 --- a/crates/rspack_plugin_lazy_compilation/src/plugin.rs +++ b/crates/rspack_plugin_lazy_compilation/src/plugin.rs @@ -3,7 +3,7 @@ use std::{fmt::Debug, sync::Arc}; use rspack_core::{ ApplyContext, BoxModule, Compilation, CompilationId, CompilationParams, CompilerCompilation, - CompilerOptions, DependencyType, EntryDependency, LibIdentOptions, Module, ModuleFactory, + CompilerId, CompilerOptions, DependencyType, EntryDependency, Module, ModuleFactory, ModuleFactoryCreateData, NormalModuleCreateData, NormalModuleFactoryModule, Plugin, PluginContext, }; @@ -30,16 +30,26 @@ pub enum LazyCompilationTest { } pub trait LazyCompilationTestCheck: Send + Sync + Debug { - fn test(&self, compilation_id: CompilationId, module: &dyn Module) -> bool; + fn test( + &self, + compiler_id: CompilerId, + compilation_id: CompilationId, + module: &dyn Module, + ) -> bool; } impl LazyCompilationTest { - fn test(&self, compilation_id: CompilationId, module: &dyn Module) -> bool { + fn test( + &self, + compiler_id: CompilerId, + compilation_id: CompilationId, + module: &dyn Module, + ) -> bool { match self { LazyCompilationTest::Regex(regex) => { regex.test(&module.name_for_condition().unwrap_or("".into())) } - LazyCompilationTest::Fn(f) => f.test(compilation_id, module), + LazyCompilationTest::Fn(f) => f.test(compiler_id, compilation_id, module), } } } @@ -65,9 +75,14 @@ impl LazyCompilationPlugin { Self::new_inner(Mutex::new(backend), entries, imports, test, cacheable) } - fn check_test(&self, compilation_id: CompilationId, module: &BoxModule) -> bool { + fn check_test( + &self, + compiler_id: CompilerId, + compilation_id: CompilationId, + module: &BoxModule, + ) -> bool { if let Some(test) = &self.inner.test { - test.test(compilation_id, module.as_ref()) + test.test(compiler_id, compilation_id, module.as_ref()) } else { true } @@ -150,7 +165,11 @@ async fn normal_module_factory_module( } if WEBPACK_DEV_SERVER_CLIENT_RE.test(&create_data.resource_resolve_data.resource) - || !self.check_test(module_factory_create_data.compilation_id, module) + || !self.check_test( + module_factory_create_data.compiler_id, + module_factory_create_data.compilation_id, + module, + ) { return Ok(()); } diff --git a/crates/rspack_tracing/src/lib.rs b/crates/rspack_tracing/src/lib.rs index b82d572a1798..bde99a976eaa 100644 --- a/crates/rspack_tracing/src/lib.rs +++ b/crates/rspack_tracing/src/lib.rs @@ -17,7 +17,98 @@ pub mod otel { pub use tracing_opentelemetry as tracing; } -pub(crate) enum TraceWriter<'a> { +static IS_TRACING_ENABLED: AtomicBool = AtomicBool::new(false); + +// skip event because it's not useful for performance analysis +struct FilterEvent; + +impl Filter for FilterEvent { + fn enabled( + &self, + meta: &tracing::Metadata<'_>, + _cx: &tracing_subscriber::layer::Context<'_, S>, + ) -> bool { + !meta.is_event() + } +} + +pub fn enable_tracing_by_env(filter: &str, output: &str) { + if !IS_TRACING_ENABLED.swap(true, Ordering::Relaxed) { + use tracing_subscriber::{fmt, prelude::*}; + let layers = generate_common_layers(filter); + let trace_writer = TraceWriter::from(output); + + tracing_subscriber::registry() + // .with(EnvFilter::from_env("TRACE").and_then(rspack_only_layer)) + .with(layers) + .with( + fmt::layer() + .pretty() + .with_file(true) + // To keep track of the closing point of spans + .with_span_events(FmtSpan::CLOSE) + .with_writer(trace_writer.make_writer()), + ) + .init(); + tracing::trace!("enable_tracing_by_env"); + } +} + +fn generate_common_layers( + filter: &str, +) -> Vec + Send + Sync>> { + let default_level = Level::from_str(filter).ok(); + + let mut layers = vec![]; + if let Some(default_level) = default_level { + layers.push( + tracing_subscriber::filter::Targets::new() + .with_targets(vec![ + ("rspack_core", default_level), + ("rspack", default_level), + ("rspack_node", default_level), + ("rspack_plugin_javascript", default_level), + ("rspack_plugin_split_chunks", default_level), + ]) + .boxed(), + ); + } else { + // SAFETY: we know that trace_var is `Ok(StrinG)` now, + // for the second unwrap, if we can't parse the directive, then the tracing result would be + // unexpected, then panic is reasonable + let env_layer = EnvFilter::builder() + .with_regex(true) + .parse(filter) + .expect("Parse tracing directive syntax failed,for details about the directive syntax you could refer https://docs.rs/tracing-subscriber/latest/tracing_subscriber/filter/struct.EnvFilter.html#directives"); + + layers.push(env_layer.boxed()); + } + layers +} + +pub fn enable_tracing_by_env_with_chrome_layer(filter: &str, output: &str) -> Option { + if !IS_TRACING_ENABLED.swap(true, Ordering::Relaxed) { + use tracing_chrome::ChromeLayerBuilder; + use tracing_subscriber::prelude::*; + let trace_writer = TraceWriter::from(output); + let (chrome_layer, guard) = ChromeLayerBuilder::new() + .include_args(true) + .writer(trace_writer.writer()) + .build(); + let layers = generate_common_layers(filter); + // If we don't do this. chrome_layer will collect nothing. + // std::mem::forget(guard); + tracing_subscriber::registry() + .with(layers) + .with(chrome_layer.with_filter(FilterEvent {})) + .init(); + Some(guard) + } else { + None + } +} + +enum TraceWriter<'a> { Stdout, Stderr, File { path: &'a Path },