{"id":31364,"date":"2024-11-25T10:18:59","date_gmt":"2024-11-25T02:18:59","guid":{"rendered":"https:\/\/fwq.ai\/blog\/31364\/"},"modified":"2024-11-25T10:18:59","modified_gmt":"2024-11-25T02:18:59","slug":"webpack%e5%a6%82%e4%bd%95%e5%8a%a8%e6%80%81%e5%bc%95%e5%85%a5%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/31364\/","title":{"rendered":"webpack\u5982\u4f55\u52a8\u6001\u5f15\u5165\u6587\u4ef6"},"content":{"rendered":"<p style=\"text-align: left\">\u8fd9\u6b21\u7ed9\u5927\u5bb6\u5e26\u6765webpack\u5982\u4f55\u52a8\u6001\u5f15\u5165\u6587\u4ef6\uff0cwebpack\u52a8\u6001\u5f15\u5165\u6587\u4ef6\u7684\u6709\u54ea\u4e9b\uff0c\u4e0b\u9762\u5c31\u662f\u5b9e\u6218\u6848\u4f8b\uff0c\u4e00\u8d77\u6765\u770b\u4e00\u4e0b\u3002<\/p>\n<p style=\"text-align: left\">\u521a\u5f00\u59cb\u4f7f\u7528webpack\u65f6\uff0c\u53ef\u80fd\u5f88\u591a\u4eba\u90fd\u4f1a\u6709\u8fc7\u8fd9\u6837\u7684\u60f3\u6cd5\uff0c\u5728\u6587\u4ef6\u65f6\uff0c\u80fd\u4e0d\u80fd\u4e0d\u5199\u9759\u6001\u7684\u8def\u5f84\uff0c\u800c\u662f\u4f7f\u7528\u4e00\u4e2a\u66f4\u7075\u6d3b\u7684\u65b9\u5f0f\uff0c\u6bd4\u5982\u5b9a\u4e49\u4e00\u4e2a\uff0c\u6839\u636e\u5177\u4f53\u7684\u8fd0\u884c\u60c5\u51b5\u6765\u786e\u5b9a\u9700\u8981require\u54ea\u4e2a\u6587\u4ef6\uff01<\/p>\n<p style=\"text-align: left\"><strong>\u6bd4\u5982\uff0c\u7b14\u8005\u5c31\u9047\u5230\u4e86\u4e00\u4e2a\u8fd9\u6837\u7684\u9700\u6c42\u3002<\/strong><\/p>\n<p style=\"text-align: left\">\u5f53\u65f6\u662f\u4f7f\u7528vue-router\u5f00\u53d1\u4e00\u4e2a\u7ba1\u7406\u7cfb\u7edf\uff0c\u7ba1\u7406\u7cfb\u7edf\u81ea\u8eab\u6709\u4e00\u4e2a\u76ee\u5f55\u6570\u7ec4\uff0c\u800cvue-router\u4e5f\u9700\u8981\u4e00\u4e2aroute\u914d\u7f6e\u6570\u7ec4\uff0c\u800c\u8fd9\u4e24\u8005\u6070\u6070\u662f\u5bf9\u5e94\u5173\u7cfb\u7684\u3002\u5f53\u65f6\u5c31\u60f3\uff0c\u80fd\u4e0d\u80fd\u53ea\u7ef4\u62a4\u4e00\u4e2a\u76ee\u5f55\u6570\u7ec4\uff0c\u7136\u540e\u52a8\u6001\u7684\u751f\u6210route\u6570\u7ec4\u5462\uff1f<\/p>\n<p style=\"text-align: left\"><span style=\"color: #ff0000\"><strong>\u4e8e\u662f\u6211\u5b9e\u73b0\u4e86\u4e00\u4e2a\u5c0fdemo\uff0c\u5982\u4e0b\uff1a<\/strong><\/span><\/p>\n<pre>\/\/ directory\r\nlet dir = [\r\n {\r\n name: 'a',\r\n path: '\/a',\r\n componentPath: '.\/a.vue'\r\n },\r\n { \r\n ...\r\n }\r\n];\r\nlet route = [];\r\nfor (let i = 0; i &lt; dir.length; ++i) {\r\n let item = dir[i];\r\n route.push({\r\n path: item.path,\r\n component: r =&gt; require.ensure([], () =&gt; r(require(item.componentPath)), 'demo')\r\n });\r\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p style=\"text-align: left\">\u5927\u81f4\u5982\u4e0a\uff0c\u5f53\u65f6\u6ca1\u7559\u6e90\u7801\uff0c\u5927\u6982\u624b\u5199\u4e2a\u4f8b\u5b50\uff0c\u5ffd\u7565\u4e00\u4e9b\u53ef\u80fd\u7684\u3002<\/p>\n<p style=\"text-align: left\">\u5f53\u4f7f\u7528\u8fd9\u6bb5\u4ee3\u7801\u8fd0\u884c\u7684\u65f6\u5019\uff0c\u5c31\u5f00\u59cb\u62a5\u9519\u4e86\uff1aCritical dependencies\u3002<\/p>\n<p style=\"text-align: left\">\u5728\u67e5\u4e86\u4e00\u4e9b\u8d44\u6599\u540e\uff0c\u6211\u5927\u81f4\u660e\u767d\u4e86webpack\u7684\u8fd0\u884c\u673a\u5236\uff0c\u4e5f\u660e\u767d\u4e86\u4e00\u4e2a\u4e8b\u5b9e\uff0c\u90a3\u5c31\u662f\uff1a<span style=\"color: #ff0000\"><strong>\u4f7f\u7528webpack\u52a8\u6001require\u6587\u4ef6\u662f\u4e0d\u53ef\u80fd\u5b9e\u73b0\u7684\u3002<\/strong><\/span><\/p>\n<p style=\"text-align: left\">\u8981\u77e5\u9053\uff0cwebpack\u662f\u4e00\u4e2a\u6253\u5305\u5de5\u5177\uff0c\u5b83\u8fd0\u884c\u7684\u65f6\u673a\u5c5e\u4e8e\u9884\u7f16\u8bd1\uff0c\u800c\u6211\u4eec\u7684\u60f3\u6cd5\u5462\uff0c\u8981\u5728\u8fd0\u884c\u65f6\u624d\u80fd\u786e\u5b9a\u5230\u5e95\u8981require\u54ea\u4e9b\u6587\u4ef6\uff0c\u8fd9\u663e\u7136\u662f\u4e0d\u884c\u7684\uff0c\u56e0\u4e3a\u8fd9\u6837\uff0cwebpack\u4e0d\u77e5\u9053\u8be5\u628a\u54ea\u4e9b\u5305\u6253\u5305\u8d77\u6765\uff0c\u7ed3\u679c\u5c31\u662f\u90a3\u4e9b\u6587\u4ef6\u90fd\u6ca1\u6709\u88ab\u6253\u5305\u8fdb\u53bb\uff0c\u90a3\u4e48require\u4e5f\u80af\u5b9arequire\u4e0d\u5230\u554a\u3002<\/p>\n<p style=\"text-align: left\">\u660e\u767d\u4e86\u8fd9\u4e2a\u673a\u5236\uff0c\u6211\u4eec\u5c31\u5e94\u8be5\u786e\u5b9a\u76f4\u63a5\u4f20\u9012path\u6765\u52a8\u6001\u6ce8\u518croute\u662f\u4e00\u79cd\u4e0d\u53ef\u80fd\u5b9e\u73b0\u7684\u65b9\u6848\u3002<\/p>\n<p style=\"text-align: left\">\u90a3\u4e48\u5982\u679c\u53c8\u60f3\u5b9e\u73b0\u52a8\u6001\u5f15\u5165\u6587\u4ef6\uff0c\u600e\u4e48\u529e\u5462\uff1f\u6211\u4eec\u53ef\u4ee5\u8003\u8651\u4e00\u4e9b\u66f2\u7ebf\u6551\u56fd\u7684\u65b9\u6848\u3002<\/p>\n<p style=\"text-align: left\"><span style=\"background-color: #ccffcc\"><strong>1. \u9884\u7f16\u8bd1\u9636\u6bb5\u786e\u5b9arequire\u8def\u5f84\u3002<\/strong><\/span><\/p>\n<p style=\"text-align: left\">\u6211\u4eec\u4e4b\u6240\u4ee5\u4f7f\u7528\u52a8\u6001\u7684\u53d8\u91cf\u6765\u5b58\u50a8\u8def\u5f84\u5b57\u7b26\u4e32\uff0c\u65e0\u975e\u662f\u60f3\u8981\u7a0b\u5e8f\u66ff\u6211\u4eec\u6267\u884c\u4e00\u4e9b\u52a8\u4f5c\uff0c\u6bd4\u5982\u62fc\u63a5\u5b57\u7b26\u4e32\u7b49\u7b49\u3002\u90a3\u4e48\u5f88\u591a\u60c5\u51b5\u4e0b\u8fd9\u4e9b\u7a0b\u5e8f\u5728\u8fd0\u884c\u65f6\u6267\u884c\u548c\u5728\u9884\u7f16\u8bd1\u65f6\u6267\u884c\u662f\u4e00\u6837\u6548\u679c\u7684\u3002\u6bd4\u5982\u6211\u4eec\u4e0a\u9762\u7684\u4f8b\u5b50\uff0c\u5c31\u662f\u4e0d\u60f3\u624b\u52a8\u53bb\u7ef4\u62a4\u4e24\u5f20\u8868\u3002\u90a3\u4e48\u6211\u4eec\u53ef\u4ee5\u5728\u6253\u5305\u7684\u65f6\u5019\uff0c\u6267\u884c\u8bfb\u5199\u6587\u4ef6\uff0c\u6765\u52a8\u6001\u6267\u884c\u89e3\u6790\u64cd\u4f5c\uff0c\u5e76\u5c06\u89e3\u6790\u5f97\u5230\u7684route\u6570\u7ec4\u5199\u5165\u6307\u5b9a\u7684\u6587\u4ef6\u3002\u8fd9\u6837\u4e0d\u5c31\u5b9e\u73b0\u4e86\u76ee\u6807\u5417\uff1f<\/p>\n<p style=\"text-align: left\"><span style=\"background-color: #ccffcc\"><strong>2.\u76f4\u63a5\u4f20\u9012component\u5462\uff1f\u6bd4\u5982\u4e0b\u9762\u7684\u5b9e\u73b0\u65b9\u5f0f\uff1a<\/strong><\/span><\/p>\n<pre>\/\/ directory\r\nlet dir = [\r\n {\r\n name: 'a',\r\n path: '\/a',\r\n component: r =&gt; require.ensure([], () =&gt; r(require('.\/a.vue')), 'demo')\r\n \r\n },\r\n { \r\n ...\r\n }\r\n];\r\nlet route = [];\r\nfor (let i = 0; i &lt; dir.length; ++i) {\r\n let item = dir[i];\r\n route.push({\r\n path: item.path,\r\n component: item.component\r\n });\r\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p style=\"text-align: left\">\u8fd9\u79cd\u65b9\u6848\u4e3b\u8981\u9488\u5bf9\u6211\u4eec\u7684\u4f8b\u5b50\uff0c\u5f53\u7136\u5bf9\u5176\u4ed6\u60c5\u51b5\u4e5f\u80fd\u7ed9\u4e00\u4e2a\u542f\u53d1\u4f5c\u7528\u3002<\/p>\n<p style=\"text-align: left\">\u4e0d\u8fc7\u8bf4\u6765\u8bf4\u53bb\uff0c\u8fd9\u90fd\u662f\u59d4\u66f2\u6c42\u5168\u7684\u65b9\u6848\uff0c\u6211\u4eec\u5fc5\u987b\u660e\u767d\u4e00\u70b9\uff0c\u90a3\u5c31\u662f\u5728\u9884\u7f16\u8bd1\u9636\u6bb5\uff0cwebpack\u5fc5\u987b\u660e\u786e\u77e5\u9053\u8be5\u5f15\u5165\u54ea\u4e9b\u6587\u4ef6\uff0c\u5426\u5219\u4ec0\u4e48\u65b9\u6848\u90fd\u662f\u4e0d\u53ef\u884c\u7684\u3002<\/p>\n<p style=\"text-align: left\">\u987a\u4fbf\u4e00\u63d0\uff0c\u7f51\u4e0a\u8fd8\u6709\u8bf4\u6cd5require(path)\uff0c\u53ea\u8981path\u4e0d\u662f\u7eaf\u53d8\u91cf\uff0c\u6bd4\u5982require(&#8216;.\/root\/&#8217; + path)\uff0c\u8fd9\u6837\u7ec4\u5408\u4e00\u4e0b\u5c31\u884c\uff1f\u6211\u5728\u8fd9\u91ccrequire.ensure\u51fd\u6570\u4e2d\u5c1d\u8bd5\u8fc7\uff0c\u4e0d\u884c\u3002\u4e5f\u8bb8\u662f\u6211\u4f7f\u7528\u65b9\u5f0f\u4e0d\u5bf9\u5427\u3002<\/p>\n<p style=\"text-align: left\">\u518d\u4e00\u63d0\uff0c\u4f20\u9012component\u5bf9\u8c61\u65f6\uff0c\u597d\u50cf\u5728\u8de8\u6587\u4ef6\u65f6\u4f1a\u6709\u70b9\u95ee\u9898\uff0c\u53cd\u6b63\u6211\u662f\u5c06\u76ee\u5f55\u6570\u7ec4\u548croute\u6570\u7ec4\u4ee5\u53ca\u89e3\u6790\u8fc7\u7a0b\u5199\u5728\u4e00\u4e2a\u6587\u4ef6\u7684\u3002\u4e5f\u8bb8\u662f\u4f7f\u7528\u76f8\u5bf9\u8def\u5f84\u5728\u4e0d\u540c\u6587\u4ef6\u4e2d\u89e3\u6790\u7684\u95ee\u9898\uff0c\u4f7f\u7528\u7684\u65f6\u5019\u9700\u8981\u6ce8\u610f\u4e0b\u3002<\/p>\n<p>\u76f8\u4fe1\u770b\u4e86\u672c\u6587\u6848\u4f8b\u4f60\u5df2\u7ecf\u638c\u63e1\u4e86\u65b9\u6cd5\uff0c\u66f4\u591a\u7cbe\u5f69\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n<p>\u63a8\u8350\u9605\u8bfb\uff1a<\/p>\n<p><\/p>\n<p><\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fwebpack\u5982\u4f55\u52a8\u6001\u5f15\u5165\u6587\u4ef6\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd9\u6b21\u7ed9\u5927\u5bb6\u5e26\u6765webpack\u5982\u4f55\u52a8\u6001\u5f15\u5165\u6587\u4ef6\uff0cwebpack\u52a8\u6001\u5f15\u5165\u6587\u4ef6\u7684\u6709\u54ea\u4e9b\uff0c\u4e0b\u9762\u5c31\u662f\u5b9e\u6218\u6848\u4f8b\uff0c\u4e00\u8d77\u6765\u770b\u4e00\u4e0b\u3002 \u521a\u5f00\u59cb\u4f7f\u7528webpack\u65f6\uff0c\u53ef\u80fd\u5f88\u591a\u4eba\u90fd\u4f1a\u6709\u8fc7\u8fd9\u6837\u7684\u60f3\u6cd5\uff0c\u5728\u6587\u4ef6\u65f6\uff0c\u80fd\u4e0d\u80fd\u4e0d\u5199\u9759\u6001\u7684\u8def\u5f84\uff0c\u800c\u662f\u4f7f\u7528\u4e00\u4e2a\u66f4\u7075\u6d3b\u7684\u65b9\u5f0f\uff0c\u6bd4\u5982\u5b9a\u4e49\u4e00\u4e2a\uff0c\u6839\u636e\u5177\u4f53\u7684\u8fd0\u884c\u60c5\u51b5\u6765\u786e\u5b9a\u9700\u8981require\u54ea\u4e2a\u6587\u4ef6\uff01 \u6bd4\u5982\uff0c\u7b14\u8005\u5c31\u9047\u5230\u4e86\u4e00\u4e2a\u8fd9\u6837\u7684\u9700\u6c42\u3002 \u5f53\u65f6\u662f\u4f7f\u7528vue-router\u5f00\u53d1\u4e00\u4e2a\u7ba1\u7406\u7cfb\u7edf\uff0c\u7ba1\u7406\u7cfb\u7edf\u81ea\u8eab\u6709\u4e00\u4e2a\u76ee\u5f55\u6570\u7ec4\uff0c\u800cvue-router\u4e5f\u9700\u8981\u4e00\u4e2aroute\u914d\u7f6e\u6570\u7ec4\uff0c\u800c\u8fd9\u4e24\u8005\u6070\u6070\u662f\u5bf9\u5e94\u5173\u7cfb\u7684\u3002\u5f53\u65f6\u5c31\u60f3\uff0c\u80fd\u4e0d\u80fd\u53ea\u7ef4\u62a4\u4e00\u4e2a\u76ee\u5f55\u6570\u7ec4\uff0c\u7136\u540e\u52a8\u6001\u7684\u751f\u6210route\u6570\u7ec4\u5462\uff1f \u4e8e\u662f\u6211\u5b9e\u73b0\u4e86\u4e00\u4e2a\u5c0fdemo\uff0c\u5982\u4e0b\uff1a \/\/ directory let dir = [ { name: &#8216;a&#8217;, path: &#8216;\/a&#8217;, componentPath: &#8216;.\/a.vue&#8217; }, { &#8230; } ]; let route = []; for (let i = 0; i &lt; dir.length; ++i) { let item = dir[i]; route.push({ path: item.path, component: r =&gt; require.ensure([], () =&gt; r(require(item.componentPath)), &#8216;demo&#8217;) }); } \u767b\u5f55\u540e\u590d\u5236 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[],"class_list":["post-31364","post","type-post","status-publish","format-standard","hentry","category-19"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/31364","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=31364"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/31364\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=31364"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=31364"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=31364"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}