feat(transformer,minifier)!: move define and inject plugin from minifier to transformer (#6199)

This commit is contained in:
Boshen 2024-10-01 03:59:24 +00:00
parent 51a78d5946
commit 82ab68984e
14 changed files with 32 additions and 17 deletions

2
Cargo.lock generated
View file

@ -1706,7 +1706,6 @@ dependencies = [
"oxc_allocator",
"oxc_ast",
"oxc_codegen",
"oxc_diagnostics",
"oxc_mangler",
"oxc_parser",
"oxc_semantic",
@ -1988,6 +1987,7 @@ version = "0.30.5"
dependencies = [
"assert-unchecked",
"base64",
"cow-utils",
"dashmap 6.0.1",
"indexmap",
"oxc-browserslist",

View file

@ -21,11 +21,9 @@ test = true
doctest = false
[dependencies]
cow-utils = { workspace = true }
oxc_allocator = { workspace = true }
oxc_ast = { workspace = true }
oxc_codegen = { workspace = true }
oxc_diagnostics = { workspace = true }
oxc_mangler = { workspace = true }
oxc_parser = { workspace = true }
oxc_semantic = { workspace = true }
@ -33,12 +31,12 @@ oxc_span = { workspace = true }
oxc_syntax = { workspace = true }
oxc_traverse = { workspace = true }
cow-utils = { workspace = true }
num-bigint = { workspace = true }
num-traits = { workspace = true }
[dev-dependencies]
oxc_parser = { workspace = true }
cow-utils = { workspace = true }
insta = { workspace = true }
pico-args = { workspace = true }

View file

@ -7,7 +7,6 @@ mod compressor;
mod keep_var;
mod node_util;
mod options;
mod plugins;
mod tri;
mod ty;
@ -18,9 +17,7 @@ use oxc_allocator::Allocator;
use oxc_ast::ast::Program;
use oxc_mangler::Mangler;
pub use crate::{
ast_passes::CompressorPass, compressor::Compressor, options::CompressOptions, plugins::*,
};
pub use crate::{ast_passes::CompressorPass, compressor::Compressor, options::CompressOptions};
#[derive(Debug, Clone, Copy)]
pub struct MinifierOptions {

View file

@ -1,6 +1,5 @@
mod ast_passes;
mod mangler;
mod plugins;
use oxc_allocator::Allocator;
use oxc_codegen::{CodeGenerator, CodegenOptions};

View file

@ -1,2 +0,0 @@
mod inject_global_variables;
mod replace_global_defines;

View file

@ -24,6 +24,7 @@ doctest = false
oxc_allocator = { workspace = true }
oxc_ast = { workspace = true }
oxc_diagnostics = { workspace = true }
oxc_parser = { workspace = true }
oxc_regular_expression = { workspace = true }
oxc_semantic = { workspace = true }
oxc_span = { workspace = true }
@ -32,6 +33,7 @@ oxc_traverse = { workspace = true }
assert-unchecked = { workspace = true }
base64 = { workspace = true }
cow-utils = { workspace = true }
dashmap = { workspace = true }
indexmap = { workspace = true }
oxc-browserslist = { workspace = true }

View file

@ -25,6 +25,8 @@ mod react;
mod regexp;
mod typescript;
mod plugins;
mod helpers {
pub mod bindings;
pub mod module_imports;
@ -52,6 +54,7 @@ pub use crate::{
env::{EnvOptions, Targets},
es2015::{ArrowFunctionsOptions, ES2015Options},
options::{BabelOptions, TransformOptions},
plugins::*,
react::{JsxOptions, JsxRuntime, ReactRefreshOptions},
typescript::{RewriteExtensionsMode, TypeScriptOptions},
};

View file

@ -0,0 +1 @@
mod plugins;

View file

@ -4,12 +4,12 @@
use oxc_allocator::Allocator;
use oxc_codegen::{CodeGenerator, CodegenOptions};
use oxc_minifier::{InjectGlobalVariables, InjectGlobalVariablesConfig, InjectImport};
use oxc_parser::Parser;
use oxc_semantic::SemanticBuilder;
use oxc_span::SourceType;
use oxc_transformer::{InjectGlobalVariables, InjectGlobalVariablesConfig, InjectImport};
use crate::run;
use super::run;
pub(crate) fn test(source_text: &str, expected: &str, config: InjectGlobalVariablesConfig) {
let source_type = SourceType::default();
@ -25,7 +25,7 @@ pub(crate) fn test(source_text: &str, expected: &str, config: InjectGlobalVariab
.with_options(CodegenOptions { single_quote: true, ..CodegenOptions::default() })
.build(program)
.source_text;
let expected = run(expected, source_type, None);
let expected = run(expected, source_type);
assert_eq!(result, expected, "for source {source_text}");
}

View file

@ -0,0 +1,17 @@
mod inject_global_variables;
mod replace_global_defines;
use oxc_allocator::Allocator;
use oxc_codegen::{CodeGenerator, CodegenOptions};
use oxc_parser::Parser;
use oxc_span::SourceType;
fn run(source_text: &str, source_type: SourceType) -> String {
let allocator = Allocator::default();
let ret = Parser::new(&allocator, source_text, source_type).parse();
let program = allocator.alloc(ret.program);
CodeGenerator::new()
.with_options(CodegenOptions { single_quote: true, ..CodegenOptions::default() })
.build(program)
.source_text
}

View file

@ -1,11 +1,11 @@
use oxc_allocator::Allocator;
use oxc_codegen::{CodeGenerator, CodegenOptions};
use oxc_minifier::{ReplaceGlobalDefines, ReplaceGlobalDefinesConfig};
use oxc_parser::Parser;
use oxc_semantic::SemanticBuilder;
use oxc_span::SourceType;
use oxc_transformer::{ReplaceGlobalDefines, ReplaceGlobalDefinesConfig};
use crate::run;
use super::run;
pub(crate) fn test(source_text: &str, expected: &str, config: ReplaceGlobalDefinesConfig) {
let source_type = SourceType::default();
@ -21,7 +21,7 @@ pub(crate) fn test(source_text: &str, expected: &str, config: ReplaceGlobalDefin
.with_options(CodegenOptions { single_quote: true, ..CodegenOptions::default() })
.build(program)
.source_text;
let expected = run(expected, source_type, None);
let expected = run(expected, source_type);
assert_eq!(result, expected, "for source {source_text}");
}