<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Rui Wu</title>
    <description>The latest articles on DEV Community by Rui Wu (@modyqyw).</description>
    <link>https://dev.to/modyqyw</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1424952%2Fe11adf60-8c1c-444f-9bbd-c3b80a3ccf58.png</url>
      <title>DEV Community: Rui Wu</title>
      <link>https://dev.to/modyqyw</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/modyqyw"/>
    <language>en</language>
    <item>
      <title>Make your Vite applications run a little faster</title>
      <dc:creator>Rui Wu</dc:creator>
      <pubDate>Sat, 14 Sep 2024 08:49:39 +0000</pubDate>
      <link>https://dev.to/modyqyw/make-your-vite-applications-run-a-little-faster-5bp7</link>
      <guid>https://dev.to/modyqyw/make-your-vite-applications-run-a-little-faster-5bp7</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdpx6sahhwhmpa1tirko9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdpx6sahhwhmpa1tirko9.png" alt="make-your-vite-applications-run-a-little-faster"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Background
&lt;/h2&gt;

&lt;p&gt;While Vite runs fast by default, performance issues can creep in as your project's needs grow. This article hopes to present different optimizations from multiple perspectives to make your Vite application run a little bit faster, solving problems such as slow server startups, slow page loads, and slow builds.&lt;/p&gt;

&lt;h2&gt;
  
  
  Browser
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Extensions
&lt;/h3&gt;

&lt;p&gt;For the most part, we view our Vite applications in a browser and develop debugging. Some browser extensions (e.g. de-advertising extensions uBlock, adGuard) may interfere with the requests, resulting in slower startups and refreshes. In this case, it is recommended to use a plugin-free configuration dedicated to development, and it is also recommended to use Traceless Mode to get faster speeds.&lt;/p&gt;

&lt;h3&gt;
  
  
  Developer Tools
&lt;/h3&gt;

&lt;p&gt;In addition, the Vite Development Server has a strong cache of pre-packaged dependencies and a fast 304 response to source code, which may significantly affect startup speed and full page refresh time if the cache is disabled while the Developer Tools are open. Therefore, it is recommended to turn off the "Disable Cache" feature to ensure faster startup and full page refresh.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fljlpijfd1ll9ncmnwh27.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fljlpijfd1ll9ncmnwh27.png" alt="disable-cache"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Vite configuration
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Plugins
&lt;/h3&gt;

&lt;p&gt;Official Vite plugins have been optimizing performance, such as &lt;a href="https://github.com/vitejs/vite-plugin-react/pull/212" rel="noopener noreferrer"&gt;vitejs/vite-plugin-react&lt;/a&gt; which reduces Node.js startup time by dynamically importing large dependencies.&lt;/p&gt;

&lt;p&gt;Community plugins may not be as concerned about performance, which in turn affects the developer experience.&lt;/p&gt;

&lt;p&gt;As an example, &lt;a href="https://github.com/gxmari007/vite-plugin-eslint" rel="noopener noreferrer"&gt;vite-plugin-eslint&lt;/a&gt; is still recommended in many community articles, but as of today it hasn't been updated in two years, and support for newer versions of Vite and ESLint is mediocre.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvgdb5hx3vm8eivsecszx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvgdb5hx3vm8eivsecszx.png" alt="vite-plugin-eslint-2-years-ago-updated"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;On the other hand, it forces ESLint checksums to be run in the &lt;code&gt;buildStart&lt;/code&gt; and &lt;code&gt;transform&lt;/code&gt; hooks, where checksums in &lt;code&gt;buildStart&lt;/code&gt; can lead to long waits during the startup of the development server, delaying the time when the site can be accessed in the browser, and checksums in the &lt;code&gt;transform&lt;/code&gt; hooks can lead to some files loading slower than others, delaying the time when the site is loaded in the browser. The slower some files load than others, the more noticeable the request waterfall in the browser when loading the site, and the more noticeable the lag that can be felt during development.&lt;/p&gt;

&lt;p&gt;Although the purpose of ESLint validation is achieved, the development experience is degraded and the development speed is reduced, is it worth it? This is a question worth considering.&lt;/p&gt;

