{"id":3672,"date":"2024-11-10T11:36:17","date_gmt":"2024-11-10T03:36:17","guid":{"rendered":"https:\/\/fwq.ai\/blog\/3672\/"},"modified":"2024-11-10T11:36:17","modified_gmt":"2024-11-10T03:36:17","slug":"vite-%e5%a6%82%e4%bd%95%e5%83%8f-webpack-%e4%bd%bf%e7%94%a8-alias-%e4%b8%80%e6%a0%b7%e5%90%88%e5%b9%b6%e9%87%8d%e5%a4%8d%e5%8c%85%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/3672\/","title":{"rendered":"Vite \u5982\u4f55\u50cf Webpack \u4f7f\u7528 alias \u4e00\u6837\u5408\u5e76\u91cd\u590d\u5305\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173111598371532.jpg\" class=\"aligncenter\" title=\"Vite \u5982\u4f55\u50cf Webpack \u4f7f\u7528 alias \u4e00\u6837\u5408\u5e76\u91cd\u590d\u5305\uff1f\u63d2\u56fe\" alt=\"Vite \u5982\u4f55\u50cf Webpack \u4f7f\u7528 alias \u4e00\u6837\u5408\u5e76\u91cd\u590d\u5305\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>vite \u6253\u5305\u5408\u5e76\u91cd\u590d\u5305<\/strong><\/p>\n<p>\u4e0e Webpack \u4f7f\u7528 alias \u7c7b\u4f3c\uff0cVite \u4e5f\u63d0\u4f9b\u4e86\u4e00\u79cd\u5408\u5e76\u91cd\u590d\u5305\u7684\u65b9\u6cd5\u3002\u901a\u8fc7\u4fee\u6539 vite.config.js \u914d\u7f6e\u6587\u4ef6\u4e2d\u7684 resolve.alias \u9009\u9879\uff0c\u53ef\u4ee5\u4e3a\u91cd\u590d\u7684\u5305\u6307\u5b9a\u522b\u540d\u3002<\/p>\n<p><strong>\u914d\u7f6e\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>\/\/ vite.config.js\nimport { defineConfig } from 'vite';\n\nexport default defineConfig({\n  resolve: {\n    alias: {\n      \/\/ \u628a 'some-library' \u89e3\u6790\u6210 'some-library\/dist\/some-library.esm.js'\n      'some-library': 'some-library\/dist\/some-library.esm.js',\n    },\n  },\n});<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0cVite \u4f1a\u5c06\u6240\u6709\u5f15\u7528\u522b\u540d\u7684\u5305\u89e3\u6790\u5230\u6307\u5b9a\u7684\u8def\u5f84\uff0c\u4ece\u800c\u5408\u5e76\u91cd\u590d\u7684\u5305\uff0c\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u5197\u4f59\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fVite \u5982\u4f55\u50cf Webpack \u4f7f\u7528 alias \u4e00\u6837\u5408\u5e76\u91cd\u590d\u5305\uff1f\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>vite \u6253\u5305\u5408\u5e76\u91cd\u590d\u5305 \u4e0e Webpack \u4f7f\u7528 alias \u7c7b\u4f3c\uff0cVite \u4e5f\u63d0\u4f9b\u4e86\u4e00\u79cd\u5408\u5e76\u91cd\u590d\u5305\u7684\u65b9\u6cd5\u3002\u901a\u8fc7\u4fee\u6539 vite.config.js \u914d\u7f6e\u6587\u4ef6\u4e2d\u7684 resolve.alias \u9009\u9879\uff0c\u53ef\u4ee5\u4e3a\u91cd\u590d\u7684\u5305\u6307\u5b9a\u522b\u540d\u3002 \u914d\u7f6e\u793a\u4f8b\uff1a \/\/ vite.config.js import { defineConfig } from &#8216;vite&#8217;; export default defineConfig({ resolve: { alias: { \/\/ \u628a &#8216;some-library&#8217; \u89e3\u6790\u6210 &#8216;some-library\/dist\/some-library.esm.js&#8217; &#8216;some-library&#8217;: &#8216;some-library\/dist\/some-library.esm.js&#8217;, }, }, }); \u767b\u5f55\u540e\u590d\u5236 \u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0cVite \u4f1a\u5c06\u6240\u6709\u5f15\u7528\u522b\u540d\u7684\u5305\u89e3\u6790\u5230\u6307\u5b9a\u7684\u8def\u5f84\uff0c\u4ece\u800c\u5408\u5e76\u91cd\u590d\u7684\u5305\uff0c\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u5197\u4f59\u3002 \u4ee5\u4e0a\u5c31\u662fVite \u5982\u4f55\u50cf Webpack \u4f7f\u7528 alias \u4e00\u6837\u5408\u5e76\u91cd\u590d\u5305\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-3672","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3672","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=3672"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3672\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=3672"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=3672"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=3672"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}