From b94f6bac64512da4a588a1cf28bcb63d94e650b8 Mon Sep 17 00:00:00 2001 From: Boshen Date: Wed, 25 Dec 2024 20:39:11 +0800 Subject: [PATCH] feat(tasks/minsize): define `process.env.NODE_ENV` for react --- Cargo.lock | 2 ++ tasks/minsize/Cargo.toml | 2 ++ tasks/minsize/minsize.snap | 2 +- tasks/minsize/src/lib.rs | 9 +++++++++ 4 files changed, 14 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index d1d2be451c7269..f3b71332323a59 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1793,8 +1793,10 @@ dependencies = [ "oxc_codegen", "oxc_minifier", "oxc_parser", + "oxc_semantic", "oxc_span", "oxc_tasks_common", + "oxc_transformer", "rustc-hash", ] diff --git a/tasks/minsize/Cargo.toml b/tasks/minsize/Cargo.toml index bfc2bd01ed9c01..f3e25e0ec7db7f 100644 --- a/tasks/minsize/Cargo.toml +++ b/tasks/minsize/Cargo.toml @@ -23,6 +23,8 @@ oxc_codegen = { workspace = true } oxc_minifier = { workspace = true } oxc_parser = { workspace = true } oxc_span = { workspace = true } +oxc_transformer = { workspace = true } +oxc_semantic = { workspace = true } flate2 = { workspace = true } oxc_tasks_common = { workspace = true } diff --git a/tasks/minsize/minsize.snap b/tasks/minsize/minsize.snap index 76c69877c44b5e..bc9fede0fae175 100644 --- a/tasks/minsize/minsize.snap +++ b/tasks/minsize/minsize.snap @@ -1,7 +1,7 @@ | Oxc | ESBuild | Oxc | ESBuild | Original | minified | minified | gzip | gzip | Fixture ------------------------------------------------------------------------------------- -72.14 kB | 24.04 kB | 23.70 kB | 8.61 kB | 8.54 kB | react.development.js +72.14 kB | 24.00 kB | 23.70 kB | 8.58 kB | 8.54 kB | react.development.js 173.90 kB | 61.55 kB | 59.82 kB | 19.54 kB | 19.33 kB | moment.js diff --git a/tasks/minsize/src/lib.rs b/tasks/minsize/src/lib.rs index f6f7d8eed3def0..e3655b6977010a 100644 --- a/tasks/minsize/src/lib.rs +++ b/tasks/minsize/src/lib.rs @@ -11,8 +11,10 @@ use oxc_allocator::Allocator; use oxc_codegen::{CodeGenerator, CodegenOptions}; use oxc_minifier::{CompressOptions, MangleOptions, Minifier, MinifierOptions}; use oxc_parser::Parser; +use oxc_semantic::SemanticBuilder; use oxc_span::SourceType; use oxc_tasks_common::{project_root, TestFile, TestFiles}; +use oxc_transformer::{ReplaceGlobalDefines, ReplaceGlobalDefinesConfig}; use rustc_hash::FxHashMap; // #[test] @@ -137,6 +139,13 @@ fn minify(source_text: &str, source_type: SourceType, options: MinifierOptions) let allocator = Allocator::default(); let ret = Parser::new(&allocator, source_text, source_type).parse(); let mut program = ret.program; + let (symbols, scopes) = + SemanticBuilder::new().build(&program).semantic.into_symbol_table_and_scope_tree(); + let _ = ReplaceGlobalDefines::new( + &allocator, + ReplaceGlobalDefinesConfig::new(&[("process.env.NODE_ENV", "'development'")]).unwrap(), + ) + .build(symbols, scopes, &mut program); let ret = Minifier::new(options).build(&allocator, &mut program); CodeGenerator::new() .with_options(CodegenOptions { minify: true, ..CodegenOptions::default() })