Replies: 1 comment 1 reply
-
Ok so I've started to use the
And using the fn main() {
swc_core::common::GLOBALS.set(&Globals::new(), move || {
let code = "
if (globalThis.foo === 'bar') {
globalThis.bar = foo;
}
";
let source_map = Lrc::new(SourceMap::default());
let (mut module, comments) = parse(&PathBuf::new(), code, source_map.clone()).unwrap();
let top_level_mark = Mark::fresh(Mark::root());
let unresolved_mark = Mark::fresh(Mark::root());
module = module.fold_with(&mut resolver(unresolved_mark, top_level_mark, false));
let compress_options = minifier::option::CompressOptions {
// See below
};
let minifier_options = minifier::option::MinifyOptions {
rename: false,
compress: Some(compress_options),
mangle: None,
wrap: false,
enclose: false,
};
let minify_extra_options = minifier::option::ExtraOptions {
unresolved_mark,
top_level_mark,
};
let module = minifier::optimize(
Program::Module(module),
source_map.clone(),
Some(&comments),
None,
&minifier_options,
&minify_extra_options,
)
.expect_module();
let output_str = render(&module, source_map.clone());
println!("{}", output_str);
});
}
fn parse(
file_name: &Path,
code: &str,
source_map: Lrc<SourceMap>,
) -> Result<(Module, SingleThreadedComments), String> {
let source_file = source_map.new_source_file(FileName::Real(file_name.to_owned()), code.into());
let comments = SingleThreadedComments::default();
let syntax = {
let mut tsconfig = TsConfig::default();
tsconfig.tsx = true;
Syntax::Typescript(tsconfig)
};
let lexer = Lexer::new(
syntax,
swc_core::ecma::ast::EsVersion::latest(),
StringInput::from(&*source_file),
Some(&comments),
);
let mut parser = Parser::new_from(lexer);
let module = match parser.parse_module() {
Err(err) => return Err(format!("{:?}", err)),
Ok(module) => module,
};
return Ok((module, comments));
} However, despite having the same settings as the swc playground, I get a different output that is often invalid. For example my code: if (globalThis.foo === 'bar') {
globalThis.bar = foo;
} In the SWC playground, this snippet minifies to (valid code): 'bar' === globalThis.foo && (globalThis.bar = foo); However, using my minification implementation, the output minifies to (broken code): 'bar' === globalThis.foo && globalThis.bar = foo; I have matched the config in my implementation exactly with the config on the playground - where I used the default values for the properties missing from the JSON on the playground - but no matter what I am unable to produce valid minified code. Especially true if I attempt to minify something like React or any other non trivial dependency. let compress_options = minifier::option::CompressOptions {
arguments: false,
arrows: true,
bools: true,
bools_as_ints: false,
collapse_vars: true,
comparisons: true,
computed_props: true,
conditionals: true,
dead_code: true,
directives: true,
drop_console: false,
drop_debugger: true,
evaluate: true,
expr: false,
hoist_fns: false,
hoist_props: true,
hoist_vars: false,
if_return: true,
join_vars: true,
keep_classnames: false,
keep_fargs: true,
keep_fnames: false,
keep_infinity: false,
loops: true,
negate_iife: true,
reduce_fns: false,
reduce_vars: false,
side_effects: true,
switches: true,
typeofs: true,
unsafe_arrows: false,
unsafe_comps: false,
unsafe_function: false,
unsafe_math: false,
unsafe_symbols: false,
unsafe_methods: false,
unsafe_proto: false,
unsafe_regexp: false,
unsafe_undefined: false,
unused: true,
const_to_let: true,
pristine_globals: true,
ecma: EsVersion::Es2022,
global_defs: compress_options_default.global_defs,
ie8: false,
inline: compress_options_default.inline,
module: compress_options_default.module,
passes: compress_options_default.passes,
props: compress_options_default.props,
pure_getters: compress_options_default.pure_getters,
pure_funcs: compress_options_default.pure_funcs,
sequences: compress_options_default.sequences,
top_retain: compress_options_default.top_retain,
top_level: compress_options_default.top_level,
unsafe_passes: compress_options_default.unsafe_passes,
}; Any ideas? |
Beta Was this translation helpful? Give feedback.
-
UPDATED, no longer relevant
Beta Was this translation helpful? Give feedback.
All reactions