摘要:原文链接现在的不再是入门噩梦过去最让人心塞的莫过于配置文件,而诞生随之而来的是无配置。所以大家完全可以自己打一个最简单的包,还能修改插件对比前后的区别。然后运行即可在看到打包后的文件。
原文链接: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, // this
module, // 模块对象本身
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 value
if (mode & 4 && typeof value === "object" && value && value.__esModule)
return value
var 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
// 加载入口模块并返回 export
return __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: "",
}
},
})
源文件中的所有 import 和 export 都会转换为对应的辅助函数。
import 对应 __webpack_require__
export 对应 __webpack_exports__["default"] 直接赋值和 __webpack_require__.d。
整理一下整个流程:
定义 __webpack_require__ 及其辅助函数
使用 __webpack_require__ 引入入口模块
__webpack_require__ 函数载入模块,将模块放到模块缓存
调用模块
同样使用 __webpack_require__ 读取依赖(回到第 3 步)
运行模块内部功能
使用 __webpack_exports__["default"] 直接赋值和 __webpack_require__.d 输出
运行结束
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/101864.html
摘要:支持转义转义需要的依赖,支持装饰器。在中增加的配置数组中。压缩文件安装依赖在中的中增加配置打包前先清空输出目录在中增加的配置至此,配置已经基本能满足需求。 webpack 核心概念: Entry: 入口 Module:模块,webpack中一切皆是模块 Chunk:代码库,一个chunk由十多个模块组合而成,用于代码合并与分割 Loader:模块转换器,用于把模块原内容按照需求转换成...
摘要:由于项目的不断扩大,只会影响我们定位功能和问题的速度,因此对冗余文件进行清理,是很重要的。我们在项目中使用的,自动将各个图标进行。 进入公司之后,接手的便是前人留下来的一个大项目。庆幸的是整个项目拥有完善的产品功能文档,但是由于项目过于庞大,老旧。包含了打包过慢,冗余文件过多等诸多问题。想要快速的解决这些问题,想要完全把功能重构一遍的话,成本太高了。一个一个文件来过,时间成本也比较大。...
摘要:开发一个自己的脚手架了解了一些脚手架的工作方式与的基本概念,咱们就可以来创建一个属于自己的脚手架。引言 下面是一个使用脚手架来初始化项目的典型例子。 showImg(https://user-gold-cdn.xitu.io/2019/5/16/16ac081750971790); 随着前端工程化的理念不断深入,越来越多的人选择使用脚手架来从零到一...
摘要:继个实例入门并掌握二后续配置配置配置使用加快打包速度多页面打包配置编写编写编写十七配置源码地址本节使用的代码为基础我们来模拟平时开发中,将打包完的代码防止到服务器上的操作,首先打包代码然后安装一个插件在中配置一个命令运 继 24 个实例入门并掌握「Webpack4」(二) 后续: PWA 配置 TypeScript 配置 Eslint 配置 使用 DLLPlugin 加快打包速度 多...
阅读 1820·2021-11-11 16:55
阅读 1839·2021-10-08 10:16
阅读 1401·2021-09-26 10:20
阅读 3892·2021-09-01 10:47
阅读 2647·2019-08-30 15:52
阅读 2842·2019-08-30 13:18
阅读 3391·2019-08-30 13:15
阅读 1342·2019-08-30 10:55