* docs(example): update examples template * docs(example): add npm script to build examples |
||
|---|---|---|
| .. | ||
| README.md | ||
| abc.js | ||
| build.js | ||
| example.js | ||
| library.js | ||
| math.js | ||
| template.md | ||
README.md
This example demonstrates how webpack tracks the using of ES6 imports and exports. Only used exports are emitted to the resulting bundle. The minimizing step then removes the declarations because they are ununsed.
Excluding unused exports from bundles is known as "tree-shaking".
In this example, only add and multiply in ./math.js are used by the app. list is unused and is not included in the minimized bundle (Look for Array.from in the minimized bundle).
In addition to that, library.js simulates an entry point to a big library. library.js re-exports multiple identifiers from submodules. Often big parts of that is unused, like abc.js. Note how the usage information flows from example.js through library.js into abc.js and all declarations in abc.js are not included in the minimized bundle (Look for console.log("a") in the minimized bundle).
example.js
import { add } from './math';
import * as library from "./library";
add(1, 2);
library.reexportedMultiply(1, 2);
math.js
export function add() {
var sum = 0, i = 0, args = arguments, l = args.length;
while (i < l) {
sum += args[i++];
}
return sum;
}
export function multiply() {
var product = 1, i = 0, args = arguments, l = args.length;
while (i < l) {
product *= args[i++];
}
return product;
}
export function list() {
return Array.from(arguments);
}
library.js
export { a, b, c } from "./abc";
export { add as reexportedAdd, multiply as reexportedMultiply } from "./math";
js/output.js
`/******/ (function(modules) { /* webpackBootstrap */ })`
``` javascript /******/ (function(modules) { // webpackBootstrap /******/ // The module cache /******/ var installedModules = {};// // The require function // function webpack_require(moduleId) {
// // Check if module is in cache // if(installedModules[moduleId]) /******/ return installedModules[moduleId].exports;
// // Create a new module (and put it into the cache) // var module = installedModules[moduleId] = { // i: moduleId, // l: false, // exports: {} // };
// // Execute the module function // modules[moduleId].call(module.exports, module, module.exports, webpack_require);
// // Flag the module as loaded // module.l = true;
// // Return the exports of the module // return module.exports; /******/ }
// // expose the modules object (webpack_modules) // webpack_require.m = modules;
// // expose the module cache // webpack_require.c = installedModules;
// // identity function for calling harmory imports with the correct context // webpack_require.i = function(value) { return value; };
// // define getter function for harmory exports // webpack_require.d = function(exports, name, getter) { // Object.defineProperty(exports, name, { // configurable: false, // enumerable: true, // get: getter // }); // };
// // getDefaultExport function for compatibility with non-harmony modules // webpack_require.n = function(module) { // var getter = module && module.__esModule ? // function getDefault() { return module['default']; } : // function getModuleExports() { return module; }; // webpack_require.d(getter, 'a', getter); // return getter; // };
// // Object.prototype.hasOwnProperty.call // webpack_require.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
// // webpack_public_path // webpack_require.p = "js/";
// // Load entry module and return exports // return webpack_require(webpack_require.s = 3); // }) /******************************************************************/
</details>
``` javascript
/******/ ([
/* 0 */
/* exports provided: add, multiply, list */
/* exports used: add, multiply */
/*!*****************!*\
!*** ./math.js ***!
\*****************/
/***/ function(module, exports, __webpack_require__) {
"use strict";
/* harmony export (immutable) */ exports["a"] = add;
/* harmony export (immutable) */ exports["b"] = multiply;
/* unused harmony export list */
function add() {
var sum = 0, i = 0, args = arguments, l = args.length;
while (i < l) {
sum += args[i++];
}
return sum;
}
function multiply() {
var product = 1, i = 0, args = arguments, l = args.length;
while (i < l) {
product *= args[i++];
}
return product;
}
function list() {
return Array.from(arguments);
}
/***/ },
/* 1 */
/* exports provided: a, b, c, reexportedAdd, reexportedMultiply */
/* exports used: reexportedMultiply */
/*!********************!*\
!*** ./library.js ***!
\********************/
/***/ function(module, exports, __webpack_require__) {
"use strict";
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_0__abc__ = __webpack_require__(/*! ./abc */ 2);
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_1__math__ = __webpack_require__(/*! ./math */ 0);
/* unused harmony reexport a */
/* unused harmony reexport b */
/* unused harmony reexport c */
/* unused harmony reexport reexportedAdd */
/* harmony reexport (binding) */ __webpack_require__.d(exports, "a", function() { return __WEBPACK_IMPORTED_MODULE_1__math__["b"]; });
/***/ },
/* 2 */
/* exports provided: a, b, c */
/*!****************!*\
!*** ./abc.js ***!
\****************/
/***/ function(module, exports, __webpack_require__) {
"use strict";
/* unused harmony export a */
/* unused harmony export b */
/* unused harmony export c */
function a() { console.log("a"); }
function b() { console.log("b"); }
function c() { console.log("c"); }
/***/ },
/* 3 */
/* unknown exports provided */
/* all exports used */
/*!********************!*\
!*** ./example.js ***!
\********************/
/***/ function(module, exports, __webpack_require__) {
"use strict";
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_0__math__ = __webpack_require__(/*! ./math */ 0);
/* harmony import */ var __WEBPACK_IMPORTED_MODULE_1__library__ = __webpack_require__(/*! ./library */ 1);
__webpack_require__.i(__WEBPACK_IMPORTED_MODULE_0__math__["a" /* add */])(1, 2);
__WEBPACK_IMPORTED_MODULE_1__library__["a" /* reexportedMultiply */](1, 2);
/***/ }
/******/ ]);
js/output.js
!function(t){function n(e){if(r[e])return r[e].exports;var u=r[e]={i:e,l:!1,exports:{}};return t[e].call(u.exports,u,u.exports,n),u.l=!0,u.exports}var r={};return n.m=t,n.c=r,n.i=function(t){return t},n.d=function(t,n,r){Object.defineProperty(t,n,{configurable:!1,enumerable:!0,get:r})},n.n=function(t){var r=t&&t.__esModule?function(){return t.default}:function(){return t};return n.d(r,"a",r),r},n.o=function(t,n){return Object.prototype.hasOwnProperty.call(t,n)},n.p="js/",n(n.s=3)}([function(t,n,r){"use strict";function e(){for(var t=0,n=0,r=arguments,e=r.length;n<e;)t+=r[n++];return t}function u(){for(var t=1,n=0,r=arguments,e=r.length;n<e;)t*=r[n++];return t}n.a=e,n.b=u},function(t,n,r){"use strict";var e=(r(2),r(0));r.d(n,"a",function(){return e.b})},function(t,n,r){"use strict"},function(t,n,r){"use strict";var e=r(0),u=r(1);r.i(e.a)(1,2),u.a(1,2)}]);
Info
Uncompressed
Hash: a5187553da983450faff
Version: webpack 2.1.0-beta.25
Time: 130ms
Asset Size Chunks Chunk Names
output.js 4.8 kB 0 [emitted] main
Entrypoint main = output.js
chunk {0} output.js (main) 698 bytes [entry] [rendered]
> main [3] ./example.js
[0] ./math.js 347 bytes {0} [built]
[exports: add, multiply, list]
[only some exports used: add, multiply]
harmony import ./math [1] ./library.js 2:0-78
harmony import ./math [3] ./example.js 1:0-29
[1] ./library.js 111 bytes {0} [built]
[exports: a, b, c, reexportedAdd, reexportedMultiply]
[only some exports used: reexportedMultiply]
harmony import ./library [3] ./example.js 2:0-37
[2] ./abc.js 126 bytes {0} [built]
[exports: a, b, c]
[no exports used]
harmony import ./abc [1] ./library.js 1:0-32
[3] ./example.js 114 bytes {0} [built]
Minimized (uglify-js, no zip)
Hash: a5187553da983450faff
Version: webpack 2.1.0-beta.25
Time: 241ms
Asset Size Chunks Chunk Names
output.js 866 bytes 0 [emitted] main
Entrypoint main = output.js
chunk {0} output.js (main) 698 bytes [entry] [rendered]
> main [3] ./example.js
[0] ./math.js 347 bytes {0} [built]
[exports: add, multiply, list]
[only some exports used: add, multiply]
harmony import ./math [1] ./library.js 2:0-78
harmony import ./math [3] ./example.js 1:0-29
[1] ./library.js 111 bytes {0} [built]
[exports: a, b, c, reexportedAdd, reexportedMultiply]
[only some exports used: reexportedMultiply]
harmony import ./library [3] ./example.js 2:0-37
[2] ./abc.js 126 bytes {0} [built]
[exports: a, b, c]
[no exports used]
harmony import ./abc [1] ./library.js 1:0-32
[3] ./example.js 114 bytes {0} [built]