使用dependOn配置进行代码分割。
默认情况下,每个entry 入口都会将入口使用的模块全部打包在一起。dependOn可以让一个entry依赖另一个的entry。
文件
Snipaste_2020-09-11_20-22-48.png

webpack.config.js

  1. module.exports = {
  2. entry: {
  3. app: { import: "./app.js", dependOn: ["other-vendors"] },
  4. page1: { import: "./page1.js", dependOn: ["app", "react-vendors"] },
  5. "react-vendors": ["react", "react-dom", "prop-types"],
  6. "other-vendors": "./other-vendors"
  7. },
  8. optimization: {
  9. runtimeChunk: "single",
  10. chunkIds: "named" // To keep filename consistent between different modes (for example building only)
  11. },
  12. devtool: false,
  13. stats: {
  14. chunks: true,
  15. chunkRelations: true
  16. }
  17. };

app.js

  1. import isomorphicFetch from "isomorphic-fetch";
  2. import lodash from "lodash";
  3. console.log(isomorphicFetch, lodash);

page1.js

  1. import isomorphicFetch from "isomorphic-fetch";
  2. import react from "react";
  3. import reactDOM from "react-dom";
  4. console.log(isomorphicFetch, react, reactDOM);
  5. import("./lazy");

lazy.js

  1. import lodash from "lodash";
  2. import propTypes from "prop-types";
  3. console.log(lodash, propTypes);

other-vendors.js

  1. import lodash from "lodash";
  2. import isomorphicFetch from "isomorphic-fetch";
  3. // Additional initializations
  4. console.log(lodash, isomorphicFetch);

other-vendors包含了模块lodash, isomorphicFetch
打包后dist/app.js并不会有lodash, isomorphicFetch模块,是通过引入dist/other-vendors.js使用这两个模块。
dist/page1.js 依赖了app.js和react-vendors,所以打包后文件包含这两个模块依赖的模块的引入,但不包含模块代码。

  1. (self["webpackChunk"] = self["webpackChunk"] || []).push([["page1"],{
  2. /***/ "./page1.js":
  3. /*!******************!*\
  4. !*** ./page1.js ***!
  5. \******************/
  6. /*! namespace exports */
  7. /*! exports [not provided] [no usage info] */
  8. /*! runtime requirements: __webpack_require__, __webpack_require__.n, __webpack_require__.r, __webpack_exports__, __webpack_require__.e, __webpack_require__.* */
  9. /***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {
  10. "use strict";
  11. __webpack_require__.r(__webpack_exports__);
  12. /* harmony import */ var isomorphic_fetch__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! isomorphic-fetch */ "./node_modules/isomorphic-fetch.js");
  13. /* harmony import */ var isomorphic_fetch__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(isomorphic_fetch__WEBPACK_IMPORTED_MODULE_0__);
  14. /* harmony import */ var react__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! react */ "./node_modules/react.js");
  15. /* harmony import */ var react__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(react__WEBPACK_IMPORTED_MODULE_1__);
  16. /* harmony import */ var react_dom__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! react-dom */ "./node_modules/react-dom.js");
  17. /* harmony import */ var react_dom__WEBPACK_IMPORTED_MODULE_2___default = /*#__PURE__*/__webpack_require__.n(react_dom__WEBPACK_IMPORTED_MODULE_2__);
  18. console.log((isomorphic_fetch__WEBPACK_IMPORTED_MODULE_0___default()), (react__WEBPACK_IMPORTED_MODULE_1___default()), (react_dom__WEBPACK_IMPORTED_MODULE_2___default()));
  19. __webpack_require__.e(/*! import() */ "lazy_js").then(__webpack_require__.bind(__webpack_require__, /*! ./lazy */ "./lazy.js"));
  20. /***/ })
  21. },
  22. 0,[["./page1.js","app","runtime","react-vendors","other-vendors"]]]);