{"id":344324,"date":"2021-02-10T09:03:19","date_gmt":"2021-02-10T01:03:19","guid":{"rendered":"http:\/\/4563.org\/?p=344324"},"modified":"2021-02-10T09:03:19","modified_gmt":"2021-02-10T01:03:19","slug":"%e9%82%a3%e4%b8%aa%e5%95%a5-babel-plugin-import-%e5%88%b0%e5%ba%95%e8%a6%81%e5%92%8b%e9%85%8d%e7%bd%ae%e6%89%8d%e8%83%bd%e6%8c%89%e9%9c%80%e5%bc%95%e5%85%a5","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=344324","title":{"rendered":"\u90a3\u4e2a\u5565 babel-plugin-import \u5230\u5e95\u8981\u548b\u914d\u7f6e\u624d\u80fd\u6309\u9700\u5f15\u5165"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u90a3\u4e2a\u5565 babel-plugin-import \u5230\u5e95\u8981\u548b\u914d\u7f6e\u624d\u80fd\u6309\u9700\u5f15\u5165               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : zxCoder <\/span>  <span><i><\/i> 3<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u6211\u6309\u6587\u6863\u8fd9\u6837\u505a\u7684<\/p>\n<p>1 <code>create-react-app<\/code>\u521b\u5efa\u4e86\u4e00\u4e2a\u9879\u76ee\uff0c\u7136\u540e\u88c5\u4e86<code>antd<\/code>\u548c<code>babel-plugin-import<\/code>\uff0c\u7136\u540e\u6309\u6587\u6863\u8bf4\u7684\uff0c\u914d\u7f6e\u4e86<code>.babelrc<\/code>\uff0c\u5185\u5bb9\u662f\uff1a<\/p>\n<pre><code>{   \"plugins\": [     [       \"import\",       {         \"libraryName\": \"antd\",         \"style\": \"css\"       }     ]   ] } <\/code><\/pre>\n<p>\u7136\u540e\u8fd8\u662f\u4e0d\u80fd\u6309\u9700\u52a0\u8f7d\uff0c\u63a7\u5236\u53f0\u4ecd\u7136\u663e\u793a<\/p>\n<pre><code>index.js:8 You are using a whole package of antd, please use https:\/\/www.npmjs.com\/package\/babel-plugin-import to reduce app bundle size. <\/code><\/pre>\n<p>package.json \u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n<pre><code>{   \"name\": \"mongodb-pbac-client-react\",   \"version\": \"0.1.0\",   \"private\": true,   \"dependencies\": {     \"@testing-library\/jest-dom\": \"^5.11.9\",     \"@testing-library\/react\": \"^11.2.5\",     \"@testing-library\/user-event\": \"^12.7.1\",     \"antd\": \"^4.12.3\",     \"react\": \"^17.0.1\",     \"react-dom\": \"^17.0.1\",     \"react-scripts\": \"4.0.2\",     \"web-vitals\": \"^1.1.0\"   },   \"scripts\": {     \"start\": \"react-scripts start\",     \"build\": \"react-scripts build\",     \"test\": \"react-scripts test\",     \"eject\": \"react-scripts eject\"   },   \"eslintConfig\": {     \"extends\": [       \"react-app\",       \"react-app\/jest\"     ]   },   \"browserslist\": {     \"production\": [       \"&gt;0.2%\",       \"not dead\",       \"not op_mini all\"     ],     \"development\": [       \"last 1 chrome version\",       \"last 1 firefox version\",       \"last 1 safari version\"     ]   },   \"devDependencies\": {     \"babel-plugin-import\": \"^1.13.3\"   } } <\/code><\/pre>\n<p>\u53ea\u6709\u4e00\u4e2a\u7ec4\u4ef6\uff0c\u91cc\u9762\u5f15\u5165\u90e8\u5206\u662f\u8fd9\u6837\u7684\uff1a<\/p>\n<pre><code>import { Button } from 'antd';  \/\/ \u53ea\u5f15\u5165\u8fd9\u4e2a\uff0c\u6709\u6309\u94ae\uff0c\u6ca1\u6709\u6837\u5f0f\uff0c\u800c\u4e14 console \u663e\u793a\u6ca1\u6709\u6309\u9700\u52a0\u8f7d \/\/ import 'antd\/dist\/antd.css'; \/\/ \u5f15\u5165\u6240\u6709\u6837\u5f0f \/\/ import Button from 'antd\/lib\/button'; \/\/ import 'antd\/lib\/button\/style\/css';  <\/code><\/pre>\n<p>\u8fd9\u4e1c\u897f\u5230\u5e95\u600e\u4e48\u73a9\u7684\uff0c\u53ea\u662f\u8981\u8fd9\u4e2a\u7b80\u5355\u7684\u529f\u80fd\uff0c\u7f51\u4e0a\u67e5\u5230\u7684\u90fd\u5f88\u590d\u6742\uff0c\u4e0d\u77e5\u9053\u600e\u4e48\u6539<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>13<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"5306738\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : nnnToTnnn <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             eject                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5306739\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : nnnToTnnn <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             see https:\/\/github.com\/facebook\/create-react-app#philosophy<\/p>\n<p>https:\/\/ant.design\/docs\/react\/getting-started-cn#%E6%8C%89%E9%9C%80%E5%8A%A0%E8%BD%BD                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5306740\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : mdn <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \/\/ant.design\/docs\/react\/use-with-create-react-app-cn<br \/>\u5b98\u65b9\u6587\u6863\u4e0d\u662f\u6709\u4ecb\u7ecd\u5417\uff1f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5306741\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : zxCoder <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @nnnToTnnn <br \/>@mdn <\/p>\n<p>\u6587\u6863\u770b\u4e86\u5440 \u5c31\u662f\u6309\u6587\u6863\u8bf4\u7684\u505a\uff0c\u4e0a\u9762\u5199\u4e86<\/p>\n<p>&#8220;`<br \/>\u6309\u9700\u52a0\u8f7d#<br \/>antd \u9ed8\u8ba4\u652f\u6301\u57fa\u4e8e ES modules \u7684 tree shaking\uff0c\u5bf9\u4e8e js \u90e8\u5206\uff0c\u76f4\u63a5\u5f15\u5165 import { Button } from &#8216;antd&#8217; \u5c31\u4f1a\u6709\u6309\u9700\u52a0\u8f7d\u7684\u6548\u679c\u3002<\/p>\n<p>\u5982\u679c\u4f60\u5728\u5f00\u53d1\u73af\u5883\u7684\u63a7\u5236\u53f0\u770b\u5230\u4e0b\u9762\u7684\u63d0\u793a\uff0c\u90a3\u4e48\u4f60\u53ef\u80fd\u8fd8\u5728\u4f7f\u7528 [email&#160;protected] \u6216\u8005 tree shaking \u5931\u6548\uff0c\u8bf7\u5347\u7ea7\u6216\u68c0\u67e5\u76f8\u5173\u914d\u7f6e\u3002<br \/>&#8220;`                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5306742\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : zxCoder <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @mdn \u8fd9\u4e2a\u662f\u5168\u90e8\u5f15\u5165\u5427                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5306743\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : murmur <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6309\u9700\u5f15\u5165\u6211\u8bb0\u5f97\u662f\u7528\u6ce8\u91ca\uff0c\u641c babel dynamic import\uff0c\u81f3\u5c11\u662f\u6709\u4e00\u4e2a comment:true \u7684\u9009\u9879<br \/>\u5982\u679c\u662f react \u7248\u672c\u592a\u8001\uff0c\u6709\u7b2c\u4e09\u65b9\u7ec4\u4ef6\u5b9e\u73b0\u6309\u9700\u5f15\u5165                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5306744\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : murmur <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u987a\u4fbf\uff0c\u4e3b\u8bf4\u7684\u662f\u6309\u9700\u5f15\u7528\u8fd8\u662f\u5206\u6a21\u5757\u6253\u5305\uff0c\u8fd8\u662f\u6211\u7406\u89e3\u96be\u4e86                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5306745\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : zxCoder <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @murmur \u6211\u7528\u7684\u5c31\u662f\u90a3\u4e2a\u4ec0\u4e48\u7b2c\u4e09\u65b9\u63d2\u4ef6 babel-plugin-import \u5427 \u90fd\u662f\u6309\u5b98\u65b9\u6587\u6863\u5f04\u7684\uff0c\u5c31\u662f\u8dd1\u4e0d\u8d77\u6765                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5306746\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : zxCoder <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @nnnToTnnn \u4f60\u7684\u610f\u601d\u662f\u8981 eject \u4e4b\u540e \u628a\u914d\u7f6e\u5199\u5728 config \u91cc\u9762\u5417                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5306747\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : Sapp <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6211\u8fd9\u4e48\u8bbe\u7f6e\u662f\u6210\u529f\u7684\uff0c\u6211\u731c\u5e94\u8be5\u662f\u4f60\u7684 babel \u6839\u672c\u6ca1\u751f\u6548\uff0c\u662f\u4e0d\u662f create-react \u6709\u4ed6\u81ea\u5df1\u7684 babel \u914d\u7f6e\u7ed9\u4f60\u8986\u76d6\u4e86\uff1f\u4e0a\u9762\u8bf4\u7684 eject \u5e94\u8be5\u662f\u5bf9\u7684                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5306748\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : zxCoder <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @Sapp \u5947\u4e86\u602a\u4e86\uff0c\u6362\u6210 vue \u7684 antd\uff0c\u4e5f\u662f import {Button} from &#8216;ant-design-vue&#8217;; \u7ed3\u679c\u6ca1\u914d\u7f6e babel \u4e5f\u80fd\u6309\u9700\u5f15\u5165\u3002\u3002\u3002f12 \u6ca1\u8b66\u544a\u3002\u3002\u3002\u3002\u592a\u795e\u5947\u4e86<\/p>\n<p>\u5230\u5e95\u8be5\u4e0d\u8be5\u914d\u7f6e\u8fd9\u73a9\u610f \u6211\u8fd8\u662f\u641e\u4e0d\u6e05\u695a                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5306749\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : triple7 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             cra \u4e0d\u652f\u6301\u4f60\u76f4\u63a5\u914d\u7f6e babelrc\uff0c\u6240\u4ee5\u4f60\u7684\u914d\u7f6e\u6ca1\u6709\u751f\u6548\uff0c\u4f60\u9700\u8981 eject \u66b4\u9732\u51fa\u6765\u76f4\u63a5\u4fee\u6539 webpack \u3002<\/p>\n<p>\u6216\u8005\u53ef\u4ee5\u4f7f\u7528 react-app-rewired \u548c customize-cra \u81ea\u5b9a\u4e49 webpack \u914d\u7f6e\u3002<\/p>\n<p>\u53ef\u4ee5\u53c2\u8003\u6211\u4e4b\u524d\u7684\u6587\u7ae0\uff1a https:\/\/zhuanlan.zhihu.com\/p\/96103181                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"5306750\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : zxCoder <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @triple7 \u539f\u6765\u5982\u6b64\u3002\u3002\u3002\u8fd9\u4e48\u590d\u6742                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u90a3\u4e2a\u5565 babel-plugin-&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\/344324"}],"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=344324"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/344324\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=344324"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=344324"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=344324"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}