-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(es): Apply
paren_remover
for minify (#8442)
**Related issue:** - Closes #8437
- Loading branch information
Showing
2 changed files
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import swc from "../../.."; | ||
|
||
it("should output same result", async () => { | ||
const origin = ` | ||
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); | ||
` | ||
|
||
async function minify() { | ||
const { code } = await swc.minify(origin, { | ||
compress: true, | ||
mangle: false | ||
}); | ||
return code; | ||
} | ||
|
||
async function transform() { | ||
const { code } = await swc.transform(origin, { | ||
jsc: { | ||
minify: { | ||
compress: true, | ||
mangle: false | ||
}, | ||
}, | ||
isModule: false, | ||
minify: true | ||
}); | ||
return code; | ||
} | ||
|
||
const [minifyResult, transformResult] = await Promise.all([minify(), transform()]); | ||
expect(minifyResult).toEqual(transformResult); | ||
}); |