-
Notifications
You must be signed in to change notification settings - Fork 413
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: tree shake module side effect (#1395)
- Loading branch information
1 parent
3cf75b8
commit 6df56a9
Showing
27 changed files
with
272 additions
and
9 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
29 changes: 29 additions & 0 deletions
29
crates/rolldown_binding/src/options/binding_input_options/treeshake.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,29 @@ | ||
use rolldown::{InnerOptions, ModuleSideEffects}; | ||
use rolldown_common::js_regex::HybridRegex; | ||
use serde::Deserialize; | ||
|
||
#[napi_derive::napi(object)] | ||
#[derive(Deserialize, Debug, Default)] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct BindingTreeshake { | ||
pub module_side_effects: String, | ||
} | ||
|
||
impl TryFrom<BindingTreeshake> for rolldown::TreeshakeOptions { | ||
fn try_from(value: BindingTreeshake) -> anyhow::Result<Self> { | ||
match value.module_side_effects.as_str() { | ||
"true" => { | ||
Ok(Self::Option(InnerOptions { module_side_effects: ModuleSideEffects::Boolean(true) })) | ||
} | ||
"false" => { | ||
Ok(Self::Option(InnerOptions { module_side_effects: ModuleSideEffects::Boolean(false) })) | ||
} | ||
_ => { | ||
let regex = HybridRegex::new(&value.module_side_effects)?; | ||
Ok(Self::Option(InnerOptions { module_side_effects: ModuleSideEffects::Regex(regex) })) | ||
} | ||
} | ||
} | ||
|
||
type Error = anyhow::Error; | ||
} |
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
40 changes: 40 additions & 0 deletions
40
crates/rolldown_common/src/inner_bundler_options/types/treeshake.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,40 @@ | ||
use crate::types::js_regex::HybridRegex; | ||
|
||
#[derive(Debug)] | ||
pub enum TreeshakeOptions { | ||
False, | ||
Option(InnerOptions), | ||
} | ||
|
||
impl Default for TreeshakeOptions { | ||
/// Used for snapshot testing | ||
fn default() -> Self { | ||
TreeshakeOptions::Option(InnerOptions { module_side_effects: ModuleSideEffects::Boolean(true) }) | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
pub enum ModuleSideEffects { | ||
Regex(HybridRegex), | ||
Boolean(bool), | ||
} | ||
|
||
impl ModuleSideEffects { | ||
pub fn resolve(&self, path: &str) -> bool { | ||
match self { | ||
ModuleSideEffects::Regex(reg) => reg.matches(path), | ||
ModuleSideEffects::Boolean(b) => *b, | ||
} | ||
} | ||
} | ||
|
||
impl TreeshakeOptions { | ||
pub fn enabled(&self) -> bool { | ||
matches!(self, TreeshakeOptions::Option(_)) | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct InnerOptions { | ||
pub module_side_effects: ModuleSideEffects, | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/// According to the doc of `regress`, https://docs.rs/regress/0.10.0/regress/#comparison-to-regex-crate | ||
/// **regress supports features that regex does not, in particular backreferences and zero-width lookaround assertions.** | ||
/// these features are not commonly used, so in most cases the slow path will not be reached. | ||
#[derive(Debug)] | ||
pub enum HybridRegex { | ||
Optimize(regex::Regex), | ||
Ecma(regress::Regex), | ||
} | ||
|
||
impl HybridRegex { | ||
pub fn new(source: &str) -> anyhow::Result<Self> { | ||
match regex::Regex::new(source).map(HybridRegex::Optimize) { | ||
Ok(reg) => Ok(reg), | ||
Err(_) => regress::Regex::new(source).map(HybridRegex::Ecma).map_err(anyhow::Error::from), | ||
} | ||
} | ||
|
||
pub fn matches(&self, text: &str) -> bool { | ||
match self { | ||
HybridRegex::Optimize(reg) => reg.is_match(text), | ||
HybridRegex::Ecma(reg) => reg.find(text).is_some(), | ||
} | ||
} | ||
} |
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
Oops, something went wrong.