版本对比
"webpack": "^4.44.1",
"webpack-cli": "^3.3.12"
vs
"webpack": "^5.0.0-beta.29"
"webpack-cli": "^4.0.0-beta.8"
单文件打包
原始文件
// index.js
console.log("test");
打包后的文件(简化后)
主要删除了用于浏览器兼容的代码,简洁呈现
// webpack4
(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: {}
};
// 调用执行当前的函数
modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
return module.exports;
}
// Load entry module and return exports
return __webpack_require__("./src/index.js");
})
({
"./src/index.js": (function (module, exports) {
console.log("test");
})
});
// webpack5
(() => {
console.log("test");
})();
总结
相较于 webpack4,webpack5 在单文件打包方面更加的简介,抛弃了单文件不需要的 module.exports
部分,保留了最原始的部分。
包含import打包
原始文件
// importInfo.js
export const data = "from import data";
// index.js
import {data} from "./importInfo";
console.log("test");
console.log(data);
打包后的文件(简化后)
// webpack4 还是那个熟悉的闭包那个自执行函数
(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: {}
};
// 调用执行当前的函数
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;
}
// Load entry module and return exports
return __webpack_require__("./src/index.js");
})
({
"./src/importInfo.js": (function (module, __webpack_exports__, __webpack_require__) {
Object.defineProperty(__webpack_exports__,
"data", {
get: function () {
return data;
}
});
const data = "from import data";
}),
"./src/index.js": (function (module, __webpack_exports__, __webpack_require__) {
var _importInfo__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__("./src/importInfo.js");
console.log("test");
console.log(_importInfo__WEBPACK_IMPORTED_MODULE_0__["data"]);
})
});
// webpack5
// 将原本传入的key value 文件对象直接放到了函数里面
(() => {
var __webpack_modules__ = ({
"./src/importInfo.js": ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {
// 这里的兼容写法有所变化
const definition = {
"data": () => data
};
for (var key in definition) {
Object.defineProperty(exports, key, {
get: definition[key]
});
}
const data = "from import data";
}),
"./src/index.js": ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {
var _importInfo__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__("./src/importInfo.js");
console.log("test");
console.log(_importInfo__WEBPACK_IMPORTED_MODULE_0__.data);
})
});
var __webpack_module_cache__ = {};
// The require function
function __webpack_require__(moduleId) {
// Check if module is in cache
if (__webpack_module_cache__[moduleId]) {
return __webpack_module_cache__[moduleId].exports;
}
// Create a new module (and put it into the cache)
var module = __webpack_module_cache__[moduleId] = {
//去除了不需要情况下的i 和 l
// 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_require__("./src/index.js");
// This entry module used 'exports' so it can't be inlined
})();
总结
在有导入的情况下,相较于webpack4
,
- 将原本的
function
改成了 箭头函数,这样this的指向更加清晰,祛除了原本使用call的调用方式。 - 将原本使用参数传入的文件名和对应内容的 key value 的object,直接写到了函数中。