{"id":102550,"date":"2020-05-04T13:41:40","date_gmt":"2020-05-04T05:41:40","guid":{"rendered":"http:\/\/4563.org\/?p=102550"},"modified":"2020-05-04T13:41:40","modified_gmt":"2020-05-04T05:41:40","slug":"%e6%b1%82-webpack-%e6%8f%92%e4%bb%b6%ef%bc%8c%e6%8e%92%e9%99%a4%e7%89%b9%e5%ae%9a-js-%e6%96%87%e4%bb%b6%e3%80%82","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=102550","title":{"rendered":"\u6c42 webpack \u63d2\u4ef6\uff0c\u6392\u9664\u7279\u5b9a js \u6587\u4ef6\u3002"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u6c42 webpack \u63d2\u4ef6\uff0c\u6392\u9664\u7279\u5b9a js \u6587\u4ef6\u3002               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : xiaoming1992 <\/span>  <span><i><\/i> 59<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u5047\u8bbe\u6211\u7684\u6587\u4ef6\u7ed3\u6784\u5982\u4e0b\uff1a<\/p>\n<pre><code>src |--index.js |--componentA \/\/ \u7ec4\u4ef6 A\uff0c\u4f9d\u8d56 Z |--componentB \/\/ \u7ec4\u4ef6 B\uff0c\u4f9d\u8d56 Z |--componentC \/\/ \u7ec4\u4ef6 C\uff0c\u4f9d\u8d56 Z |--fileZ \/\/ \u72ec\u7acb js \u6587\u4ef6\uff0c\u65e0\u4f9d\u8d56 <\/code><\/pre>\n<p>\u73b0\u5728\u6211\u5e0c\u671b\u5728\u6253\u5305\u65f6\uff0c\u5ffd\u7565\u6587\u4ef6 Z\uff0c\u800c\u5c06\u7ec4\u4ef6 A\u3001B\u3001C \u4e2d\u6240\u4f9d\u8d56\u7684 fileZ \u4e2d\u7684\u5185\u5bb9\u8f6c\u53d8\u4e3a\u4f9d\u8d56 window<\/p>\n<pre><code>\/\/ \u4f8b\u5982  \/\/ \u7ec4\u4ef6 A \u6e90\u7801 import { funcOne } from \"fileZ\"  \/\/ \u8f6c\u8bd1\u6210\u5982\u4e0b\uff0c\u6682\u65f6\u5148\u4e0d\u7ba1\u6211 window \u91cc\u9762\u6709\u6ca1\u6709\u8fd9\u4e2a\u65b9\u6cd5 const { funcOne } = window <\/code><\/pre>\n<p>ps: \u4e0d\u80fd\u4fee\u6539\u4ee3\u7801\u903b\u8f91\uff0c\u4ec5\u5728 webpack \u914d\u7f6e\u6587\u4ef6\u4e2d\u5904\u7406\u8fd9\u4e9b\u5de5\u4f5c<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>12<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"687857\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : ceet <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             webpack \u53ef\u4ee5\u914d\u7f6e\u6253\u5305\u73af\u5883\u548c\u5f00\u53d1\u73af\u5883 \u6839\u636e\u4e0d\u540c\u7684\u73af\u5883\u52a0\u8f7d\u4e0d\u540c\u7684 js                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"687858\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : xiaoming1992 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u81ea\u95ee\u81ea\u7b54\uff0c\u597d\u50cf\u53ef\u4ee5\u76f4\u63a5\u7528 external<br \/>(\u4e3a\u4ec0\u4e48\u6211\u4e0d\u80fd\u7f16\u8f91\u95ee\u9898\u4e86\u3002\u3002\u3002)                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"687859\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : joe237 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4e0d\u662f\u597d\u50cf\uff0c\u662f\u786e\u5b9e\u8be5\u8fd9\u4e48\u7528\u3002<\/p>\n<p>\u5e38\u89c1\u7684\u573a\u666f\u5c31\u662f\u4e00\u4e9b\u8001\u7684\u7b2c\u4e09\u65b9\u5e93\uff0c\u5bf9\u8c61\u90fd\u662f\u76f4\u63a5\u6302\u8f7d\u5728 window \u5bf9\u8c61\u4e0a\u7684\uff0c\u8fd9\u65f6\u5019\u76f4\u63a5\u5728 html \u6a21\u677f\u4e2d\u901a\u8fc7 script \u6807\u7b7e\u5f15\u5165\uff0c\u5b8c\u4e86\u5728 webpack \u7684 external \u4e2d\u5c06\u5bf9\u5e94\u7684\u5bf9\u8c61\u66b4\u9732\u51fa\u53bb\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u5728\u7ec4\u4ef6\u4e2d\u76f4\u63a5\u901a\u8fc7 import \u5f15\u7528\u4e86                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"687860\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : YuTengjing <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             [webpack \u5b98\u65b9\u6587\u6863 externals]( https:\/\/www.webpackjs.com\/configuration\/externals\/)                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"687861\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : xiaoming1992 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @joe237 \u6211\u662f\u5f15\u7528\u7684\u81ea\u5df1\u7684\u6587\u4ef6\uff0c\u5f00\u53d1\u5b8c\u4e86\u4e4b\u540e\uff0c\u53d1\u73b0\u6709\u4e00\u4e2a\u6587\u4ef6\u4fee\u6539\u7279\u522b\u9891\u7e41\uff0c\u4fee\u6539\u4e00\u6b21\u5c31\u8981\u5168\u90e8\u91cd\u65b0\u6784\u5efa\uff0c\u800c\u4e14\u6211\u7684 output file name \u91c7\u7528\u4e86 hash\uff0c\u7b49\u4e8e\u8bf4\u7528\u6237\u9700\u8981\u5168\u90e8\u91cd\u65b0\u52a0\u8f7d\u4e00\u904d\uff0c\u611f\u89c9\u4e0d\u5408\u7406\uff0c\u5c31\u60f3\u628a\u90a3\u4e2a\u6587\u4ef6\u5355\u72ec\u6302\u5230 html \u7684 inline <br \/> script \u91cc\u9762\uff0c\u5bfc\u51fa\u5230 window\uff0c\u5176\u4ed6\u6587\u4ef6\u53bb window \u91cc\u9762\u5f15\u7528\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"687862\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : codeduan <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @xiaoming1992 \u628a\u4f60\u90a3\u4e2a\u6587\u4ef6\u5355\u72ec\u4f5c\u4e3a\u4e00\u4e2a entry\uff0c\u7136\u540e\u914d\u7f6e externals \u5c31\u597d\u4e86\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"687863\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : codeduan <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @xiaoming1992 \u4e00\u4e2a entry \u662f fileZ\uff0c\u53e6\u5916\u4e00\u4e2a entry \u662f index \u914d\u7f6e externals \u5ffd\u7565 fileZ\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"687864\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : SoloCompany <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             mockFileZ.js<\/p>\n<p>module.exports = window<\/p>\n<p>alias {<br \/> fileZ : mockFileZ<br \/>}                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"687865\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : PainAndLove <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             externals \u539f\u6765\u662f\u8fd9\u4e48\u7528\u7684\u3002\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"687866\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : xiaoming1992 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @codeduan \u5bf9\uff0c\u6211\u53e6\u5916\u5f00\u4e86\u4e00\u4e2a webpack.config\uff0c\u5e76\u8bbe\u7f6e output.libraryTarget \u4e3a window\uff0c\u7136\u540e\u7528\u4e00\u4e2a inline-script-webpack-plugin \u5c06\u4e4b\u6253\u5305\u5230 html \u91cc\u9762\uff0c\u4f46\u662f\u5176\u5b9e\u8fd8\u662f\u5f88\u4e11\uff0c\u4e0d\u4f18\u96c5\uff0c\u6211\u53ef\u80fd\u8fd8\u5f97\u60f3\u60f3\u5176\u4ed6\u529e\u6cd5\u91cd\u65b0\u641e\u4e00\u4e0b\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"687867\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : xiaoming1992 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4e24\u4e2a entry \u4e0d\u80fd\u5728\u540c\u4e00\u4e2a webpackConfig \u91cc\u9762\uff0c\u5426\u5219\u8fd9\u4e2a\u9891\u7e41\u4fee\u6539\u7684\u6587\u4ef6\u4f1a\u5f71\u54cd\u5176\u4ed6\u6587\u4ef6\u7684 hash                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"687868\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : azh7138m <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             hash \u53ef\u4ee5\u7528 content hash<br \/>\u6587\u4ef6\u5185\u5bb9\u4e0d\u53d8\uff0chash \u5c31\u4e0d\u53d8                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u6c42 webpack \u63d2\u4ef6\uff0c\u6392\u9664\u7279\u5b9a&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/102550"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=102550"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/102550\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=102550"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=102550"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=102550"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}