Skip to content

Commit

Permalink
feat: lazy compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
JSerFeng committed May 14, 2024
1 parent 9b0c660 commit d8fe4a5
Show file tree
Hide file tree
Showing 40 changed files with 2,144 additions and 78 deletions.
18 changes: 18 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 @@ -171,7 +171,8 @@ export enum BuiltinPluginName {
SwcCssMinimizerRspackPlugin = 'SwcCssMinimizerRspackPlugin',
BundlerInfoRspackPlugin = 'BundlerInfoRspackPlugin',
CssExtractRspackPlugin = 'CssExtractRspackPlugin',
JsLoaderRspackPlugin = 'JsLoaderRspackPlugin'
JsLoaderRspackPlugin = 'JsLoaderRspackPlugin',
LazyCompilationPlugin = 'LazyCompilationPlugin'
}

export function cleanupGlobalTrace(): void
Expand Down Expand Up @@ -964,6 +965,14 @@ export interface RawJavascriptParserOptions {
wrappedContextCritical: boolean
}

export interface RawLazyCompilationOption {
module: (err: Error | null, arg: RawModuleArg) => any
test?: RawRegexMatcher
entries: boolean
imports: boolean
cacheable: boolean
}

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

export interface RawModuleArg {
module: string
path: string
}

export interface RawModuleFilenameTemplateFnCtx {
identifier: string
shortIdentifier: string
Expand All @@ -1013,6 +1027,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 @@ -41,6 +41,7 @@ rspack_plugin_html = { path = "../rspack_plugin_html" }
rspack_plugin_ignore = { path = "../rspack_plugin_ignore" }
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 @@ -4,6 +4,7 @@ mod raw_copy;
mod raw_css_extract;
mod raw_html;
mod raw_ignore;
mod raw_lazy_compilation;
mod raw_limit_chunk_count;
mod raw_mf;
mod raw_progress;
Expand All @@ -14,7 +15,7 @@ mod raw_to_be_deprecated;

use napi::{bindgen_prelude::FromNapiValue, Env, 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 @@ -68,6 +69,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 @@ -79,6 +81,7 @@ pub use self::{
use self::{
raw_bundle_info::{RawBundlerInfoModeWrapper, RawBundlerInfoPluginOptions},
raw_css_extract::RawCssExtractPluginOption,
raw_lazy_compilation::{JsBackend, RawLazyCompilationOption},
raw_mf::{RawConsumeSharedPluginOptions, RawContainerReferencePluginOptions, RawProvideOptions},
raw_runtime_chunk::RawRuntimeChunkOptions,
raw_size_limits::RawSizeLimitsPluginOptions,
Expand Down Expand Up @@ -165,6 +168,7 @@ pub enum BuiltinPluginName {
// rspack js adapter plugins
// naming format follow XxxRspackPlugin
JsLoaderRspackPlugin,
LazyCompilationPlugin,
}

#[napi(object)]
Expand Down Expand Up @@ -468,6 +472,24 @@ impl BuiltinPlugin {
JsLoaderResolverPlugin::new(downcast_into::<JsLoaderRunner>(self.options)?).boxed(),
);
}
BuiltinPluginName::LazyCompilationPlugin => {
let options = downcast_into::<RawLazyCompilationOption>(self.options)?;
let js_backend = JsBackend::from(&options);
plugins.push(Box::new(
rspack_plugin_lazy_compilation::plugin::LazyCompilationPlugin::new(
options.cacheable,
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,70 @@
use napi_derive::napi;
use rspack_core::ModuleIdentifier;
use rspack_napi::threadsafe_function::ThreadsafeFunction;
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, object_to_js = false)]
pub struct RawLazyCompilationOption {
pub module: ThreadsafeFunction<RawModuleArg, RawModuleInfo>,
pub test: Option<RawRegexMatcher>,
pub entries: bool,
pub imports: bool,
pub cacheable: bool,
}

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

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

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

impl From<&RawLazyCompilationOption> for JsBackend {
fn from(value: &RawLazyCompilationOption) -> Self {
Self {
module: value.module.clone(),
}
}
}

#[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,
})
.await
.expect("channel should have result");

Ok(ModuleInfo {
active: module_info.active,
client: module_info.client,
data: module_info.data,
})
}
}
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::SugarPath;

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

#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct ModuleFactoryCreateData {
pub resolve_options: Option<Box<Resolve>>,
pub context: Context,
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

0 comments on commit d8fe4a5

Please sign in to comment.