Skip to content

Commit

Permalink
feat: lazy compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
JSerFeng committed Mar 12, 2024
1 parent da56c4f commit 1281a4f
Show file tree
Hide file tree
Showing 32 changed files with 1,157 additions and 215 deletions.
20 changes: 20 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 21 additions & 1 deletion crates/node_binding/binding.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,8 @@ export enum BuiltinPluginName {
HtmlRspackPlugin = 'HtmlRspackPlugin',
SwcJsMinimizerRspackPlugin = 'SwcJsMinimizerRspackPlugin',
SwcCssMinimizerRspackPlugin = 'SwcCssMinimizerRspackPlugin',
BundlerInfoRspackPlugin = 'BundlerInfoRspackPlugin'
BundlerInfoRspackPlugin = 'BundlerInfoRspackPlugin',
LazyCompilation = 'LazyCompilation'
}

export function cleanupGlobalTrace(): void
Expand Down Expand Up @@ -915,6 +916,14 @@ export interface RawJavascriptParserOptions {
url: string
}

export interface RawLazyCompilationOption {
module: (...args: any[]) => any
dispose: (...args: any[]) => any
test?: RawRegexMatcher
entries: boolean
imports: boolean
}

export interface RawLibraryAuxiliaryComment {
root?: string
commonjs?: string
Expand Down Expand Up @@ -950,6 +959,11 @@ export interface RawLimitChunkCountPluginOptions {
maxChunks: number
}

export interface RawModuleArg {
module: string
path: string
}

export interface RawModuleFilenameTemplateFnCtx {
identifier: string
shortIdentifier: string
Expand All @@ -964,6 +978,12 @@ export interface RawModuleFilenameTemplateFnCtx {
namespace: string
}

export interface RawModuleInfo {
active: boolean
client: string
data: string
}

export interface RawModuleOptions {
rules: Array<RawModuleRule>
parser?: Record<string, RawParserOptions>
Expand Down
1 change: 1 addition & 0 deletions crates/rspack_binding_options/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ rspack_plugin_hmr = { path = "../rspack_plugin_hmr" }
rspack_plugin_html = { path = "../rspack_plugin_html" }
rspack_plugin_javascript = { path = "../rspack_plugin_javascript" }
rspack_plugin_json = { path = "../rspack_plugin_json" }
rspack_plugin_lazy_compilation = { path = "../rspack_plugin_lazy_compilation" }
rspack_plugin_library = { path = "../rspack_plugin_library" }
rspack_plugin_limit_chunk_count = { path = "../rspack_plugin_limit_chunk_count" }
rspack_plugin_merge_duplicate_chunks = { path = "../rspack_plugin_merge_duplicate_chunks" }
Expand Down
24 changes: 23 additions & 1 deletion crates/rspack_binding_options/src/options/raw_builtins/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mod raw_banner;
mod raw_bundle_info;
mod raw_copy;
mod raw_html;
mod raw_lazy_compilation;
mod raw_limit_chunk_count;
mod raw_mf;
mod raw_progress;
Expand All @@ -10,7 +11,7 @@ mod raw_to_be_deprecated;

use napi::{bindgen_prelude::FromNapiValue, JsUnknown};
use napi_derive::napi;
use rspack_core::{BoxPlugin, Define, DefinePlugin, PluginExt, Provide, ProvidePlugin};
use rspack_core::{BoxPlugin, Define, DefinePlugin, Plugin, PluginExt, Provide, ProvidePlugin};
use rspack_error::Result;
use rspack_ids::{
DeterministicChunkIdsPlugin, DeterministicModuleIdsPlugin, NamedChunkIdsPlugin,
Expand Down Expand Up @@ -58,6 +59,7 @@ use rspack_plugin_warn_sensitive_module::WarnCaseSensitiveModulesPlugin;
use rspack_plugin_wasm::{enable_wasm_loading_plugin, AsyncWasmPlugin};
use rspack_plugin_web_worker_template::web_worker_template_plugin;
use rspack_plugin_worker::WorkerPlugin;
use rspack_regex::RspackRegex;

pub use self::{
raw_banner::RawBannerPluginOptions, raw_copy::RawCopyRspackPluginOptions,
Expand All @@ -67,6 +69,7 @@ pub use self::{
};
use self::{
raw_bundle_info::{RawBundlerInfoModeWrapper, RawBundlerInfoPluginOptions},
raw_lazy_compilation::{JsBackend, RawLazyCompilationOption},
raw_mf::{RawConsumeSharedPluginOptions, RawContainerReferencePluginOptions, RawProvideOptions},
};
use crate::{
Expand Down Expand Up @@ -138,6 +141,8 @@ pub enum BuiltinPluginName {
SwcJsMinimizerRspackPlugin,
SwcCssMinimizerRspackPlugin,
BundlerInfoRspackPlugin,

LazyCompilation,
}

#[napi(object)]
Expand Down Expand Up @@ -391,6 +396,23 @@ impl BuiltinPlugin {
.boxed(),
)
}
BuiltinPluginName::LazyCompilation => {
let options = downcast_into::<RawLazyCompilationOption>(self.options)?;
let js_backend = JsBackend::try_from(&options).expect("failed to convert function");
plugins.push(Box::new(
rspack_plugin_lazy_compilation::plugin::LazyCompilationPlugin::new(
js_backend,
options.test.map(|s| {
RspackRegex::with_flags(&s.source, &s.flags).unwrap_or_else(|_| {
let msg = format!("[lazyCompilation]incorrect regex {:?}", s);
panic!("{msg}");
})
}),
options.entries,
options.imports,
),
) as Box<dyn Plugin>)
}
}
Ok(())
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
use napi::{Env, JsFunction, Result};
use napi_derive::napi;
use rspack_binding_macros::js_fn_into_threadsafe_fn;
use rspack_core::ModuleIdentifier;
use rspack_napi_shared::{
get_napi_env,
threadsafe_function::{ThreadsafeFunction, ThreadsafeFunctionCallMode},
};
use rspack_plugin_lazy_compilation::backend::{Backend, ModuleInfo};

use crate::RawRegexMatcher;

#[napi(object)]
pub struct RawModuleInfo {
pub active: bool,
pub client: String,
pub data: String,
}

#[napi(object)]
pub struct RawLazyCompilationOption {
pub module: JsFunction,
pub dispose: JsFunction,
pub test: Option<RawRegexMatcher>,
pub entries: bool,
pub imports: bool,
}

#[napi(object)]
pub struct RawModuleArg {
pub module: String,
pub path: String,
}

pub(crate) struct JsBackend {
module: ThreadsafeFunction<RawModuleArg, RawModuleInfo>,
dispose: ThreadsafeFunction<(), ()>,
}

impl std::fmt::Debug for JsBackend {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("JsBackend").finish()
}
}

impl TryFrom<&RawLazyCompilationOption> for JsBackend {
type Error = napi::Error;
fn try_from(value: &RawLazyCompilationOption) -> Result<Self> {
Ok(Self {
module: js_fn_into_threadsafe_fn!(value.module, Env::from(get_napi_env())),
dispose: js_fn_into_threadsafe_fn!(value.dispose, Env::from(get_napi_env())),
})
}
}

#[async_trait::async_trait]
impl Backend for JsBackend {
async fn module(
&mut self,
identifier: ModuleIdentifier,
path: String,
) -> rspack_error::Result<ModuleInfo> {
let module_info = self
.module
.call(
RawModuleArg {
module: identifier.to_string(),
path,
},
ThreadsafeFunctionCallMode::NonBlocking,
)
.map_err(|err| rspack_error::error!("failed to invoke backend.module: {}", err))?
.await
.expect("channel should have result")?;

Ok(ModuleInfo {
active: module_info.active,
client: module_info.client,
data: module_info.data,
})
}

async fn dispose(&mut self) -> rspack_error::Result<()> {
self
.dispose
.call((), ThreadsafeFunctionCallMode::NonBlocking)
.map_err(|err| rspack_error::error!("failed to invoke backend.dispose: {}", err))?
.await
.expect("channel should have result")?;

Ok(())
}
}
2 changes: 2 additions & 0 deletions crates/rspack_core/src/dependency/dependency_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ pub enum DependencyType {
/// Webpack is included
WebpackIsIncluded,
LoaderImport,
LazyImport,
Custom(Box<str>), // TODO it will increase large layout size
}

Expand Down Expand Up @@ -149,6 +150,7 @@ impl DependencyType {
DependencyType::ProvideModuleForShared => Cow::Borrowed("provide module for shared"),
DependencyType::ConsumeSharedFallback => Cow::Borrowed("consume shared fallback"),
DependencyType::WebpackIsIncluded => Cow::Borrowed("__webpack_is_included__"),
DependencyType::LazyImport => Cow::Borrowed("lazy import()"),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/rspack_core/src/module_factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use sugar_path::SugarPathBuf;

use crate::{BoxDependency, BoxModule, Context, FactoryMeta, ModuleIdentifier, Resolve};

#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct ModuleFactoryCreateData {
pub resolve_options: Option<Box<Resolve>>,
pub context: Context,
Expand Down
8 changes: 6 additions & 2 deletions crates/rspack_core/src/normal_module_factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use rspack_error::{error, Result};
use rspack_hook::AsyncSeriesBailHook;
use rspack_loader_runner::{get_scheme, Loader, Scheme};
use sugar_path::{AsPath, SugarPath};
use swc_core::common::Span;
use swc_core::common::{util::take::Take, Span};

use crate::{
cache::Cache,
Expand Down Expand Up @@ -575,13 +575,16 @@ impl NormalModuleFactory {
)
})?();

let mut diagnostics = data.diagnostics.take();
let mut create_data = NormalModuleCreateData {
dependency_type: data.dependency.dependency_type().clone(),
resolve_data_request: dependency.request(),
resource_resolve_data: resource_data.clone(),
context: data.context.clone(),
diagnostics: &mut data.diagnostics,
diagnostics: &mut diagnostics,
create_data: data,
};

let module = if let Some(module) = self
.plugin_driver
.normal_module_factory_create_module(&mut create_data)
Expand Down Expand Up @@ -610,6 +613,7 @@ impl NormalModuleFactory {
.plugin_driver
.normal_module_factory_module(module, &mut create_data)
.await?;
data.diagnostics = diagnostics;

data.add_file_dependencies(file_dependencies);
data.add_file_dependency(file_dependency);
Expand Down
6 changes: 4 additions & 2 deletions crates/rspack_core/src/plugin/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ use rustc_hash::FxHashSet as HashSet;

use crate::{
BoxModule, Chunk, ChunkInitFragments, ChunkUkey, Compilation, Context, ContextModuleFactory,
DependencyCategory, DependencyType, ErrorSpan, FactoryMeta, ModuleDependency, ModuleIdentifier,
NormalModuleFactory, Resolve, RuntimeGlobals, SharedPluginDriver, Stats,
DependencyCategory, DependencyType, ErrorSpan, FactoryMeta, ModuleDependency,
ModuleFactoryCreateData, ModuleIdentifier, NormalModuleFactory, Resolve, RuntimeGlobals,
SharedPluginDriver, Stats,
};

#[derive(Debug)]
Expand Down Expand Up @@ -80,6 +81,7 @@ pub struct NormalModuleCreateData<'a> {
pub resource_resolve_data: ResourceData,
pub context: Context,
pub diagnostics: &'a mut Vec<Diagnostic>,
pub create_data: &'a ModuleFactoryCreateData,
}

#[derive(Debug, Clone)]
Expand Down
8 changes: 8 additions & 0 deletions crates/rspack_core/src/utils/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ impl<T, K: Eq + PartialEq + std::hash::Hash> WorkerQueue<T, K> {
}
}

pub fn len(&self) -> usize {
self.inner.len()
}

pub fn is_empty(&self) -> bool {
self.inner.is_empty()
}

pub fn add_task(&mut self, task: T) -> usize {
self.inner.push_back(task);
self.inner.len()
Expand Down
8 changes: 8 additions & 0 deletions crates/rspack_database/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ impl<Item: Any> Database<Item> {
}
}

pub fn len(&self) -> usize {
self.inner.len()
}

pub fn is_empty(&self) -> bool {
self.inner.is_empty()
}

pub fn contains(&self, id: &Ukey<Item>) -> bool {
self.inner.contains_key(id)
}
Expand Down
5 changes: 0 additions & 5 deletions crates/rspack_plugin_hmr/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,6 @@ impl AsyncSeries<Compilation> for HotModuleReplacementPluginProcessAssetsHook {
}
}

// println!(
// "updated_modules: {:?}\n, remove modules {:?}",
// updated_modules, completely_removed_modules
// );

for (identifier, old_runtime_module_content) in &old_runtime_modules {
if let Some(new_runtime_module_content) = now_runtime_modules.get(identifier) {
// updated
Expand Down
21 changes: 21 additions & 0 deletions crates/rspack_plugin_lazy_compilation/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[package]
edition = "2021"
license = "MIT"
name = "rspack_plugin_lazy_compilation"
version = "0.1.0"

[dependencies]
async-trait = { workspace = true }
dashmap = { workspace = true }
once_cell = { workspace = true }
regex = { workspace = true }
rustc-hash = { workspace = true }
tokio = { workspace = true }
urlencoding = { workspace = true }

rspack_core = { path = "../rspack_core" }
rspack_error = { path = "../rspack_error" }
rspack_identifier = { path = "../rspack_identifier" }
rspack_plugin_javascript = { path = "../rspack_plugin_javascript" }
rspack_regex = { path = "../rspack_regex" }
rspack_util = { path = "../rspack_util" }
Loading

0 comments on commit 1281a4f

Please sign in to comment.