-
-
Notifications
You must be signed in to change notification settings - Fork 587
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: πΈ json tree shaking (#4858)
* 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
1 parent
77f57e5
commit 1ca2e0f
Showing
19 changed files
with
345 additions
and
34 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
crates/rspack_plugin_json/src/json_exports_dependency.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Oops, something went wrong.