&lt;p&gt;There are many ways to identify this performance issue, such as using &lt;code&gt;vite --debug plugin-transform&lt;/code&gt; or &lt;a href="https://github.com/antfu-collective/vite-plugin-inspect" rel="noopener noreferrer"&gt;vite-plugin-inspect&lt;/a&gt; to Alternatively, you can visit the site after running &lt;code&gt;vite --profile&lt;/code&gt;, press &lt;code&gt;p + enter&lt;/code&gt; in the terminal to record a &lt;code&gt;.cpuprofile&lt;/code&gt;, and then use a tool like &lt;a href="https://www.speedscope.app/" rel="noopener noreferrer"&gt;speedscope&lt;/a&gt; to examine the configuration file and identify bottlenecks.&lt;/p&gt;

&lt;p&gt;Once you've identified a performance issue, you can consider forking and improving the appropriate plugin, or just using a replacement. For the example above, you might consider replacing it with &lt;a href="https://github.com/fi3ework/vite-plugin-checker" rel="noopener noreferrer"&gt;vite-plugin-checker&lt;/a&gt;, &lt;a href="https://github%20.com/nabla/vite-plugin-eslint" rel="noopener noreferrer"&gt;@nabla/vite-plugin-eslint&lt;/a&gt; or &lt;a href="https://github.com/ModyQyW/vite-plugin-eslint2" rel="noopener noreferrer"&gt;vite-plugin-eslint2&lt;/a&gt;, all of which support asynchronous ESLint checksums so as not to particularly impact performance and development experience, while still serving the purpose of ESLint checksums.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Interests: I am the author of vite-plugin-eslint2, welcome to communicate and discuss.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Toolchain
&lt;/h3&gt;

&lt;p&gt;Streamlining your toolchain is a great way to speed things up.&lt;/p&gt;

&lt;p&gt;As an example, many people use SCSS primarily for variables and nesting, but in reality CSS &lt;a href="https://caniuse.com/css-variables" rel="noopener noreferrer"&gt;variables&lt;/a&gt; and &lt;a href="https://caniuse.com/css-nesting" rel="noopener noreferrer"&gt;nesting&lt;/a&gt; have both officially landed.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F629ig409hvbd8fkm4xm0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F629ig409hvbd8fkm4xm0.png" alt="css-variables"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftcusf644zq9m3f8vsvn8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftcusf644zq9m3f8vsvn8.png" alt="css-nesting"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If the production environment is going to support slightly older versions of browsers that don't support nesting, it's entirely possible to use PostCSS to handle nesting in production builds so that it doesn't impact the speed of the Vite development server. Very, very old versions of browsers are neither secure nor fast and have a poor experience, and should be gradually guided to phase out this part of the process to improve the user experience and the development experience, check out &lt;a href="https://browser-update.org/update-browser.html" rel="noopener noreferrer"&gt;browser-update&lt;/a&gt; if you're interested.&lt;/p&gt;

&lt;p&gt;Using a more raw toolchain is also a good way to speed things up; the SWC website shows it to be 20 to 70 times faster than Babel, and there are tons of speed advantages in complex real-world applications, which proves that rawness can be a big help in speeding things up. Instead of &lt;code&gt;vite-plugin-react&lt;/code&gt;, you can use &lt;a href="https://github.com/vitejs/vite-plugin-react-swc" rel="noopener noreferrer"&gt;@vitejs/plugin-react-swc&lt;/a&gt;, with &lt;a href="//https://%20github.com/vitejs/vite/discussions/13835"&gt;LightningCSS&lt;/a&gt; instead of PostCSS, &lt;a href="https://swc.rs/" rel="noopener noreferrer"&gt;SWC&lt;/a&gt; or &lt;a href="https://esbuild.github.io/" rel="noopener noreferrer"&gt;esbuild&lt;/a&gt; instead of Babel, etc. etc. to achieve better performance.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F779y2z2qxrgr96rcqzeb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F779y2z2qxrgr96rcqzeb.png" alt="swc-speed"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj7p13uawhufl3vyntzbm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj7p13uawhufl3vyntzbm.png" alt="lightningcss-speed"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Dependency pre-bundling
&lt;/h3&gt;

&lt;p&gt;Vite converts dependencies provided as CommonJS or UMD into ES modules, and also converts ESM dependencies with many internal modules into a single module - this is Vite's dependency pre-bundling.&lt;/p&gt;

