Skip to content

Commit

Permalink
chore: add swc css and html minifier
Browse files Browse the repository at this point in the history
  • Loading branch information
wre232114 committed Apr 22, 2023
1 parent 310440a commit ac706d8
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 31 deletions.
93 changes: 69 additions & 24 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/plugin_runtime/src/lib.rs
Expand Up @@ -379,6 +379,7 @@ impl Plugin for FarmPluginRuntime {
context.config.script.target.clone(),
context.meta.script.cm.clone(),
None,
context.config.minify
)
.map_err(|e| CompilationError::GenerateResourcesError {
name: resource_pot.id.to_string(),
Expand Down
1 change: 1 addition & 0 deletions crates/plugin_script/src/lib.rs
Expand Up @@ -280,6 +280,7 @@ impl Plugin for FarmPluginScript {
context.config.script.target.clone(),
context.meta.script.cm.clone(),
Some(&mut src_map_buf),
context.config.minify,
)
.map_err(|e| CompilationError::GenerateResourcesError {
name: resource_pot.id.to_string(),
Expand Down
6 changes: 3 additions & 3 deletions crates/plugin_tree_shake/tests/remove_useless_stmts.rs
Expand Up @@ -52,7 +52,7 @@ export default 'default';
// println!("import_info: {:#?}", import_info);
// println!("export_info: {:#?}", export_info);

let bytes = codegen_module(swc_module, EsVersion::EsNext, cm, None).unwrap();
let bytes = codegen_module(swc_module, EsVersion::EsNext, cm, None, false).unwrap();
let result = String::from_utf8(bytes).unwrap();
let expect = r#"import { aValue } from './foo';
const a = aValue;
Expand Down Expand Up @@ -128,7 +128,7 @@ export * from './src/foo';
// println!("import_info: {:#?}", import_info);
// println!("export_info: {:#?}", export_info);

let bytes = codegen_module(swc_module, EsVersion::EsNext, cm, None).unwrap();
let bytes = codegen_module(swc_module, EsVersion::EsNext, cm, None, false).unwrap();
let result = String::from_utf8(bytes).unwrap();
assert_eq!(
result,
Expand Down Expand Up @@ -187,7 +187,7 @@ export * from './src/bar';
// println!("import_info: {:#?}", import_info);
// println!("export_info: {:#?}", export_info);

let bytes = codegen_module(swc_module, EsVersion::EsNext, cm, None).unwrap();
let bytes = codegen_module(swc_module, EsVersion::EsNext, cm, None, false).unwrap();
let result = String::from_utf8(bytes).unwrap();
assert_eq!(
result,
Expand Down
2 changes: 2 additions & 0 deletions crates/toolkit/Cargo.toml
Expand Up @@ -28,9 +28,11 @@ swc_ecma_transforms = { version = "0.217.14", features = [
swc_css_parser = { version = "0.146.4" }
swc_css_visit = { version = "0.136.4" }
swc_css_codegen = { version = "0.147.4" }
swc_css_minifier = { version = "0.112.5" }
swc_html_parser = { version = "0.37.4" }
swc_html_visit = { version = "0.31.4" }
swc_html_codegen = { version = "0.40.4" }
swc_html_minifier = { version = "0.120.23" }
swc_error_reporters = { version = "0.15.4" }
swc_atoms = { version = "0.5.3" }
swc_ecma_transforms_base = "0.126.10"
Expand Down
2 changes: 2 additions & 0 deletions crates/toolkit/src/lib.rs
Expand Up @@ -20,10 +20,12 @@ pub use swc_ecma_transforms_base;
pub use swc_css_codegen;
pub use swc_css_parser;
pub use swc_css_visit;
pub use swc_css_minifier;

pub use swc_html_codegen;
pub use swc_html_parser;
pub use swc_html_visit;
pub use swc_html_minifier;

pub use lazy_static;
pub use regex;
5 changes: 2 additions & 3 deletions crates/toolkit/src/script/mod.rs
Expand Up @@ -69,6 +69,7 @@ pub fn codegen_module(
target: EsVersion,
cm: Arc<SourceMap>,
src_map: Option<&mut Vec<(BytePos, LineCol)>>,
minify: bool,
) -> std::result::Result<Vec<u8>, std::io::Error> {
let mut buf = vec![];

Expand All @@ -80,10 +81,8 @@ pub fn codegen_module(
cfg: swc_ecma_codegen::Config {
target,
ascii_only: false,
// TODO make this configurable
minify: true,
minify,
omit_last_semi: true,
..Default::default()
},
// TODO preserve comments
comments: None,
Expand Down
2 changes: 1 addition & 1 deletion crates/toolkit/tests/script.rs
Expand Up @@ -21,7 +21,7 @@ fn parse_and_codegen_module() {

assert_eq!(ast.body.len(), 3);

let bytes = codegen_module(&ast, Default::default(), cm, None).unwrap();
let bytes = codegen_module(&ast, Default::default(), cm, None, false).unwrap();

let code = String::from_utf8(bytes).unwrap();
assert_eq!(
Expand Down
1 change: 1 addition & 0 deletions packages/core/binding/index.d.ts
Expand Up @@ -165,6 +165,7 @@ export interface Config {
};
lazyCompilation?: boolean;
treeShaking?: boolean;
minify?: boolean;
};
jsPlugins?: object[];
// [rustPluginFilePath, jsonStringifiedOptions]
Expand Down

0 comments on commit ac706d8

Please sign in to comment.