<?xml version="1.0" encoding="utf-8" ?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>Fun with ones and zeros - javascript</title>
<subtitle>Barry&#039;s notes on computer software and hardware</subtitle>
<link href="/blog/tags/javascript"></link>
<updated>2026-05-21T03:39:41-07:00</updated>
<id>urn:uuid:1d0a54c9-1d86-916a-6941-f41d43259c9e</id>
<entry>
<title>Dynamic loading of Vue CSS assets with Vite</title>
<link href="/blog/entries/dynamic-loading-of-vue-css-assets-with-vite"></link>
<id>urn:uuid:cf211cfc-fb90-1d78-ecdb-db669be884e2</id>
<updated>2021-06-02T12:15:00-07:00</updated>
<author><name>Barry Pederson</name>
<email>bp@barryp.org</email>
</author>
<content type="html">&lt;body&gt;&lt;p&gt;I&#039;ve been experimenting with switching from Webpack to &lt;a href=&quot;https://vitejs.dev/&quot;&gt;Vite&lt;/a&gt;, because when developing with Vite it&#039;s really fast
and you get some nice features like Hot Module Reloading (HMR), so that when you&#039;re editing a Vue component and save
the file, you instantly see the results in your browser window without having to reload the page.&lt;/p&gt;
&lt;p&gt;I&#039;ve been using Vite with the &lt;a href=&quot;https://vitejs.dev/guide/backend-integration.html&quot;&gt;Backend Integration&lt;/a&gt; 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&#039;t being injected into the &amp;lt;head&amp;gt; the way the were when running
in development mode.  Basically my page would be there but totally unstyled.&lt;/p&gt;
&lt;p&gt;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.   &lt;/p&gt;
&lt;p&gt;Ultimately I realized the working site had as part of its JS build, some code from vite called &lt;code&gt;preload-helper&lt;/code&gt;, which seems
to be responsible for injecting stylesheets into the page.  My other site wasn&#039;t including the &lt;code&gt;preload-helper&lt;/code&gt;, and it
seems the difference was that the 2nd site wasn&#039;t using the dynamic JS &lt;code&gt;import()&lt;/code&gt;, feature (because it was such a
simple site)&lt;/p&gt;
&lt;p&gt;Once I recoded my entrypoint to dynamically import my Vue3 root component like this:&lt;/p&gt;
&lt;div class=&quot;source&quot;&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&quot;kr&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;createApp&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;&#039;vue&#039;&lt;/span&gt;

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