<?xml version="1.0" encoding="utf-8" ?>
<rss version="2.0">
<channel>
<title>Fun with ones and zeros - javascript</title>
<description><![CDATA[Barry's notes on computer software and hardware]]></description>
<link>/blog/tags/javascript</link>
<lastBuildDate>Thu, 21 May 2026 03:40:17 -0700</lastBuildDate>
<item>
<title>Dynamic loading of Vue CSS assets with Vite</title>
<link>/blog/entries/dynamic-loading-of-vue-css-assets-with-vite</link>
<pubDate>Wed, 02 Jun 2021 12:15:00 -0700</pubDate>
<author>bp@barryp.org (Barry Pederson)</author>
<description><![CDATA[<body><p>I've been experimenting with switching from Webpack to <a href="https://vitejs.dev/">Vite</a>, because when developing with Vite it's really fast
and you get some nice features like Hot Module Reloading (HMR), so that when you're editing a Vue component and save
the file, you instantly see the results in your browser window without having to reload the page.</p>
<p>I've been using Vite with the <a href="https://vitejs.dev/guide/backend-integration.html">Backend Integration</a> instructions, and on a Laravel Lumen site one
problem I had was that while Vite worked great for development - when I did a production build I found the CSS from
the Vue SFCs (Single File Components) weren't being injected into the &lt;head&gt; the way the were when running
in development mode.  Basically my page would be there but totally unstyled.</p>
<p>I had another Vite site that worked totally fine with the production build (this one), and it took a while to figure
out what the difference was.   </p>
<p>Ultimately I realized the working site had as part of its JS build, some code from vite called <code>preload-helper</code>, which seems
to be responsible for injecting stylesheets into the page.  My other site wasn't including the <code>preload-helper</code>, and it
seems the difference was that the 2nd site wasn't using the dynamic JS <code>import()</code>, feature (because it was such a
simple site)</p>
<p>Once I recoded my entrypoint to dynamically import my Vue3 root component like this:</p>
<div class="source"><pre><span></span><span class="kr">import</span> <span class="p">{</span> <span class="nx">createApp</span> <span class="p">}</span> <span class="nx">from</span> <span class="s1">'vue'</span>

<span class="kr">import</span><span class="p">(</span><span class="s1">'./App.vue'</span><span class="p">)</span>
    <span class="p">.</span><span class="nx">then</span><span class="p">(({</span> <span class="k">default</span><span class="o">:</span> <span class="nx">rootComponent</span> <span class="p">})</span> <span class="p">=&gt;</span> <span class="p">{</span>
        <span class="kr">const</span> <span class="nx">app</span> <span class="o">=</span> <span class="nx">createApp</span><span class="p">(</span><span class="nx">rootComponent</span><span class="p">);</span>
        <span class="nx">app</span><span class="p">.</span><span class="nx">mount</span><span class="p">(</span><span class="s1">'#app'</span><span class="p">);</span>
    <span class="p">});</span>
</pre></div>
<p>then the build included the <code>preload-helper</code> and everything works as expected.</p>
<p>Without this type of loading, a person would have to configure Vite to enable generation of <code>manifest.json</code>, and figure
out which CSS file to reference in your page's HEAD, similar to how you figure out the JS to load (because of doing
Backend Integration).  However at the moment the <code>manifest.json</code> doesn't include the name of the generated CSS file - I
filed a <a href="https://github.com/vitejs/vite/issues/3629">Github Issue</a> about it.</p></body>]]></description>
</item>
</channel>
</rss>