乐趣区

Terser为什么不压缩这一段代码是因为副作用

上代码:

import  * as ute from 'bbbb';
console.log(ute.cube(65,2));
console.log(ute.func1f(89));

bbbb:

export {cube,addX1,addX2,addX3,addX4} from './Math.js';
export {ut_a} from './ut.js';

Math.js:

export function square(x) {return x * x;}

export function cube(x) {return x * x * x* eval('addX3')();}


export function addX1(x) {return x + Math.random() * 1111111111;
}

export function addX2(x) {return x + Math.random() * 2222222222;
}

export function addX3(x) {return x + Math.random() * 3333333333;
}

export function addX4(x) {return x + Math.random() * 45444444444;
}

经过 WEBPACK 处理后,变成如下:

(window["webpackJsonp"] = window["webpackJsonp"] || []).push([["zpageATS"],{

/***/ "./node_modules/bbbb/Math.js":
/***/ (function(module, __webpack_exports__, __webpack_require__) {

"use strict";
/* unused harmony export square */
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "a", function() {return cube;});
/* unused harmony export addX1 */
/* unused harmony export addX2 */
/* unused harmony export addX3 */
/* unused harmony export addX4 */
function square(x) {return x * x;}

function cube(x) {return x * x * x* eval('addX3')();}


function addX1(x) {return x + Math.random() * 1111111111;
}

function addX2(x) {return x + Math.random() * 2222222222;
}

function addX3(x) {return x + Math.random() * 3333333333;
}

function addX4(x) {return x + Math.random() * 45444444444;
}

/***/ }),

/***/ "./node_modules/bbbb/index.js":
/***/ (function(module, __webpack_exports__, __webpack_require__) {

"use strict";
/*import * as ute from './Math.js';
export {ute};*/






/***/ }),

/***/ "./pageATS.js":
/***/ (function(module, __webpack_exports__, __webpack_require__) {

"use strict";
__webpack_require__.r(__webpack_exports__);
/* harmony import */ var bbbb__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__("./node_modules/bbbb/Math.js");
/* harmony import */ var bbbb__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__("./node_modules/bbbb/index.js");

console.log(bbbb__WEBPACK_IMPORTED_MODULE_0__[/* cube */ "a"](65,2));
console.log(bbbb__WEBPACK_IMPORTED_MODULE_1__["func1f"](89));


/***/ })

},[["./pageATS.js","pageInit"]]]);

TERSER 后:

(window.webpackJsonp = window.webpackJsonp || []).push([["zpageATS"], {"./node_modules/bbbb/Math.js": function(module, __webpack_exports__, __webpack_require__) {
            "use strict";

            function square(n) {return n * n}

            function cube(x) {return x * x * x * eval("addX3")()}

            function addX1(n) {return n + 1111111111 * Math.random()
            }

            function addX2(n) {return n + 2222222222 * Math.random()
            }

            function addX3(n) {return n + 3333333333 * Math.random()
            }

            function addX4(n) {return n + 45444444444 * Math.random()
            }
            __webpack_require__.d(__webpack_exports__, "a", (function() {return cube}))
        },
        "./node_modules/bbbb/index.js": function(n, e, o) {},
        "./pageATS.js": function(n, e, o) {
            "use strict";
            o.r(e);
            var u = o("./node_modules/bbbb/Math.js"),
                a = o("./node_modules/bbbb/index.js");
            console.log(u.a(65, 2)), console.log(a.func1f(89))
        }
    },
    [["./pageATS.js", "pageInit"]
    ]
]);

肉眼可见有代码并没有被 shaking 掉

而如果 Math.js: 换成这个呢?

export function square(x) {return x * x;}

export function cube(x) {return x * x * x* 67;}


export function addX1(x) {return x + Math.random() * 1111111111;
}

export function addX2(x) {return x + Math.random() * 2222222222;
}

export function addX3(x) {return x + Math.random() * 3333333333;
}

export function addX4(x) {return x + Math.random() * 45444444444;
}

TERSER 前:

(window["webpackJsonp"] = window["webpackJsonp"] || []).push([["zpageATS"],{

/***/ "./node_modules/bbbb/Math.js":
/***/ (function(module, __webpack_exports__, __webpack_require__) {

"use strict";
/* unused harmony export square */
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "a", function() {return cube;});
/* unused harmony export addX1 */
/* unused harmony export addX2 */
/* unused harmony export addX3 */
/* unused harmony export addX4 */
function square(x) {return x * x;}

function cube(x) {return x * x * x* 67;}


function addX1(x) {return x + Math.random() * 1111111111;
}

function addX2(x) {return x + Math.random() * 2222222222;
}

function addX3(x) {return x + Math.random() * 3333333333;
}

function addX4(x) {return x + Math.random() * 45444444444;
}

/***/ }),

/***/ "./node_modules/bbbb/index.js":
/***/ (function(module, __webpack_exports__, __webpack_require__) {

"use strict";
/*import * as ute from './Math.js';
export {ute};*/






/***/ }),

/***/ "./pageATS.js":
/***/ (function(module, __webpack_exports__, __webpack_require__) {

"use strict";
__webpack_require__.r(__webpack_exports__);
/* harmony import */ var bbbb__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__("./node_modules/bbbb/Math.js");
/* harmony import */ var bbbb__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__("./node_modules/bbbb/index.js");

console.log(bbbb__WEBPACK_IMPORTED_MODULE_0__[/* cube */ "a"](65,2));
console.log(bbbb__WEBPACK_IMPORTED_MODULE_1__["func1f"](89));


/***/ })

},[["./pageATS.js","pageInit"]]]);

TERSER 后:

(window.webpackJsonp = window.webpackJsonp || []).push([["zpageATS"], {"./node_modules/bbbb/Math.js": function(n, o, e) {
            "use strict";

            function s(n) {return n * n * n * 67}
            e.d(o, "a", (function() {return s}))
        },
        "./node_modules/bbbb/index.js": function(n, o, e) {},
        "./pageATS.js": function(n, o, e) {
            "use strict";
            e.r(o);
            var s = e("./node_modules/bbbb/Math.js"),
                b = e("./node_modules/bbbb/index.js");
            console.log(s.a(65, 2)), console.log(b.func1f(89))
        }
    },
    [["./pageATS.js", "pageInit"]
    ]
]);

可见该移除的都已经被完美移除了,

差别就在这里:

退出移动版