Unverified Commit 949b8d8e authored by Donny/강동윤's avatar Donny/강동윤 Committed by GitHub

refactor(turbopack): Simplify `minify()` (#76139)

### What?

Simplify `minify()` of turbopack. `swc::Compiler` is not required for the minification, considering that we are not using the minification API of it.

### Why?

It has some tiny overheads, and it disturbs future refactoring for source maps.
parent ec1fefca
...@@ -2,7 +2,7 @@ use std::{io::Write, sync::Arc}; ...@@ -2,7 +2,7 @@ use std::{io::Write, sync::Arc};
use anyhow::{bail, Context, Result}; use anyhow::{bail, Context, Result};
use swc_core::{ use swc_core::{
base::{try_with_handler, Compiler}, base::try_with_handler,
common::{ common::{
comments::{Comments, SingleThreadedComments}, comments::{Comments, SingleThreadedComments},
BytePos, FileName, FilePathMapping, LineCol, Mark, SourceMap as SwcSourceMap, GLOBALS, BytePos, FileName, FilePathMapping, LineCol, Mark, SourceMap as SwcSourceMap, GLOBALS,
...@@ -41,8 +41,7 @@ pub async fn minify( ...@@ -41,8 +41,7 @@ pub async fn minify(
let cm = Arc::new(SwcSourceMap::new(FilePathMapping::empty())); let cm = Arc::new(SwcSourceMap::new(FilePathMapping::empty()));
let (src, mut src_map_buf) = { let (src, mut src_map_buf) = {
let compiler = Arc::new(Compiler::new(cm.clone())); let fm = cm.new_source_file(
let fm = compiler.cm.new_source_file(
FileName::Custom(path.path.to_string()).into(), FileName::Custom(path.path.to_string()).into(),
code.source_code().to_str()?.into_owned(), code.source_code().to_str()?.into_owned(),
); );
...@@ -71,48 +70,46 @@ pub async fn minify( ...@@ -71,48 +70,46 @@ pub async fn minify(
let unresolved_mark = Mark::new(); let unresolved_mark = Mark::new();
let top_level_mark = Mark::new(); let top_level_mark = Mark::new();
Ok(compiler.run_transform(handler, false, || { let program = program.apply(paren_remover(Some(&comments)));
let program = program.apply(paren_remover(Some(&comments)));
let mut program = program.apply(swc_core::ecma::transforms::base::resolver(
let mut program = program.apply(swc_core::ecma::transforms::base::resolver( unresolved_mark,
unresolved_mark, top_level_mark,
top_level_mark, false,
false, ));
));
program = swc_core::ecma::minifier::optimize(
program = swc_core::ecma::minifier::optimize( program,
program, cm.clone(),
cm.clone(), Some(&comments),
Some(&comments), None,
None, &MinifyOptions {
&MinifyOptions { compress: Some(CompressOptions {
compress: Some(CompressOptions { // Only run 2 passes, this is a tradeoff between performance and
// Only run 2 passes, this is a tradeoff between performance and // compression size. Default is 3 passes.
// compression size. Default is 3 passes. passes: 2,
passes: 2,
..Default::default()
}),
mangle: if mangle {
Some(MangleOptions {
reserved: vec!["AbortSignal".into()],
..Default::default()
})
} else {
None
},
..Default::default() ..Default::default()
}),
mangle: if mangle {
Some(MangleOptions {
reserved: vec!["AbortSignal".into()],
..Default::default()
})
} else {
None
}, },
&ExtraOptions { ..Default::default()
top_level_mark, },
unresolved_mark, &ExtraOptions {
mangle_name_cache: None, top_level_mark,
}, unresolved_mark,
); mangle_name_cache: None,
},
);
program.apply(ecma::transforms::base::fixer::fixer(Some( Ok(program.apply(ecma::transforms::base::fixer::fixer(Some(
&comments as &dyn Comments, &comments as &dyn Comments,
))) ))))
}))
}) })
})?; })?;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment