项目作者: karimsa

项目描述 :
Remove IIFEs at compile-time.
高级语言: TypeScript
项目地址: git://github.com/karimsa/iife-pop.git
创建时间: 2018-04-03T22:02:50Z
项目社区:https://github.com/karimsa/iife-pop

开源协议:MIT License

下载


iife-pop

Remove IIFE-s at compile-time.



Build Status

Motivation

The purpose of this babel plugin is to be able to evaluate & remove the existence of
an IIFE at compile-time where it is possible to do so. The idea is to simplify code as
much as possible at compile-time so that optimizations such as constant-folding can actually
reduce code even further than they currently do.

As an example, look at this code:

  1. const myValue = (function (arg) {
  2. switch (arg) {
  3. case 'A':
  4. return 0
  5. case 'B':
  6. return 1
  7. case 'C':
  8. return 2
  9. default:
  10. return 3
  11. }
  12. }( 'some-value' ))

Modern-day minifiers such as babel-minify and uglify-js are able to reduce this down to:

  1. const myValue=function(a){return'A'===a?0:'B'===a?1:'C'===a?2:3}('some-value');

Though this saves quite a few bytes via mangling, constant folding is not able to take place
because of the IIFE. If we replace the IIFE in the original code, it might look something like this:

  1. const _arg = 'some-value'
  2. let _returnValue
  3. switch (_arg) {
  4. case 'A':
  5. _returnValue = 0
  6. break
  7. case 'B':
  8. _returnValue = 1
  9. break
  10. case 'C':
  11. _returnValue = 2
  12. break
  13. default:
  14. _returnValue = 3
  15. break
  16. }
  17. const myValue = _arg

The code is the same, just without an IIFE. This new code can be minified down to:

  1. const myValue=3;

Which saves a further 63 bytes in this case (~80%) - plus much less work for the runtime to do.

License

Licensed under MIT license.

Copyright (C) 2018-present Karim Alibhai.