Skip to content

Instantly share code, notes, and snippets.

@andrewiggins
Last active January 28, 2022 19:10
Show Gist options
  • Save andrewiggins/471c6451ff17ebf6f8d9b3d21789c106 to your computer and use it in GitHub Desktop.
Save andrewiggins/471c6451ff17ebf6f8d9b3d21789c106 to your computer and use it in GitHub Desktop.
Webpack example
node_modules
package-lock.json
/******/ (() => { // webpackBootstrap
/******/ "use strict";
/******/ var __webpack_modules__ = ({
/***/ "./log.js":
/*!****************!*\
!*** ./log.js ***!
\****************/
/***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {
__webpack_require__.r(__webpack_exports__);
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */ "log": () => (/* binding */ log),
/* harmony export */ "warn": () => (/* binding */ warn),
/* harmony export */ "error": () => (/* binding */ error)
/* harmony export */ });
function log(msg) {
console.log(msg);
}
function warn(msg) {
console.warn(msg);
}
function error(msg) {
console.error(msg);
}
/***/ }),
/***/ "./maths.js":
/*!******************!*\
!*** ./maths.js ***!
\******************/
/***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {
__webpack_require__.r(__webpack_exports__);
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */ "square": () => (/* binding */ square),
/* harmony export */ "cube": () => (/* binding */ cube)
/* harmony export */ });
// maths.js
// This function isn't used anywhere, so
// Rollup excludes it from the bundle...
function square ( x ) {
return x * x;
}
// This function gets included
function cube ( x ) {
// rewrite this as `square( x ) * x`
// and see what happens!
return x * x * x;
}
/***/ })
/******/ });
/************************************************************************/
/******/ // The module cache
/******/ var __webpack_module_cache__ = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/ // Check if module is in cache
/******/ var cachedModule = __webpack_module_cache__[moduleId];
/******/ if (cachedModule !== undefined) {
/******/ return cachedModule.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;
/******/ }
/******/
/************************************************************************/
/******/ /* webpack/runtime/define property getters */
/******/ (() => {
/******/ // define getter functions for harmony exports
/******/ __webpack_require__.d = (exports, definition) => {
/******/ for(var key in definition) {
/******/ if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
/******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
/******/ }
/******/ }
/******/ };
/******/ })();
/******/
/******/ /* webpack/runtime/hasOwnProperty shorthand */
/******/ (() => {
/******/ __webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))
/******/ })();
/******/
/******/ /* webpack/runtime/make namespace object */
/******/ (() => {
/******/ // define __esModule on exports
/******/ __webpack_require__.r = (exports) => {
/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
/******/ }
/******/ Object.defineProperty(exports, '__esModule', { value: true });
/******/ };
/******/ })();
/******/
/************************************************************************/
var __webpack_exports__ = {};
// This entry need to be wrapped in an IIFE because it need to be isolated against other modules in the chunk.
(() => {
/*!*****************!*\
!*** ./main.js ***!
\*****************/
__webpack_require__.r(__webpack_exports__);
/* harmony import */ var _maths_js__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./maths.js */ "./maths.js");
/* harmony import */ var _log_js__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./log.js */ "./log.js");
/* TREE-SHAKING */
(0,_log_js__WEBPACK_IMPORTED_MODULE_1__.log)( (0,_maths_js__WEBPACK_IMPORTED_MODULE_0__.cube)( 5 ) ); // 125
})();
/******/ })()
;
export function log(msg) {
console.log(msg);
}
export function warn(msg) {
console.warn(msg);
}
export function error(msg) {
console.error(msg);
}
/* TREE-SHAKING */
import { cube } from './maths.js';
import { log } from './log.js';
log( cube( 5 ) ); // 125
// maths.js
// This function isn't used anywhere, so
// Rollup excludes it from the bundle...
export function square ( x ) {
return x * x;
}
// This function gets included
export function cube ( x ) {
// rewrite this as `square( x ) * x`
// and see what happens!
return x * x * x;
}
{
"name": "webpack-example",
"version": "1.0.0",
"description": "",
"main": "dist/main.js",
"scripts": {
"build": "webpack --entry ./main.js --mode development --no-devtool"
},
"repository": {
"type": "git",
"url": "git+https://gist.github.com/471c6451ff17ebf6f8d9b3d21789c106.git"
},
"author": "",
"license": "ISC",
"bugs": {
"url": "https://gist.github.com/471c6451ff17ebf6f8d9b3d21789c106"
},
"homepage": "https://gist.github.com/471c6451ff17ebf6f8d9b3d21789c106",
"devDependencies": {
"webpack": "^5.67.0",
"webpack-cli": "^4.9.2"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment