babel / minify

:scissors: An ES6+ aware minifier based on the Babel toolchain (beta)
https://babeljs.io/repl
MIT License
4.39k stars 225 forks source link

simplify produces an incorrect result #905

Open jsg2021 opened 6 years ago

jsg2021 commented 6 years ago

Describe the bug

Incorrect optimization.

To Reproduce

Minimal code to reproduce the bug

function draw () {
    const {canvas, currentLayout: layout} = this;

    canvas.width = layout.canvas.width + 2 * CANVAS_PADDING;
    canvas.height = layout.canvas.height + 2 * CANVAS_PADDING;
}

Actual Output

If there is no Error thrown,

function draw() {
  const {
    canvas,
    currentLayout: layout
  } = this;

  canvas.width += 2 * CANVAS_PADDING, canvas.height += 2 * CANVAS_PADDING;
}

Expected Output

function draw() {
  const {
    canvas,
    currentLayout: layout
  } = this;

  canvas.width = layout.canvas.width + 2 * CANVAS_PADDING, canvas.height = layout.canvas.width + 2 * CANVAS_PADDING;
}

Configuration

How are you using babel-minify? babel --plugins minify-simplify test.js

babel-minify version: 0.4.3

babel version : 7.1.0

babel-minify-config: none babelrc: none

jsg2021 commented 6 years ago

This is rather persistent... even this triggers it:

function draw () {
    const {canvas, layout} = this;

    const padding = 2 * CANVAS_PADDING;
    canvas.width = layout.canvas.width + padding;
    canvas.height = layout.canvas.height + padding;
}
jsg2021 commented 6 years ago
sorry, actually off topic I found another bug with simplify... run babel with `babel-preset-minify` in your presets, run on this [file](https://github.com/snd/url-pattern/blob/master/lib/url-pattern.js) ... then turn off simplify: `['babel-preset-minify', { simplify: false }]`... default: ```sh > babel node_modules/url-pattern/lib/url-pattern.js var slice=[].slice;(function(a,b){return"function"==typeof define&&null!=define.amd?define([],b):"undefined"!=typeof exports&&null!==exports?module.exports=b():a.UrlPattern=b()}) ... ``` with simplify off: ```sh ⟩babel node_modules/url-pattern/lib/url-pattern.js var slice=[].slice;(function(a,b){if("function"==typeof define&&null!=define.amd){return define([],b)}else if("undefined"!=typeof exports&&null!==exports){return module.exports=b()}else{return a.UrlPattern=b()}}) ... ``` notice the `return"function"==typeof define` (no space between return and ")... webpack's further transforms this to `returntrue`...

Update: Webpack fixed the "returntrue" problem. I've updated this comment to hide what is now an off-topic comment.

j-f1 commented 6 years ago

Can you try running the replacements before the minification?

j-f1 commented 6 years ago

Can your build process substitute typeof define === "function" with true before you minify? This will also allow the minifier to remove unnecessary branches/conditionals.

jsg2021 commented 6 years ago

My build process is webpack. I am using this minifier as a preset in the babel-loader... so, not really. Anyways, this has accidentally hijacked the original issue and I have found to be unrelated. (Which is why I updated my comment from two days ago) I originally thought this was related to the original issue, but I believe is intended behavior (the no-space between return and the quote)