Skip to content

Commit

Permalink
feat: 🎸 json tree shaking (#4858)
Browse files Browse the repository at this point in the history
* feat: 🎸 json tree shaking

* chore: πŸ€– ck point

* chore: πŸ€– ck points

* chore: πŸ€– ck point

* chore: πŸ€– partial runtime opt

* chore: πŸ€– rebase

* fix: πŸ› mangle issue

* chore: πŸ€– update

* chore: πŸ€– clean

* chore: πŸ€– lint

* chore: πŸ€– update snap

* chore: πŸ€– recover

* chore: πŸ€– move import-named to packages/rspack/tests

* chore: πŸ€– json.parse opt

* fix: πŸ› ci issue

* chore: πŸ€– update snap

* fix: πŸ› compile error

* chore: πŸ€– snapshot
  • Loading branch information
IWANABETHATGUY authored Dec 12, 2023
1 parent 77f57e5 commit 1ca2e0f
Show file tree
Hide file tree
Showing 19 changed files with 345 additions and 34 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions crates/rspack_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ glob-match = "0.2.1"
hashlink = { workspace = true }
indexmap = { workspace = true }
itertools = { workspace = true }
json = { workspace = true }
mime_guess = { workspace = true }
nodejs-resolver = { version = "0.1.1" }
once_cell = { workspace = true }
Expand Down
3 changes: 2 additions & 1 deletion crates/rspack_core/src/exports_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,8 @@ impl ExportsInfoId {
changed
}

/// # Panic
/// this function would panic if name doesn't exists in current exportsInfo
pub fn get_read_only_export_info<'a>(
&self,
name: &JsWord,
Expand Down Expand Up @@ -1364,7 +1366,6 @@ impl ExportInfo {
);
let new_exports_info = ExportsInfo::new(other_exports_info.id, side_effects_only_info.id);
let new_exports_info_id = new_exports_info.id;

mg.exports_info_map
.insert(new_exports_info_id, new_exports_info);
mg.export_info_map
Expand Down
2 changes: 2 additions & 0 deletions crates/rspack_core/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::path::PathBuf;
use std::{any::Any, borrow::Cow, fmt::Debug};

use async_trait::async_trait;
use json::JsonValue;
use rspack_error::{IntoTWithDiagnosticArray, Result, TWithDiagnosticArray};
use rspack_hash::{RspackHash, RspackHashDigest};
use rspack_identifier::{Identifiable, Identifier};
Expand Down Expand Up @@ -47,6 +48,7 @@ pub struct BuildInfo {
pub harmony_named_exports: HashSet<JsWord>,
pub all_star_exports: Vec<DependencyId>,
pub need_create_require: bool,
pub json_data: Option<JsonValue>,
}

#[derive(Debug, Default, Clone, Hash, PartialEq, Eq)]
Expand Down
6 changes: 6 additions & 0 deletions crates/rspack_core/src/utils/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,12 @@ pub fn extract_member_expression_chain<'e, T: Into<MaybeExpr<'e>>>(
}) = expr.prop
{
members.push_front((val.value.clone(), val.span.ctxt));
} else if let MemberProp::Computed(ComputedPropName {
expr: box Expr::Lit(Lit::Num(ref val)),
..
}) = expr.prop
{
members.push_front((val.value.to_string().into(), val.span.ctxt));
} else if let MemberProp::Ident(ref ident) = expr.prop {
members.push_front((ident.sym.clone(), ident.span.ctxt));
members_spans.push_front(ident.span);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,15 +262,7 @@ impl ModuleDependency for HarmonyImportSpecifierDependency {
}

fn get_condition(&self) -> Option<DependencyCondition> {
// dbg!(
// &self.ids,
// &self.specifier,
// self.request(),
// self.used_by_exports.as_ref()
// );
let ret = get_dependency_used_by_exports_condition(self.id, self.used_by_exports.as_ref());
// dbg!(&ret);
ret
get_dependency_used_by_exports_condition(self.id, self.used_by_exports.as_ref())
}

fn get_referenced_exports(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,6 @@ impl Visit for HarmonyImportRefDependencyScanner<'_> {
.map(|item| item.0.clone())
.collect::<Vec<_>>(),
);
// dbg!(&ids);
self
.rewrite_usage_span
.insert(member_expr.span, ExtraSpanInfo::ReWriteUsedByExports);
Expand Down Expand Up @@ -596,6 +595,7 @@ mod test {
harmony_named_exports: Default::default(),
all_star_exports: Default::default(),
need_create_require: false,
json_data: None,
};
let mut import_map = Default::default();
let mut deps = vec![];
Expand Down
101 changes: 101 additions & 0 deletions crates/rspack_plugin_json/src/json_exports_dependency.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
use json::JsonValue;
use rspack_core::{
AsContextDependency, AsModuleDependency, Dependency, DependencyId, DependencyTemplate,
ExportNameOrSpec, ExportSpec, ExportsOfExportsSpec, ExportsSpec, ModuleGraph, TemplateContext,
TemplateReplaceSource,
};
#[derive(Debug, Clone)]
pub struct JsonExportsDependency {
id: DependencyId,
data: JsonValue,
}

impl JsonExportsDependency {
pub fn new(data: JsonValue) -> Self {
Self {
data,
id: DependencyId::new(),
}
}
}

impl Dependency for JsonExportsDependency {
fn id(&self) -> &rspack_core::DependencyId {
&self.id
}

fn dependency_debug_name(&self) -> &'static str {
"JsonExportsDependency"
}

fn get_exports(&self, _mg: &ModuleGraph) -> Option<ExportsSpec> {
Some(ExportsSpec {
exports: get_exports_from_data(&self.data).unwrap_or(ExportsOfExportsSpec::Null),
..Default::default()
})
}
}

impl AsModuleDependency for JsonExportsDependency {}
impl AsContextDependency for JsonExportsDependency {}

impl DependencyTemplate for JsonExportsDependency {
fn apply(
&self,
_source: &mut TemplateReplaceSource,
_code_generatable_context: &mut TemplateContext,
) {
}
}

fn get_exports_from_data(data: &JsonValue) -> Option<ExportsOfExportsSpec> {
let ret = match data {
JsonValue::Null
| JsonValue::Short(_)
| JsonValue::String(_)
| JsonValue::Number(_)
| JsonValue::Boolean(_) => {
return None;
}
JsonValue::Object(obj) => ExportsOfExportsSpec::Array(
obj
.iter()
.map(|(k, v)| {
ExportNameOrSpec::ExportSpec(ExportSpec {
name: k.into(),
can_mangle: Some(true),
exports: get_exports_from_data(v).map(|item| match item {
ExportsOfExportsSpec::True => unreachable!(),
ExportsOfExportsSpec::Null => unreachable!(),
ExportsOfExportsSpec::Array(arr) => arr,
}),
..Default::default()
})
})
.collect::<Vec<_>>(),
),
JsonValue::Array(arr) => {
if arr.len() > 100 {
return None;
}
ExportsOfExportsSpec::Array(
arr
.iter()
.enumerate()
.map(|(i, item)| {
ExportNameOrSpec::ExportSpec(ExportSpec {
name: format!("{i}").into(),
can_mangle: Some(true),
exports: get_exports_from_data(item).map(|item| match item {
ExportsOfExportsSpec::True | ExportsOfExportsSpec::Null => unreachable!(),
ExportsOfExportsSpec::Array(arr) => arr,
}),
..Default::default()
})
})
.collect::<Vec<_>>(),
)
}
};
Some(ret)
}
Loading

0 comments on commit 1ca2e0f

Please sign in to comment.