awesome-interview/assets/js/0ba2ede9.56884b73.js

1 line
22 KiB
JavaScript

"use strict";(self.webpackChunkjjbook=self.webpackChunkjjbook||[]).push([[5722],{3905:(e,t,n)=>{n.d(t,{Zo:()=>m,kt:()=>d});var a=n(67294);function l(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function o(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var a=Object.getOwnPropertySymbols(e);t&&(a=a.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,a)}return n}function r(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{};t%2?o(Object(n),!0).forEach((function(t){l(e,t,n[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(n)):o(Object(n)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(n,t))}))}return e}function i(e,t){if(null==e)return{};var n,a,l=function(e,t){if(null==e)return{};var n,a,l={},o=Object.keys(e);for(a=0;a<o.length;a++)n=o[a],t.indexOf(n)>=0||(l[n]=e[n]);return l}(e,t);if(Object.getOwnPropertySymbols){var o=Object.getOwnPropertySymbols(e);for(a=0;a<o.length;a++)n=o[a],t.indexOf(n)>=0||Object.prototype.propertyIsEnumerable.call(e,n)&&(l[n]=e[n])}return l}var p=a.createContext({}),c=function(e){var t=a.useContext(p),n=t;return e&&(n="function"==typeof e?e(t):r(r({},t),e)),n},m=function(e){var t=c(e.components);return a.createElement(p.Provider,{value:t},e.children)},k="mdxType",s={inlineCode:"code",wrapper:function(e){var t=e.children;return a.createElement(a.Fragment,{},t)}},u=a.forwardRef((function(e,t){var n=e.components,l=e.mdxType,o=e.originalType,p=e.parentName,m=i(e,["components","mdxType","originalType","parentName"]),k=c(n),u=l,d=k["".concat(p,".").concat(u)]||k[u]||s[u]||o;return n?a.createElement(d,r(r({ref:t},m),{},{components:n})):a.createElement(d,r({ref:t},m))}));function d(e,t){var n=arguments,l=t&&t.mdxType;if("string"==typeof e||l){var o=n.length,r=new Array(o);r[0]=u;var i={};for(var p in t)hasOwnProperty.call(t,p)&&(i[p]=t[p]);i.originalType=e,i[k]="string"==typeof e?e:l,r[1]=i;for(var c=2;c<o;c++)r[c]=n[c];return a.createElement.apply(null,r)}return a.createElement.apply(null,n)}u.displayName="MDXCreateElement"},72681:(e,t,n)=>{n.r(t),n.d(t,{assets:()=>p,contentTitle:()=>r,default:()=>s,frontMatter:()=>o,metadata:()=>i,toc:()=>c});var a=n(87462),l=(n(67294),n(3905));const o={sidebar_label:"\u5de5\u7a0b\u5316\uff1awebpack \u5de5\u4f5c\u6d41\u7a0b",sidebar_position:3},r="webpack \u5de5\u4f5c\u6d41\u7a0b",i={unversionedId:"book1/engineer-webpack-workflow",id:"book1/engineer-webpack-workflow",title:"webpack \u5de5\u4f5c\u6d41\u7a0b",description:"\u672c\u7bc7\u5185\u5bb9\u548c\u914d\u7f6e\u57fa\u4e8e webpack v5.52.1 \u8bb2\u89e3",source:"@site/docs/book1/engineer-webpack-workflow.md",sourceDirName:"book1",slug:"/book1/engineer-webpack-workflow",permalink:"/awesome-interview/book1/engineer-webpack-workflow",draft:!1,tags:[],version:"current",sidebarPosition:3,frontMatter:{sidebar_label:"\u5de5\u7a0b\u5316\uff1awebpack \u5de5\u4f5c\u6d41\u7a0b",sidebar_position:3},sidebar:"tutorialSidebar",previous:{title:"\u6d4f\u89c8\u5668\uff1a\u6d4f\u89c8\u5668\u7684\u91cd\u6392\u91cd\u7ed8",permalink:"/awesome-interview/book1/browser-repain-reflow"},next:{title:"\u6846\u67b6\uff1aVue \u7684\u6570\u636e\u7ed1\u5b9a\u673a\u5236",permalink:"/awesome-interview/book1/frame-vue-data-binding"}},p={},c=[{value:"\u76f8\u5173\u95ee\u9898",id:"\u76f8\u5173\u95ee\u9898",level:2},{value:"\u56de\u7b54\u5173\u952e\u70b9",id:"\u56de\u7b54\u5173\u952e\u70b9",level:2},{value:"\u77e5\u8bc6\u70b9\u6df1\u5165",id:"\u77e5\u8bc6\u70b9\u6df1\u5165",level:2},{value:"1. webpack \u521d\u59cb\u5316\u8fc7\u7a0b",id:"1-webpack-\u521d\u59cb\u5316\u8fc7\u7a0b",level:3},{value:"2. webpack \u6784\u5efa\u9636\u6bb5\u505a\u4e86\u4ec0\u4e48",id:"2-webpack-\u6784\u5efa\u9636\u6bb5\u505a\u4e86\u4ec0\u4e48",level:3},{value:"3. webpack \u751f\u6210\u9636\u6bb5\u505a\u4e86\u4ec0\u4e48",id:"3-webpack-\u751f\u6210\u9636\u6bb5\u505a\u4e86\u4ec0\u4e48",level:3},{value:"3.1 \u751f\u6210\u4ea7\u7269",id:"31-\u751f\u6210\u4ea7\u7269",level:4},{value:"3.2 \u6587\u4ef6\u8f93\u51fa",id:"32-\u6587\u4ef6\u8f93\u51fa",level:4},{value:"\u53c2\u8003\u8d44\u6599",id:"\u53c2\u8003\u8d44\u6599",level:2}],m={toc:c},k="wrapper";function s(e){let{components:t,...n}=e;return(0,l.kt)(k,(0,a.Z)({},m,n,{components:t,mdxType:"MDXLayout"}),(0,l.kt)("h1",{id:"webpack-\u5de5\u4f5c\u6d41\u7a0b"},"webpack \u5de5\u4f5c\u6d41\u7a0b"),(0,l.kt)("blockquote",null,(0,l.kt)("p",{parentName:"blockquote"},"\u672c\u7bc7\u5185\u5bb9\u548c\u914d\u7f6e\u57fa\u4e8e webpack v5.52.1 \u8bb2\u89e3")),(0,l.kt)("h2",{id:"\u76f8\u5173\u95ee\u9898"},"\u76f8\u5173\u95ee\u9898"),(0,l.kt)("ul",null,(0,l.kt)("li",{parentName:"ul"},"webpack \u5de5\u4f5c\u6d41\u7a0b\u662f\u600e\u6837\u7684"),(0,l.kt)("li",{parentName:"ul"},"webpack \u5728\u4e0d\u540c\u9636\u6bb5\u505a\u4e86\u4ec0\u4e48\u4e8b\u60c5")),(0,l.kt)("h2",{id:"\u56de\u7b54\u5173\u952e\u70b9"},"\u56de\u7b54\u5173\u952e\u70b9"),(0,l.kt)("p",null,(0,l.kt)("inlineCode",{parentName:"p"},"\u6a21\u5757\u5316")," ",(0,l.kt)("inlineCode",{parentName:"p"},"\u6253\u5305")," ",(0,l.kt)("inlineCode",{parentName:"p"},"\u4f9d\u8d56\u751f\u6210")," ",(0,l.kt)("inlineCode",{parentName:"p"},"\u5de5\u7a0b\u5316")),(0,l.kt)("p",null,"webpack \u662f\u4e00\u79cd\u6a21\u5757\u6253\u5305\u5de5\u5177\uff0c\u53ef\u4ee5\u5c06\u5404\u7c7b\u578b\u7684\u8d44\u6e90\uff0c\u4f8b\u5982\u56fe\u7247\u3001CSS\u3001JS \u7b49\uff0c\u8f6c\u8bd1\u7ec4\u5408\u4e3a JS \u683c\u5f0f\u7684 bundle \u6587\u4ef6\u3002"),(0,l.kt)("p",null,(0,l.kt)("img",{parentName:"p",src:"https://user-images.githubusercontent.com/15681693/132979267-8ec79dc5-5249-4d2c-9843-7efa909d7320.png",alt:"webpack"})),(0,l.kt)("blockquote",null,(0,l.kt)("p",{parentName:"blockquote"},"\u56fe\u7247\u6765\u6e90 ",(0,l.kt)("a",{parentName:"p",href:"https://webpack.js.org/"},"webpack \u5b98\u7f51"))),(0,l.kt)("p",null,"webpack \u6784\u5efa\u7684\u6838\u5fc3\u4efb\u52a1\u662f\u5b8c\u6210\u5185\u5bb9\u8f6c\u5316\u548c\u8d44\u6e90\u5408\u5e76\u3002\u4e3b\u8981\u5305\u542b\u4ee5\u4e0b 3 \u4e2a\u9636\u6bb5\uff1a"),(0,l.kt)("ol",null,(0,l.kt)("li",{parentName:"ol"},"\u521d\u59cb\u5316\u9636\u6bb5",(0,l.kt)("ul",{parentName:"li"},(0,l.kt)("li",{parentName:"ul"},(0,l.kt)("strong",{parentName:"li"},"\u521d\u59cb\u5316\u53c2\u6570"),"\uff1a\u4ece\u914d\u7f6e\u6587\u4ef6\u3001\u914d\u7f6e\u5bf9\u8c61\u548c Shell \u53c2\u6570\u4e2d\u8bfb\u53d6\u5e76\u4e0e\u9ed8\u8ba4\u53c2\u6570\u8fdb\u884c\u5408\u5e76\uff0c\u7ec4\u5408\u6210\u6700\u7ec8\u4f7f\u7528\u7684\u53c2\u6570\u3002"),(0,l.kt)("li",{parentName:"ul"},(0,l.kt)("strong",{parentName:"li"},"\u521b\u5efa\u7f16\u8bd1\u5bf9\u8c61"),"\uff1a\u7528\u4e0a\u4e00\u6b65\u5f97\u5230\u7684\u53c2\u6570\u521b\u5efa Compiler \u5bf9\u8c61\u3002"),(0,l.kt)("li",{parentName:"ul"},(0,l.kt)("strong",{parentName:"li"},"\u521d\u59cb\u5316\u7f16\u8bd1\u73af\u5883"),"\uff1a\u5305\u62ec\u6ce8\u5165\u5185\u7f6e\u63d2\u4ef6\u3001\u6ce8\u518c\u5404\u79cd\u6a21\u5757\u5de5\u5382\u3001\u521d\u59cb\u5316 RuleSet \u96c6\u5408\u3001\u52a0\u8f7d\u914d\u7f6e\u7684\u63d2\u4ef6\u7b49\u3002"))),(0,l.kt)("li",{parentName:"ol"},"\u6784\u5efa\u9636\u6bb5",(0,l.kt)("ul",{parentName:"li"},(0,l.kt)("li",{parentName:"ul"},(0,l.kt)("strong",{parentName:"li"},"\u5f00\u59cb\u7f16\u8bd1"),"\uff1a\u6267\u884c Compiler \u5bf9\u8c61\u7684 run \u65b9\u6cd5\uff0c\u521b\u5efa Compilation \u5bf9\u8c61\u3002"),(0,l.kt)("li",{parentName:"ul"},(0,l.kt)("strong",{parentName:"li"},"\u786e\u8ba4\u7f16\u8bd1\u5165\u53e3"),"\uff1a\u8fdb\u5165 entryOption \u9636\u6bb5\uff0c\u8bfb\u53d6\u914d\u7f6e\u7684 Entries\uff0c\u9012\u5f52\u904d\u5386\u6240\u6709\u7684\u5165\u53e3\u6587\u4ef6\uff0c\u8c03\u7528 Compilation.addEntry \u5c06\u5165\u53e3\u6587\u4ef6\u8f6c\u6362\u4e3a Dependency \u5bf9\u8c61\u3002"),(0,l.kt)("li",{parentName:"ul"},(0,l.kt)("strong",{parentName:"li"},"\u7f16\u8bd1\u6a21\u5757\uff08make\uff09"),"\uff1a \u8c03\u7528 normalModule \u4e2d\u7684 build \u5f00\u542f\u6784\u5efa\uff0c\u4ece entry \u6587\u4ef6\u5f00\u59cb\uff0c\u8c03\u7528 loader \u5bf9\u6a21\u5757\u8fdb\u884c\u8f6c\u8bd1\u5904\u7406\uff0c\u7136\u540e\u8c03\u7528 JS \u89e3\u91ca\u5668\uff08",(0,l.kt)("a",{parentName:"li",href:"https://www.npmjs.com/package/acorn"},"acorn"),"\uff09\u5c06\u5185\u5bb9\u8f6c\u5316\u4e3a AST \u5bf9\u8c61\uff0c\u7136\u540e\u9012\u5f52\u5206\u6790\u4f9d\u8d56\uff0c\u4f9d\u6b21\u5904\u7406\u5168\u90e8\u6587\u4ef6\u3002"),(0,l.kt)("li",{parentName:"ul"},(0,l.kt)("strong",{parentName:"li"},"\u5b8c\u6210\u6a21\u5757\u7f16\u8bd1"),"\uff1a\u5728\u4e0a\u4e00\u6b65\u5904\u7406\u597d\u6240\u6709\u6a21\u5757\u540e\uff0c\u5f97\u5230\u6a21\u5757\u7f16\u8bd1\u4ea7\u7269\u548c\u4f9d\u8d56\u5173\u7cfb\u56fe\u3002"))),(0,l.kt)("li",{parentName:"ol"},"\u751f\u6210\u9636\u6bb5",(0,l.kt)("ul",{parentName:"li"},(0,l.kt)("li",{parentName:"ul"},(0,l.kt)("strong",{parentName:"li"},"\u8f93\u51fa\u8d44\u6e90\uff08seal\uff09"),"\uff1a\u6839\u636e\u5165\u53e3\u548c\u6a21\u5757\u4e4b\u95f4\u7684\u4f9d\u8d56\u5173\u7cfb\uff0c\u7ec4\u88c5\u6210\u591a\u4e2a\u5305\u542b\u591a\u4e2a\u6a21\u5757\u7684 Chunk\uff0c\u518d\u628a\u6bcf\u4e2a Chunk \u8f6c\u6362\u6210\u4e00\u4e2a Asset \u52a0\u5165\u5230\u8f93\u51fa\u5217\u8868\uff0c\u8fd9\u6b65\u662f\u53ef\u4ee5\u4fee\u6539\u8f93\u51fa\u5185\u5bb9\u7684\u6700\u540e\u673a\u4f1a\u3002"),(0,l.kt)("li",{parentName:"ul"},(0,l.kt)("strong",{parentName:"li"},"\u5199\u5165\u6587\u4ef6\u7cfb\u7edf\uff08emitAssets\uff09"),"\uff1a\u786e\u5b9a\u597d\u8f93\u51fa\u5185\u5bb9\u540e\uff0c\u6839\u636e\u914d\u7f6e\u7684 output \u5c06\u5185\u5bb9\u5199\u5165\u6587\u4ef6\u7cfb\u7edf\u3002")))),(0,l.kt)("h2",{id:"\u77e5\u8bc6\u70b9\u6df1\u5165"},"\u77e5\u8bc6\u70b9\u6df1\u5165"),(0,l.kt)("h3",{id:"1-webpack-\u521d\u59cb\u5316\u8fc7\u7a0b"},"1. webpack \u521d\u59cb\u5316\u8fc7\u7a0b"),(0,l.kt)("p",null,"\u4ece webpack \u9879\u76ee webpack.js \u6587\u4ef6 webpack \u65b9\u6cd5\u51fa\u53d1\uff0c\u53ef\u4ee5\u770b\u5230\u521d\u59cb\u5316\u8fc7\u7a0b\u5982\u4e0b\uff1a\n",(0,l.kt)("img",{parentName:"p",src:"https://user-images.githubusercontent.com/15681693/138093156-3b4b60be-4bab-4dcb-970d-c83e4ba1dcf6.png",alt:"webpack \u521d\u59cb\u5316\u6d41\u7a0b\u56fe"})),(0,l.kt)("ol",null,(0,l.kt)("li",{parentName:"ol"},"\u5c06\u547d\u4ee4\u884c\u53c2\u6570\u548c\u7528\u6237\u7684\u914d\u7f6e\u6587\u4ef6\u8fdb\u884c\u5408\u5e76\u3002"),(0,l.kt)("li",{parentName:"ol"},"\u8c03\u7528 getValidateSchema \u5bf9\u914d\u7f6e\u8fdb\u884c\u6821\u9a8c\u3002"),(0,l.kt)("li",{parentName:"ol"},"\u8c03\u7528 createCompiler \u521b\u5efa Compiler \u5bf9\u8c61\u3002",(0,l.kt)("ol",{parentName:"li"},(0,l.kt)("li",{parentName:"ol"},"\u5c06\u7528\u6237\u914d\u7f6e\u548c\u9ed8\u8ba4\u914d\u7f6e\u8fdb\u884c\u5408\u5e76\u5904\u7406\u3002"),(0,l.kt)("li",{parentName:"ol"},"\u5b9e\u4f8b\u5316 Compiler\u3002"),(0,l.kt)("li",{parentName:"ol"},"\u5b9e\u4f8b\u5316 NodeEnvironmentPlugin\u3002"),(0,l.kt)("li",{parentName:"ol"},"\u5904\u7406\u7528\u6237\u914d\u7f6e\u7684 plugins\uff0c\u6267\u884c plugin \u7684 apply \u65b9\u6cd5\u3002"),(0,l.kt)("li",{parentName:"ol"},"\u89e6\u53d1 environment \u548c afterEnvironment \u4e0a\u6ce8\u518c\u7684\u4e8b\u4ef6\u3002"),(0,l.kt)("li",{parentName:"ol"},"\u6ce8\u518c webpack \u5185\u90e8\u63d2\u4ef6\u3002"),(0,l.kt)("li",{parentName:"ol"},"\u89e6\u53d1 initialize \u4e8b\u4ef6\u3002")))),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-javascript"},'// lib/webpack.js 122 \u884c \u90e8\u5206\u4ee3\u7801\u7701\u7565\u5904\u7406\nconst create = () => {\n if (!webpackOptionsSchemaCheck(options)) {\n // \u6821\u9a8c\u53c2\u6570\n getValidateSchema()(webpackOptionsSchema, options);\n }\n // \u521b\u5efa compiler \u5bf9\u8c61\n compiler = createCompiler(webpackOptions);\n};\n\n// lib/webpack.js 57 \u884c\nconst createCompiler = (rawOptions) => {\n // \u7edf\u4e00\u5408\u5e76\u5904\u7406\u53c2\u6570\n const options = getNormalizedWebpackOptions(rawOptions);\n applyWebpackOptionsBaseDefaults(options);\n // \u5b9e\u4f8b\u5316 compiler\n const compiler = new Compiler(options.context);\n // \u628a options \u6302\u8f7d\u5230\u5bf9\u8c61\u4e0a\n compiler.options = options;\n // NodeEnvironmentPlugin \u662f\u5bf9 fs \u6a21\u5757\u7684\u5c01\u88c5\uff0c\u7528\u6765\u5904\u7406\u6587\u4ef6\u8f93\u5165\u8f93\u51fa\u7b49\n new NodeEnvironmentPlugin({\n infrastructureLogging: options.infrastructureLogging,\n }).apply(compiler);\n // \u6ce8\u518c\u7528\u6237\u914d\u7f6e\u63d2\u4ef6\n if (Array.isArray(options.plugins)) {\n for (const plugin of options.plugins) {\n if (typeof plugin === "function") {\n plugin.call(compiler, compiler);\n } else {\n plugin.apply(compiler);\n }\n }\n }\n applyWebpackOptionsDefaults(options);\n // \u89e6\u53d1 environment \u548c afterEnvironment \u4e0a\u6ce8\u518c\u7684\u4e8b\u4ef6\n compiler.hooks.environment.call();\n compiler.hooks.afterEnvironment.call();\n // \u6ce8\u518c webpack \u5185\u7f6e\u63d2\u4ef6\n new WebpackOptionsApply().process(options, compiler);\n compiler.hooks.initialize.call();\n return compiler;\n};\n')),(0,l.kt)("h3",{id:"2-webpack-\u6784\u5efa\u9636\u6bb5\u505a\u4e86\u4ec0\u4e48"},"2. webpack \u6784\u5efa\u9636\u6bb5\u505a\u4e86\u4ec0\u4e48"),(0,l.kt)("p",null,"\u5728 webpack \u51fd\u6570\u6267\u884c\u5b8c\u4e4b\u540e\uff0c\u5c31\u5230\u4e3b\u8981\u7684\u6784\u5efa\u9636\u6bb5\uff0c\u9996\u5148\u6267\u884c compiler.run()\uff0c\u7136\u540e\u89e6\u53d1\u4e00\u7cfb\u5217\u94a9\u5b50\u51fd\u6570\uff0c\u6267\u884c compiler.compile()\u3002\n",(0,l.kt)("img",{parentName:"p",src:"https://user-images.githubusercontent.com/15681693/134162956-d7210d3a-2881-4d0c-b84b-0a1b813a8d0f.png",alt:"webpack \u6267\u884c\u6d41\u7a0b"})),(0,l.kt)("ol",null,(0,l.kt)("li",{parentName:"ol"},"\u5728\u5b9e\u4f8b\u5316 compiler \u4e4b\u540e\uff0c\u6267\u884c compiler.run()\u3002"),(0,l.kt)("li",{parentName:"ol"},"\u6267\u884c newCompilation \u51fd\u6570\uff0c\u8c03\u7528 createCompilation \u521d\u59cb\u5316 Compilation \u5bf9\u8c61\u3002"),(0,l.kt)("li",{parentName:"ol"},"\u6267\u884c ","_","addEntryItem \u5c06\u5165\u53e3\u6587\u4ef6\u5b58\u5165 this.entries\uff08map \u5bf9\u8c61\uff09\uff0c\u904d\u5386 this.entries \u5bf9\u8c61\u6784\u5efa chunk\u3002"),(0,l.kt)("li",{parentName:"ol"},"\u6267\u884c handleModuleCreation\uff0c\u5f00\u59cb\u521b\u5efa\u6a21\u5757\u5b9e\u4f8b\u3002"),(0,l.kt)("li",{parentName:"ol"},"\u6267\u884c moduleFactory.create \u521b\u5efa\u6a21\u5757\u3002",(0,l.kt)("ol",{parentName:"li"},(0,l.kt)("li",{parentName:"ol"},"\u6267\u884c factory.hooks.factorize.call \u94a9\u5b50\uff0c\u7136\u540e\u4f1a\u8c03\u7528 ExternalModuleFactoryPlugin \u4e2d\u6ce8\u518c\u7684\u94a9\u5b50\uff0c\u7528\u4e8e\u914d\u7f6e\u5916\u90e8\u6587\u4ef6\u7684\u6a21\u5757\u52a0\u8f7d\u65b9\u5f0f\u3002"),(0,l.kt)("li",{parentName:"ol"},"\u4f7f\u7528 enhanced-resolve \u89e3\u6790\u6a21\u5757\u548c loader \u7684\u771f\u5b9e\u7edd\u5bf9\u8def\u5f84\u3002"),(0,l.kt)("li",{parentName:"ol"},"\u6267\u884c new NormalModule()\u521b\u5efa module \u5b9e\u4f8b\u3002"))),(0,l.kt)("li",{parentName:"ol"},"\u6267\u884c addModule\uff0c\u5b58\u50a8 module\u3002"),(0,l.kt)("li",{parentName:"ol"},"\u6267\u884c buildModule\uff0c\u6dfb\u52a0\u6a21\u5757\u5230\u6a21\u5757\u961f\u5217 buildQueue\uff0c\u5f00\u59cb\u6784\u5efa\u6a21\u5757, \u8fd9\u91cc\u4f1a\u8c03\u7528 normalModule \u4e2d\u7684 build \u5f00\u542f\u6784\u5efa\u3002",(0,l.kt)("ol",{parentName:"li"},(0,l.kt)("li",{parentName:"ol"},"\u521b\u5efa loader \u4e0a\u4e0b\u6587\u3002"),(0,l.kt)("li",{parentName:"ol"},"\u6267\u884c runLoaders\uff0c\u901a\u8fc7 enhanced-resolve \u89e3\u6790\u5f97\u5230\u7684\u6a21\u5757\u548c loader \u7684\u8def\u5f84\u83b7\u53d6\u51fd\u6570\uff0c\u6267\u884c loader\u3002"),(0,l.kt)("li",{parentName:"ol"},"\u751f\u6210\u6a21\u5757\u7684 hash\u3002"))),(0,l.kt)("li",{parentName:"ol"},"\u6240\u6709\u4f9d\u8d56\u90fd\u89e3\u6790\u5b8c\u6bd5\u540e\uff0c\u6784\u5efa\u9636\u6bb5\u7ed3\u675f\u3002")),(0,l.kt)("pre",null,(0,l.kt)("code",{parentName:"pre",className:"language-javascript"}," // \u6784\u5efa\u8fc7\u7a0b\u6d89\u53ca\u6d41\u7a0b\u6bd4\u8f83\u590d\u6742\uff0c\u4ee3\u7801\u4f1a\u505a\u7701\u7565\n\n // lib/webpack.js 1284\u884c\n // \u5f00\u542f\u7f16\u8bd1\u6d41\u7a0b\n compiler.run((err, stats) => {\n compiler.close(err2 => {\n callback(err || err2, stats);\n });\n });\n\n // lib/compiler.js 1081\u884c\n // \u5f00\u542f\u7f16\u8bd1\u6d41\u7a0b\n compile(callback) {\n const params = this.newCompilationParams();\n // \u521b\u5efa Compilation \u5bf9\u8c61\n const Compilation = this.newCompilation(params);\n }\n\n // lib/Compilation.js 1865\u884c\n // \u786e\u8ba4\u5165\u53e3\u6587\u4ef6\n addEntry() {\n this._addEntryItem();\n }\n\n // lib/Compilation.js 1834\u884c\n // \u5f00\u59cb\u521b\u5efa\u6a21\u5757\u6d41\u7a0b\uff0c\u521b\u5efa\u6a21\u5757\u5b9e\u4f8b\n addModuleTree() {\n this.handleModuleCreation()\n }\n\n // lib/Compilation.js 1548\u884c\n // \u5f00\u59cb\u521b\u5efa\u6a21\u5757\u6d41\u7a0b\uff0c\u521b\u5efa\u6a21\u5757\u5b9e\u4f8b\n handleModuleCreation() {\n this.factorizeModule()\n }\n\n // lib/Compilation.js 1712\u884c\n // \u6dfb\u52a0\u5230\u521b\u5efa\u6a21\u5757\u961f\u5217\uff0c\u6267\u884c\u521b\u5efa\u6a21\u5757\n factorizeModule(options, callback) {\n this.factorizeQueue.add(options, callback);\n }\n\n // lib/Compilation.js 1834\u884c\n // \u4fdd\u5b58\u9700\u8981\u6784\u5efa\u6a21\u5757\n _addModule(module, callback) {\n this.modules.add(module);\n }\n\n // lib/Compilation.js 1284\u884c\n // \u6dfb\u52a0\u6a21\u5757\u8fdb\u6a21\u5757\u7f16\u8bd1\u961f\u5217\uff0c\u5f00\u59cb\u7f16\u8bd1\n buildModule(module, callback) {\n this.buildQueue.add(module, callback);\n }\n")),(0,l.kt)("h3",{id:"3-webpack-\u751f\u6210\u9636\u6bb5\u505a\u4e86\u4ec0\u4e48"},"3. webpack \u751f\u6210\u9636\u6bb5\u505a\u4e86\u4ec0\u4e48"),(0,l.kt)("p",null,"\u6784\u5efa\u9636\u6bb5\u56f4\u7ed5 module \u5c55\u5f00\uff0c\u751f\u6210\u9636\u6bb5\u5219\u56f4\u7ed5 chunks \u5c55\u5f00\u3002\u7ecf\u8fc7\u6784\u5efa\u9636\u6bb5\u4e4b\u540e\uff0cwebpack \u5f97\u5230\u8db3\u591f\u7684\u6a21\u5757\u5185\u5bb9\u4e0e\u6a21\u5757\u5173\u7cfb\u4fe1\u606f\uff0c\u4e4b\u540e\u901a\u8fc7 Compilation.seal \u51fd\u6570\u751f\u6210\u6700\u7ec8\u8d44\u6e90\u3002"),(0,l.kt)("h4",{id:"31-\u751f\u6210\u4ea7\u7269"},"3.1 \u751f\u6210\u4ea7\u7269"),(0,l.kt)("p",null,"\u6267\u884c Compilation.seal \u8fdb\u884c\u4ea7\u7269\u7684\u5c01\u88c5\u3002"),(0,l.kt)("ol",null,(0,l.kt)("li",{parentName:"ol"},"\u6784\u5efa\u672c\u6b21\u7f16\u8bd1\u7684 ChunkGraph \u5bf9\u8c61\uff0c\u6267\u884c buildChunkGraph\uff0c\u8fd9\u91cc\u4f1a\u5c06 import()\u3001require.ensure \u7b49\u65b9\u6cd5\u751f\u6210\u7684\u52a8\u6001\u6a21\u5757\u6dfb\u52a0\u5230 chunks \u4e2d\u3002"),(0,l.kt)("li",{parentName:"ol"},"\u904d\u5386 Compilation.modules \u96c6\u5408\uff0c\u5c06 module \u6309 ",(0,l.kt)("strong",{parentName:"li"},"entry/\u52a8\u6001\u5f15\u5165")," \u7684\u89c4\u5219\u5206\u914d\u7ed9\u4e0d\u540c\u7684 Chunk \u5bf9\u8c61\u3002"),(0,l.kt)("li",{parentName:"ol"},"\u8c03\u7528 Compilation.emitAssets \u65b9\u6cd5\u5c06 assets \u4fe1\u606f\u8bb0\u5f55\u5230 Compilation.assets \u5bf9\u8c61\u4e2d\u3002"),(0,l.kt)("li",{parentName:"ol"},"\u6267\u884c hooks.optimizeChunkModules \u7684\u94a9\u5b50\uff0c\u8fd9\u91cc\u5f00\u59cb\u8fdb\u884c\u4ee3\u7801\u751f\u6210\u548c\u5c01\u88c5\u3002",(0,l.kt)("ol",{parentName:"li"},(0,l.kt)("li",{parentName:"ol"},"\u6267\u884c\u4e00\u7cfb\u5217\u94a9\u5b50\u51fd\u6570\uff08reviveModules, moduleId, optimizeChunkIds \u7b49\uff09\u3002"),(0,l.kt)("li",{parentName:"ol"},"\u6267\u884c createModuleHashes \u66f4\u65b0\u6a21\u5757 hash\u3002"),(0,l.kt)("li",{parentName:"ol"},"\u6267\u884c JavascriptGenerator \u751f\u6210\u6a21\u5757\u4ee3\u7801\uff0c\u8fd9\u91cc\u4f1a\u904d\u5386 modules\uff0c\u521b\u5efa\u6784\u5efa\u4efb\u52a1\uff0c\u5faa\u73af\u4f7f\u7528 JavascriptGenerator \u6784\u5efa\u4ee3\u7801\uff0c\u8fd9\u65f6\u4f1a\u5c06 import \u7b49\u6a21\u5757\u5f15\u5165\u65b9\u5f0f\u66ff\u6362\u4e3a ",(0,l.kt)("strong",{parentName:"li"},"webpack_require")," \u7b49\uff0c\u5e76\u5c06\u751f\u6210\u7ed3\u679c\u5b58\u5165\u7f13\u5b58\u3002"),(0,l.kt)("li",{parentName:"ol"},"\u6267\u884c processRuntimeRequirements\uff0c\u6839\u636e\u751f\u6210\u7684\u5185\u5bb9\u6240\u4f7f\u7528\u5230\u7684 webpack_require \u7684\u51fd\u6570\uff0c\u6dfb\u52a0\u5bf9\u5e94\u7684\u4ee3\u7801\u3002"),(0,l.kt)("li",{parentName:"ol"},"\u6267\u884c createHash \u521b\u5efa chunk \u7684 hash\u3002"),(0,l.kt)("li",{parentName:"ol"},"\u6267\u884c clearAssets \u6e05\u9664 chunk \u7684 files \u548c auxiliary\uff0c\u8fd9\u91cc\u7f13\u5b58\u7684\u662f\u751f\u6210\u7684 chunk \u7684\u6587\u4ef6\u540d\uff0c\u4e3b\u8981\u662f\u6e05\u9664\u4e0a\u6b21\u6784\u5efa\u4ea7\u751f\u7684\u5e9f\u5f03\u5185\u5bb9\u3002")))),(0,l.kt)("h4",{id:"32-\u6587\u4ef6\u8f93\u51fa"},"3.2 \u6587\u4ef6\u8f93\u51fa"),(0,l.kt)("p",null,"\u56de\u5230 Compiler \u7684\u6d41\u7a0b\u4e2d\uff0c\u6267\u884c onCompiled \u56de\u8c03\u3002"),(0,l.kt)("ol",null,(0,l.kt)("li",{parentName:"ol"},"\u89e6\u53d1 shouldEmit \u94a9\u5b50\u51fd\u6570\uff0c\u8fd9\u91cc\u662f\u6700\u540e\u80fd\u4f18\u5316\u4ea7\u7269\u7684\u94a9\u5b50\u3002"),(0,l.kt)("li",{parentName:"ol"},"\u904d\u5386 module \u96c6\u5408\uff0c\u6839\u636e entry \u914d\u7f6e\u53ca\u5f15\u5165\u8d44\u6e90\u7684\u65b9\u5f0f\uff0c\u5c06 module \u5206\u914d\u5230\u4e0d\u540c\u7684 chunk\u3002"),(0,l.kt)("li",{parentName:"ol"},"\u904d\u5386 chunk \u96c6\u5408\uff0c\u8c03\u7528 Compilation.emitAsset \u65b9\u6cd5\u6807\u8bb0 chunk \u7684\u8f93\u51fa\u89c4\u5219\uff0c\u5373\u8f6c\u5316\u4e3a assets \u96c6\u5408\u3002"),(0,l.kt)("li",{parentName:"ol"},"\u5199\u5165\u672c\u5730\u6587\u4ef6\uff0c\u7528\u7684\u662f webpack \u51fd\u6570\u6267\u884c\u65f6\u521d\u59cb\u5316\u7684\u6587\u4ef6\u6d41\u5de5\u5177\u3002"),(0,l.kt)("li",{parentName:"ol"},"\u6267\u884c done \u94a9\u5b50\u51fd\u6570\uff0c\u8fd9\u91cc\u4f1a\u6267\u884c compiler.run() \u7684\u56de\u8c03\uff0c\u518d\u6267\u884c compiler.close()\uff0c\u7136\u540e\u6267\u884c\u6301\u4e45\u5316\u5b58\u50a8\uff08\u524d\u63d0\u662f\u4f7f\u7528\u7684 filesystem \u7f13\u5b58\u6a21\u5f0f\uff09\u3002")),(0,l.kt)("h2",{id:"\u53c2\u8003\u8d44\u6599"},"\u53c2\u8003\u8d44\u6599"),(0,l.kt)("ol",null,(0,l.kt)("li",{parentName:"ol"},(0,l.kt)("a",{parentName:"li",href:"https://webpack.js.org/"},"webpack5 \u5b98\u65b9\u6587\u6863")),(0,l.kt)("li",{parentName:"ol"},(0,l.kt)("a",{parentName:"li",href:"https://www.npmjs.com/package/acorn"},"acorn"))))}s.isMDXComponent=!0}}]);