dependOn feature

Co-authored-by: Tobias Koppers <tobias.koppers@googlemail.com>
This commit is contained in:
Sergey Melyukov 2020-01-31 18:54:42 +03:00 committed by Tobias Koppers
parent 16c751ba9b
commit c31d941899
46 changed files with 1350 additions and 3 deletions

View File

@ -542,6 +542,10 @@ export interface EntryObject {
* via the `definition` "EntryDescription". * via the `definition` "EntryDescription".
*/ */
export interface EntryDescription { export interface EntryDescription {
/**
* The entrypoints that the current entrypoint depend on. They must be loaded when this entrypoint is loaded.
*/
dependOn?: string | NonEmptyArrayOfUniqueStringValues;
/** /**
* The filename of the entrypoint as relative path inside the `output.path` directory. * The filename of the entrypoint as relative path inside the `output.path` directory.
*/ */

View File

@ -0,0 +1,543 @@
This example shows how to use Code Splitting with entrypoint dependOn
# webpack.config.js
```javascript
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": ["lodash", "isomorphic-fetch"]
},
optimization: {
runtimeChunk: "single",
chunkIds: "deterministic" // To keep filename consistent between different modes (for example building only)
}
};
```
# app.js
```javascript
import isomorphicFetch from "isomorphic-fetch";
import lodash from "lodash";
console.log(isomorphicFetch, lodash);
```
# page1.js
```javascript
import isomorphicFetch from "isomorphic-fetch";
import lodash from "lodash";
import react from "react";
import reactDOM from "react-dom";
import propTypes from "prop-types";
console.log(isomorphicFetch, lodash, react, reactDOM, propTypes);
```
# other-vendors.js
```javascript
export { default as lodash } from "lodash";
export { default as isomorphicFetch } from "isomorphic-fetch";
```
# react-vendors.js
```javascript
export { default as react } from "react";
export { default as reactDOM } from "react-dom";
export { default as propTypes } from "prop-types";
```
# dist/runtime.js
```javascript
/******/ (() => { // webpackBootstrap
/******/ "use strict";
/******/ var __webpack_modules__ = ({});
```
<details><summary><code>/* webpack runtime code */</code></summary>
``` js
/************************************************************************/
/******/ // The module cache
/******/ 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] = {
/******/ // 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;
/******/ }
/******/
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = __webpack_modules__;
/******/
/************************************************************************/
/******/ /* webpack/runtime/compat get default export */
/******/ (() => {
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = (module) => {
/******/ var getter = module && module.__esModule ?
/******/ () => module['default'] :
/******/ () => module;
/******/ __webpack_require__.d(getter, { a: getter });
/******/ return getter;
/******/ };
/******/ })();
/******/
/******/ /* webpack/runtime/define property getters */
/******/ (() => {
/******/ // define getter functions for harmony exports
/******/ __webpack_require__.d = (exports, definition) => {
/******/ for(var key in definition) {
/******/ if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
/******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
/******/ }
/******/ }
/******/ };
/******/ })();
/******/
/******/ /* webpack/runtime/hasOwnProperty shorthand */
/******/ (() => {
/******/ __webpack_require__.o = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop)
/******/ })();
/******/
/******/ /* webpack/runtime/jsonp chunk loading */
/******/ (() => {
/******/ // object to store loaded and loading chunks
/******/ // undefined = chunk not loaded, null = chunk preloaded/prefetched
/******/ // Promise = chunk loading, 0 = chunk loaded
/******/ var installedChunks = {
/******/ 666: 0
/******/ };
/******/
/******/ var deferredModules = [
/******/
/******/ ];
/******/ // no chunk on demand loading
/******/
/******/ // no prefetching
/******/
/******/ // no preloaded
/******/
/******/ // no HMR
/******/
/******/ // no HMR manifest
/******/
/******/ var checkDeferredModules = () => {
/******/
/******/ };
/******/ function checkDeferredModulesImpl() {
/******/ var result;
/******/ for(var i = 0; i < deferredModules.length; i++) {
/******/ var deferredModule = deferredModules[i];
/******/ var fulfilled = true;
/******/ for(var j = 1; j < deferredModule.length; j++) {
/******/ var depId = deferredModule[j];
/******/ if(installedChunks[depId] !== 0) fulfilled = false;
/******/ }
/******/ if(fulfilled) {
/******/ deferredModules.splice(i--, 1);
/******/ result = __webpack_require__(__webpack_require__.s = deferredModule[0]);
/******/ }
/******/ }
/******/ if(deferredModules.length === 0) {
/******/ __webpack_require__.x();
/******/ __webpack_require__.x = () => {
/******/
/******/ }
/******/ }
/******/ return result;
/******/ }
/******/ __webpack_require__.x = () => {
/******/ // reset startup function so it can be called again when more startup code is added
/******/ __webpack_require__.x = () => {
/******/
/******/ }
/******/ jsonpArray = jsonpArray.slice();
/******/ for(var i = 0; i < jsonpArray.length; i++) webpackJsonpCallback(jsonpArray[i]);
/******/ return (checkDeferredModules = checkDeferredModulesImpl)();
/******/ };
/******/
/******/ // install a JSONP callback for chunk loading
/******/ function webpackJsonpCallback(data) {
/******/ var chunkIds = data[0];
/******/ var moreModules = data[1];
/******/ var executeModules = data[2];
/******/ var runtime = data[3];
/******/ // add "moreModules" to the modules object,
/******/ // then flag all "chunkIds" as loaded and fire callback
/******/ var moduleId, chunkId, i = 0, resolves = [];
/******/ for(;i < chunkIds.length; i++) {
/******/ chunkId = chunkIds[i];
/******/ if(__webpack_require__.o(installedChunks, chunkId) && installedChunks[chunkId]) {
/******/ resolves.push(installedChunks[chunkId][0]);
/******/ }
/******/ installedChunks[chunkId] = 0;
/******/ }
/******/ for(moduleId in moreModules) {
/******/ if(__webpack_require__.o(moreModules, moduleId)) {
/******/ __webpack_require__.m[moduleId] = moreModules[moduleId];
/******/ }
/******/ }
/******/ if(runtime) runtime(__webpack_require__);
/******/ if(parentJsonpFunction) parentJsonpFunction(data);
/******/ while(resolves.length) {
/******/ resolves.shift()();
/******/ }
/******/
/******/ // add entry modules from loaded chunk to deferred list
/******/ if(executeModules) deferredModules.push.apply(deferredModules, executeModules);
/******/
/******/ // run deferred modules when all chunks ready
/******/ return checkDeferredModules();
/******/ };
/******/
/******/ var jsonpArray = window["webpackJsonp"] = window["webpackJsonp"] || [];
/******/ var oldJsonpFunction = jsonpArray.push.bind(jsonpArray);
/******/ jsonpArray.push = webpackJsonpCallback;
/******/ var parentJsonpFunction = oldJsonpFunction;
/******/ })();
/******/
/************************************************************************/
```
</details>
``` js
/******/ // run startup
/******/ return __webpack_require__.x();
/******/ })()
;
```
# dist/app.js
```javascript
(window["webpackJsonp"] = window["webpackJsonp"] || []).push([[143],[
/* 0 */
/*!****************!*\
!*** ./app.js ***!
\****************/
/*! namespace exports */
/*! exports [not provided] [unused] */
/*! runtime requirements: __webpack_require__, __webpack_require__.n, __webpack_require__.* */
/***/ ((__unused_webpack_module, __unused_webpack___webpack_exports__, __webpack_require__) => {
"use strict";
/* harmony import */ var isomorphic_fetch__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! isomorphic-fetch */ 1);
/* harmony import */ var isomorphic_fetch__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(isomorphic_fetch__WEBPACK_IMPORTED_MODULE_0__);
/* harmony import */ var lodash__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! lodash */ 2);
/* harmony import */ var lodash__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(lodash__WEBPACK_IMPORTED_MODULE_1__);
console.log((isomorphic_fetch__WEBPACK_IMPORTED_MODULE_0___default()), (lodash__WEBPACK_IMPORTED_MODULE_1___default()));
/***/ })
],[[0,666,205]]]);
```
# dist/page1.js
```javascript
(window["webpackJsonp"] = window["webpackJsonp"] || []).push([[484],{
/***/ 3:
/*!******************!*\
!*** ./page1.js ***!
\******************/
/*! namespace exports */
/*! exports [not provided] [unused] */
/*! runtime requirements: __webpack_require__, __webpack_require__.n, __webpack_require__.* */
/***/ ((__unused_webpack_module, __unused_webpack___webpack_exports__, __webpack_require__) => {
"use strict";
/* harmony import */ var isomorphic_fetch__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! isomorphic-fetch */ 1);
/* harmony import */ var isomorphic_fetch__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(isomorphic_fetch__WEBPACK_IMPORTED_MODULE_0__);
/* harmony import */ var lodash__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! lodash */ 2);
/* harmony import */ var lodash__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(lodash__WEBPACK_IMPORTED_MODULE_1__);
/* harmony import */ var react__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! react */ 4);
/* harmony import */ var react__WEBPACK_IMPORTED_MODULE_2___default = /*#__PURE__*/__webpack_require__.n(react__WEBPACK_IMPORTED_MODULE_2__);
/* harmony import */ var react_dom__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! react-dom */ 5);
/* harmony import */ var react_dom__WEBPACK_IMPORTED_MODULE_3___default = /*#__PURE__*/__webpack_require__.n(react_dom__WEBPACK_IMPORTED_MODULE_3__);
/* harmony import */ var prop_types__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(/*! prop-types */ 6);
/* harmony import */ var prop_types__WEBPACK_IMPORTED_MODULE_4___default = /*#__PURE__*/__webpack_require__.n(prop_types__WEBPACK_IMPORTED_MODULE_4__);
console.log((isomorphic_fetch__WEBPACK_IMPORTED_MODULE_0___default()), (lodash__WEBPACK_IMPORTED_MODULE_1___default()), (react__WEBPACK_IMPORTED_MODULE_2___default()), (react_dom__WEBPACK_IMPORTED_MODULE_3___default()), (prop_types__WEBPACK_IMPORTED_MODULE_4___default()));
/***/ })
},[[3,666,205,703]]]);
```
# dist/other-vendors.js
```javascript
(window["webpackJsonp"] = window["webpackJsonp"] || []).push([[205],[
/* 0 */,
/* 1 */
/*!******************************************!*\
!*** ./node_modules/isomorphic-fetch.js ***!
\******************************************/
/*! unknown exports (runtime-defined) */
/*! export default [maybe provided (runtime-defined)] [used] [usage and provision prevents renaming] */
/*! other exports [maybe provided (runtime-defined)] [maybe used (runtime-defined)] */
/*! runtime requirements: module */
/***/ ((module) => {
module.exports = "isomorphic-fetch";
/***/ }),
/* 2 */
/*!********************************!*\
!*** ./node_modules/lodash.js ***!
\********************************/
/*! unknown exports (runtime-defined) */
/*! export default [maybe provided (runtime-defined)] [used] [usage and provision prevents renaming] */
/*! other exports [maybe provided (runtime-defined)] [maybe used (runtime-defined)] */
/*! runtime requirements: module */
/***/ ((module) => {
module.exports = 'lodash';
/***/ })
],[[2,666],[1,666]]]);
```
# dist/react-vendors.js
```javascript
(window["webpackJsonp"] = window["webpackJsonp"] || []).push([[703],[
/* 0 */,
/* 1 */,
/* 2 */,
/* 3 */,
/* 4 */
/*!*******************************!*\
!*** ./node_modules/react.js ***!
\*******************************/
/*! unknown exports (runtime-defined) */
/*! export default [maybe provided (runtime-defined)] [used] [usage and provision prevents renaming] */
/*! other exports [maybe provided (runtime-defined)] [maybe used (runtime-defined)] */
/*! runtime requirements: module */
/***/ ((module) => {
module.exports = 'react';
/***/ }),
/* 5 */
/*!***********************************!*\
!*** ./node_modules/react-dom.js ***!
\***********************************/
/*! unknown exports (runtime-defined) */
/*! export default [maybe provided (runtime-defined)] [used] [usage and provision prevents renaming] */
/*! other exports [maybe provided (runtime-defined)] [maybe used (runtime-defined)] */
/*! runtime requirements: module */
/***/ ((module) => {
module.exports = 'react-dom';
/***/ }),
/* 6 */
/*!************************************!*\
!*** ./node_modules/prop-types.js ***!
\************************************/
/*! unknown exports (runtime-defined) */
/*! export default [maybe provided (runtime-defined)] [used] [usage and provision prevents renaming] */
/*! other exports [maybe provided (runtime-defined)] [maybe used (runtime-defined)] */
/*! runtime requirements: module */
/***/ ((module) => {
module.exports = 'react';
/***/ })
],[[4,666],[5,666],[6,666]]]);
```
# Info
## Unoptimized
```
Hash: 0a1b2c3d4e5f6a7b8c9d
Version: webpack 5.0.0-beta.13
Asset Size
app.js 1.07 KiB [emitted] [name: app]
other-vendors.js 1.02 KiB [emitted] [name: other-vendors]
page1.js 1.98 KiB [emitted] [name: page1]
react-vendors.js 1.47 KiB [emitted] [name: react-vendors]
runtime.js 6.07 KiB [emitted] [name: runtime]
Entrypoint app = runtime.js app.js other-vendors.js
Entrypoint page1 = runtime.js page1.js other-vendors.js react-vendors.js
Entrypoint react-vendors = runtime.js react-vendors.js
Entrypoint other-vendors = runtime.js other-vendors.js
chunk app.js (app) 116 bytes [initial] [rendered]
> ./app.js app
./app.js 116 bytes [built]
[no exports]
[no exports used]
entry ./app.js app
chunk other-vendors.js (other-vendors) 64 bytes [initial] [rendered]
> ./app.js app
> isomorphic-fetch other-vendors
> lodash other-vendors
> ./page1.js page1
./node_modules/isomorphic-fetch.js 37 bytes [built]
harmony side effect evaluation isomorphic-fetch ./app.js 1:0-47
harmony import specifier isomorphic-fetch ./app.js 4:12-27
cjs self exports reference ./node_modules/isomorphic-fetch.js 1:0-14
harmony side effect evaluation isomorphic-fetch ./page1.js 1:0-47
harmony import specifier isomorphic-fetch ./page1.js 7:12-27
entry isomorphic-fetch other-vendors
./node_modules/lodash.js 27 bytes [built]
harmony side effect evaluation lodash ./app.js 2:0-28
harmony import specifier lodash ./app.js 4:29-35
cjs self exports reference ./node_modules/lodash.js 1:0-14
harmony side effect evaluation lodash ./page1.js 2:0-28
harmony import specifier lodash ./page1.js 7:29-35
entry lodash other-vendors
chunk page1.js (page1) 241 bytes [initial] [rendered]
> ./page1.js page1
./page1.js 241 bytes [built]
[no exports]
[no exports used]
entry ./page1.js page1
chunk runtime.js (runtime) 3.15 KiB [entry] [rendered]
> ./app.js app
> isomorphic-fetch other-vendors
> lodash other-vendors
> ./page1.js page1
> prop-types react-vendors
> react react-vendors
> react-dom react-vendors
4 chunk modules
chunk react-vendors.js (react-vendors) 82 bytes [initial] [rendered]
> ./page1.js page1
> prop-types react-vendors
> react react-vendors
> react-dom react-vendors
./node_modules/prop-types.js 26 bytes [built]
cjs self exports reference ./node_modules/prop-types.js 1:0-14
harmony side effect evaluation prop-types ./page1.js 5:0-35
harmony import specifier prop-types ./page1.js 7:54-63
entry prop-types react-vendors
./node_modules/react-dom.js 30 bytes [built]
cjs self exports reference ./node_modules/react-dom.js 1:0-14
harmony side effect evaluation react-dom ./page1.js 4:0-33
harmony import specifier react-dom ./page1.js 7:44-52
entry react-dom react-vendors
./node_modules/react.js 26 bytes [built]
cjs self exports reference ./node_modules/react.js 1:0-14
harmony side effect evaluation react ./page1.js 3:0-26
harmony import specifier react ./page1.js 7:37-42
entry react react-vendors
```
## Production mode
```
Hash: 0a1b2c3d4e5f6a7b8c9d
Version: webpack 5.0.0-beta.13
Asset Size
app.js 166 bytes [emitted] [name: app]
other-vendors.js 148 bytes [emitted] [name: other-vendors]
page1.js 234 bytes [emitted] [name: page1]
react-vendors.js 175 bytes [emitted] [name: react-vendors]
runtime.js 1000 bytes [emitted] [name: runtime]
Entrypoint app = runtime.js app.js other-vendors.js
Entrypoint page1 = runtime.js page1.js other-vendors.js react-vendors.js
Entrypoint react-vendors = runtime.js react-vendors.js
Entrypoint other-vendors = runtime.js other-vendors.js
chunk app.js (app) 116 bytes [initial] [rendered]
> ./app.js app
./app.js 116 bytes [built]
[no exports]
[no exports used]
entry ./app.js app
chunk other-vendors.js (other-vendors) 64 bytes [initial] [rendered]
> ./app.js app
> isomorphic-fetch other-vendors
> lodash other-vendors
> ./page1.js page1
./node_modules/isomorphic-fetch.js 37 bytes [built]
harmony side effect evaluation isomorphic-fetch ./app.js 1:0-47
harmony import specifier isomorphic-fetch ./app.js 4:12-27
cjs self exports reference ./node_modules/isomorphic-fetch.js 1:0-14
harmony side effect evaluation isomorphic-fetch ./page1.js 1:0-47
harmony import specifier isomorphic-fetch ./page1.js 7:12-27
entry isomorphic-fetch other-vendors
./node_modules/lodash.js 27 bytes [built]
harmony side effect evaluation lodash ./app.js 2:0-28
harmony import specifier lodash ./app.js 4:29-35
cjs self exports reference ./node_modules/lodash.js 1:0-14
harmony side effect evaluation lodash ./page1.js 2:0-28
harmony import specifier lodash ./page1.js 7:29-35
entry lodash other-vendors
chunk page1.js (page1) 241 bytes [initial] [rendered]
> ./page1.js page1
./page1.js 241 bytes [built]
[no exports]
[no exports used]
entry ./page1.js page1
chunk runtime.js (runtime) 3.15 KiB [entry] [rendered]
> ./app.js app
> isomorphic-fetch other-vendors
> lodash other-vendors
> ./page1.js page1
> prop-types react-vendors
> react react-vendors
> react-dom react-vendors
4 chunk modules
chunk react-vendors.js (react-vendors) 82 bytes [initial] [rendered]
> ./page1.js page1
> prop-types react-vendors
> react react-vendors
> react-dom react-vendors
./node_modules/prop-types.js 26 bytes [built]
cjs self exports reference ./node_modules/prop-types.js 1:0-14
harmony side effect evaluation prop-types ./page1.js 5:0-35
harmony import specifier prop-types ./page1.js 7:54-63
entry prop-types react-vendors
./node_modules/react-dom.js 30 bytes [built]
cjs self exports reference ./node_modules/react-dom.js 1:0-14
harmony side effect evaluation react-dom ./page1.js 4:0-33
harmony import specifier react-dom ./page1.js 7:44-52
entry react-dom react-vendors
./node_modules/react.js 26 bytes [built]
cjs self exports reference ./node_modules/react.js 1:0-14
harmony side effect evaluation react ./page1.js 3:0-26
harmony import specifier react ./page1.js 7:37-42
entry react react-vendors
```

View File

@ -0,0 +1,4 @@
import isomorphicFetch from "isomorphic-fetch";
import lodash from "lodash";
console.log(isomorphicFetch, lodash);

View File

@ -0,0 +1,2 @@
global.NO_TARGET_ARGS = true;
require("../build-common");

View File

@ -0,0 +1 @@
module.exports = "isomorphic-fetch";

View File

@ -0,0 +1 @@
module.exports = 'lodash';

View File

@ -0,0 +1 @@
module.exports = 'react';

View File

@ -0,0 +1 @@
module.exports = 'react-dom';

View File

@ -0,0 +1 @@
module.exports = 'react';

View File

@ -0,0 +1,2 @@
export { default as lodash } from "lodash";
export { default as isomorphicFetch } from "isomorphic-fetch";

View File

@ -0,0 +1,7 @@
import isomorphicFetch from "isomorphic-fetch";
import lodash from "lodash";
import react from "react";
import reactDOM from "react-dom";
import propTypes from "prop-types";
console.log(isomorphicFetch, lodash, react, reactDOM, propTypes);

View File

@ -0,0 +1,3 @@
export { default as react } from "react";
export { default as reactDOM } from "react-dom";
export { default as propTypes } from "prop-types";

View File

@ -0,0 +1,75 @@
This example shows how to use Code Splitting with entrypoint dependOn
# webpack.config.js
```javascript
_{{webpack.config.js}}_
```
# app.js
```javascript
_{{app.js}}_
```
# page1.js
```javascript
_{{page1.js}}_
```
# other-vendors.js
```javascript
_{{other-vendors.js}}_
```
# react-vendors.js
```javascript
_{{react-vendors.js}}_
```
# dist/runtime.js
```javascript
_{{dist/runtime.js}}_
```
# dist/app.js
```javascript
_{{dist/app.js}}_
```
# dist/page1.js
```javascript
_{{dist/page1.js}}_
```
# dist/other-vendors.js
```javascript
_{{dist/other-vendors.js}}_
```
# dist/react-vendors.js
```javascript
_{{dist/react-vendors.js}}_
```
# Info
## Unoptimized
```
_{{stdout}}_
```
## Production mode
```
_{{production:stdout}}_
```

View File

@ -0,0 +1,12 @@
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": ["lodash", "isomorphic-fetch"]
},
optimization: {
runtimeChunk: "single",
chunkIds: "deterministic" // To keep filename consistent between different modes (for example building only)
}
};

View File

@ -0,0 +1,366 @@
This example shows how to use Code Splitting with entrypoint dependOn
# webpack.config.js
```javascript
module.exports = {
entry: {
app: { import: "./app.js", dependOn: ["react-vendors"] },
"react-vendors": ["react", "react-dom", "prop-types"]
},
optimization: {
chunkIds: "deterministic" // To keep filename consistent between different modes (for example building only)
}
};
```
# app.js
```javascript
import react from "react";
import reactDOM from "react-dom";
import propTypes from "prop-types";
console.log(react, reactDOM, propTypes);
```
# react-vendors.js
```javascript
export { default as react } from "react";
export { default as reactDOM } from "react-dom";
export { default as propTypes } from "prop-types";
```
# dist/app.js
```javascript
(window["webpackJsonp"] = window["webpackJsonp"] || []).push([[143],[
/* 0 */
/*!****************!*\
!*** ./app.js ***!
\****************/
/*! namespace exports */
/*! exports [not provided] [unused] */
/*! runtime requirements: __webpack_require__, __webpack_require__.n, __webpack_require__.* */
/***/ ((__unused_webpack_module, __unused_webpack___webpack_exports__, __webpack_require__) => {
"use strict";
/* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! react */ 1);
/* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(react__WEBPACK_IMPORTED_MODULE_0__);
/* harmony import */ var react_dom__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! react-dom */ 2);
/* harmony import */ var react_dom__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(react_dom__WEBPACK_IMPORTED_MODULE_1__);
/* harmony import */ var prop_types__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! prop-types */ 3);
/* harmony import */ var prop_types__WEBPACK_IMPORTED_MODULE_2___default = /*#__PURE__*/__webpack_require__.n(prop_types__WEBPACK_IMPORTED_MODULE_2__);
console.log((react__WEBPACK_IMPORTED_MODULE_0___default()), (react_dom__WEBPACK_IMPORTED_MODULE_1___default()), (prop_types__WEBPACK_IMPORTED_MODULE_2___default()));
/***/ })
],[[0,703]]]);
```
# dist/react-vendors.js
```javascript
/******/ (() => { // webpackBootstrap
/******/ var __webpack_modules__ = ([
/* 0 */,
/* 1 */
/*!*******************************!*\
!*** ./node_modules/react.js ***!
\*******************************/
/*! unknown exports (runtime-defined) */
/*! export default [maybe provided (runtime-defined)] [used] [usage and provision prevents renaming] */
/*! other exports [maybe provided (runtime-defined)] [maybe used (runtime-defined)] */
/*! runtime requirements: module */
/***/ ((module) => {
module.exports = 'react';
/***/ }),
/* 2 */
/*!***********************************!*\
!*** ./node_modules/react-dom.js ***!
\***********************************/
/*! unknown exports (runtime-defined) */
/*! export default [maybe provided (runtime-defined)] [used] [usage and provision prevents renaming] */
/*! other exports [maybe provided (runtime-defined)] [maybe used (runtime-defined)] */
/*! runtime requirements: module */
/***/ ((module) => {
module.exports = 'react-dom';
/***/ }),
/* 3 */
/*!************************************!*\
!*** ./node_modules/prop-types.js ***!
\************************************/
/*! unknown exports (runtime-defined) */
/*! export default [maybe provided (runtime-defined)] [used] [usage and provision prevents renaming] */
/*! other exports [maybe provided (runtime-defined)] [maybe used (runtime-defined)] */
/*! runtime requirements: module */
/***/ ((module) => {
module.exports = 'react';
/***/ })
/******/ ]);
```
<details><summary><code>/* webpack runtime code */</code></summary>
``` js
/************************************************************************/
/******/ // The module cache
/******/ 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] = {
/******/ // 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;
/******/ }
/******/
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__.m = __webpack_modules__;
/******/
/************************************************************************/
/******/ /* webpack/runtime/compat get default export */
/******/ (() => {
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = (module) => {
/******/ var getter = module && module.__esModule ?
/******/ () => module['default'] :
/******/ () => module;
/******/ __webpack_require__.d(getter, { a: getter });
/******/ return getter;
/******/ };
/******/ })();
/******/
/******/ /* webpack/runtime/define property getters */
/******/ (() => {
/******/ // define getter functions for harmony exports
/******/ __webpack_require__.d = (exports, definition) => {
/******/ for(var key in definition) {
/******/ if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
/******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
/******/ }
/******/ }
/******/ };
/******/ })();
/******/
/******/ /* webpack/runtime/hasOwnProperty shorthand */
/******/ (() => {
/******/ __webpack_require__.o = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop)
/******/ })();
/******/
/******/ /* webpack/runtime/jsonp chunk loading */
/******/ (() => {
/******/ // object to store loaded and loading chunks
/******/ // undefined = chunk not loaded, null = chunk preloaded/prefetched
/******/ // Promise = chunk loading, 0 = chunk loaded
/******/ var installedChunks = {
/******/ 703: 0
/******/ };
/******/
/******/ var deferredModules = [
/******/ [1],
/******/ [2],
/******/ [3]
/******/ ];
/******/ // no chunk on demand loading
/******/
/******/ // no prefetching
/******/
/******/ // no preloaded
/******/
/******/ // no HMR
/******/
/******/ // no HMR manifest
/******/
/******/ var checkDeferredModules = () => {
/******/
/******/ };
/******/ function checkDeferredModulesImpl() {
/******/ var result;
/******/ for(var i = 0; i < deferredModules.length; i++) {
/******/ var deferredModule = deferredModules[i];
/******/ var fulfilled = true;
/******/ for(var j = 1; j < deferredModule.length; j++) {
/******/ var depId = deferredModule[j];
/******/ if(installedChunks[depId] !== 0) fulfilled = false;
/******/ }
/******/ if(fulfilled) {
/******/ deferredModules.splice(i--, 1);
/******/ result = __webpack_require__(__webpack_require__.s = deferredModule[0]);
/******/ }
/******/ }
/******/ if(deferredModules.length === 0) {
/******/ __webpack_require__.x();
/******/ __webpack_require__.x = () => {
/******/
/******/ }
/******/ }
/******/ return result;
/******/ }
/******/ __webpack_require__.x = () => {
/******/ // reset startup function so it can be called again when more startup code is added
/******/ __webpack_require__.x = () => {
/******/
/******/ }
/******/ jsonpArray = jsonpArray.slice();
/******/ for(var i = 0; i < jsonpArray.length; i++) webpackJsonpCallback(jsonpArray[i]);
/******/ return (checkDeferredModules = checkDeferredModulesImpl)();
/******/ };
/******/
/******/ // install a JSONP callback for chunk loading
/******/ function webpackJsonpCallback(data) {
/******/ var chunkIds = data[0];
/******/ var moreModules = data[1];
/******/ var executeModules = data[2];
/******/ var runtime = data[3];
/******/ // add "moreModules" to the modules object,
/******/ // then flag all "chunkIds" as loaded and fire callback
/******/ var moduleId, chunkId, i = 0, resolves = [];
/******/ for(;i < chunkIds.length; i++) {
/******/ chunkId = chunkIds[i];
/******/ if(__webpack_require__.o(installedChunks, chunkId) && installedChunks[chunkId]) {
/******/ resolves.push(installedChunks[chunkId][0]);
/******/ }
/******/ installedChunks[chunkId] = 0;
/******/ }
/******/ for(moduleId in moreModules) {
/******/ if(__webpack_require__.o(moreModules, moduleId)) {
/******/ __webpack_require__.m[moduleId] = moreModules[moduleId];
/******/ }
/******/ }
/******/ if(runtime) runtime(__webpack_require__);
/******/ if(parentJsonpFunction) parentJsonpFunction(data);
/******/ while(resolves.length) {
/******/ resolves.shift()();
/******/ }
/******/
/******/ // add entry modules from loaded chunk to deferred list
/******/ if(executeModules) deferredModules.push.apply(deferredModules, executeModules);
/******/
/******/ // run deferred modules when all chunks ready
/******/ return checkDeferredModules();
/******/ };
/******/
/******/ var jsonpArray = window["webpackJsonp"] = window["webpackJsonp"] || [];
/******/ var oldJsonpFunction = jsonpArray.push.bind(jsonpArray);
/******/ jsonpArray.push = webpackJsonpCallback;
/******/ var parentJsonpFunction = oldJsonpFunction;
/******/ })();
/******/
/************************************************************************/
```
</details>
``` js
/******/ // run startup
/******/ return __webpack_require__.x();
/******/ })()
;
```
# Info
## Unoptimized
```
Hash: 0a1b2c3d4e5f6a7b8c9d
Version: webpack 5.0.0-beta.13
Asset Size
app.js 1.34 KiB [emitted] [name: app]
react-vendors.js 7.44 KiB [emitted] [name: react-vendors]
Entrypoint app = app.js react-vendors.js
Entrypoint react-vendors = react-vendors.js
chunk app.js (app) 139 bytes [initial] [rendered]
> ./app.js app
./app.js 139 bytes [built]
[no exports]
[no exports used]
entry ./app.js app
chunk react-vendors.js (react-vendors) 82 bytes (javascript) 3.16 KiB (runtime) [entry] [rendered]
> ./app.js app
> prop-types react-vendors
> react react-vendors
> react-dom react-vendors
./node_modules/prop-types.js 26 bytes [built]
harmony side effect evaluation prop-types ./app.js 3:0-35
harmony import specifier prop-types ./app.js 5:29-38
cjs self exports reference ./node_modules/prop-types.js 1:0-14
entry prop-types react-vendors
./node_modules/react-dom.js 30 bytes [built]
harmony side effect evaluation react-dom ./app.js 2:0-33
harmony import specifier react-dom ./app.js 5:19-27
cjs self exports reference ./node_modules/react-dom.js 1:0-14
entry react-dom react-vendors
./node_modules/react.js 26 bytes [built]
harmony side effect evaluation react ./app.js 1:0-26
harmony import specifier react ./app.js 5:12-17
cjs self exports reference ./node_modules/react.js 1:0-14
entry react react-vendors
+ 4 hidden chunk modules
```
## Production mode
```
Hash: 0a1b2c3d4e5f6a7b8c9d
Version: webpack 5.0.0-beta.13
Asset Size
app.js 183 bytes [emitted] [name: app]
react-vendors.js 1.06 KiB [emitted] [name: react-vendors]
Entrypoint app = app.js react-vendors.js
Entrypoint react-vendors = react-vendors.js
chunk app.js (app) 139 bytes [initial] [rendered]
> ./app.js app
./app.js 139 bytes [built]
[no exports]
[no exports used]
entry ./app.js app
chunk react-vendors.js (react-vendors) 82 bytes (javascript) 3.17 KiB (runtime) [entry] [rendered]
> ./app.js app
> prop-types react-vendors
> react react-vendors
> react-dom react-vendors
./node_modules/prop-types.js 26 bytes [built]
harmony side effect evaluation prop-types ./app.js 3:0-35
harmony import specifier prop-types ./app.js 5:29-38
cjs self exports reference ./node_modules/prop-types.js 1:0-14
entry prop-types react-vendors
./node_modules/react-dom.js 30 bytes [built]
harmony side effect evaluation react-dom ./app.js 2:0-33
harmony import specifier react-dom ./app.js 5:19-27
cjs self exports reference ./node_modules/react-dom.js 1:0-14
entry react-dom react-vendors
./node_modules/react.js 26 bytes [built]
harmony side effect evaluation react ./app.js 1:0-26
harmony import specifier react ./app.js 5:12-17
cjs self exports reference ./node_modules/react.js 1:0-14
entry react react-vendors
+ 4 hidden chunk modules
```

View File

@ -0,0 +1,5 @@
import react from "react";
import reactDOM from "react-dom";
import propTypes from "prop-types";
console.log(react, reactDOM, propTypes);

View File

@ -0,0 +1,2 @@
global.NO_TARGET_ARGS = true;
require("../build-common");

View File

@ -0,0 +1 @@
module.exports = 'react';

View File

@ -0,0 +1 @@
module.exports = 'react-dom';

View File

@ -0,0 +1 @@
module.exports = 'react';

View File

@ -0,0 +1,3 @@
export { default as react } from "react";
export { default as reactDOM } from "react-dom";
export { default as propTypes } from "prop-types";

View File

@ -0,0 +1,45 @@
This example shows how to use Code Splitting with entrypoint dependOn
# webpack.config.js
```javascript
_{{webpack.config.js}}_
```
# app.js
```javascript
_{{app.js}}_
```
# react-vendors.js
```javascript
_{{react-vendors.js}}_
```
# dist/app.js
```javascript
_{{dist/app.js}}_
```
# dist/react-vendors.js
```javascript
_{{dist/react-vendors.js}}_
```
# Info
## Unoptimized
```
_{{stdout}}_
```
## Production mode
```
_{{production:stdout}}_
```

View File

@ -0,0 +1,9 @@
module.exports = {
entry: {
app: { import: "./app.js", dependOn: ["react-vendors"] },
"react-vendors": ["react", "react-dom", "prop-types"]
},
optimization: {
chunkIds: "deterministic" // To keep filename consistent between different modes (for example building only)
}
};

View File

@ -142,6 +142,7 @@ const { arrayToSetDeprecation } = require("./util/deprecation");
/** /**
* @typedef {Object} EntryOptions * @typedef {Object} EntryOptions
* @property {string} name name of the entrypoint * @property {string} name name of the entrypoint
* @property {string[]=} dependOn list of other entries on which this entry depends
* @property {string=} filename filename of entry chunk * @property {string=} filename filename of entry chunk
*/ */

View File

@ -59,6 +59,11 @@ class EntryOptionPlugin {
* @returns {void} * @returns {void}
*/ */
const applyEntryDescriptionPlugins = (entry, options) => { const applyEntryDescriptionPlugins = (entry, options) => {
if (entry.dependOn) {
options.dependOn = Array.isArray(entry.dependOn)
? Array.from(entry.dependOn)
: [entry.dependOn];
}
applyEntryItemPlugins(entry.import, { applyEntryItemPlugins(entry.import, {
...options, ...options,
filename: entry.filename filename: entry.filename

View File

@ -6,7 +6,10 @@
"use strict"; "use strict";
const AsyncDependencyToInitialChunkError = require("./AsyncDependencyToInitialChunkError"); const AsyncDependencyToInitialChunkError = require("./AsyncDependencyToInitialChunkError");
const { connectChunkGroupParentAndChild } = require("./GraphHelpers"); const {
connectChunkGroupParentAndChild,
connectChunkGroupAndChunk
} = require("./GraphHelpers");
/** @typedef {import("./AsyncDependenciesBlock")} AsyncDependenciesBlock */ /** @typedef {import("./AsyncDependenciesBlock")} AsyncDependenciesBlock */
/** @typedef {import("./Chunk")} Chunk */ /** @typedef {import("./Chunk")} Chunk */
@ -67,7 +70,7 @@ const bySetSize = (a, b) => {
* @param {Compilation} compilation the compilation * @param {Compilation} compilation the compilation
* @returns {Map<DependenciesBlock, Iterable<Module>>} the mapping block to modules * @returns {Map<DependenciesBlock, Iterable<Module>>} the mapping block to modules
*/ */
const extraceBlockModulesMap = compilation => { const extractBlockModulesMap = compilation => {
const { moduleGraph } = compilation; const { moduleGraph } = compilation;
/** @type {Map<DependenciesBlock, Iterable<Module>>} */ /** @type {Map<DependenciesBlock, Iterable<Module>>} */
@ -150,7 +153,7 @@ const visitModules = (
const { moduleGraph, chunkGraph } = compilation; const { moduleGraph, chunkGraph } = compilation;
logger.time("visitModules: prepare"); logger.time("visitModules: prepare");
const blockModulesMap = extraceBlockModulesMap(compilation); const blockModulesMap = extractBlockModulesMap(compilation);
let nextChunkGroupIndex = 0; let nextChunkGroupIndex = 0;
let nextFreeModulePreOrderIndex = 0; let nextFreeModulePreOrderIndex = 0;
@ -668,6 +671,67 @@ const visitModules = (
} }
}; };
/**
*
* @param {Compilation} compilation compilation
*/
const handleDependOn = compilation => {
for (const [
name,
{
options: { dependOn = [] }
}
] of compilation.entries) {
/** @type {Map<Module, Set<Chunk>>} */
const modulesToDeleteMap = new Map();
const child = compilation.entrypoints.get(name);
if (dependOn.length === 1) {
const parent = compilation.entrypoints.get(dependOn[0]);
child.setRuntimeChunk(parent.runtimeChunk);
}
for (const dependName of dependOn) {
const parent = compilation.entrypoints.get(dependName);
connectChunkGroupParentAndChild(parent, child);
for (const chunk of parent.chunks) {
const modules = compilation.chunkGraph.getChunkModules(chunk);
for (const module of modules) {
let moduleChunks = modulesToDeleteMap.get(module);
if (!moduleChunks) {
modulesToDeleteMap.set(module, (moduleChunks = new Set()));
}
moduleChunks.add(chunk);
}
}
}
if (dependOn.length) {
const chunksToPush = new Set();
for (const chunk of child.chunks) {
for (const [module, parentChunks] of modulesToDeleteMap) {
if (compilation.chunkGraph.isModuleInChunk(module, chunk)) {
// get the first chunk with this module
const parentChunk = parentChunks.values().next().value;
compilation.chunkGraph.disconnectChunkAndModule(chunk, module);
chunksToPush.add(parentChunk);
}
}
}
// push to a child group only these chunks
// that includes the modules, that child entrypoint are requiring
for (const chunk of chunksToPush) {
connectChunkGroupAndChunk(child, chunk);
}
}
}
};
/** /**
* *
* @param {Compilation} compilation the compilation * @param {Compilation} compilation the compilation
@ -816,6 +880,12 @@ const buildChunkGraph = (compilation, inputChunkGroups) => {
); );
logger.timeEnd("connectChunkGroups"); logger.timeEnd("connectChunkGroups");
// PART THREE
logger.time("handleDependOn");
handleDependOn(compilation);
logger.timeEnd("handleDependOn");
// Cleaup work // Cleaup work
logger.time("cleanup"); logger.time("cleanup");

View File

@ -43,6 +43,24 @@
"type": "object", "type": "object",
"additionalProperties": false, "additionalProperties": false,
"properties": { "properties": {
"dependOn": {
"description": "The entrypoints that the current entrypoint depend on. They must be loaded when this entrypoint is loaded.",
"oneOf": [
{
"description": "The name of another entrypoint.",
"type": "string",
"minLength": 1
},
{
"description": "A list of other entrypoints.",
"anyOf": [
{
"$ref": "#/definitions/NonEmptyArrayOfUniqueStringValues"
}
]
}
]
},
"filename": { "filename": {
"description": "The filename of the entrypoint as relative path inside the `output.path` directory.", "description": "The filename of the entrypoint as relative path inside the `output.path` directory.",
"type": "string", "type": "string",

View File

@ -2498,6 +2498,7 @@ LOG from webpack.buildChunkGraph
<t> visitModules: visiting: Xms <t> visitModules: visiting: Xms
<t> visitModules: Xms <t> visitModules: Xms
<t> connectChunkGroups: Xms <t> connectChunkGroups: Xms
<t> handleDependOn: Xms
<t> cleanup: Xms <t> cleanup: Xms
LOG from webpack.SplitChunksPlugin LOG from webpack.SplitChunksPlugin

View File

@ -0,0 +1,2 @@
import isomorphicFetch from "isomorphic-fetch";
import lodash from "lodash";

View File

@ -0,0 +1,7 @@
import lodash from "lodash";
import propTypes from "prop-types";
export default function() {
expect(lodash).toBe("lodash");
expect(propTypes).toBe("prop-types");
}

View File

@ -0,0 +1 @@
module.exports = "isomorphic-fetch";

View File

@ -0,0 +1 @@
module.exports = 'lodash';

View File

@ -0,0 +1 @@
module.exports = 'prop-types';

View File

@ -0,0 +1 @@
module.exports = 'react-dom';

View File

@ -0,0 +1 @@
module.exports = 'react';

View File

@ -0,0 +1,2 @@
export let value = "fail";
export function set(v) { value = v; }

View File

@ -0,0 +1,5 @@
import lodash from "lodash";
import isomorphicFetch from "isomorphic-fetch";
import { set } from "test";
set("ok");

View File

@ -0,0 +1,26 @@
import isomorphicFetch from "isomorphic-fetch";
import react from "react";
import reactDOM from "react-dom";
it("should be able to load the modules", () => {
expect(isomorphicFetch).toBe("isomorphic-fetch");
expect(react).toBe("react");
expect(reactDOM).toBe("react-dom");
});
it("should have the correct modules in a lazy chunk", () => {
const promise = import(/* webpackChunkName: "lazy" */ "./lazy").then(
module => {
module.default();
}
);
__non_webpack_require__("./lazy.js");
document.head._children[0].onload();
return promise;
});
import { value } from "test";
it("other-vendors should run too", () => {
expect(value).toBe("ok");
});

View File

@ -0,0 +1,11 @@
module.exports = {
findBundle: function() {
return [
"./app.js",
"./runtime.js",
"./page1.js",
"./react-vendors.js",
"./other-vendors.js"
];
}
};

View File

@ -0,0 +1,51 @@
module.exports = {
entry() {
return Promise.resolve({
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"
});
},
target: "web",
optimization: {
runtimeChunk: "single"
},
output: {
filename: "[name].js"
},
plugins: [
function() {
/**
* @param {Compilation} compilation compilation
* @returns {void}
*/
const handler = compilation => {
compilation.hooks.afterSeal.tap("testcase", () => {
const { chunkGraph } = compilation;
const chunkModules = {};
for (const chunk of compilation.chunks) {
chunkModules[chunk.name] = new Set();
for (const module of chunkGraph.getChunkModulesIterable(chunk)) {
chunkModules[chunk.name].add(module);
}
}
expect([...chunkModules.app]).toStrictEqual(
expect.not.arrayContaining([...chunkModules["other-vendors"]])
);
expect([...chunkModules.page1]).toStrictEqual(
expect.not.arrayContaining([
...chunkModules["other-vendors"],
...chunkModules["react-vendors"],
...chunkModules["app"]
])
);
});
};
this.hooks.compilation.tap("testcase", handler);
}
]
};

View File

@ -0,0 +1,9 @@
import react from "react";
import reactDOM from "react-dom";
import propTypes from "prop-types";
it("should load modules correctly", () => {
expect(react).toBe("react");
expect(reactDOM).toBe("react-dom");
expect(propTypes).toBe("prop-types");
});

View File

@ -0,0 +1 @@
module.exports = 'prop-types';

View File

@ -0,0 +1 @@
module.exports = 'react-dom';

View File

@ -0,0 +1 @@
module.exports = 'react';

View File

@ -0,0 +1,5 @@
module.exports = {
findBundle: function() {
return ["./app.js", "./react-vendors.js"];
}
};

View File

@ -0,0 +1,36 @@
module.exports = {
entry: {
app: { import: "./app.js", dependOn: "react-vendors" },
"react-vendors": ["react", "react-dom", "prop-types"]
},
target: "web",
output: {
filename: "[name].js"
},
plugins: [
function() {
/**
* @param {Compilation} compilation compilation
* @returns {void}
*/
const handler = compilation => {
compilation.hooks.afterSeal.tap("testcase", () => {
const { chunkGraph } = compilation;
const chunkModules = {};
for (const chunk of compilation.chunks) {
chunkModules[chunk.name] = new Set();
for (const module of chunkGraph.getChunkModulesIterable(chunk)) {
chunkModules[chunk.name].add(module);
}
}
expect([...chunkModules.app]).toStrictEqual(
expect.not.arrayContaining([...chunkModules["react-vendors"]])
);
});
};
this.hooks.compilation.tap("testcase", handler);
}
]
};