&lt;p&gt;The former is for compatibility reasons, which we won't discuss in detail here. The latter is for performance reasons; without this step, when we execute &lt;code&gt;import { debounce } from 'lodash-es'&lt;/code&gt;, the browser will make more than 600 HTTP requests at the same time, corresponding to more than 600 modules of &lt;code&gt;lodash-es&lt;/code&gt;! Even though the server can handle them easily, the large number of requests causes network congestion on the browser side and makes the page load significantly slower.&lt;/p&gt;

&lt;p&gt;This is why we need to rely on pre-bundling dependencies; Vite itself does this automatically and transparently, and we can explicitly specify which dependencies need to be pre-bundled via configuration.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;

&lt;span class="c1"&gt;// vite.config.ts&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;defineConfig&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;vite&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;pkg&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./package.json&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nf"&gt;defineConfig&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;optimizeDeps&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;include&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;keys&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pkg&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;dependencies&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  Warming up
&lt;/h3&gt;

&lt;p&gt;Warming up is an often overlooked optimization, and one that I've barely seen in some of the articles that have been written about Vite, but it can be an effective way to improve the development experience and reduce wait times.&lt;/p&gt;

&lt;p&gt;By default, the Vite Development Server only converts files requested by the browser on demand, which allows it to start up quickly and only perform conversions on the files it uses.&lt;/p&gt;

&lt;p&gt;However, this practice can leave the Vite Development Server idle, resulting in a wait for switching pages to load, leading to a poor development experience. If certain files are expected to be requested for a short period of time, they can be converted and cached in advance to improve page load speed. This practice is known as warming up.&lt;/p&gt;

&lt;p&gt;Frequently used files can be found by running &lt;code&gt;vite --debug transform&lt;/code&gt; and examining the logs, then adding it inside the Vite configuration.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

vite:transform 28.72ms /@vite/client +1ms
vite:transform 62.95ms /src/components/BigComponent.vue +1ms
vite:transform 102.54ms /src/utils/big-utils.js +1ms


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;

&lt;span class="c1"&gt;// vite.config.ts&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;defineConfig&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;vite&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nf"&gt;defineConfig&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;server&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;warmup&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;clientFiles&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./src/components/BigComponent.vue&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./src/utils/big-utils.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Note that only frequently used files should be warmed up, this is to avoid overloading the Vite development server. However, I still prefer to just warm up them all straight away. 🤪&lt;/p&gt;

&lt;p&gt;The latest version of Nuxt has this feature configured, you can also check &lt;a href="https://cn.vitejs.dev/config/server-options.html#server-warmup" rel="noopener noreferrer"&gt;Vite / Configuration / server.warmup&lt;/a&gt; for more information.&lt;/p&gt;

&lt;p&gt;For 4.3 &amp;lt;= Vite &amp;lt; 5, &lt;a href="https://github.com/bluwy/vite-plugin-warmup" rel="noopener noreferrer"&gt;vite-plugin-warmup&lt;/a&gt; is required, there is no support for lower versions.&lt;/p&gt;

&lt;h2&gt;
  
  
  Project
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Path resolution
&lt;/h3&gt;

&lt;p&gt;It may be difficult to predict how expensive it is to parse imported paths.&lt;/p&gt;

&lt;p&gt;When you try to use &lt;code&gt;import './Component'&lt;/code&gt; to import &lt;code&gt;./Component.jsx&lt;/code&gt;, Vite will run the following steps to parse it:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Check if &lt;code&gt;./Component&lt;/code&gt; exists, it does not.&lt;/li&gt;
&lt;li&gt;Check for &lt;code&gt;./Component.mjs&lt;/code&gt; exists and does not exist.&lt;/li&gt;
&lt;li&gt;Checks if &lt;code&gt;./Component.js&lt;/code&gt; exists and does not exist.&lt;/li&gt;
&lt;li&gt;Check &lt;code&gt;./Component.mts&lt;/code&gt; exists, does not exist.&lt;/li&gt;
&lt;li&gt;Checks if &lt;code&gt;./Component.ts&lt;/code&gt; exists and does not exist.&lt;/li&gt;
&lt;li&gt;Checks if &lt;code&gt;./Component.jsx&lt;/code&gt; exists, it does!&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Parsing a simple imported path takes 6 filesystem checks! The more implicit imports there are, the more time it takes to resolve the path.&lt;/p&gt;

