移除未使用的代码

example.js

  1. const inc = require("./increment").increment;
  2. var a = 1;
  3. inc(a); // 2

increment.js

  1. const add = require("./math").add;
  2. exports.increment = function increment(val) {
  3. return add(val, 1);
  4. };
  5. exports.incrementBy2 = function incrementBy2(val) {
  6. return add(val, 2);
  7. };
  8. exports.decrement = function decrement(val) {
  9. return add(val, 1);
  10. };

math.js

  1. exports.add = function add() {
  2. var sum = 0,
  3. i = 0,
  4. args = arguments,
  5. l = args.length;
  6. while (i < l) {
  7. sum += args[i++];
  8. }
  9. return sum;
  10. };
  11. exports.multiply = function multiply() {
  12. var product = 0,
  13. i = 0,
  14. args = arguments,
  15. l = args.length;
  16. while (i < l) {
  17. sum *= args[i++];
  18. }
  19. return sum;
  20. };

webpack.config.js

  1. module.exports = [
  2. {
  3. entry: "./example.js",
  4. output: {
  5. pathinfo: true,
  6. filename: "output.js"
  7. },
  8. optimization: {
  9. moduleIds: "size",
  10. usedExports: true,
  11. mangleExports: true
  12. }
  13. },
  14. {
  15. entry: "./example.js",
  16. output: {
  17. pathinfo: true,
  18. filename: "without.js"
  19. },
  20. optimization: {
  21. moduleIds: "size",
  22. usedExports: false,
  23. mangleExports: false
  24. }
  25. }
  26. ];

usedExports可以告诉webpack生成模块导出和使用的信息,这个信息会被其他优化配置项或其他插件使用。、mangleExports 告诉webpack是否将导出的模块名称简写。打包后的文件。
可以看到每个模块的上边都有注释信息,未使用的被标记 [unused]
导出的模块名也被简写了(Mj, nP, nP)
/! default exports /
/! export decrement [provided] [unused] [renamed to Mj] /
/! export increment [provided] [used in main] [renamed to nP] /
/! export incrementBy2 [provided] [unused] [renamed to pN] /

  1. /******/ (() => { // webpackBootstrap
  2. /******/ var __webpack_modules__ = ([
  3. /* 0 */,
  4. /* 1 */
  5. /*!**********************!*\
  6. !*** ./increment.js ***!
  7. \**********************/
  8. /*! default exports */
  9. /*! export decrement [provided] [unused] [renamed to Mj] */
  10. /*! export increment [provided] [used in main] [renamed to nP] */
  11. /*! export incrementBy2 [provided] [unused] [renamed to pN] */
  12. /*! runtime requirements: __webpack_require__, __webpack_exports__ */
  13. /***/ ((__unused_webpack_module, exports, __webpack_require__) => {
  14. var __webpack_unused_export__;
  15. const add = __webpack_require__(/*! ./math */ 2)/* .add */ .I;
  16. exports.nP = function increment(val) {
  17. return add(val, 1);
  18. };
  19. __webpack_unused_export__ = function incrementBy2(val) {
  20. return add(val, 2);
  21. };
  22. __webpack_unused_export__ = function decrement(val) {
  23. return add(val, 1);
  24. };
  25. /***/ }),
  26. /* 2 */
  27. /*!*****************!*\
  28. !*** ./math.js ***!
  29. \*****************/
  30. /*! default exports */
  31. /*! export add [provided] [used in main] [renamed to I] */
  32. /*! export multiply [provided] [unused] [renamed to J] */
  33. /*! runtime requirements: __webpack_exports__ */
  34. /***/ ((__unused_webpack_module, exports) => {
  35. var __webpack_unused_export__;
  36. exports.I = function add() {
  37. var sum = 0,
  38. i = 0,
  39. args = arguments,
  40. l = args.length;
  41. while (i < l) {
  42. sum += args[i++];
  43. }
  44. return sum;
  45. };
  46. __webpack_unused_export__ = function multiply() {
  47. var product = 0,
  48. i = 0,
  49. args = arguments,
  50. l = args.length;
  51. while (i < l) {
  52. sum *= args[i++];
  53. }
  54. return sum;
  55. };
  56. /***/ })
  57. /******/ ]);
  58. /************************************************************************/
  59. /******/ // The module cache
  60. /******/ var __webpack_module_cache__ = {};
  61. /******/
  62. /******/ // The require function
  63. /******/ function __webpack_require__(moduleId) {
  64. /******/ // Check if module is in cache
  65. /******/ if(__webpack_module_cache__[moduleId]) {
  66. /******/ return __webpack_module_cache__[moduleId].exports;
  67. /******/ }
  68. /******/ // Create a new module (and put it into the cache)
  69. /******/ var module = __webpack_module_cache__[moduleId] = {
  70. /******/ // no module.id needed
  71. /******/ // no module.loaded needed
  72. /******/ exports: {}
  73. /******/ };
  74. /******/
  75. /******/ // Execute the module function
  76. /******/ __webpack_modules__[moduleId](module, module.exports, __webpack_require__);
  77. /******/
  78. /******/ // Return the exports of the module
  79. /******/ return module.exports;
  80. /******/ }
  81. /******/
  82. /************************************************************************/
  83. (() => {
  84. /*!********************!*\
  85. !*** ./example.js ***!
  86. \********************/
  87. /*! unknown exports (runtime-defined) */
  88. /*! runtime requirements: __webpack_require__ */
  89. const inc = __webpack_require__(/*! ./increment */ 1)/* .increment */ .nP;
  90. var a = 1;
  91. inc(a); // 2
  92. })();
  93. /******/ })()
  94. ;