-
-
Notifications
You must be signed in to change notification settings - Fork 496
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(minifier): add ReplaceGlobalDefinitions ast pass
- Loading branch information
Showing
9 changed files
with
109 additions
and
4 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
77 changes: 77 additions & 0 deletions
77
crates/oxc_minifier/src/ast_passes/replace_global_defines.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,77 @@ | ||
use oxc_allocator::Allocator; | ||
use oxc_ast::{ | ||
ast::*, | ||
// visit::walk_mut::{walk_expression_mut, walk_statements_mut}, | ||
AstBuilder, | ||
VisitMut, | ||
}; | ||
use oxc_diagnostics::OxcDiagnostic; | ||
use oxc_parser::Parser; | ||
use oxc_span::SourceType; | ||
|
||
struct IdentifierDefine<'a> { | ||
key: String, | ||
value: Expression<'a>, | ||
} | ||
|
||
impl<'a> IdentifierDefine<'a> { | ||
pub fn new(key: String, value: Expression<'a>) -> Self { | ||
Self { key, value } | ||
} | ||
} | ||
|
||
/// Replace Global Defines | ||
/// | ||
/// References: | ||
/// | ||
/// * <https://github.com/terser/terser?tab=readme-ov-file#conditional-compilation> | ||
/// * <https://esbuild.github.io/api/#define> | ||
pub struct ReplaceGlobalDefines<'a> { | ||
ast: AstBuilder<'a>, | ||
identifier_defines: Vec<IdentifierDefine<'a>>, | ||
} | ||
|
||
impl<'a> ReplaceGlobalDefines<'a> { | ||
pub fn new(allocator: &'a Allocator) -> Self { | ||
Self { ast: AstBuilder::new(allocator), identifier_defines: vec![] } | ||
} | ||
|
||
pub fn with_definitions<S>(mut self, key_values: &[(S, S)]) -> Result<Self, Vec<OxcDiagnostic>> | ||
where | ||
S: AsRef<str>, | ||
{ | ||
for (key, value) in key_values.iter() { | ||
self.add_definition(key.as_ref(), value.as_ref())?; | ||
} | ||
Ok(self) | ||
} | ||
|
||
fn add_definition(&mut self, key: &str, value: &str) -> Result<(), Vec<OxcDiagnostic>> { | ||
// TODO: add dot defines | ||
|
||
let source_text = self.ast.allocator.alloc(value.to_string()); | ||
let value = self.parse(source_text)?; | ||
self.identifier_defines.push(IdentifierDefine::new(key.to_string(), value)); | ||
Ok(()) | ||
} | ||
|
||
// TODO: add `parse_expression` to parser | ||
fn parse(&self, source_text: &'a str) -> Result<Expression<'a>, Vec<OxcDiagnostic>> { | ||
let mut ret = Parser::new(&self.ast.allocator, source_text, SourceType::default()).parse(); | ||
if !ret.errors.is_empty() { | ||
return Err(ret.errors); | ||
} | ||
let Some(Statement::ExpressionStatement(expr_stmt)) = ret.program.body.get_mut(0) else { | ||
return Err(vec![OxcDiagnostic::error(format!( | ||
"Expression `{source_text}` cannot be parsed." | ||
))]); | ||
}; | ||
Ok(self.ast.move_expression(&mut expr_stmt.expression)) | ||
} | ||
|
||
pub fn build(&mut self, program: &mut Program<'a>) { | ||
self.visit_program(program); | ||
} | ||
} | ||
|
||
impl<'a> VisitMut<'a> for ReplaceGlobalDefines<'a> {} |
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 |
---|---|---|
|
@@ -2,3 +2,4 @@ mod code_removal; | |
mod folding; | ||
mod precedence; | ||
mod remove_dead_code; | ||
mod replace_global_defines; |
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 @@ | ||
use oxc_allocator::Allocator; | ||
use oxc_codegen::WhitespaceRemover; | ||
use oxc_minifier::ReplaceGlobalDefines; | ||
use oxc_parser::Parser; | ||
use oxc_span::SourceType; | ||
|
||
fn minify(source_text: &str, defs: &[(&str, &str)]) -> String { | ||
let source_type = SourceType::default(); | ||
let allocator = Allocator::default(); | ||
let ret = Parser::new(&allocator, source_text, source_type).parse(); | ||
let program = allocator.alloc(ret.program); | ||
ReplaceGlobalDefines::new(&allocator).with_definitions(defs.into()).unwrap().build(program); | ||
WhitespaceRemover::new().build(program).source_text | ||
} | ||
|
||
pub(crate) fn test(source_text: &str, expected: &str, defs: &[(&str, &str)]) { | ||
let minified = minify(source_text, defs); | ||
assert_eq!(minified, expected, "for source {}", source_text); | ||
} | ||
|
||
#[test] | ||
fn replace_global_definitions() { | ||
test("id, str", "text, \"text\"", &[("id", "str"), ("id", "'str'")]); | ||
} |