{"id":332213,"date":"2022-12-21T06:02:46","date_gmt":"2022-12-20T22:02:46","guid":{"rendered":"https:\/\/www.idc.net\/help\/332213\/"},"modified":"2022-12-21T06:02:46","modified_gmt":"2022-12-20T22:02:46","slug":"%e4%bb%8e0%e5%88%b01%e6%9e%84%e5%bb%ba%e5%9f%ba%e4%ba%8e%e8%87%aa%e8%ba%ab%e4%b8%9a%e5%8a%a1%e7%9a%84%e5%89%8d%e7%ab%af%e5%b7%a5%e5%85%b7%e5%ba%93","status":"publish","type":"post","link":"https:\/\/idc.net\/help\/332213\/","title":{"rendered":"\u4ece0\u52301\u6784\u5efa\u57fa\u4e8e\u81ea\u8eab\u4e1a\u52a1\u7684\u524d\u7aef\u5de5\u5177\u5e93"},"content":{"rendered":"<p style=\"text-align: justify\">\u4f5c\u8005\uff1a\u4eac\u4e1c\u96f6\u552e<\/p>\n<h2>\u524d\u8a00<\/h2>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">\u2022\u5728\u5b9e\u9645\u9879\u76ee\u5f00\u53d1\u4e2d\u65e0\u8bba M \u7aef\u3001PC \u7aef\uff0c\u6216\u591a\u6216\u5c11\u90fd\u6709\u4e00\u4e2a utils \u6587\u4ef6\u76ee\u5f55\u53bb\u7ba1\u7406\u9879\u76ee\u4e2d\u7528\u5230\u7684\u4e00\u4e9b\u5e38\u7528\u7684\u5de5\u5177\u65b9\u6cd5\uff0c\u6bd4\u5982\uff1a\u65f6\u95f4\u5904\u7406\u3001\u4ef7\u683c\u5904\u7406\u3001\u89e3\u6790url\u53c2\u6570\u3001\u52a0\u8f7d\u811a\u672c\u7b49\uff0c\u5176\u4e2d\u5f88\u591a\u662f\u91cd\u590d\u3001\u57fa\u7840\u3001\u6216\u57fa\u4e8e\u67d0\u79cd\u4e1a\u52a1\u573a\u666f\u7684\u5de5\u5177\uff0c\u5b58\u5728\u9879\u76ee\u95f4\u5197\u4f59\u7684\u75db\u70b9\u4ee5\u53ca\u5de5\u5177\u65b9\u6cd5\u89c4\u8303\u4e0d\u7edf\u4e00\u7684\u95ee\u9898\u3002<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">\u2022\u5728\u5b9e\u9645\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c\u7ecf\u5e38\u4f7f\u7528\u4e00\u4e9b\u5f00\u6e90\u5de5\u5177\u5e93\uff0c\u5982 lodash\uff0c\u4ee5\u65b9\u4fbf\u3001\u5feb\u6377\u7684\u8fdb\u884c\u9879\u76ee\u5f00\u53d1\u3002\u4f46\u662f\u5f53 npm\u4e0a\u6ca1\u6709\u81ea\u5df1\u4e2d\u610f\u6216\u7b26\u5408\u81ea\u8eab\u4e1a\u52a1\u7684\u5de5\u5177\u65f6\uff0c\u6211\u4eec\u4e0d\u5f97\u4e0d\u81ea\u5df1\u52a8\u624b\uff0c\u6b64\u65f6\u62e5\u6709\u81ea\u5df1\u7684\u3001\u57fa\u4e8e\u4e1a\u52a1\u7684\u5de5\u5177\u5e93\u5c31\u663e\u5f97\u5c24\u4e3a\u91cd\u8981\u3002<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">\u2022\u6211\u4eec\u6240\u719f\u77e5\u7684Vue\u3001React\u7b49\u8bf8\u591a\u77e5\u540d\u524d\u7aef\u6846\u67b6\uff0c\u6216\u516c\u53f8\u63d0\u4f9b\u7684\u4e00\u4e9b\u7c7b\u5e93\uff0c\u5b83\u4eec\u662f\u5982\u4f55\u5f00\u53d1\u3001\u6784\u5efa\u3001\u6253\u5305\u51fa\u6765\u7684\uff0c\u672c\u6587\u5c06\u5e26\u9886\u4f60\u4e86\u89e3\u5230\u5982\u4f55\u4ece0\u52301\u6784\u5efa\u57fa\u4e8e\u81ea\u8eab\u4e1a\u52a1\u7684\u524d\u7aef\u5de5\u5177\u5e93\u3002<\/span><\/p>\n<h2>\u6784\u5efa\u5de5\u5177\u5e93\u4e3b\u6d41\u65b9\u6848<\/h2>\n<p><span style=\"color: #333333\">1. WEBPACK<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">\u2022 webpack \u63d0\u4f9b\u4e86\u6784\u5efa\u548c\u6253\u5305\u4e0d\u540c\u6a21\u5757\u5316\u89c4\u5219\u7684\u5e93\uff0c\u53ea\u662f\u9700\u8981\u81ea\u5df1\u53bb\u642d\u5efa\u5f00\u53d1\u5e95\u5c42\u67b6\u6784\u3002<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">\u2022 vue-cli\uff0c\u57fa\u4e8e webpack \uff0c vue-cli \u811a\u624b\u67b6\u5de5\u5177\u53ef\u4ee5\u5feb\u901f\u521d\u59cb\u5316\u4e00\u4e2a vue \u5e94\u7528\uff0c\u5b83\u4e5f\u53ef\u4ee5\u521d\u59cb\u5316\u4e00\u4e2a\u6784\u5efa\u5e93\u3002<\/span><\/p>\n<p><span style=\"color: #333333\">2. ROLLUP<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">\u2022 rollup \u662f\u4e00\u4e2a\u4e13\u95e8\u9488\u5bf9JavaScript\u6a21\u5757\u6253\u5305\u5668\uff0c\u53ef\u4ee5\u5c06\u5e94\u7528\u6216\u5e93\u7684\u5c0f\u5757\u4ee3\u7801\u7f16\u8bd1\u6210\u66f4\u590d\u6742\u7684\u529f\u80fd\u4ee3\u7801\u3002<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">\u2022 Vue\u3001React \u7b49\u8bb8\u591a\u6d41\u884c\u524d\u7aef\u6846\u67b6\u7684\u6784\u5efa\u548c\u6253\u5305\u90fd\u80fd\u770b\u5230 rollup \u7684\u8eab\u5f71\u3002<\/span><\/p>\n<h2>\u4e3a\u4ec0\u4e48\u91c7\u7528 ROLLUP \u800c\u4e0d\u662f WEBPACK<\/h2>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">\u2022webpack \u4e3b\u8981\u804c\u80fd\u662f\u5f00\u53d1\u5e94\u7528\uff0c\u800c rollup \u4e3b\u8981\u9488\u5bf9\u7684\u5c31\u662f js \u5e93\u7684\u5f00\u53d1\uff0c\u5982\u679c\u4f60\u8981\u5f00\u53d1 js \u5e93\uff0c\u90a3 webpack \u7684\u7e41\u7410\u914d\u7f6e\u548c\u6253\u5305\u540e\u7684\u6587\u4ef6\u4f53\u79ef\u5c31\u4e0d\u592a\u9002\u7528\u4e86\uff0c\u901a\u8fc7webpack\u6253\u5305\u6784\u5efa\u51fa\u6765\u7684\u6e90\u4ee3\u7801\u589e\u52a0\u4e86\u5f88\u591a\u5de5\u5177\u51fd\u6570\u4ee5\u5916\u7684\u6a21\u5757\u4f9d\u8d56\u4ee3\u7801\u3002<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">\u2022rollup \u53ea\u662f\u628a\u4e1a\u52a1\u4ee3\u7801\u8f6c\u7801\u6210\u76ee\u6807 js \uff0c\u5c0f\u5de7\u4e14\u8f7b\u4fbf\u3002rollup\u5bf9\u4e8e\u4ee3\u7801\u7684Tree-shaking\u548cES6\u6a21\u5757\u6709\u7740\u7b97\u6cd5\u4f18\u52bf\u4e0a\u7684\u652f\u6301\uff0c\u5982\u679c\u53ea\u60f3\u6784\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u5e93\uff0c\u5e76\u4e14\u662f\u57fa\u4e8eES6\u5f00\u53d1\u7684\uff0c\u52a0\u4e0a\u5176\u7b80\u6d01\u7684API\uff0crollup\u5f97\u5230\u66f4\u591a\u5f00\u53d1\u8005\u7684\u9752\u7750\u3002<\/span><\/p>\n<h2>\u5de5\u5177\u5e93\u5e95\u5c42\u67b6\u6784\u8bbe\u8ba1<\/h2>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">\u6784\u5efa\u5de5\u5177\u5e93\u5e95\u5c42\u67b6\u6784\u5927\u6982\u9700\u8981\u54ea\u4e9b\u529f\u80fd\u7684\u652f\u6301\uff1a<\/span><\/p>\n<p style=\"text-align:center\">\n<h2>\u67b6\u6784\u4f9d\u8d56\u9700\u77e5<\/h2>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">\u5728\u5bf9\u5e95\u5c42\u67b6\u6784\u8bbe\u8ba1\u7684\u57fa\u7840\u4e0a\uff0c\u9996\u5148\u9700\u8981\u628a\u7528\u5230\u7684\u4f9d\u8d56\u5e93\u7b80\u5355\u719f\u6089\u4e00\u4e0b\uff1a<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"color: #333333\"><strong>rollup \u5168\u5bb6\u6876<\/strong><\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">\u2022\uf06c rollup\uff08\u5de5\u5177\u5e93\u6253\u5305\u6784\u5efa\u6838\u5fc3\u5305\uff09<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">\u2022\uf06c rollup-plugin-livereload\uff08rollup \u63d2\u4ef6\uff0c\u70ed\u66f4\u65b0\uff0c\u65b9\u4fbf\u672c\u5730 debugger \u5f00\u53d1\uff09<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">\u2022\uf06c rollup-plugin-serve\uff08rollup \u63d2\u4ef6\uff0c\u672c\u5730\u670d\u52a1\u4ee3\u7406\uff0c\u65b9\u4fbf\u5728\u672c\u5730 html \u4e2d\u8c03\u8bd5\u5de5\u5177\uff09<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">\u2022\uf06c rollup-plugin-terser\uff08rollup \u63d2\u4ef6\uff0c\u4ee3\u7801\u538b\u7f29\u6df7\u6dc6\uff09<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">\u2022\uf06c rollup-plugin-visualizer\uff08rollup \u63d2\u4ef6\uff0c\u53ef\u89c6\u5316\u5e76\u5206\u6790 Rollup bundle\uff0c\u4ee5\u67e5\u770b\u6a21\u5757\u5360\u7528\uff09<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">\u2022\uf06c @rollup\/plugin-babel\uff08rollup \u63d2\u4ef6\uff0crollup \u7684 babel \u63d2\u4ef6\uff0cES6 \u8f6c ES5\uff09<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">\u2022\uf06c @rollup\/plugin-commonjs\uff08rollup \u63d2\u4ef6\uff0c\u7528\u6765\u5c06 CommonJS \u6a21\u5757\u8f6c\u6362\u4e3a ES6\uff0c\u8fd9\u6837\u5b83\u4eec\u5c31\u53ef\u4ee5\u5305\u542b\u5728 Rollup \u5305\u4e2d\uff09<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">\u2022\uf06c @rollup\/plugin-json\uff08rollup \u63d2\u4ef6\uff0c\u5b83\u5c06.json \u6587\u4ef6\u8f6c\u6362\u4e3a ES6 \u6a21\u5757\uff09<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">\u2022\uf06c @<br \/>rollup\/plugin-node-resolve\uff08rollup \u63d2\u4ef6\uff0c\u5b83\u4f7f\u7528\u8282\u70b9\u89e3\u6790\u7b97\u6cd5\u5b9a\u4f4d\u6a21\u5757\uff0c\u7528\u4e8e\u5728\u8282\u70b9\u6a21\u5757\u4e2d\u4f7f\u7528\u7b2c\u4e09\u65b9 node_modules \u5305\uff09<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">\u2022\uf06c @rollup\/plugin-typescript\uff08rollup \u63d2\u4ef6\uff0c\u5bf9 typescript \u7684\u652f\u6301\uff0c\u5c06 typescript \u8fdb\u884c tsc \u8f6c\u4e3a js\uff09<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"color: #333333\"><strong>typescript \u76f8\u5173<\/strong><\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">\u2022\uf06c typescript\uff08\u4f7f\u7528 ts \u5f00\u53d1\u5de5\u5177\u5e93\uff09<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">\u2022\uf06c tslib\uff08TypeScript \u7684\u8fd0\u884c\u5e93\uff0c\u5b83\u5305\u542b\u4e86 TypeScript \u6240\u6709\u7684\u5e2e\u52a9\u51fd\u6570\uff09<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">\u2022\uf06c @<br \/>typescript-eslint\/eslint-plugin\uff08TypeScript \u7684 eslint \u63d2\u4ef6\uff0c\u7ea6\u675f ts \u4e66\u5199\u89c4\u8303\uff09<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">\u2022\uf06c @typescript-eslint\/parser\uff08ESLint \u89e3\u6790\u5668\uff0c\u5b83\u5229\u7528 TypeScript ESTree \u6765\u5141\u8bb8 ESLint \u68c0\u6d4b TypeScript \u6e90\u4ee3\u7801\uff09<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"color: #333333\"><strong>\u6587\u6863\u76f8\u5173<\/strong><\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">\u2022\uf06c typedoc\uff08TypeScript \u9879\u76ee\u7684\u6587\u6863\u751f\u6210\u5668\uff09<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">\u2022\uf06c gulp\uff08\u4f7f\u7528 gulp \u6784\u5efa\u6587\u6863\u7cfb\u7edf\uff09<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">\u2022\uf06c gulp-typedoc\uff08Gulp \u63d2\u4ef6\u6765\u6267\u884c TypeDoc \u5de5\u5177\uff09<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">\u2022\uf06c browser-sync\uff08\u6587\u6863\u7cfb\u7edf\u70ed\u66f4\u65b0\uff09<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"color: #333333\"><strong>\u5355\u5143\u6d4b\u8bd5\u76f8\u5173<\/strong><\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">\u2022\uf06c jest\uff08\u4e00\u6b3e\u4f18\u96c5\u3001\u7b80\u6d01\u7684 JavaScript \u6d4b\u8bd5\u6846\u67b6\uff09<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">\u2022\uf06c @types\/jest\uff08Jest \u7684\u7c7b\u578b\u5b9a\u4e49\uff09<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">\u2022\uf06c ts-jest\uff08\u4e00\u4e2a\u652f\u6301\u6e90\u6620\u5c04\u7684 Jest \u8f6c\u6362\u5668\uff0c\u5141\u8bb8\u60a8\u4f7f\u7528 Jest \u6765\u6d4b\u8bd5\u7528 TypeScript \u7f16\u5199\u7684\u9879\u76ee\uff09<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">\u2022\uf06c @babel\/preset-typescript\uff08TypeScript \u7684 Babel \u9884\u8bbe\uff09<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"color: #333333\"><strong>\u5176\u4ed6\u4f9d\u8d56<\/strong><\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">\u2022\uf06c eslint\uff08\u4ee3\u7801\u89c4\u8303\u7ea6\u675f\uff09<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">\u2022\uf06c @babel\/core\uff08@rollup\/plugin-babel \u4f9d\u8d56\u7684 babel \u89e3\u6790\u63d2\u4ef6\uff09<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">\u2022\uf06c @<br \/>babel\/plugin-transform-runtime\uff08babel \u8f6c\u8bd1\u4f9d\u8d56\uff09<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">\u2022\uf06c @babel\/preset-env\uff08babel \u8f6c\u8bd1\u4f9d\u8d56\uff09<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">\u2022\uf06c chalk\uff08\u63a7\u5236\u53f0\u5b57\u7b26\u6837\u5f0f\uff09<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">\u2022\uf06c rimraf\uff08UNIX \u547d\u4ee4 rm -rf \u7528\u4e8e node\uff09<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">\u2022\uf06c cross-env\uff08\u8de8\u5e73\u53f0\u8bbe\u7f6e node \u73af\u5883\u53d8\u91cf\uff09<\/span><\/p>\n<h2>\u5e95\u5c42\u67b6\u6784\u642d\u5efa<\/h2>\n<h2>1. \u521d\u59cb\u5316\u9879\u76ee<\/h2>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">\u65b0\u5efa\u4e00\u4e2a\u6587\u4ef6\u5939 utils-demo\uff0c\u6267\u884c npm init\uff0c\u8fc7\u7a0b\u4f1a\u8be2\u95ee\u6784\u5efa\u9879\u76ee\u7684\u57fa\u672c\u4fe1\u606f\uff0c\u6309\u9700\u586b\u5199\u5373\u53ef\uff1a<\/span><\/p>\n<pre><code>npm init<\/code><\/pre>\n<h2>2. \u7ec4\u7ec7\u5de5\u5177\u5e93\u4e1a\u52a1\u5f00\u53d1 SRC \u76ee\u5f55\u7ed3\u6784<\/h2>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">\u521b\u5efa\u5de5\u5177\u5e93\u4e1a\u52a1\u5f00\u53d1 src \u6587\u4ef6\u76ee\u5f55\uff0c\u660e\u786e\u600e\u6837\u89c4\u5212\u5de5\u5177\u5e93\u5305\uff0c\u91cc\u9762\u653e\u7f6e\u7684\u662f\u5de5\u5177\u5e93\u5f00\u53d1\u9700\u8981\u7684\u4e1a\u52a1\u4ee3\u7801\uff1a<\/span><\/p>\n<p style=\"text-align:center\">\n<h2>3. \u5b89\u88c5\u9879\u76ee\u4f9d\u8d56<\/h2>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">\u8981\u5bf9 typescript \u4ee3\u7801\u8fdb\u884c\u89e3\u6790\u652f\u6301\u9700\u8981\u5b89\u88c5\u5bf9 ts \u652f\u6301\u7684\u4f9d\u8d56\uff0c\u4ee5\u53ca\u5bf9\u5f00\u53d1\u7684\u5de5\u5177\u7684\u4e00\u4e9b\u4f9d\u8d56\u5305\uff1a<\/span><\/p>\n<pre><code>yarn add typescript tslib rollup rollup-plugin-livereload rollup-plugin-serve rollup-plugin-terser rollup-plugin-visualizer \n@rollup\/plugin-babel @rollup\/plugin-commonjs @rollup\/plugin-json @rollup\/plugin-node-resolve @rollup\/plugin-typescript \n@babel\/core @babel\/plugin-transform-runtime @babel\/preset-env rimraf lodash chalk@^4.1.2 -D<\/code><\/pre>\n<p style=\"text-align: justify\"><span style=\"color: #cf1322\">\u8fd9\u91cc\u9047\u5230\u4e00\u4e2a\u5751\uff0c\u5173\u4e8e\u6700\u65b0 chalk5.0.0 \u4e0d\u652f\u6301\u5728 nodejs \u4e2d require()\u5bfc\u5165\uff0c\u6240\u4ee5\u9501\u5b9a\u5305\u7248\u672c chalk@^4.1.2<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">\u8981\u5bf9 typescript \u8fdb\u884c\u89e3\u6790\u548c\u7f16\u8bd1\u8fd8\u9700\u8981\u914d\u7f6e tsconfig.json\uff0c\u8be5\u6587\u4ef6\u4e2d\u6307\u5b9a\u4e86\u7528\u6765\u7f16\u8bd1\u8fd9\u4e2a\u9879\u76ee\u7684\u6839\u6587\u4ef6\u548c\u7f16\u8bd1\u9009\u9879\uff0c\u5728\u9879\u76ee\u6839\u76ee\u5f55\uff0c\u4f7f\u7528 tsc --init \u547d\u4ee4\u5feb\u901f\u751f\u6210 tsconfig.json \u6587\u4ef6\uff08\u524d\u63d0\u5168\u5c40\u5b89\u88c5 typescript\uff09<\/span><\/p>\n<pre><code>npm i typescript -g\ntsc --init<\/code><\/pre>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">\u521d\u59cb\u5316 tsconfig \u5b8c\u6210\u4e4b\u540e\uff0c\u6839\u76ee\u5f55\u81ea\u52a8\u751f\u6210 tsconfig.json \u6587\u4ef6\uff0c\u9700\u8981\u5bf9\u5176\u8fdb\u884c\u7b80\u5355\u7684\u914d\u7f6e\uff0c\u4ee5\u9002\u7528\u4e8e ts \u9879\u76ee\uff0c\u5176\u4e2d\u5177\u4f53\u542b\u4e49\u53ef\u4ee5\u53c2\u8003tsconfig.json\u5b98\u7f51<\/span><\/p>\n<h2>4. \u7ec4\u7ec7\u9879\u76ee\u6253\u5305\u6784\u5efa SCRIPTS \u76ee\u5f55\u7ed3\u6784<\/h2>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">1) \u6839\u76ee\u5f55\u521b\u5efa\u9879\u76ee\u6253\u5305\u6784\u5efa scripts \u811a\u672c\u6587\u4ef6\u76ee\u5f55\uff0c\u91cc\u9762\u653e\u7f6e\u7684\u662f\u6709\u5173\u4e8e\u9879\u76ee\u6253\u5305\u6784\u5efa\u9700\u8981\u7684\u6587\u4ef6\uff1a<\/span><\/p>\n<p style=\"text-align:center\">\n<p style=\"text-align: justify\"><span style=\"color: #333333\">\u751f\u6210rollup\u914d\u7f6e\u9879\u51fd\u6570\u6838\u5fc3\u4ee3\u7801\uff1a<\/span><\/p>\n<pre><code>const moduleName = camelCase(name) \/\/ \u5f53format\u4e3aiife\u548cumd\u65f6\u5fc5\u987b\u63d0\u4f9b\uff0c\u5c06\u4f5c\u4e3a\u5168\u5c40\u53d8\u91cf\u6302\u5728window\u4e0b\uff1awindow.moduleName=...\nconst banner = generateBanner() \/\/ \u5305\u8bf4\u660e\u6587\u6848\n\/\/ \u751f\u6210rollup\u914d\u7f6e\u6587\u4ef6\u51fd\u6570\nconst generateConfigs = (options) =&gt; {\n  const { input, outputFile } = options\n  console.log(chalk.greenBright(`\u83b7\u53d6\u6253\u5305\u5165\u53e3\uff1a${input}`))\n  const result = []\n  const pushPlugins = ({ format, plugins, ext }) =&gt; {\n    result.push({\n      input, \/\/ \u6253\u5305\u5165\u53e3\u6587\u4ef6\n      external: [], \/\/ \u5982\u679c\u6253\u5305\u51fa\u6765\u7684\u6587\u4ef6\u6709\u9879\u76ee\u4f9d\u8d56\uff0c\u53ef\u4ee5\u5728\u8fd9\u91cc\u914d\u7f6e\u662f\u5426\u5c06\u9879\u76ee\u4f9d\u8d56\u4e00\u8d77\u6253\u5230\u5305\u91cc\u9762\u8fd8\u662f\u4f5c\u4e3a\u5916\u90e8\u4f9d\u8d56\n      \/\/ \u6253\u5305\u51fa\u53e3\u6587\u4ef6\n      output: {\n        file: `${outputFile}${ext}`, \/\/ \u51fa\u53e3\u6587\u4ef6\u540d\u79f0\n        sourcemap: true, \/\/ \/\/ \u662f\u5426\u751f\u6210sourcemap\n        format, \/\/ \u6253\u5305\u7684\u6a21\u5757\u5316\u683c\u5f0f\n        name: moduleName, \/\/ \u5f53format\u4e3aiife\u548cumd\u65f6\u5fc5\u987b\u63d0\u4f9b\uff0c\u5c06\u4f5c\u4e3a\u5168\u5c40\u53d8\u91cf\u6302\u5728window\u4e0b\uff1awindow.moduleName=...\n        exports: 'named' \/** Disable warning for default imports *\/,\n        banner, \/\/ \u6253\u5305\u51fa\u6765\u7684\u6587\u4ef6\u5728\u6700\u9876\u90e8\u7684\u8bf4\u660e\u6587\u6848\n        globals: {} \/\/ \u5982\u679cexternal\u8bbe\u7f6e\u4e86\u6253\u5305\u5ffd\u7565\u7684\u9879\u76ee\u4f9d\u8d56\uff0c\u5728\u6b64\u914d\u7f6e\uff0c\u9879\u76ee\u4f9d\u8d56\u7684\u5168\u5c40\u53d8\u91cf\n      },\n      plugins \/\/ rollup\u63d2\u4ef6\n    })\n  }\n  buildType.forEach(({ format, ext }) =&gt; {\n    let plugins = [...defaultPlugins]\n    \/\/ \u751f\u4ea7\u73af\u5883\u52a0\u5165\u5305\u5206\u6790\u4ee5\u53ca\u4ee3\u7801\u538b\u7f29\n    plugins = [\n      ...plugins,\n      visualizer({\n        gzipSize: true,\n        brotliSize: true\n      }),\n      terser()\n    ]\n\n    pushPlugins({ format, plugins, ext })\n  })\nreturn result\n}<\/code><\/pre>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">2) rollup \u5728\u6253\u5305\u6784\u5efa\u7684\u8fc7\u7a0b\u4e2d\u9700\u8981\u8fdb\u884c babel \u7684\u8f6c\u8bd1\uff0c\u9700\u8981\u5728\u6839\u76ee\u5f55\u6dfb\u52a0.babelrc \u6587\u4ef6\u544a\u77e5 babel\uff1a<\/span><\/p>\n<pre><code>{\n  \"presets\": [\n    [\n      \"@babel\/preset-env\"\n    ]\n  ],\n  \"plugins\": [\"@babel\/plugin-transform-runtime\"]\n}<\/code><\/pre>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">3) \u6b64\u65f6\u8ddd\u79bb\u6253\u5305\u6784\u5efa\u5de5\u5177\u5e93\u53ea\u5dee\u4e00\u6b65\u4e4b\u9065\uff0c\u914d\u7f6e\u6253\u5305\u811a\u672c\u547d\u4ee4\uff0c\u5728 package.json \u4e2d\u914d\u7f6e\u547d\u4ee4\uff1a<\/span><\/p>\n<pre><code>\"scripts\": {\n    \"build\": \"rimraf lib &amp;&amp; rollup -c .\/scripts\/rollup.config.js\" \/\/ rollup\u6253\u5305\n },<\/code><\/pre>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">4) \u6267\u884c yarn build\uff0c\u6839\u76ee\u5f55\u4f1a\u6784\u5efa\u51fa\u4e00\u4e2a lib \u6587\u4ef6\u5939\uff0c\u91cc\u9762\u6709\u6253\u5305\u6784\u5efa\u7684\u6587\u4ef6\uff0c\u8fd8\u591a\u4e86\u4e00\u4e2a stats.html\uff0c\u8fd9\u4e2a\u662f\u53ef\u89c6\u5316\u5e76\u5206\u6790 Rollup bundle\uff0c\u7528\u6765\u67e5\u770b\u5de5\u5177\u6a21\u5757\u5360\u7528\u7a7a\u95f4\uff1a<\/span><\/p>\n<p style=\"text-align:center\">\n<\/p>\n<h2>\u67b6\u6784\u642d\u5efa\u4f18\u5316<\/h2>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">\u9879\u76ee\u642d\u5efa\u5230\u8fd9\u91cc\uff0c\u4e0d\u77e5\u673a\u667a\u7684\u4f60\u80fd\u5426\u53d1\u73b0\u95ee\u9898\uff1a<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">1) \u53ea\u8981\u6dfb\u52a0\u4e86\u4e00\u4e2a\u5de5\u5177\uff0c\u5c31\u8981\u5728\u5165\u53e3\u6587\u4ef6\u5bfc\u51fa\u9700\u8981\u6253\u5305\u6784\u5efa\u7684\u5de5\u5177\uff0c\u5728\u591a\u4eba\u5f00\u53d1\u63d0\u4ea4\u4ee3\u7801\u7684\u65f6\u5019\u5c06\u5f15\u6765\u51b2\u7a81\u7684\u4ea7\u751f\uff1a<\/span><\/p>\n<p style=\"text-align:center\">\n<p style=\"text-align: justify\"><span style=\"color: #333333\">2) \u4f7f\u7528\u5de5\u5177\u5e93\u7684\u65f6\u5019\uff0c\u6309\u9700\u5f15\u7528\u7684\u9897\u7c92\u5ea6\u592a\u7ec6\u4e86\uff0c\u4e0d\u80fd\u6ee1\u8db3\u4e00\u4e9b\u8981\u6c42\u9897\u7c92\u5ea6\u7c97\u7684\u670b\u53cb\uff0c\u6bd4\u5982\uff1a<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">\u2022\uf06c \u6211\u60f3\u4f7f\u7528\u8be5\u5305\u91cc\u9762 date \u76f8\u5173\u5de5\u5177\uff0c\u8981\u8fd9\u6837\u5417\uff1f<\/span><\/p>\n<pre><code>import { dateA, dateB, dateC } from \"utils-demo\"<\/code><\/pre>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">\u80fd\u4e0d\u80fd\u8fd9\u6837\uff1f<\/span><\/p>\n<pre><code>import { date } from \"utils-demo\"\ndate.dateA()\ndate.dateB()\ndate.dateC()<\/code><\/pre>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">\u2022\uf06c \u5728\u4e00\u4e9b\u4f7f\u7528 script \u811a\u672c\u5f15\u5165\u7684\u573a\u666f\u4e0b\uff0c\u5c31\u4ec5\u4ec5\u9700\u8981 date \u76f8\u5173\u7684\u5de5\u5177\uff0c\u8981\u8fd9\u6837\u5417\uff1f<\/span><\/p>\n<pre><code>&lt;script src=\"https:\/\/xxx\/main.min.js\"&gt;<\/code><\/pre>\n<p style=\"text-align: justify\"><span style=\"color: #000000\">\u80fd\u4e0d\u80fd\u8fd9\u6837\uff1f<\/span><\/p>\n<pre><code>&lt;script src=\"https:\/\/xxx\/date.min.js\"&gt;<\/code><\/pre>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">\u8fd9\u6837\u4ec5\u4ec5\u4f7f\u7528 date \u91cc\u9762\u7684\u5de5\u5177\uff0c\u5c31\u6ca1\u6709\u5fc5\u8981\u5c06\u6240\u6709\u7684\u5de5\u5177\u90fd\u5f15\u5165\u4e86<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">\u89e3\u51b3\u65b9\u6848\uff1a<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">1) \u9488\u5bf9\u7b2c\u4e00\u4e2a\u4ee3\u7801\u51b2\u7a81\u7684\u95ee\u9898\uff0c\u53ef\u4ee5\u6839\u636e src &gt; modules \u4e0b\u76ee\u5f55\u7ed3\u6784\u81ea\u52a8\u751f\u6210\u5165\u53e3\u6587\u4ef6 index.ts<\/span><\/p>\n<p style=\"text-align:center\">\n<\/p>\n<p><span style=\"color: #333333\">\u81ea\u52a8\u6784\u5efa\u5165\u53e3\u6587\u4ef6\u6838\u5fc3\u4ee3\u7801\uff1a<\/span><\/p>\n<pre><code>const fs = require('fs') \/\/ node fs\u6a21\u5757\nconst chalk = require('chalk') \/\/ \u81ea\u5b9a\u4e49\u8f93\u51fa\u6837\u5f0f\nconst { resolveFile, getEntries } = require('.\/utils')\nlet srcIndexContent = `\n\/\/ tips\uff1a\u6b64\u6587\u4ef6\u662f\u81ea\u52a8\u751f\u6210\u7684\uff0c\u65e0\u9700\u624b\u52a8\u6dfb\u52a0\n`\ngetEntries(resolveFile('src\/modules\/*')).forEach(({ baseName, entry }) =&gt; {\n  let moduleIndexContent = `\n\/\/ tips\uff1a\u6b64\u6587\u4ef6\u662f\u81ea\u52a8\u751f\u6210\u7684\uff0c\u65e0\u9700\u624b\u52a8\u6dfb\u52a0\n`\n  try {\n    \/\/ \u5224\u65ad\u662f\u5426\u6587\u4ef6\u5939\n    const stats = fs.statSync(entry)\n    if (stats.isDirectory()) {\n      getEntries(`${entry}\/*.ts`).forEach(({ baseName }) =&gt; {\n        baseName = baseName.split('.')[0]\n        if (baseName.indexOf('index') === -1) {\n          moduleIndexContent += `\nexport * from '.\/${baseName}'\n`\n        }\n      })\n      fs.writeFileSync(`${entry}\/index.ts`, moduleIndexContent, 'utf-8')\n      srcIndexContent += `\nexport * from '.\/modules\/${baseName}'\nexport * as ${baseName} from '.\/modules\/${baseName}'\n`\n    } else {\n      srcIndexContent += `\nexport * from '.\/modules\/${baseName.split('.')[0]}'\n`\n    }\n  } catch (e) {\n    console.error(e)\n  }\n})\nfs.writeFileSync(resolveFile('src\/index.ts'), srcIndexContent, 'utf-8')<\/code><\/pre>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">2) \u9488\u5bf9\u9897\u7c92\u5ea6\u7684\u95ee\u9898\uff0c\u53ef\u4ee5\u5c06 modules \u4e0b\u5404\u79cd\u7c7b\u578b\u5de5\u5177\u6587\u4ef6\u5939\u4e0b\u9762\u4e5f\u81ea\u52a8\u751f\u6210\u5165\u53e3\u6587\u4ef6\uff0c\u9664\u4e86\u5168\u90e8\u5bfc\u51fa\uff0c\u518d\u8ffd\u52a0 import * as \u6a21\u5757\u7c7b\u540d\u79f0 \u7c7b\u578b\u7684\u5bfc\u51fa<\/span><\/p>\n<p style=\"text-align:center\">\n<\/p>\n<p><span style=\"color: #000000\">\u81f3\u6b64\uff0c\u57fa\u672c\u4e0a\u89e3\u51b3\u4e86\u5de5\u5177\u5e93\u6253\u5305\u7684\u95ee\u9898\uff0c\u4f46\u662f\u67b6\u6784\u4e2d\u8fd8\u7f3a\u5c11\u672c\u5730\u5f00\u53d1\u8c03\u8bd5\u7684\u73af\u5883\uff0c\u4e0b\u9762\u4e3a\u5927\u5bb6\u4ecb\u7ecd\u5982\u4f55\u67b6\u6784\u4e2d\u6dfb\u52a0\u672c\u5730\u5f00\u53d1\u8c03\u8bd5\u7684\u7cfb\u7edf\u3002<\/span><\/p>\n<h2>\u672c\u5730\u5f00\u53d1\u8c03\u8bd5\u7cfb\u7edf<\/h2>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">\u9996\u5148\u8981\u660e\u786e\u8981\u52a0\u5165\u672c\u5730\u5f00\u53d1\u8c03\u8bd5\u7cfb\u7edf\u7684\u652f\u6301\uff0c\u9700\u8981\u505a\u5230\u4ee5\u4e0b\uff1a<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">\u2022\uf06c \u8de8\u5e73\u53f0\uff08window\u4e0d\u652f\u6301NODE_ENV=xxx\uff09\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\uff0c\u6839\u636e\u73af\u5883\u914d\u7f6e\u4e0d\u540c\u7684 rollup \u914d\u7f6e\u9879<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">\u2022\uf06c \u5f15\u5165\u672c\u5730\u5f00\u53d1\u9700\u8981\u7684 html \u9759\u6001\u670d\u52a1\u5668\u73af\u5883\uff0c\u5e76\u80fd\u505a\u5230\u70ed\u66f4\u65b0<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">1) \u8de8\u5e73\u53f0\u8bbe\u7f6e\u73af\u5883\u53d8\u91cf\u5f88\u7b80\u5355\uff0c\u4f7f\u7528 cross-env \u6307\u5b9a node \u7684\u73af\u5883<\/span><\/p>\n<pre><code>yarn add cross-env -D<\/code><\/pre>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">2) \u914d\u7f6e package.json \u547d\u4ee4<\/span><\/p>\n<pre><code>\"scripts\": {\n    \"entry\": \"node .\/scripts\/build-entry.js\",\n    \"dev\": \"rimraf lib &amp;&amp; yarn entry &amp;&amp; cross-env NODE_ENV=development rollup -w -c .\/scripts\/rollup.config.js\", \/\/ -w \u8868\u793a\u76d1\u542c\u7684\u5de5\u5177\u6a21\u5757\u7684\u4fee\u6539\n    \"build\": \"rimraf lib &amp;&amp; yarn entry &amp;&amp; cross-env NODE_ENV=production rollup -c .\/scripts\/rollup.config.js\"\n  },<\/code><\/pre>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">3) \u6839\u636e\u6700\u5f00\u59cb\u67b6\u6784\u8bbe\u8ba1\u7684\u6a21\u5757\uff0c\u5728\u9879\u76ee\u6839\u76ee\u5f55\u65b0\u5efa debugger \u6587\u4ef6\u5939\uff0c\u91cc\u9762\u5b58\u653e\u7684\u662f\u5de5\u5177\u8c03\u8bd5\u7684 html \u9759\u6001\u9875\u9762<\/span><\/p>\n<p style=\"text-align:center\">\n<\/p>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">4) \u63a5\u4e0b\u6765\u5c31\u662f\u914d\u7f6e scripts &gt; rollup.config.js \uff0c\u5c06 NODE_ENV=development \u73af\u5883\u52a0\u5165 rollup \u914d\u7f6e\uff0c\u4fee\u6539\u751f\u6210rollup\u914d\u7f6e\u9879\u51fd\u6570\u6838\u5fc3\u4ee3\u7801\uff1a<\/span><\/p>\n<pre><code>(isProd ? buildType : devType).forEach(({ format, ext }) =&gt; {\n    let plugins = [...defaultPlugins]\n    if (isProd) {\n      \/\/ \u751f\u4ea7\u73af\u5883\u52a0\u5165\u5305\u5206\u6790\u4ee5\u53ca\u4ee3\u7801\u538b\u7f29\n      plugins = [...plugins, visualizer({\n        gzipSize: true,\n        brotliSize: true\n      }), terser()]\n    } else {\n      \/\/ \u975e\u751f\u4ea7\u73af\u5883\u52a0\u5165\u70ed\u66f4\u65b0\u548c\u672c\u5730\u670d\u52a1\u63d2\u4ef6\uff0c\u65b9\u4fbf\u672c\u5730debugger\n      plugins = [...plugins, ...[\n        \/\/ \u70ed\u66f4\u65b0\n        rollUpLiveLoad({\n          watch: ['debugger', 'lib'],\n          delay: 300\n        }),\n        \/\/ \u672c\u5730\u670d\u52a1\u4ee3\u7406\n        rollupServe({\n          open: true,\n          \/\/ resolveFile('')\u4ee3\u7406\u6839\u76ee\u5f55\u539f\u56e0\u662f\u4e3a\u4e86\u5728ts\u4ee3\u7801\u91ccdebugger\u65f6\u53ef\u4ee5\u65b9\u4fbf\u770b\u5230\u8c03\u8bd5\u4fe1\u606f\n          contentBase: [resolveFile('debugger'), resolveFile('lib'), resolveFile('')]\n        })\n      ]]\n    }\n    pushPlugins({ format, plugins, ext })\n  })<\/code><\/pre>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">5) \u6267\u884c yarn dev \u4e4b\u540e\u6d4f\u89c8\u5668\u4f1a\u65b0\u6253\u5f00\u7a97\u53e3\uff0c\u8f93\u5165\u521a\u6dfb\u52a0\u7684\u5de5\u5177\u94fe\u63a5\uff0c\u5e76\u4e14\u5b83\u662f\u70ed\u66f4\u65b0\u7684\uff1a<\/span><\/p>\n<p style=\"text-align:center\">\n<\/p>\n<hr>\n<p style=\"text-align: justify\"><\/p>\n<h2>\u5de5\u5177\u5e93\u6587\u6863\u7cfb\u7edf<\/h2>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">\u4e00\u4e2a\u5b8c\u5907\u7684\u5de5\u5177\u5e93\u9700\u8981\u6709\u4e00\u4e2a\u6587\u6863\u6765\u5c55\u793a\u5f00\u53d1\u7684\u5de5\u5177\u51fd\u6570\uff0c\u5b83\u53ef\u80fd\u9700\u8981\u5177\u5907\u4ee5\u4e0b\u51e0\u70b9\u652f\u6301\uff1a<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">\u2022\uf06c \u652f\u6301\u5de5\u5177\u5e93\u4e2d\u65b9\u6cd5\u7684\u53ef\u89c6\u5316\u9884\u89c8<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">\u2022\uf06c \u652f\u6301\u4fee\u6539\u5de5\u5177\u7684\u65f6\u5019\uff0c\u5177\u5907\u70ed\u66f4\u65b0\u673a\u5236<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">typedoc\uff08TypeScript \u9879\u76ee\u7684\u6587\u6863\u751f\u6210\u5668\uff09\u80fd\u5b8c\u7f8e\u652f\u6301 typescript \u5f00\u53d1\u5de5\u5177\u5e93\u7684\u6587\u6863\u751f\u6210\u5668\u7684\u652f\u6301\uff0c\u5b83\u7684\u6838\u5fc3\u539f\u7406\u5c31\u662f\u8bfb\u53d6\u6e90\u4ee3\u7801\uff0c\u6839\u636e\u5de5\u5177\u7684\u6ce8\u91ca\u3001ts\u7684\u7c7b\u578b\u89c4\u8303\u7b49\uff0c\u81ea\u52a8\u751f\u6210\u6587\u6863\u9875\u9762<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">\u5173\u4e8e\u70ed\u66f4\u65b0\u673a\u5236\u7684\u652f\u6301\uff0c\u7b2c\u4e00\u4e2a\u81ea\u7136\u60f3\u5230 browser-sync\uff08\u6587\u6863\u7cfb\u7edf\u70ed\u66f4\u65b0\uff09<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">\u7531\u4e8e\u6587\u6863\u7cfb\u7edf\u7684\u9884\u89c8\u529f\u80fd\u6709\u5f88\u591a\u63d2\u4ef6\u7ec4\u5408\u6765\u5b9e\u73b0\u7684\uff0c\u53ef\u4ee5\u501f\u52a9 gulp \uff08\u57fa\u4e8e\u6d41\u7684\u81ea\u52a8\u5316\u6784\u5efa\u5de5\u5177\uff09\uff0ctypedoc\u6b63\u597d\u6709\u5bf9\u5e94\u7684 gulp-typedocGulp \u63d2\u4ef6\u6765\u6267\u884c TypeDoc \u5de5\u5177\u63d2\u4ef6<\/span><\/p>\n<p style=\"text-align:center\">\n<\/p>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">\u6784\u5efa\u5b8c\u6210\u540e\u6253\u5f00\u6587\u6863\u7cfb\u7edf\uff0c\u5e76\u4e14\u5b83\u662f\u70ed\u66f4\u65b0\u7684\uff0c\u4fee\u6539\u5de5\u5177\u65b9\u6cd5\u540e\u81ea\u52a8\u66f4\u65b0\u6587\u6863\uff1a<\/span><\/p>\n<p style=\"text-align:center\">\n<h2>\u5355\u5143\u6d4b\u8bd5<\/h2>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">\u4e3a\u786e\u4fdd\u7528\u6237\u4f7f\u7528\u7684\u5de5\u5177\u4ee3\u7801\u7684\u5b89\u5168\u6027\u3001\u6b63\u786e\u6027\u4ee5\u53ca\u53ef\u9760\u6027\uff0c\u5de5\u5177\u5e93\u7684\u5355\u5143\u6d4b\u8bd5\u5fc5\u4e0d\u53ef\u5c11\u3002\u5355\u5143\u6d4b\u8bd5\u9009\u7528\u7684\u662f Facebook \u51fa\u54c1\u7684 Jest \u6d4b\u8bd5\u6846\u67b6\uff0c\u5b83\u5bf9\u4e8e TypeScript \u6709\u5f88\u597d\u7684\u652f\u6301\u3002<\/span><\/p>\n<h2>1. \u73af\u5883\u642d\u5efa<\/h2>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">1) \u9996\u5148\u5168\u5c40\u5b89\u88c5 jest \u4f7f\u7528 init \u6765\u521d\u59cb\u5316 jest \u914d\u7f6e\u9879<\/span><\/p>\n<pre><code>npm jest -g\njest --init\n\u4e0b\u9762\u662f\u672c\u4eba\u8bbe\u7f6e\u7684jest\u7684\u914d\u7f6e\u9879\n Would you like to use Jest when running \"test\" script in \"package.json\"? \u2026 yes\n Would you like to use Typescript for the configuration file? \u2026 yes\n Choose the test environment that will be used for testing \u203a jsdom (browser-like)\n Do you want Jest to add coverage reports? \u2026 yes\n Which provider should be used to instrument code for coverage? \u203a babel\n Automatically clear mock calls, instances and results before every test? \u2026 yes<\/code><\/pre>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">\u6267\u884c\u5b8c\u4e4b\u540e\u6839\u76ee\u5f55\u4f1a\u81ea\u52a8\u751f\u6210jest.config.ts \u6587\u4ef6\uff0c\u91cc\u9762\u8bbe\u7f6e\u4e86\u5355\u5143\u6d4b\u8bd5\u7684\u914d\u7f6e\u89c4\u5219\uff0cpackage.json \u91cc\u9762\u4e5f\u591a\u4e86\u4e00\u4e2a script \u6307\u4ee4 test\u3002<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">2) \u5173\u4e8ejest.config.js\u6587\u4ef6\u914d\u7f6e\u9879\u5177\u4f53\u542b\u4e49\u53ef\u4ee5\u67e5\u770b\u5b98\u7f51\uff0c\u8981\u60f3\u5b8c\u6210 jest \u5bf9\u4e8e TypeScript \u7684\u6d4b\u8bd5\uff0c\u8fd8\u9700\u8981\u5b89\u88c5\u4e00\u4e9b\u4f9d\u8d56\uff1a<\/span><\/p>\n<pre><code>yarn add jest ts-jest @babel\/preset-typescript @types\/jest -D<\/code><\/pre>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">3) jest \u8fd8\u9700\u8981\u501f\u52a9 .babelrc \u53bb\u89e3\u6790 TypeScript \u6587\u4ef6\uff0c\u518d\u8fdb\u884c\u6d4b\u8bd5\uff0c\u7f16\u8f91 .babelrc \u6587\u4ef6\uff0c\u6dfb\u52a0\u4f9d\u8d56\u5305 @babel\/preset-typescript\uff1a<\/span><\/p>\n<pre><code>{\n  \"presets\": [\n    \"@babel\/preset-typescript\",\n    [\n      \"@babel\/preset-env\"\n    ]\n  ],\n  \"plugins\": [\"@babel\/plugin-transform-runtime\"]\n}<\/code><\/pre>\n<h2>2. \u5355\u5143\u6d4b\u8bd5\u6587\u4ef6\u7684\u7f16\u5199<\/h2>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">1) \u901a\u8fc7\u4ee5\u4e0a\u73af\u8282\uff0cjest \u5355\u5143\u6d4b\u8bd5\u73af\u5883\u57fa\u672c\u642d\u5efa\u5b8c\u6bd5\uff0c\u63a5\u4e0b\u6765\u5728__tests__\u4e0b\u7f16\u5199\u6d4b\u8bd5\u7528\u4f8b<\/span><\/p>\n<p style=\"text-align:center\">\n<\/p>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">2) \u6267\u884c yarn test<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">\u53ef\u4ee5\u770b\u5230\u5173\u4e8e debounce \u9632\u6296\u5de5\u5177\u51fd\u6570\u7684\u6d4b\u8bd5\u60c5\u51b5\u663e\u793a\u5728\u4e86\u63a7\u5236\u53f0\uff1a<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">\u2022\uf06c stmts \u662f\u8bed\u53e5\u8986\u76d6\u7387\uff08statement coverage\uff09\uff1a\u662f\u4e0d\u662f\u6bcf\u4e2a\u8bed\u53e5\u90fd\u6267\u884c\u4e86\uff1f<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">\u2022\uf06c Branch \u5206\u652f\u8986\u76d6\u7387\uff08branch coverage\uff09\uff1a\u662f\u4e0d\u662f\u6bcf\u4e2a if \u4ee3\u7801\u5757\u90fd\u6267\u884c\u4e86\uff1f<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">\u2022\uf06c Funcs \u51fd\u6570\u8986\u76d6\u7387\uff08function coverage\uff09\uff1a\u662f\u4e0d\u662f\u6bcf\u4e2a\u51fd\u6570\u90fd\u8c03\u7528\u4e86\uff1f<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">\u2022\uf06c Lines \u884c\u8986\u76d6\u7387\uff08line coverage\uff09\uff1a\u662f\u4e0d\u662f\u6bcf\u4e00\u884c\u90fd\u6267\u884c\u4e86\uff1f<\/span><\/p>\n<p style=\"text-align:center\">\n<\/p>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">3) \u540c\u65f6\u8fd8\u4f1a\u53d1\u73b0\u9879\u76ee\u6839\u76ee\u5f55\u591a\u4e86\u4e00\u4e2a coverage \u6587\u4ef6\u5939\uff0c\u91cc\u9762\u5c31\u662f jest \u751f\u6210\u7684\u6d4b\u8bd5\u62a5\u544a\uff1a<\/span><\/p>\n<p style=\"text-align:center\">\n<\/p>\n<h2>3. \u5355\u5143\u6d4b\u8bd5\u6587\u4ef6\u7684\u7f16\u5199\u5f15\u53d1\u7684\u601d\u8003<\/h2>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">\u6bcf\u6b21\u4fee\u6539\u5355\u5143\u6d4b\u8bd5\u90fd\u8981\u6267\u884c yarn test \u53bb\u67e5\u770b\u6d4b\u8bd5\u7ed3\u679c\uff0c\u600e\u4e48\u89e3\u51b3\uff1f<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">jest\u63d0\u4f9b\u4e86 watch \u6307\u4ee4\uff0c\u53ea\u9700\u8981\u914d\u7f6e scripts \u811a\u672c\u5c31\u53ef\u4ee5\u505a\u5230\uff0c\u5355\u5143\u6d4b\u8bd5\u7684\u70ed\u66f4\u65b0\u3002<\/span><\/p>\n<pre><code>\"scripts\": {\n  \"test\": \"jest --watchAll\"\n},<\/code><\/pre>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">\u4ee5\u540e\u4f1a\u5199\u5f88\u591a\u5de5\u5177\u7684\u6d4b\u8bd5\u7528\u4f8b\uff0c\u6bcf\u6b21 test \u90fd\u5c06\u6240\u6709\u5de5\u5177\u90fd\u8fdb\u884c\u4e86\u6d4b\u8bd5\uff0c\u80fd\u5426\u53ea\u6d4b\u8bd5\u81ea\u5df1\u5199\u7684\u5de5\u5177\uff1f<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">jest \u4e5f\u63d0\u4f9b\u4e86\u6d4b\u8bd5\u5355\u4e2a\u6587\u4ef6\u7684\u65b9\u6cd5\uff0c\u8fd9\u6837 jest \u53ea\u4f1a\u5bf9\u9632\u6296\u51fd\u6570\u8fdb\u884c\u6d4b\u8bd5\uff08\u524d\u63d0\u5168\u5c40\u5b89\u88c5\u4e86 jest\uff09\u3002<\/span><\/p>\n<pre><code>jest debounce.test.ts --watch<\/code><\/pre>\n<h2>\u5de5\u5177\u5e93\u5305\u7684\u53d1\u5e03<\/h2>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">\u81f3\u6b64\u5de5\u5177\u5e93\u8ddd\u79bb\u5f00\u53d1\u8005\u4f7f\u7528\u4ec5\u4e00\u6b65\u4e4b\u9065\uff0c\u5c31\u662f\u53d1\u5e03\u5230npm\u4e0a\uff0c\u53d1\u5305\u524d\u9700\u8981\u5728 package.json \u4e2d\u58f0\u660e\u5e93\u7684\u4e00\u4e9b\u5165\u53e3\uff0c\u5173\u952e\u8bcd\u7b49\u4fe1\u606f\u3002<\/span><\/p>\n<pre><code>\"main\": \"lib\/main.js\", \/\/ \u544a\u77e5\u5f15\u7528\u8be5\u5305\u6a21\u5757\u5316\u65b9\u5f0f\u7684\u9ed8\u8ba4\u6587\u4ef6\u8def\u5f84\n  \"module\": \"lib\/main.esm.js\", \/\/ \u544a\u77e5\u5f15\u7528\u8be5\u5305\u6a21\u5757\u5316\u65b9\u5f0f\u7684\u6587\u4ef6\u8def\u5f84\n  \"types\": \"lib\/types\/index.d.ts\", \/\/ \u544a\u77e5\u5f15\u7528\u8be5\u5305\u7684\u7c7b\u578b\u58f0\u660e\u6587\u4ef6\u8def\u5f84\n  \"sideEffects\": false, \/\/ false \u4e3a\u4e86\u544a\u8bc9 webpack \u6211\u8fd9\u4e2a npm \u5305\u91cc\u7684\u6240\u6709\u6587\u4ef6\u4ee3\u7801\u90fd\u662f\u6ca1\u6709\u526f\u4f5c\u7528\u7684\n  \"files\": [ \/\/ \u5f00\u53d1\u8005\u5f15\u7528\u8be5\u5305\u540enode_modules\u5305\u91cc\u9762\u7684\u6587\u4ef6\n    \"lib\",\n    \"README.md\"\n  ],\n  \"keywords\": [\n    \"typescript\",\n    \"utils-demo\",\n    \"utils\"\n  ],\n  \"scripts\": {\n    \"pub\": \"npm publish\"\n  },<\/code><\/pre>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">\u767b\u9646npm\uff0c\u4f60\u4f1a\u770b\u5230\u81ea\u5df1\u7684 packages \u91cc\u9762\u6709\u4e86\u521a\u521a\u53d1\u5e03\u7684\u5de5\u5177\u5e93\u5305\uff1a<\/span><\/p>\n<p style=\"text-align:center\">\n<h2>\u5199\u5728\u6700\u540e<\/h2>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">\u4ee5\u4e0a\u5c31\u662f\u4f5c\u8005\u6574\u7406\u7684\u4ece0\u52301\u6784\u5efa\u57fa\u4e8e\u81ea\u8eab\u4e1a\u52a1\u7684\u524d\u7aef\u5de5\u5177\u5e93\u7684\u5168\u8fc7\u7a0b\uff0c\u5e0c\u671b\u80fd\u7ed9\u9605\u8bfb\u672c\u6587\u7684\u5f00\u53d1\u4eba\u5458\u5e26\u6765\u4e00\u4e9b\u65b0\u7684\u60f3\u6cd5\u4e0e\u5c1d\u8bd5\u3002<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">\u5728\u6b64\u57fa\u7840\u4e0a\u5df2\u7ecf\u6210\u529f\u5728\u4eac\u4e1cnpm\u6e90\u53d1\u5e03\u4e86\u5e94\u7528\u4e8e\u4eac\u4e1c\u6c7d\u8f66\u524d\u7aef\u7684\u5de5\u5177\u5e93@jdcar\/car-utils\uff0c\u5e76\u5728\u5404\u4e2a\u4e1a\u52a1\u7ebf\u53ca\u7cfb\u7edf\u5f97\u5230\u843d\u5730\u3002<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">\u5f53\u7136\uff0c\u67b6\u6784\u4f18\u5316\u4e4b\u8def\u4e5f\u8fd8\u8fdc\u672a\u7ed3\u675f\uff0c\u6bd4\u5982\uff1a\u6253\u5305\u6784\u5efa\u7684\u901f\u5ea6\u3001\u672c\u5730\u5f00\u53d1\u6309\u9700\u6784\u5efa\u3001\u5de5\u5177\u5e93\u811a\u624b\u67b6\u5316\u7b49\uff0c\u540e\u7eed\u6211\u4eec\u4e5f\u4f1a\u57fa\u4e8e\u81ea\u8eab\u4e1a\u52a1\u4ee5\u53ca\u4e00\u4e9b\u65b0\u6280\u672f\uff0c\u6301\u7eed\u6df1\u5165\u4f18\u5316\uff0c\u5728\u6027\u80fd\u4e0a\u8fdb\u4e00\u6b65\u63d0\u5347\uff0c\u5728\u529f\u80fd\u4e0a\u8fdb\u4e00\u6b65\u4e30\u5bcc\u3002\u672c\u6587\u6216\u5b58\u5728\u4e00\u4e9b\u6d45\u663e\u4e0d\u8db3\u4e4b\u5904\uff0c\u4e5f\u6b22\u8fce\u5927\u5bb6\u8bc4\u8bba\u6307\u70b9\u3002<\/span><\/p>\n<h2>\u53c2\u8003\u8d44\u6599<\/h2>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">[1] rollup \u82f1\u6587\u6587\u6863\uff08<\/span><span style=\"color: #333333\">https:\/\/rollupjs.org\/guide\/en\/#quick-start\uff09<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">[2] rollup \u4e2d\u6587\u6587\u6863\uff08<\/span><span style=\"color: #333333\">https:\/\/rollupjs.org\/guide\/zh\/#introduction\uff09<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">[3] Rollup.js \u5b9e\u6218\u5b66\u4e60\u7b14\u8bb0\uff08<\/span><span style=\"color: #333333\">https:\/\/chenshenhai.github.io\/rollupjs-note\/\uff09<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">[4] Rollup \u6253\u5305\u5de5\u5177\u7684\u4f7f\u7528\uff08<\/span><span style=\"color: #333333\">https:\/\/juejin.cn\/post\/6844904058394771470\uff09<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">[5] TypeScript\u3001Rollup \u642d\u5efa\u5de5\u5177\u5e93\uff08<\/span><span style=\"color: #333333\">https:\/\/juejin.cn\/post\/6844904035309322254\uff09<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">[6] \u4f7f\u7528 rollup.js \u5c01\u88c5\u5404\u9879\u76ee\u5171\u7528\u7684\u5de5\u5177\u5305\uff08<\/span><span style=\"color: #333333\">https:\/\/juejin.cn\/post\/6993720790046736420\uff09<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">[7] \u5982\u4f55\u5f00\u53d1\u4e00\u4e2a\u57fa\u4e8e TypeScript \u7684\u5de5\u5177\u5e93\u5e76\u81ea\u52a8\u751f\u6210\u6587\u6863\uff08<\/span><span style=\"color: #333333\">https:\/\/juejin.cn\/post\/6844903881030238221\uff09<\/span><\/p>\n<p style=\"text-align: justify\"><span style=\"color: #333333\">[8] \u4e00\u6b3e\u4f18\u96c5\u3001\u7b80\u6d01\u7684 JavaScript \u6d4b\u8bd5\u6846\u67b6\uff08<\/span><span style=\"color: #333333\">https:\/\/jestjs.io\/zh-Hans\/\uff09<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f5c\u8005\uff1a\u4eac\u4e1c\u96f6\u552e \u524d\u8a00 \u2022\u5728\u5b9e\u9645\u9879\u76ee\u5f00\u53d1\u4e2d\u65e0\u8bba M \u7aef\u3001PC \u7aef\uff0c\u6216\u591a\u6216\u5c11\u90fd\u6709\u4e00\u4e2a utils \u6587\u4ef6\u76ee\u5f55\u53bb\u7ba1\u7406\u9879 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":332214,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[61],"tags":[],"class_list":["post-332213","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-website"],"_links":{"self":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/332213","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/comments?post=332213"}],"version-history":[{"count":0,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/332213\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/media\/332214"}],"wp:attachment":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/media?parent=332213"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/categories?post=332213"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/tags?post=332213"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}