{"id":6135110,"date":"2021-05-14T18:38:25","date_gmt":"2021-05-14T10:38:25","guid":{"rendered":"https:\/\/longxi.me\/?p=6135110"},"modified":"2021-06-16T14:35:04","modified_gmt":"2021-06-16T06:35:04","slug":"vite2-vue2%ef%bc%8c%e5%9f%ba%e6%9c%ac%e5%8f%af%e7%94%a8","status":"publish","type":"post","link":"https:\/\/longxi.me\/?p=6135110","title":{"rendered":"Vite2 + Vue2\uff0c\u57fa\u672c\u53ef\u7528"},"content":{"rendered":"\n<p>Vite2 \u5df2\u7ecf\u53d1\u5e03\uff0c\u5c1d\u8bd5\u5c06\u4e00\u4e2a\u57fa\u4e8e Vue CLI \u521b\u5efa\u7684 Vue2 \u9879\u76ee\u8fc1\u79fb\u5230 Vite \u4e0a\u6765\uff0c\u591a\u4e2a\u5730\u65b9\u9700\u8981\u8c03\u6574\uff0c\u5728\u6b64\u8bb0\u5f55\u3002<\/p>\n\n\n\n<h2>1\u3001Vite<\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">npm i -D vite<\/pre>\n\n\n\n<h2>2\u3001<a rel=\"noreferrer noopener\" href=\"https:\/\/github.com\/underfin\/vite-plugin-vue2\" target=\"_blank\">Vue2<\/a> \u652f\u6301<\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">npm i -D vite-plugin-vue2\n\n\/\/ vite.config.js\nimport { createVuePlugin } from 'vite-plugin-vue2'\n\nplugins: [\n    createVuePlugin()\n]<\/pre>\n\n\n\n<h2>3\u3001index.html<\/h2>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" loading=\"lazy\" width=\"700\" height=\"311\" src=\"https:\/\/longxi.me\/wp-content\/uploads\/2021\/05\/new.index_.html.png\" alt=\"\" class=\"wp-image-6135122\" srcset=\"https:\/\/longxi.me\/wp-content\/uploads\/2021\/05\/new.index_.html.png 700w, https:\/\/longxi.me\/wp-content\/uploads\/2021\/05\/new.index_.html-300x133.png 300w\" sizes=\"(max-width: 700px) 100vw, 700px\" \/><\/figure>\n\n\n\n<!--more-->\n\n\n\n<h2>4\u3001vite.config.js<\/h2>\n\n\n\n<h3>alias<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">resolve: {<br>  alias: {<br>    '@': path.resolve(__dirname, 'src')<br>  }<br>}<\/pre>\n\n\n\n<h3>\u73af\u5883\u53d8\u91cf<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">export default ({ mode }) =&gt; {\n  const isProd = mode === 'production'\n  ...\n}<\/pre>\n\n\n\n<h3><a href=\"https:\/\/cn.vitejs.dev\/guide\/features.html#css-pre-processors\" target=\"_blank\" rel=\"noreferrer noopener\">sass<\/a><\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">npm i -D sass\n\n<a rel=\"noreferrer noopener\" href=\"https:\/\/cn.vitejs.dev\/config\/#css-preprocessoroptions\" data-type=\"URL\" data-id=\"https:\/\/cn.vitejs.dev\/config\/#css-preprocessoroptions\" target=\"_blank\">sass \u53d8\u91cf<\/a>\n<code>\/\/ vite.config.js<\/code>\ncss: {\n  preprocessorOptions: {\n    scss: {\n      additionalData: `$baseUrl: \"\/base\";`\n    }\n  }\n}<\/pre>\n\n\n\n<h2>5\u3001\u6d4f\u89c8\u5668\u517c\u5bb9<\/h2>\n\n\n\n<p>\u9ed8\u8ba4\u53ea<a rel=\"noreferrer noopener\" href=\"https:\/\/caniuse.com\/es6-module\" target=\"_blank\">\u652f\u6301\u539f\u751f ES \u6a21\u5757\u7684\u6d4f\u89c8\u5668<\/a>\uff0c\u5982\u679c\u9700\u8981\u652f\u6301\u66f4\u65e7\u7684\u6d4f\u89c8\u5668\uff0c\u5219\u9700\u8981 <a rel=\"noreferrer noopener\" href=\"https:\/\/github.com\/vitejs\/vite\/tree\/main\/packages\/plugin-legacy\" target=\"_blank\">@vitejs\/plugin-legacy<\/a>\uff0c\u5b83\u4f1a\u989d\u5916\u751f\u6210\u4f20\u7edf\u7248\u672c\u7684 chunk\u3002<\/p>\n\n\n\n<h2>6\u3001<a rel=\"noreferrer noopener\" href=\"https:\/\/github.com\/anncwb\/vite-plugin-svg-icons\" data-type=\"URL\" data-id=\"https:\/\/github.com\/anncwb\/vite-plugin-svg-icons\" target=\"_blank\">SVG \u56fe\u6807<\/a><\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">npm i -D vite-plugin-svg-icons\n\n\/\/ vite.config.js\nimport viteSvgIcons from 'vite-plugin-svg-icons'\nplugins: [\n  viteSvgIcons({\n    \/\/ \u6307\u5b9asvg\u56fe\u6807\u6240\u5728\u76ee\u5f55\n    iconDirs: [path.resolve(__dirname, 'src\/assets\/svg')],\n    \/\/ \u81ea\u5b9a\u4e49 symbolId \u683c\u5f0f\n    symbolId: 'svg-[name]'\n  })\n]\n\n\u6ce8\u610f\uff1a\u540c\u65f6\u4f7f\u7528 @vitejs\/plugin-legacy \u63d2\u4ef6\u65f6\uff0c\u6b64\u56fe\u6807\u63d2\u4ef6\u4e0d\u80fd\u5728\u4e0d\u652f\u6301ES\u6a21\u5757\u7684\u6d4f\u89c8\u5668\u4e0a\u6b63\u5e38\u5de5\u4f5c\uff0c\u5373\u770b\u4e0d\u5230\u56fe\u6807\u3002<\/pre>\n\n\n\n<h2>7\u3001.vue \u540e\u7f00<\/h2>\n\n\n\n<p>import vue \u7ec4\u4ef6\u65f6\uff0c\u9ed8\u8ba4\u627e\u4e0d\u5230\u672a\u5199 .vue \u540e\u7f00\u7684\u7ec4\u4ef6\uff0c<a rel=\"noreferrer noopener\" href=\"https:\/\/github.com\/vitejs\/vite\/issues\/178#issuecomment-630138450\" data-type=\"URL\" data-id=\"https:\/\/github.com\/vitejs\/vite\/issues\/178#issuecomment-630138450\" target=\"_blank\">\u63a8\u8350\u5199\u5168<\/a>\uff0c<a href=\"https:\/\/cn.vitejs.dev\/config\/#resolve-extensions\" data-type=\"URL\" data-id=\"https:\/\/cn.vitejs.dev\/config\/#resolve-extensions\" target=\"_blank\" rel=\"noreferrer noopener\">\u4e0d\u5efa\u8bae\u5ffd\u7565 .vue<\/a>\u3002<\/p>\n\n\n\n<h2>8\u3001\u5c1d\u8bd5CompositionAPI<\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">\/\/ main.js\nimport VueCompositionAPI from '@vue\/composition-api'\n\nVue.use(VueCompositionAPI)\n\n\/\/ xxx.vue  xxx.js\nimport { ref, reactive } from '@vue\/composition-api'<\/pre>\n\n\n\n<h2>9\u3001\u5f00\u53d1\u73af\u5883\u62a5\u9519\uff0c\u6253\u5305\u8fd0\u884c\u6b63\u5e38<\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">global is undefined\n\n\/\/index.html\n&lt;head&gt;\n&nbsp;&nbsp;&lt;script&gt;global&nbsp;=&nbsp;globalThis&lt;\/script&gt;\n&lt;\/head&gt;<\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\">\u82e5\u6240\u4f7f\u7528\u7684 npm \u5305\u4f7f\u7528\u4e86 node \u5185\u7f6e\u65b9\u6cd5\uff0c\u5219\u5e94\u5bfc\u5165\u5176 dist \u7248\uff0c\u4f8b\u5982\nimport&nbsp;SockJS&nbsp;from&nbsp;'sockjs-client'\n\u6539\u4e3a\nimport&nbsp;SockJS&nbsp;from&nbsp;'sockjs-client\/dist\/sockjs'\n\n\u56e0\u4e3a sockjs-client \u91cc\u9762\u5f15\u7528\u7684 eventsource \u4f7f\u7528\u4e86 node \u5185\u7f6e\u7684 util<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Vite2 \u5df2\u7ecf\u53d1\u5e03\uff0c\u5c1d\u8bd5\u5c06\u4e00\u4e2a\u57fa\u4e8e Vue CLI \u521b\u5efa\u7684 Vue2 \u9879\u76ee\u8fc1\u79fb\u5230 Vite \u4e0a\u6765\uff0c\u591a\u4e2a\u5730\u65b9\u9700 &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/longxi.me\/?p=6135110\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">\u201cVite2 + Vue2\uff0c\u57fa\u672c\u53ef\u7528\u201d<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[21],"tags":[25,22],"_links":{"self":[{"href":"https:\/\/longxi.me\/index.php?rest_route=\/wp\/v2\/posts\/6135110"}],"collection":[{"href":"https:\/\/longxi.me\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/longxi.me\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/longxi.me\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/longxi.me\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6135110"}],"version-history":[{"count":20,"href":"https:\/\/longxi.me\/index.php?rest_route=\/wp\/v2\/posts\/6135110\/revisions"}],"predecessor-version":[{"id":6135150,"href":"https:\/\/longxi.me\/index.php?rest_route=\/wp\/v2\/posts\/6135110\/revisions\/6135150"}],"wp:attachment":[{"href":"https:\/\/longxi.me\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6135110"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/longxi.me\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6135110"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/longxi.me\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6135110"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}