当前位置: 首页 > 编程日记 > 正文

webpack 最简打包结果分析

原文链接:https://ssshooter.com/2019-02...

现在的 webpack 不再是入门噩梦,过去 webpack 最让人心塞的莫过于配置文件,而 webpack4 诞生随之而来的是无配置 webpack。

使用 webpack4,至少只需要安装 webpack 和 webpack cli。所以大家完全可以自己打一个最简单的包,还能修改插件对比前后的区别。

npm i webpack webpack-cli -D 安装后,因为 webpack4 会默认 src 为入口目录,所以先新建 src/index.js

// src/index.js
import { sth } from './shouldImport'
import other from './shouldImport'let test = 'this is a variable'export default {a: test + ',' + sth,other,
}

为了更了解 webpack  导入机制所以再新建 src/shouldImport.js

// src/shouldImport.js
export let sth = 'something you need'export default {others: '',
}

然后运行 node_modules/.bin/webpack --mode development 即可在 dist/main.js 看到打包后的文件。

但是默认设置中模块文件会被 eval 包裹导致不便查看,所以需要再在设置做一点修改,把 devtool 属性改为 'source-map'

// 在根目录新建 webpack.config.js 文件
module.exports = mode => {if (mode === 'production') {return {}}return {devtool: 'source-map',}
}

然后再打包应该就能看到类似一下的文件结构,开发环境下打包得到的文件自带注释,理解起来不难:

