{"id":254243,"date":"2022-10-11T10:09:52","date_gmt":"2022-10-11T02:09:52","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=254243"},"modified":"2022-10-04T11:12:05","modified_gmt":"2022-10-04T03:12:05","slug":"vite-or-vue-cli","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/vite-or-vue-cli.html","title":{"rendered":"Vite\u6216Vue CLI\u6765\u9009\u4e00\u4e2a\u6700\u9002\u5408\u4f60\u7684"},"content":{"rendered":"
\u5bfc\u8bfb<\/td>\n | \u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u6df1\u5165\u63a2\u8ba8 Vite \u4e0e Vue CLI\uff0c\u4ee5\u4fbf\u4f60\u53ef\u4ee5\u6839\u636e\u81ea\u5df1\u7684\u9700\u6c42\u505a\u51fa\u6700\u4f73\u9009\u62e9\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n \u968f\u7740 Vue \u751f\u6001\u7cfb\u7edf\u7684\u6210\u719f\uff0c\u5df2\u7ecf\u5f15\u5165\u4e86\u8bb8\u591a\u65b0\u6280\u672f\u3002\u867d\u7136\u4e00\u4e9b\u5f00\u53d1\u4eba\u5458\u53ef\u80fd\u4ece\u4e00\u4e2a\u666e\u901a\u7684 JavaScript \u6587\u4ef6\u5f00\u59cb\uff0c\u5e76\u5728\u9875\u9762\u4e2d\u5305\u542b Vue\u3002\u4f46\u968f\u7740\u65f6\u95f4\u7684\u63a8\u79fb\uff0c\u5728\u5f00\u53d1 Vue \u5e94\u7528\u7a0b\u5e8f\u65f6\uff0c\u4f7f\u7528\u6253\u5305\u5de5\u5177\u548c\u547d\u4ee4\u884c\u754c\u9762\u6210\u4e3a\u66f4\u52a0\u6807\u51c6\u7684\u5f00\u53d1\u6d41\u7a0b\u3002<\/p>\n Vue \u4e0d\u4f1a\u5f3a\u5236\u4f60\u9009\u62e9\u67d0\u79cd\u5f00\u53d1\u3001\u6253\u5305\u65b9\u5f0f\uff0c\u4f46vue\u7684\u751f\u6001\u7cfb\u7edf\u63d0\u4f9b\u4e86\u8bb8\u591a\u5de5\u5177\uff0c\u4f8b\u5982 Vue CLI \u548c\u6700\u8fd1\u7684\u63a8\u51fa\u7684Vite js\u3002\u90a3\u4e48\u4f60\u5e94\u8be5\u4f7f\u7528 Vite \u8fd8\u662f Vue CLI\uff1f\u5728\u505a\u51fa\u9009\u62e9\u7684\u65f6\u5019\uff0c\u4e86\u89e3 Vite \u548c Vue CLI \u4e4b\u95f4\u7684\u533a\u522b\u4f1a\u5bf9\u6211\u4eec\u6709\u5f88\u5927\u7684\u5e2e\u52a9\u3002<\/p>\n \u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u6df1\u5165\u63a2\u8ba8 Vite \u4e0e Vue CLI\uff0c\u4ee5\u4fbf\u4f60\u53ef\u4ee5\u6839\u636e\u81ea\u5df1\u7684\u9700\u6c42\u505a\u51fa\u6700\u4f73\u9009\u62e9\u3002<\/p>\n Vue CLI \u662f\u5982\u4f55\u5de5\u4f5c\u7684\uff1f<\/strong><\/div>\n Vue \u547d\u4ee4\u884c\u754c\u9762 (CLI) \u56f4\u7ed5 Webpack \u521b\u5efa\u4e00\u4e2a\u5305\u88c5\u5668\u6765\u7f16\u8bd1\u4f60\u7684 Vue \u9879\u76ee\u3002\u4f46\u662f\u6211\u4eec\u4e3a\u4ec0\u4e48\u8981\u7f16\u8bd1\u5b83\u5462\uff1f\u5728 Webpack \u548c Rollup \u8fd9\u6837\u7684\u6253\u5305\u5de5\u5177\u51fa\u73b0\u4e4b\u524d\uff0c\u6211\u4eec\u53ea\u9700\u8981\u5c06\u6211\u4eec\u7684 JavaScript \u6587\u4ef6\u90e8\u7f72\u5230\u670d\u52a1\u5668\u5e76\u5c06\u5b83\u4eec\u5168\u90e8\u5305\u542b\u5728\u4e00\u4e2a\u9875\u9762\u4e0a\u3002\u5f53\u6211\u4eec\u7684\u9879\u76ee\u5f88\u5c0f\u7684\u65f6\u5019\uff0c\u8fd9\u79cd\u6548\u679c\u5f88\u597d\u3002\u4f46\u662f\u968f\u7740 Vue \u7684\u53d1\u5c55\uff0c\u6211\u4eec\u5f00\u59cb\u9700\u8981\u67d0\u79cd\u7a0b\u5ea6\u7684\u6253\u5305\u529f\u80fd\u3002\u5355\u6587\u4ef6\u7ec4\u4ef6 (SFC) \u548c TypeScript\u7684\u4f7f\u7528\u90fd\u9700\u8981\u501f\u52a9\u6253\u5305\u5de5\u5177\u6765\u6784\u5efa\u6211\u4eec\u7684\u9879\u76ee\u3002\u8fd9\u5c31\u662f\u4e3a\u4ec0\u4e48\u8981\u5f15\u5165Vue CLI \u3002<\/p>\n Vue CLI \u5141\u8bb8\u6211\u4eec\u5728\u5f00\u53d1\u548c\u751f\u4ea7\u671f\u95f4\u521b\u5efa\u548c\u6784\u5efa\u9879\u76ee\u3002CLI \u9690\u85cf\u4e86 Webpack \u7684\u590d\u6742\u914d\u7f6e\u7ec6\u8282\uff0c\u5e76\u5728\u5f00\u53d1\u548c\u751f\u4ea7\u8fc7\u7a0b\u4e2d\u4e3a\u6211\u4eec\u7684\u9879\u76ee\u63d0\u4f9b\u670d\u52a1\u3002\u4f46\u662f\uff0c\u5982\u679c\u6211\u4eec\u60f3\u8981\u5c06\u5b83\u4e0e Vite \u8fdb\u884c\u6bd4\u8f83\uff0c\u90a3\u5c31\u8ba9\u6211\u4eec\u518d\u6df1\u5165\u4e00\u70b9\u3002<\/p>\n Webpack<\/strong><\/span><\/div>\n \u6b63\u5982 Webpack \u7f51\u7ad9\u6240\u63cf\u8ff0\u7684\uff0cWebpack \u662f\u4e00\u4e2a\u9759\u6001\u6a21\u5757\u6253\u5305\u5de5\u5177\u3002\u4f46\u662f\uff0c\u8fd9\u662f\u4ec0\u4e48\u610f\u601d\uff1fWebpack \u7684\u4e3b\u8981\u76ee\u6807\u662f\u83b7\u53d6\u4f60\u7684 Web \u9879\u76ee\u7684\u8d44\u4ea7\u5e76\u5c06\u5b83\u4eec\u6253\u5305\u5230\u5c11\u91cf\u6587\u4ef6\u4e2d\u4ee5\u4f9b\u6d4f\u89c8\u5668\u4e0b\u8f7d\u3002\u8fd9\u5e76\u4e0d\u610f\u5473\u7740\u7acb\u5373\u52a0\u8f7d\u9879\u76ee\u7684\u6240\u6709\u6587\u4ef6\uff0c\u4e5f\u53ef\u4ee5\u6309\u9700\u4e0b\u8f7d\u3002<\/p>\n \u867d\u7136 Webpack \u53ef\u4ee5\u6253\u5305\u975e\u4ee3\u7801\u8d44\u4ea7\uff0c\u4f46\u5b83\u771f\u6b63\u7684\u95ea\u5149\u70b9\u662f\u6253\u5305\u76f8\u5173\u7684\u4ee3\u7801\u3002\u4f60\u53ef\u4ee5\u8ba4\u4e3a\u6253\u5305\u5c31\u662f\u7b80\u5355\u5730\u5c06\u6240\u6709\u4ee3\u7801\u6587\u4ef6\uff08\u4f8b\u5982JavaScript\/TypeScript\uff09\u5408\u5e76\u5728\u4e00\u8d77\uff0c\u4f46\u5b9e\u9645\u4e0a\u8fd9\u91cc\u8fd8\u6709\u66f4\u591a\u7684\u5185\u5bb9\u3002<\/p>\n Webpack \u5904\u7406\u4ee3\u7801\u4e2d\u7684 imports\/require \u8bed\u53e5\u65f6\uff0c\u53ea\u5305\u542b\u5b9e\u9645\u9700\u8981\u7684\u6587\u4ef6\u3002\u6b64\u5916\uff0c\u5b83\u7ecf\u5e38\u8fdb\u884c\u201ctree-shaking\u201d\uff0c\u8fd9\u53ef\u4ee5\u5220\u9664\u5b9e\u9645\u4e0a\u4ece\u672a\u5f15\u7528\u8fc7\u7684\u4ee3\u7801\u5757\uff08\u4f8b\u5982\u7c7b\u3001\u51fd\u6570\uff09\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0cWebpack \u53ef\u4ee5\u975e\u5e38\u9ad8\u6548\u5730\u6253\u5305\u4f60\u7684\u9879\u76ee\u3002\u4f46\u662f\uff0c\u5982\u679c\u6211\u4eec\u6b63\u5728\u7814\u7a76 Vue CLI\uff08\u5305\u542b Webpack\uff09\u4e0e Vite \u7684\u6bd4\u8f83\uff0c\u6211\u4eec\u9700\u8981\u7ee7\u7eed\u770b\u770b\u5728\u5f00\u53d1\u65f6\u4f1a\u53d1\u751f\u4ec0\u4e48\u3002<\/p>\n \u5f00\u53d1\u8fc7\u7a0b\u4e2d\u7684 Webpack<\/strong><\/span><\/div>\n \u5728 Vue CLI \u4e2d\uff0cWebpack \u7528\u4e8e\u6a21\u5757\u7ed1\u5b9a\u3002\u5728\u5f00\u53d1\u65f6\u4e5f\u4f1a\u53d1\u751f\u8fd9\u79cd\u7ed1\u5b9a\u3002\u5f53\u4f60\u901a\u8fc7 CLI \u5f00\u53d1 Vue \u9879\u76ee\u65f6\uff0c\u5b83\u4f1a\u6307\u793a Webpack \u4ee5\u589e\u91cf\u65b9\u5f0f\u6784\u5efa\u9879\u76ee\u5e76\u76d1\u542c\u66f4\u6539\uff0c\u4ee5\u4fbf\u5b83\u53ef\u4ee5\u5c06\u66f4\u6539\u7684\u6587\u4ef6\u6253\u5305\u5e76\u5728\u6d4f\u89c8\u5668\u4e2d\u8fdb\u884c\u66ff\u6362\u3002\u56e0\u4e3a\u6784\u5efa\u5305\u662f\u589e\u91cf\u7684\uff0c\u6240\u4ee5\u5b83\u53ef\u4ee5\u5728\u4f60\u5f00\u53d1\u9879\u76ee\u65f6\u5feb\u901f\u7684\u5c06\u66f4\u6539\u540c\u6b65\u3002\u7b2c\u4e00\u6b21\u6784\u5efa\u9879\u76ee\u65f6\u53ef\u80fd\u4f1a\u8f83\u6162\uff08\u5373 3 \u79d2\u6216\u66f4\u957f\u65f6\u95f4\uff0c\u5177\u4f53\u53d6\u51b3\u4e8e\u4f60\u7684\u9879\u76ee\u5927\u5c0f\uff09\uff0c\u4f46\u4e00\u65e6\u9879\u76ee\u542f\u52a8\uff0c\u589e\u91cf\u66f4\u6539\u7684\u540c\u6b65\u4f1a\u975e\u5e38\u5feb\uff08\u5373 < 1 \u79d2\uff0c\u901a\u5e38\u5c0f\u4e8e 100 \u6beb\u79d2\uff09\u3002\n\n[yiji]Vite js \u662f\u5982\u4f55\u5de5\u4f5c\u7684\uff1f[\/yiji]\n\u5982\u679c\u4f60\u662fVite\u7684\u65b0\u624b\uff0c\u6211\u5efa\u8bae\u76f4\u63a5\u4ece\u5b83\u7684\u521b\u9020\u8005\u5c24\u96e8\u6eaa\u90a3\u91cc\u5b66\u4e60\uff0c\u4ed6\u5728Vue Mastery\u7684\u8bfe\u7a0b\u201c\u4f7f\u7528vite\u5feb\u901f\u6784\u5efa\u201d\u4e2d\u6559\u6211\u4eec\u5173\u4e8eVite\u7684\u77e5\u8bc6\u3002\n\n\u5982\u679c\u4f60\u770b\u8fc7\u8be5\u8bfe\u7a0b\u7684\u7b2c\u4e00\u8bfe\uff08\u5b83\u662f\u514d\u8d39\u7684\uff09\uff0c\u4f60\u5c06\u4e86\u89e3\u5982\u4f55\u5f00\u59cb\uff0c\u4f46\u5728\u6211\u4eec\u5c06\u5176\u4e0e Vue CLI \u548c Webpack \u8fdb\u884c\u6bd4\u8f83\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u4e86\u89e3 Vite \u7684\u5de5\u4f5c\u539f\u7406\u3002\n\n\u5f53\u4f60\u521b\u5efa\u4e00\u4e2a\u57fa\u672c\u7684 Vite \u9879\u76ee\uff08\u5728\u6211\u4eec\u7684\u4f8b\u5b50\u4e2d\uff0c\u662f Vue 3 \u7684 Vite \u9879\u76ee\uff09\u65f6\uff0cindex.html\u6587\u4ef6\u662f\u975e\u5e38\u57fa\u672c\u7684\uff1a\n\n[erji]index.html[\/erji]\n\n \u5f53\u8fd9\u4e2a\u8bf7\u6c42\u4f20\u5165\u65f6\uff0c\u5b83\u4f1a\u5c06main.js\u6587\u4ef6\u4f5c\u4e3a\u539f\u751f ES \u6a21\u5757\u53d1\u9001\u5230\u6d4f\u89c8\u5668\u3002\u8fd9\u610f\u5473\u7740\u5b83\u6839\u672c\u4e0d\u4f1a\u6253\u5305\u4f60\u7684\u4ee3\u7801\u3002\u5b9e\u9645\u4e0a\uff0c\u6e90\u6587\u4ef6\u53ea\u662f\u4e00\u4e2a\u7b80\u5355\u7684Vue\u542f\u52a8\u6587\u4ef6\uff1a<\/p>\n main.js<\/strong><\/span><\/div>\n <\/p>\n \u8fd9\u91cc\u53d1\u751f\u4e86\u4ec0\u4e48\uff1fVite \u6b63\u5728\u5229\u7528\u539f\u751f ES \u6a21\u5757\u548c\u52a8\u6001 ESM \u6a21\u5757\uff0c\u4ee5\u4fbf\u6839\u636e\u9700\u8981\u5c06\u4ee3\u7801\u6ce8\u5165\u6d4f\u89c8\u5668\u3002\u672c\u8d28\u4e0a\uff0c\u9879\u76ee\u4e2d\u7684\u6bcf\u6b21\u5bfc\u5165\u90fd\u4f1a\u5f3a\u5236\u6d4f\u89c8\u5668\u6839\u636e\u9700\u8981\u52a0\u8f7d\u6bcf\u4e2a\u6709\u6548\u8d1f\u8f7d\u3002\u8fd9\u4e9b\u5bfc\u5165\u5c06\u7ea7\u8054\u4ee5\u83b7\u5f97\u7279\u5b9a\u60c5\u51b5\u4e0b\u6240\u9700\u7684\u6240\u6709\u4ee3\u7801\u3002<\/p>\n \u8fd9\u610f\u5473\u7740\u5728\u5f00\u53d1\u8fc7\u7a0b\u4e2d\uff0c Vite \u4e0d\u4f46\u4e3a\u4f60\u7684\u7f51\u7ad9\u63d0\u4f9b\u670d\u52a1\uff0c\u5e76\u4e14\u5b83\u4e5f\u652f\u6301\u52a8\u6001\u52a0\u8f7d\uff08\u5728\u652f\u6301\u5b83\u7684\u65b0\u6d4f\u89c8\u5668\u4e2d\uff0c\u5e76\u4e14\u4e5f\u517c\u5bb9\u65e7\u6d4f\u89c8\u5668\uff09\u3002<\/p>\n \u6b64\u5916\uff0cVite \u5728\u4f60\u5f00\u53d1\u9879\u76ee\u65f6\u4f7f\u7528\u6a21\u5757\u70ed\u66ff\u6362 (HMR) \u6765\u66f4\u65b0\u4fee\u6539\u7684\u4ee3\u7801\u3002\u4f7f\u4f60\u80fd\u591f\u5728\u5f00\u53d1\u65f6\u83b7\u5f97\u5373\u65f6\u7684\u542f\u52a8\u901f\u5ea6\uff0c\u8fd9\u4f1a\u5927\u5927\u63d0\u9ad8\u5f00\u53d1\u4f53\u9a8c\u3002<\/p>\n \u4f46\u662f Vite \u662f\u5982\u4f55\u5728\u751f\u4ea7\u73af\u5883\u4e2d\u5b9e\u73b0\u8fd9\u4e00\u70b9\u7684\u5462\uff1f\u8ba9\u6211\u4eec\u6765\u770b\u770b\u3002<\/p>\n Vite \u548c\u751f\u4ea7\u73af\u5883<\/strong><\/div>\n \u867d\u7136 Vite \u5728\u5f00\u53d1\u8fc7\u7a0b\u4e2d\u4f7f\u7528\u5176\u63d0\u4f9b\u6587\u4ef6\u7684\u80fd\u529b\u6765\u52a0\u901f\u8be5\u8fc7\u7a0b\uff0c\u4f46 Vite \u672c\u8eab\u5e76\u6ca1\u6709\u771f\u6b63\u6253\u5305\u4f60\u7684\u9879\u76ee\u3002\u76f8\u53cd\uff0c\u5b83\u4f9d\u8d56\u4e8e\u4e00\u4e2a\u540d\u4e3a\"Rollup\"\u7684\u6253\u5305\u7a0b\u5e8f\u6765\u8fdb\u884c\u5b9e\u9645\u7684\u6253\u5305\u3002<\/p>\n \u901a\u8fc7\u4f7f\u7528Vite\u4e2d\u7684build\u547d\u4ee4\uff0c\u5b83\u5c06\u4f7f\u7528rollup\u6765\u6784\u5efa\u4f60\u7684\u9879\u76ee\uff1a<\/p>\n \u547d\u4ee4\u884c<\/strong><\/span><\/div>\n \r\n> vite build\r\n<\/pre>\n |