使用dependOn配置进行代码分割。
默认情况下,每个entry 入口都会将入口使用的模块全部打包在一起。dependOn可以让一个entry依赖另一个的entry。
文件
webpack.config.js
module.exports = {
entry: {
app: { import: "./app.js", dependOn: ["other-vendors"] },
page1: { import: "./page1.js", dependOn: ["app", "react-vendors"] },
"react-vendors": ["react", "react-dom", "prop-types"],
"other-vendors": "./other-vendors"
},
optimization: {
runtimeChunk: "single",
chunkIds: "named" // To keep filename consistent between different modes (for example building only)
},
devtool: false,
stats: {
chunks: true,
chunkRelations: true
}
};
app.js
import isomorphicFetch from "isomorphic-fetch";
import lodash from "lodash";
console.log(isomorphicFetch, lodash);
page1.js
import isomorphicFetch from "isomorphic-fetch";
import react from "react";
import reactDOM from "react-dom";
console.log(isomorphicFetch, react, reactDOM);
import("./lazy");
lazy.js
import lodash from "lodash";
import propTypes from "prop-types";
console.log(lodash, propTypes);
other-vendors.js
import lodash from "lodash";
import isomorphicFetch from "isomorphic-fetch";
// Additional initializations
console.log(lodash, isomorphicFetch);
other-vendors包含了模块lodash, isomorphicFetch
打包后dist/app.js并不会有lodash, isomorphicFetch模块,是通过引入dist/other-vendors.js使用这两个模块。
dist/page1.js 依赖了app.js和react-vendors,所以打包后文件包含这两个模块依赖的模块的引入,但不包含模块代码。
(self["webpackChunk"] = self["webpackChunk"] || []).push([["page1"],{
/***/ "./page1.js":
/*!******************!*\
!*** ./page1.js ***!
\******************/
/*! namespace exports */
/*! exports [not provided] [no usage info] */
/*! runtime requirements: __webpack_require__, __webpack_require__.n, __webpack_require__.r, __webpack_exports__, __webpack_require__.e, __webpack_require__.* */
/***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {
"use strict";
__webpack_require__.r(__webpack_exports__);
/* harmony import */ var isomorphic_fetch__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! isomorphic-fetch */ "./node_modules/isomorphic-fetch.js");
/* harmony import */ var isomorphic_fetch__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(isomorphic_fetch__WEBPACK_IMPORTED_MODULE_0__);
/* harmony import */ var react__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! react */ "./node_modules/react.js");
/* harmony import */ var react__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(react__WEBPACK_IMPORTED_MODULE_1__);
/* harmony import */ var react_dom__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! react-dom */ "./node_modules/react-dom.js");
/* harmony import */ var react_dom__WEBPACK_IMPORTED_MODULE_2___default = /*#__PURE__*/__webpack_require__.n(react_dom__WEBPACK_IMPORTED_MODULE_2__);
console.log((isomorphic_fetch__WEBPACK_IMPORTED_MODULE_0___default()), (react__WEBPACK_IMPORTED_MODULE_1___default()), (react_dom__WEBPACK_IMPORTED_MODULE_2___default()));
__webpack_require__.e(/*! import() */ "lazy_js").then(__webpack_require__.bind(__webpack_require__, /*! ./lazy */ "./lazy.js"));
/***/ })
},
0,[["./page1.js","app","runtime","react-vendors","other-vendors"]]]);