;(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// 执行模块函数(有点不懂为什么 this 要传入 module.exports)modules[moduleId].call(module.exports, // thismodule, // 模块对象本身module.exports, // 模块对象的 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// Object.prototype.hasOwnProperty.call__webpack_require__.o = function(object, property) {return Object.prototype.hasOwnProperty.call(object, property)}// define getter function for harmony exports// 为 ES6 export 定义 getter 函数__webpack_require__.d = function(exports, name, getter) {if (!__webpack_require__.o(exports, name)) {// 检查属性是否存在Object.defineProperty(exports, name, { enumerable: true, get: getter })}}// define __esModule on exports// 于 export 定义 __esModule__webpack_require__.r = function(exports) {if (typeof Symbol !== 'undefined' && Symbol.toStringTag) {Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' })}Object.defineProperty(exports, '__esModule', { value: true })}// create a fake namespace object// 创建代用命名空间对象// mode & 1: value is a module id, require it// value 是模块 id,必要// mode & 2: merge all properties of value into the ns// 合并 value 所有属性到 ns// mode & 4: return value when already ns object// ns 已经是对象时返回 value// mode & 8|1: behave like require// 表现如 require__webpack_require__.t = function(value, mode) {if (mode & 1) value = __webpack_require__(value)if (mode & 8) return valueif (mode & 4 && typeof value === 'object' && value && value.__esModule)return valuevar ns = Object.create(null)__webpack_require__.r(ns)Object.defineProperty(ns, 'default', { enumerable: true, value: value })if (mode & 2 && typeof value != 'string')for (var key in value)__webpack_require__.d(ns,key,function(key) {return value[key]}.bind(null, key))return ns}// getDefaultExport function for compatibility with non-harmony modules// 用于兼容非 ES6 模块的 getDefaultExport 函数__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}// __webpack_public_path____webpack_require__.p = ''// Load entry module and return exports// 加载入口模块并返回 exportreturn __webpack_require__((__webpack_require__.s = './src/index.js'))
})({'./src/index.js':/*! exports provided: default */function(module, __webpack_exports__, __webpack_require__) {'use strict'__webpack_require__.r(__webpack_exports__) // 于 export 定义 __esModule/* harmony import */var _shouldImport__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__('./src/shouldImport.js')let test = 'this is a variable'/* harmony default export */__webpack_exports__['default'] = {a: test + ',' + _shouldImport__WEBPACK_IMPORTED_MODULE_0__['sth'],other: _shouldImport__WEBPACK_IMPORTED_MODULE_0__['default'],}},'./src/shouldImport.js':/*! exports provided: sth, default */function(module, __webpack_exports__, __webpack_require__) {'use strict'__webpack_require__.r(__webpack_exports__)/* harmony export (binding) */__webpack_require__.d(__webpack_exports__, 'sth', function() {return sth})let sth = 'something you need'__webpack_exports__['default'] = {others: '',}},
})

源文件中的所有 importexport 都会转换为对应的辅助函数。

  • import 对应 __webpack_require__
  • export 对应 __webpack_exports__['default'] 直接赋值和 __webpack_require__.d

整理一下整个流程:

  1. 定义 __webpack_require__ 及其辅助函数
  2. 使用 __webpack_require__ 引入入口模块
  3. __webpack_require__ 函数载入模块,将模块放到模块缓存
  4. 调用模块

    1. 同样使用 __webpack_require__ 读取依赖(回到第 3 步)
    2. 运行模块内部功能
    3. 使用 __webpack_exports__['default'] 直接赋值和 __webpack_require__.d 输出
  5. 运行结束

相关文章:

orb-slam2(学习笔记)+相机

单目(Monocular)、双目(Stereo)、深度相机(RGB-D) 深度相机能够读取每个像素离相机的距离 ,单目相机 只使用一个摄像头进行SLAM的做法叫做单目SLAM(Monocular SLAM),结构…

如何在nuget上传自己的包+搭建自己公司的NuGet服务器(新方法)

运维相关:http://www.cnblogs.com/dunitian/p/4822808.html#iis 先注册一个nuget账号https://www.nuget.org/ 下载并安装一下NuGetPackageExplorer:https://github.com/NuGetPackageExplorer/NuGetPackageExplorer 创建一个包(vs其实也是可以…

【Codeforces】1080C Masha and two friends (棋盘染色)

http://codeforces.com/problemset/problem/1080/C 给定一个棋盘,(1,1)的位置是白色,观察可以知道,如果横纵坐标之和是偶数,那么是白色,奇数的话就是黑色。 只要算出染色以后白色方…

Java多线程001——一图读懂线程与进程

本博客 猫叔的博客,转载请申明出处前言 本系列将由浅入深,学习Java并发多线程。 一图读懂线程与进程 1、一个进程可以包含一个或多个线程。(其实你经常听到“多线程”,没有听过“多进程”嘛)2、进程存在堆和方法区 3、…

TermCriteria模板类

学习写vo过程中遇到的 类功能:模板类,作为迭代算法的终止条件。 构造函数: TermCriteria(int type,int maxCount,double epsilon); 参数说明: type 迭代终止条件类型 typeTermCriteria::MAX_ITER/TermCrite…

SQL优化快速入门

最近遇到一个专门进行SQL技术优化的项目,对很多既有的老存储过程进行调优(现在已经不再新增任何存储过程),因此系统的对SQL语句编写进行一次科学的学习变得很有必要。这儿将基于黄德承大神的Oracle SQL语句优化一书,选…

【HDU】4509 湫湫系列故事——减肥记II (区间覆盖 暴力)

http://acm.hdu.edu.cn/showproblem.php?pid4509 给出的时间段是被占用的时间&#xff0c;24h 1440 min&#xff0c;求出这些区间以外的区间长度 把00&#xff1a;00 - 23&#xff1a;59 变成0-1440 1-5都是被占用的区域&#xff0c;暴力很好理解 #include <iostream>…

Java并发编程71道面试题及答案

Java并发编程71道面试题及答案 1、在java中守护线程和本地线程区别&#xff1f; java中的线程分为两种&#xff1a;守护线程&#xff08;Daemon&#xff09;和用户线程&#xff08;User&#xff09;。 任何线程都可以设置为守护线程和用户线程&#xff0c;通过方法Thread.setDa…

USB获取图像实时处理

手写&#xff36;&#xff2f;的准备工作&#xff0c;调用&#xff35;&#xff21;&#xff22;或者本地相机获取视频图像&#xff0c;读取并保存视频。 #include "opencv2/core/core.hpp" #include "opencv2/highgui/highgui.hpp" #include "openc…

常见数据库 性能

2019独角兽企业重金招聘Python工程师标准>>> 转载于:https://my.oschina.net/u/582827/blog/778908

【Codeforces】1093C Mishka and the Last Exam

http://codeforces.com/problemset/problem/1093/C 已知b[i]求a[i]和a[n-i1]&#xff0c;n是a数组中元素的个数&#xff0c;a数组的下标从1开始 输出b[1]的时候&#xff0c;直接让a[1] 0&#xff0c;a[n] b[0] 接下来的每一组&#xff0c;先让a[i] b[i]&#xff0c;然后求…

PHP面试常考内容之Memcache和Redis(2)

你好&#xff0c;是我琉忆。继周一&#xff08;2019.2-18&#xff09;发布的“PHP面试常考内容之Memcache和Redis&#xff08;1&#xff09;”后&#xff0c;这是第二篇&#xff0c;感谢你的支持和阅读。本周&#xff08;2019.2-18至2-22&#xff09;的文章内容点为以下几点&am…

毫米波雷达、ADAS中的应用以及毫米波雷达的检测、测距、测速和角度测量

毫米波雷达的检测、测距、测速和角度测量 毫米波(Millimeter-Wave&#xff0c;缩写&#xff1a;MMW)&#xff0c;是指长度在1~10mm的电磁波&#xff0c;对应的频率范围为30~300GHz&#xff0e;根据波的传播理论&#xff0c;频率越高&#xff0c;波长越短&#xff0c;分辨率越高…

【VMC实验室】在QCloud上创建您的SQL Cluster(1)

在国内公有云厂商上搭建一套SQL Cluster的难度相信做Windows的童鞋都会很清楚&#xff0c;并非它的搭建有多少难度&#xff0c;只是很多细节需要注意。腾讯云&#xff0c;QCloud&#xff0c;为什么选择QCloud来做这个实验&#xff0c;除了QCloud是我的东家&#xff08;啊呸&…

【Codeforces】808D Array Division(前后两部分和相等)

http://codeforces.com/contest/808/problem/D 给你一个数组&#xff0c;问&#xff1a;是否可以通过移动一个数字的位置&#xff0c;求只能移动一次&#xff0c;使得这个数组前后部分的和相等&#xff0c;前后部分不一定等长 一个a数组储存数据&#xff0c;另一个b数组b[i]表…

想要确保架构目标达成?适合度函数了解一下

Paula Paul和Rosemary Wang撰写的一篇博文中介绍了适应度函数&#xff08;fitness function&#xff09;的基本概念、入门方法&#xff0c;并给出了如何验证各种架构质量的一些实例。文中提出&#xff0c;适应度函数驱动开发的方法可用于编写测定系统符合架构目标的测试&#x…

标定(二)----------双目相机与IMU联合标定(工具箱使用方法——Kalibr)

16个相机参数&#xff1a; Overview ethz-asl/kalibr is a toolbox that solves the following calibration problems: Multiple camera calibration: intrinsic and extrinsic calibration of a camera-systems with non-globally shared overlapping fields of view Cam…

【Codeforces】659B Qualifying Contest (sort)

http://codeforces.com/problemset/problem/659/B n个人&#xff0c;m个地区&#xff0c;选出每个地区分数最高的两个人 下面有n行&#xff0c;每一行的第一个数表示姓名&#xff0c;第二个数是地区的序号&#xff0c;第三个数是分数 It is guaranteed that all surnames of…

Protractor测试环境搭建

2019独角兽企业重金招聘Python工程师标准>>> 安装node.js.然后&#xff0c;在cmd下&#xff0c;进入E盘下的&#xff0c;我们自己新建的protractor文件夹下&#xff0c; npm install -g protractor 这样就会在本地安装好两个命令行工具&#xff1a;protractor和webd…

Promise和Promise的方法

&#xff08;Promise和Promise的方法&#xff09; Promise是ES6一个伟大的发明&#xff0c;他使我们从回调地狱中能够走出来。 什么是Promise 从字面上来看&#xff0c;Promise就是一个承诺。那么&#xff0c;在ES6当中&#xff0c;Promise通常用来控制异步操作。当一个异步操作…

镜头评价指标及测试方法(三)--------测量原理及3D相机调查

1.测量原理&#xff1a; 1.1、通过红外结构光(Structured Light)来测量像素距离&#xff0c;如Kinect1、Project Tango1、Intel Realsense等&#xff1b; 通过近红外激光器&#xff0c;将具有一定结构特征的光&#xff08;比如离散光斑、条纹光、编码结构光等&#xff09;投射到…

Android--Retrofit的简单使用(一)

1&#xff0c;如果不太了解retrofit的同学可以先去官网学习一下简单使用&#xff1a;http://square.github.io/retrofit/&#xff0c;这里我们以一个简单的Get请求的例子来练习一下 2&#xff0c;https://api.douban.com/v2/movie/top250?start0&count10 目标地址&#xf…

【Codeforces】920A Water The Garden(浇花)

http://codeforces.com/problemset/problem/920/A 给你花坛的数目&#xff0c;花坛都是连续的&#xff0c;某一些花坛里有水龙头&#xff0c;给出了这些有水龙头的位置 The garden from test 1. White colour denotes a garden bed without a tap, red colour — a garden be…

Work with Alexa :Echo匹配连接到Alexa

背景&#xff1a; 通过蓝牙可以让你的智能家居设备和Echo设备连接起来&#xff0c;从而达到Echo通过语音控制智能家居设备。 什么事Alexa Gadgets&#xff1f; 我的理解Alexa Gadgets是智能家居设备的一部分&#xff0c;包含硬件和软件。硬件&#xff1a;蓝牙模块&#xff1b;软…

Maven 学习Tips

2019独角兽企业重金招聘Python工程师标准>>> 使用 deploy 应该注意的地方 在项目的pom文件中增加如下节点&#xff1a;<project><distributionManagement><repository><id>releases</id><url>http://192.168.1.99:8081/nexus/c…

【Codeforces】1015B Obtaining the String(字符串 交换)

http://codeforces.com/contest/1015/problem/B n 表示 字符串的长度 然后是两个字符串&#xff0c;每一次只能交换相邻的两个&#xff0c;如果选择第i个字符的话&#xff0c;只能交换第i个和第i1个 然后输出每一次交换选定的位置 暴力&#xff0c;模拟&#xff0c;queue&am…

07 分支管理 —— Feature分支

2019独角兽企业重金招聘Python工程师标准>>> 07 分支管理 —— Feature分支 软件开发中&#xff0c;总有无穷无尽的新的功能要不断添加进来。添加一个新功能时&#xff0c;你肯定不希望因为一些实验性质的代码&#xff0c;把主分支搞乱了&#xff0c;所以&#xff0…

适用于0基础小伙伴的HTML知识点总结 先到先得哟

HTML基础知识点分享 前段时间一直忙于私下学习消化一些PHP中的知识体系&#xff0c;导致博客开通至今一直没发表相关技能贴。最近呢~博主夏天我正在进行前半段知识体系和框架的总结阶段。借此&#xff0c;抽空给大家分享一下私下自己学习时的一些心得体会以及知识体系&#xff…

SLAM小结——求解退化问题解析(F、H、E)(面试经验总结)

总结&#xff1a; 1 Essential Matrix E t ^ R 为3*3的矩阵&#xff0c;奇异值为 [ u, u, 0] ^ T 的形式。为本质矩阵的内在性质。 性质&#xff1a;理论上综合旋转、平移共有6个自由度&#xff0c;因尺度等价&#xff0c;E有5个自由度。 求解&#xff1a;一般使用…

【Codeforces】1065B Vasya and Isolated Vertices (无向图的)

http://codeforces.com/problemset/problem/1065/B 一个图&#xff0c;给定顶点数&#xff0c;边数&#xff0c;求解最小孤立点数和最大孤立点数 最小的话&#xff0c;一个边连接两个顶点成为一个整体&#xff0c;如果最后有多余的点&#xff0c;那些就是孤立的点 最大就是找…