深入理解webpack

发布时间:2024-11-13 点击:50
webpack是目前最为流行的打包工具之一,其配置简单,功能强大,拥有丰富的加载器和插件系统,为前端开发者提供了诸多便利。笔者默认各位看官在阅读本章之前已经有了一定的使用经验,所以对webpack的使用方式不做赘述。
阅读本章,你可以了解到如下内容:
webpack打包后代码结构webpack核心架构 —— tapablewebpack事件流webpack插件实现机制webpack加载器实现机制
webpack打包后代码结构
简单打包
我们首先写一个最简单的方法,然后使用webpack进行打包:
// /webpack/bundles/simple/modulea.jswindow.printa = function printa() { console.log(`this is module a!`);}一个比较基本的webpack配置文件:
// /webpack/bundles/simple/webpack.config.jsconst path = require('path');const webpack = require('webpack');const htmlwebpackplugin = require('html-webpack-plugin');module.exports = { entry: { main: './modulea.js' }, output: { path: path.resolve(__dirname, 'dist'), filename: 'simple.bundle.js' }, plugins: [ new htmlwebpackplugin({ template: './index.html' }) ]}创建一个html文件用于在浏览器环境下测试:
<!doctype html><html lang="en"><head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="x-ua-compatible" content="ie=edge"> <title>webpack - simple bundle</title></head><body> </body></html>执行打包命令webpack 后我们获得了一个 dist 目录,我们打开 simple.bundle.js 文件:
// (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;//// // define getter function for harmony exports// __webpack_require__.d = function(exports, name, getter) {// if(!__webpack_require__.o(exports, name)) {// 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 = "";//// // load entry module and return exports// return __webpack_require__(__webpack_require__.s = 0);// })//// ([/* 0 *//*/ (function(module, exports) {window.printa = function printa() { console.log(`this is module a!`);}/*/ })// ]);主要看这段:
// ......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 modul

Adobe Flash Player发布新版本22.0.0.192,修复了重大安全漏洞
【全局盘点】华为云政企全栈技术创新能力图谱
域名的后缀名用哪个好
购买云服务器镜像怎么选
“.手机”域名开启乌镇时间,中文畅行网络重任在肩
商标注册完成有后续费用吗
用容器or虚拟机?
转发不生效-域名及账户问题