原文連接:https://github.com/webpack/we...
譯者:@justjavacjavascript
這個示例演示了與代碼拆分相結合的 Scope Hoisting。前端
這是示例的依賴圖:(實線表示同步導入,虛線表示異步導入)java
除 cjs
以外的全部模塊都是 EcmaScript 模塊。cjs
是 CommonJs 模塊。node
有趣的是,將全部模塊放在單一範圍(scope)內將沒法正常工做,緣由以下:webpack
lazy
, c
, d
和 cjs
須要在一個單獨的塊(chunk)shared
能夠被兩個 chunk 訪問(不一樣的範圍)cjs
是一個 CommonJs 模塊
所以,Webpack 使用一種稱爲「局部範圍提高(Partial Scope Hoisting)」或「模塊級聯」的方法,該方法選擇能夠能夠覆蓋 Scope Hoisting 範圍的 ES 模塊的最大子集,並將其與默認的 webpack 原語(primitives)相結合。git
爲了不衝突,模塊中的模塊鏈接標識符被重命名,並簡化了內部導入。根模塊的外部導入和導出使用現有的 ESM 結構。github
import { a, x, y } from "a"; import * as b from "b"; import("./lazy").then(function(lazy) { console.log(a, b.a(), x, y, lazy.c, lazy.d.a, lazy.x, lazy.y); });
export * from "c"; import * as d from "d"; export { d };
// module a export var a = "a"; export * from "shared";
// module b export function a() { return "b"; };
// module c import { c as e } from "cjs"; export var c = String.fromCharCode(e.charCodeAt(0) - 2); export { x, y } from "shared";
// module d export var a = "d";
// module cjs (commonjs) exports.c = "e";
// shared module export var x = "x"; export * from "shared2";
// shared2 module export var y = "y";
var webpack = require("../../"); module.exports = { plugins: [ new webpack.optimize.ModuleConcatenationPlugin() ] };
/******/ (function(modules) { /* webpackBootstrap */ }) ......
/******/ ([ /* 0 */ /*!********************************************!*\ !*** ./node_modules/shared.js + 1 modules ***! \********************************************/ /*! exports provided: x, y */ /*! exports used: x, y */ /***/ (function(module, __webpack_exports__, __webpack_require__) { "use strict"; Object.defineProperty(__webpack_exports__, "__esModule", { value: true }); // CONCATENATED MODULE: ./node_modules/shared2.js // shared2 module var y = "y"; // CONCATENATED MODULE: ./node_modules/shared.js /* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "a", function() { return x; }); /* concated harmony reexport */__webpack_require__.d(__webpack_exports__, "b", function() { return y; }); // shared module var x = "x"; /***/ }), /* 1 */ /*!********************************!*\ !*** ./example.js + 2 modules ***! \********************************/ /*! exports provided: */ /*! all exports used */ /*! ModuleConcatenation bailout: Module is an entry point */ /***/ (function(module, __webpack_exports__, __webpack_require__) { "use strict"; Object.defineProperty(__webpack_exports__, "__esModule", { value: true }); // EXTERNAL MODULE: ./node_modules/shared.js + 1 modules var shared = __webpack_require__(0); // CONCATENATED MODULE: ./node_modules/a.js // module a var a = "a"; // CONCATENATED MODULE: ./node_modules/b.js // module b function b_a() { return "b"; }; // CONCATENATED MODULE: ./example.js __webpack_require__.e/* import() */(0).then(__webpack_require__.bind(null, /*! ./lazy */ 3)).then(function(lazy) { console.log(a, b_a(), shared["a" /* x */], shared["b" /* y */], lazy.c, lazy.d.a, lazy.x, lazy.y); }); /***/ }) /******/ ]);
webpackJsonp([0],[ /* 0 */, /* 1 */, /* 2 */ /*!*****************************!*\ !*** ./node_modules/cjs.js ***! \*****************************/ /*! no static exports found */ /*! exports used: c */ /*! ModuleConcatenation bailout: Module is not an ECMAScript module */ /***/ (function(module, exports) { // module cjs (commonjs) exports.c = "e"; /***/ }), /* 3 */ /*!*****************************!*\ !*** ./lazy.js + 2 modules ***! \*****************************/ /*! exports provided: d, c, x, y */ /*! all exports used */ /*! ModuleConcatenation bailout: Module is referenced from these modules with unsupported syntax: ./example.js (referenced with import()) */ /***/ (function(module, __webpack_exports__, __webpack_require__) { "use strict"; Object.defineProperty(__webpack_exports__, "__esModule", { value: true }); var d_namespaceObject = {}; __webpack_require__.d(d_namespaceObject, "a", function() { return a; }); // EXTERNAL MODULE: ./node_modules/cjs.js var cjs = __webpack_require__(2); var cjs_default = /*#__PURE__*/__webpack_require__.n(cjs); // EXTERNAL MODULE: ./node_modules/shared.js + 1 modules var shared = __webpack_require__(0); // CONCATENATED MODULE: ./node_modules/c.js // module c var c = String.fromCharCode(cjs["c"].charCodeAt(0) - 2); // CONCATENATED MODULE: ./node_modules/d.js // module d var a = "d"; // CONCATENATED MODULE: ./lazy.js /* concated harmony reexport */__webpack_require__.d(__webpack_exports__, "c", function() { return c; }); /* concated harmony reexport */__webpack_require__.d(__webpack_exports__, "x", function() { return shared["a" /* x */]; }); /* concated harmony reexport */__webpack_require__.d(__webpack_exports__, "y", function() { return shared["b" /* y */]; }); /* concated harmony reexport */__webpack_require__.d(__webpack_exports__, "d", function() { return d_namespaceObject; }); /***/ }) ]);
Minimizedweb
webpackJsonp([0],[,,function(n,r){r.c="e"},function(n,r,t){"use strict";Object.defineProperty(r,"__esModule",{value:!0});var e={};t.d(e,"a",function(){return d});var u=t(2),c=t(0),o=String.fromCharCode(u.c.charCodeAt(0)-2),d="d";t.d(r,"c",function(){return o}),t.d(r,"x",function(){return c.a}),t.d(r,"y",function(){return c.b}),t.d(r,"d",function(){return e})}]);
Hash: 6596ce0a50ccbbaa89c6 Version: webpack 3.5.1 Asset Size Chunks Chunk Names 0.output.js 1.9 kB 0 [emitted] output.js 7.39 kB 1 [emitted] main Entrypoint main = output.js chunk {0} 0.output.js 286 bytes {1} [rendered] > [] 4:0-16 [3] ./lazy.js + 2 modules 242 bytes {0} [built] [exports: d, c, x, y] import() ./lazy [] ./example.js 4:0-16 + 1 hidden module chunk {1} output.js (main) 390 bytes [entry] [rendered] > main [] [0] ./node_modules/shared.js + 1 modules 105 bytes {1} [built] [exports: x, y] [only some exports used: x, y] harmony import shared [1] ./example.js + 2 modules 3:0-23 harmony import shared [3] ./lazy.js + 2 modules 6:0-30 [1] ./example.js + 2 modules 285 bytes {1} [built] [no exports]
Hash: 6596ce0a50ccbbaa89c6 Version: webpack 3.5.1 Asset Size Chunks Chunk Names 0.output.js 364 bytes 0 [emitted] output.js 1.66 kB 1 [emitted] main Entrypoint main = output.js chunk {0} 0.output.js 286 bytes {1} [rendered] > [] 4:0-16 [3] ./lazy.js + 2 modules 242 bytes {0} [built] [exports: d, c, x, y] import() ./lazy [] ./example.js 4:0-16 + 1 hidden module chunk {1} output.js (main) 390 bytes [entry] [rendered] > main [] [0] ./node_modules/shared.js + 1 modules 105 bytes {1} [built] [exports: x, y] [only some exports used: x, y] harmony import shared [1] ./example.js + 2 modules 3:0-23 harmony import shared [3] ./lazy.js + 2 modules 6:0-30 [1] ./example.js + 2 modules 285 bytes {1} [built] [no exports]
歡迎關注個人公衆號,關注前端文章:微信