Skip to content

Commit

Permalink
fix: Apply paren-remover while minifying (#8348)
Browse files Browse the repository at this point in the history
### Description

This is required to fix
https://vercel.slack.com/archives/C04KC8A53T7/p1717599802994489



### Testing Instructions

Closes SWC-171
  • Loading branch information
kdy1 authored Jun 7, 2024
1 parent 8950fd9 commit 7a21698
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 0 deletions.
3 changes: 3 additions & 0 deletions crates/turbopack-ecmascript/src/minify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use swc_core::{
},
minifier::option::{ExtraOptions, MinifyOptions},
parser::{lexer::Lexer, Parser, StringInput, Syntax},
transforms::base::fixer::paren_remover,
visit::FoldWith,
},
};
Expand Down Expand Up @@ -63,6 +64,8 @@ pub async fn minify(path: Vc<FileSystemPath>, code: Vc<Code>) -> Result<Vc<Code>
let top_level_mark = Mark::new();

Ok(compiler.run_transform(handler, false, || {
let program = program.fold_with(&mut paren_remover(Some(&comments)));

let mut program =
program.fold_with(&mut swc_core::ecma::transforms::base::resolver(
unresolved_mark,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
function toFixed(value, maxDecimals, roundingFunction, optionals) {
var splitValue = value.toString().split('.'),
minDecimals = maxDecimals - (optionals || 0),

optionalsRegExp,
power,
output;
var boundedPrecisions;
// var unused = 'xxxx';

// Use the smallest precision value possible to avoid errors from floating point representation
if (splitValue.length === 2) {
boundedPrecisions = Math.min(Math.max(splitValue[1].length, minDecimals), maxDecimals);
} else {
boundedPrecisions = minDecimals;
}

power = Math.pow(10, boundedPrecisions);

// Multiply up by precision, round accurately, then divide and use native toFixed():
output = (roundingFunction(value + 'e+' + boundedPrecisions) / power).toFixed(boundedPrecisions);

if (optionals > maxDecimals - boundedPrecisions) {
optionalsRegExp = new RegExp('\\.?0{1,' + (optionals - (maxDecimals - boundedPrecisions)) + '}$');
output = output.replace(optionalsRegExp, '');
}

return output;
}
toFixed(1.2345, 2, Math.round, 1);
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"minifyType": "Minify"
}

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

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

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

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

0 comments on commit 7a21698

Please sign in to comment.