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

Use Prepack's evaluator for constant evaluation #519

Open amasad opened 7 years ago

amasad commented 7 years ago

If we switched out Babili's constant evaluation with the Prepack's I think this would push Babili to the winning side in terms of bundle size and potentially execution time. It doesn't have to use all of Prepack's fanciness -- just evaluating straightforward constants (no "abstract interpretation" and "heap serialization").

On the downside, this will surely add to the compile time and might become more of a pressing problem (it can be then switched on/off via an option).

hzoo commented 7 years ago

An interesting idea that would be pretty awesome! That vs path.evaluate

boopathi commented 7 years ago

I tried this out naively, and it can work - not sure how much we can control transformations from prepack's API.

const prepack = require("prepack");

module.exports = function prepackPlugin({ template }) {
  return {
    visitor: {
      Program: {
        enter(path) {
          path.node.body = template(
            prepack.prepackFromAst(path.node, path.getSource()).code
          )();
        }
      }
    }
  };
};
amasad commented 7 years ago

My initial thoughts is that we either have to fork prepack's evaluator or convince maintainers to make it's own package. That way we can either integrate it natively into Babel replacing path.evaluate or we can just use it here.

@boopathi do you know if the evaluator is easily exportable from prepack?