&lt;p&gt;Therefore, it's usually better to explicitly import the path, e.g. &lt;code&gt;import './Component.jsx'&lt;/code&gt;, you can also narrow down the list of &lt;a href="https://cn.vitejs.dev/config/shared-options.html#resolve-extensions" rel="noopener noreferrer"&gt;resolve.extensions&lt;/a&gt; to minimize the general filesystem checking but must make sure that it also applies to files in node_modules.&lt;/p&gt;

&lt;p&gt;If you want to use ESLint to force explicit import paths, you can configure &lt;a href="https://github.com/import-js/eslint-plugin-import/blob/main/docs/rules/extensions.md" rel="noopener noreferrer"&gt;import/extensions&lt;/a&gt; or &lt;a href="https://github.com/un-ts/eslint-plugin-import-x/blob/master/docs/rules/extensions.md" rel="noopener noreferrer"&gt;import-x/extensions&lt;/a&gt; rule, as shown below.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

"import/extensions": ["warn", "ignorePackages"]
"import-x/extensions": ["warn", "ignorePackages"]


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  Barrel files
&lt;/h3&gt;

&lt;p&gt;Barrel files are files that re-export the APIs of other files in the same directory. For example:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="c1"&gt;// src/utils/index.js&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./color.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./dom.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./slash.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;When you only import a single API, such as &lt;code&gt;import { slash } from '. /utils'&lt;/code&gt;, you still need to fetch and convert all the files in the bucket file, as they may contain the slash API and may also contain other side effects that run at initialization time. This means that on initial page load, you are loading more files than needed, which in turn leads to slower page loads. As there are more and more modules, the loading time increases, and tests have shown that loading time shows a near-exponential or quasi-exponential increase with the number of modules.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvlop4l7nwjynl20kimve.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvlop4l7nwjynl20kimve.png" alt="js-tools-module-cost"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If possible, you should try to avoid using barrel files and import individual APIs directly. you can read &lt;a href="https://marvinh.dev/blog/speeding-up-javascript-ecosystem-part-7/" rel="noopener noreferrer"&gt;Speeding up the JavaScript ecosystem - The barrel file debacle&lt;/a&gt; for more details and data, the image above is from this article.&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;This article was written with reference to &lt;a href="https://vitejs.dev/guide/performance.html" rel="noopener noreferrer"&gt;Vite Official Documentation / Performance&lt;/a&gt;, and I would like to express my heartfelt thanks to the Vite team. 🙏&lt;/p&gt;

&lt;p&gt;This article describes different optimization approaches from three aspects: browser, Vite configuration, and project, with the goal of making Vite apps run a little bit faster and solving problems such as slow server startups, slow page loads, and slow builds.&lt;/p&gt;

&lt;p&gt;However, it's important to note that these methods are not a panacea and they won't affect performance in a production environment. To optimize the production environment performance, you need to consider splitting the code, optimizing static resources, introducing SSR, introducing CDN, preloading, prefetching, optimizing CSS, caching, Web Worker, reducing DOM operations and so on, these will not be here to expand in detail, interested in leaving a message to ask me to write about it.&lt;/p&gt;

&lt;p&gt;It is worth mentioning that Vite is not everything, as &lt;a href="https://www.farmfe.org/zh/docs/why-farm" rel="noopener noreferrer"&gt;Farm documentation&lt;/a&gt; said, inconsistencies between development and production, unpacking optimization difficulties and other issues still need to be resolved, all of which require us to wait patiently, or actively participate in it. If you can't wait any longer, we recommend checking out &lt;a href="https://www.farmfe.org/zh/" rel="noopener noreferrer"&gt;Farm&lt;/a&gt; or &lt;a href="https://rsbuild.dev/" rel="noopener noreferrer"&gt;Rsbuild&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I hope this article can bring you some inspiration and help!&lt;/p&gt;

</description>
      <category>frontend</category>
      <category>vite</category>
      <category>performance</category>
    </item>
  </channel>
</rss>
