example.js
const inc = require("./increment").increment;var a = 1;inc(a); // 2
increment.js
const add = require("./math").add;exports.increment = function increment(val) {return add(val, 1);};exports.incrementBy2 = function incrementBy2(val) {return add(val, 2);};exports.decrement = function decrement(val) {return add(val, 1);};
math.js
exports.add = function add() {var sum = 0,i = 0,args = arguments,l = args.length;while (i < l) {sum += args[i++];}return sum;};exports.multiply = function multiply() {var product = 0,i = 0,args = arguments,l = args.length;while (i < l) {sum *= args[i++];}return sum;};
webpack.config.js
module.exports = [{entry: "./example.js",output: {pathinfo: true,filename: "output.js"},optimization: {moduleIds: "size",usedExports: true,mangleExports: true}},{entry: "./example.js",output: {pathinfo: true,filename: "without.js"},optimization: {moduleIds: "size",usedExports: false,mangleExports: false}}];
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] /
/******/ (() => { // webpackBootstrap/******/ var __webpack_modules__ = ([/* 0 */,/* 1 *//*!**********************!*\!*** ./increment.js ***!\**********************//*! 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] *//*! runtime requirements: __webpack_require__, __webpack_exports__ *//***/ ((__unused_webpack_module, exports, __webpack_require__) => {var __webpack_unused_export__;const add = __webpack_require__(/*! ./math */ 2)/* .add */ .I;exports.nP = function increment(val) {return add(val, 1);};__webpack_unused_export__ = function incrementBy2(val) {return add(val, 2);};__webpack_unused_export__ = function decrement(val) {return add(val, 1);};/***/ }),/* 2 *//*!*****************!*\!*** ./math.js ***!\*****************//*! default exports *//*! export add [provided] [used in main] [renamed to I] *//*! export multiply [provided] [unused] [renamed to J] *//*! runtime requirements: __webpack_exports__ *//***/ ((__unused_webpack_module, exports) => {var __webpack_unused_export__;exports.I = function add() {var sum = 0,i = 0,args = arguments,l = args.length;while (i < l) {sum += args[i++];}return sum;};__webpack_unused_export__ = function multiply() {var product = 0,i = 0,args = arguments,l = args.length;while (i < l) {sum *= args[i++];}return sum;};/***/ })/******/ ]);/************************************************************************//******/ // The module cache/******/ var __webpack_module_cache__ = {};/******//******/ // The require function/******/ function __webpack_require__(moduleId) {/******/ // Check if module is in cache/******/ if(__webpack_module_cache__[moduleId]) {/******/ return __webpack_module_cache__[moduleId].exports;/******/ }/******/ // Create a new module (and put it into the cache)/******/ var module = __webpack_module_cache__[moduleId] = {/******/ // no module.id needed/******/ // no module.loaded needed/******/ exports: {}/******/ };/******//******/ // Execute the module function/******/ __webpack_modules__[moduleId](module, module.exports, __webpack_require__);/******//******/ // Return the exports of the module/******/ return module.exports;/******/ }/******//************************************************************************/(() => {/*!********************!*\!*** ./example.js ***!\********************//*! unknown exports (runtime-defined) *//*! runtime requirements: __webpack_require__ */const inc = __webpack_require__(/*! ./increment */ 1)/* .increment */ .nP;var a = 1;inc(a); // 2})();/******/ })();
