@@ -5,11 +5,13 @@ use std::{
5
5
} ;
6
6
7
7
use oxc:: {
8
+ allocator:: Allocator ,
9
+ codegen:: Codegen ,
8
10
codegen:: { CodegenOptions , CodegenReturn } ,
9
11
diagnostics:: OxcDiagnostic ,
10
12
mangler:: MangleOptions ,
11
- minifier:: CompressOptions ,
12
- parser:: { ParseOptions , ParserReturn } ,
13
+ minifier:: { CompressOptions , Compressor } ,
14
+ parser:: { ParseOptions , Parser , ParserReturn } ,
13
15
span:: SourceType ,
14
16
transformer:: TransformOptions ,
15
17
CompilerInterface ,
@@ -23,6 +25,7 @@ pub struct Driver {
23
25
// options
24
26
pub transform : Option < TransformOptions > ,
25
27
pub compress : Option < CompressOptions > ,
28
+ pub dce : bool ,
26
29
pub mangle : bool ,
27
30
pub remove_whitespace : bool ,
28
31
// states
@@ -93,6 +96,9 @@ impl Driver {
93
96
source_text : & str ,
94
97
source_type : SourceType ,
95
98
) -> Result < String , Vec < Diagnostic > > {
99
+ if self . dce {
100
+ return self . dce ( source_text, source_type) ;
101
+ }
96
102
self . path = source_path. to_path_buf ( ) ;
97
103
let mut source_type = source_type;
98
104
if source_path. extension ( ) . unwrap ( ) == "js" {
@@ -114,4 +120,16 @@ impl Driver {
114
120
Err ( errors)
115
121
}
116
122
}
123
+
124
+ pub fn dce (
125
+ & mut self ,
126
+ source_text : & str ,
127
+ source_type : SourceType ,
128
+ ) -> Result < String , Vec < Diagnostic > > {
129
+ let allocator = Allocator :: default ( ) ;
130
+ let mut ret = Parser :: new ( & allocator, source_text, source_type) . parse ( ) ;
131
+ let program = & mut ret. program ;
132
+ Compressor :: new ( & allocator, CompressOptions :: default ( ) ) . dead_code_elimination ( program) ;
133
+ Ok ( Codegen :: new ( ) . build ( program) . code )
134
+ }
117
135
}
0 commit comments