<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Christian Erwin Siber on Medium]]></title>
        <description><![CDATA[Stories by Christian Erwin Siber on Medium]]></description>
        <link>https://medium.com/@siber.christian?source=rss-48bb3760ecf3------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*xc5jvRsqiZzyANWSj7nrjw.png</url>
            <title>Stories by Christian Erwin Siber on Medium</title>
            <link>https://medium.com/@siber.christian?source=rss-48bb3760ecf3------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 08:47:57 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@siber.christian/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Evolution of a Vue.js Application]]></title>
            <link>https://medium.com/@siber.christian/evolution-of-a-vue-js-application-d493c81525b8?source=rss-48bb3760ecf3------2</link>
            <guid isPermaLink="false">https://medium.com/p/d493c81525b8</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[vue]]></category>
            <category><![CDATA[vuejs]]></category>
            <category><![CDATA[webpack]]></category>
            <dc:creator><![CDATA[Christian Erwin Siber]]></dc:creator>
            <pubDate>Sun, 23 Jun 2019 16:00:08 GMT</pubDate>
            <atom:updated>2019-06-23T16:00:08.748Z</atom:updated>
            <content:encoded><![CDATA[<p>This article describes the Journey of a Vue.js application from it’s first steps to a full grown and promising Single Page Application.</p><h3>Goals</h3><p>Actually I’m not really going to describe the process of evolving a simple Single Page Application to a complex one. The main focus will be to describe the most simplistic way of setting up a Vue.js application and how it can be adapted to still be managable with more complex requirements.</p><p>This article does not describe how you should setup a application with Vue.js. It rather serves the purpose of documenting my learning process. I hope it helps you too.</p><h3>First steps</h3><p>First of all, we’re going to create the most simplistic Vue.js application possible. It only contains the basic html boilerplate with a template that renders the text <em>“Hello World”</em> (Classic!).</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e11335d21a548230ad0d42b0721d4fba/href">https://medium.com/media/e11335d21a548230ad0d42b0721d4fba/href</a></iframe><p>To reinforce the fact that every component actually consists of an options object, I extracted the actual component definition from the Vue instantiation.</p><p>In this case the app contains of a root component modeled by the options object inside <em>app</em>. To initialize it, we need to pass this options object to the Vue constructor.</p><p>Source Code at <a href="https://github.com/chrsi/vue-journey/tree/01_first-steps">Github</a></p><h3>The first “real” component</h3><p>Components are the building blocks of Vue.js. An application consists of a root component that contains zero or more sub components. Each one is modeled by an options object.</p><p>In this step we’re going to create the appHeader component that just contains a <em>h1 </em>element with the title provided as a prop.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ea38913c3c035b129db2cf5d015b8faa/href">https://medium.com/media/ea38913c3c035b129db2cf5d015b8faa/href</a></iframe><p>The template for the component is specified as a string property of the options object. This makes it really hard to maintain and I can’t think of an IDE that can provide syntax highlighting or any other method to improve the development experience.</p><p>But Vue.js provides us with alternative ways to define the template. Since they’re quite opinionated about the Single File Components (for a reason! … but I’ll come to that a bit later) the alternative ways are spread throughout the official documentation. Anthony Gore’s found 7 ways to define the template and listed them in an amazing article [1].</p><p>For now we’re going to make use of the inline template because it provides us with reusable components and separates the template definition from the code (which gives us syntax highlighting).</p><p>With inline-templates we define the template in a script tag of type <em>text/x-template</em>. The template property of the components objects object just needs to reference the id of the script tag (#my-app-header-template).</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e47e405f488341a0b62d2c80cf16d305/href">https://medium.com/media/e47e405f488341a0b62d2c80cf16d305/href</a></iframe><p>Those templates can’t be outsourced to an external file. For that reason I wouldn’t recommend to use this approach in an enterprise application.</p><p>Source Code at Github for <a href="https://github.com/chrsi/vue-journey/tree/02_first-component">First Component</a> and <a href="https://github.com/chrsi/vue-journey/tree/02_first-component-inline">First Component with inline-template</a></p><h3>Extracting the component</h3><p>Having all the components in the index.html file might work for a simple page with a handful of components. But as the application grows bigger the need for modularization arises. So let’s try to extract the objects object modeling the component from the html file.</p><p>We’re going to make use of the ES6 module standard. To do this, we just need to change the type of the script tags to <em>module</em>. This change enables us to use the import/export syntax.</p><p>The component can be extracted to it’s own file exporting the options object as a constant.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1881a4f213cf2484f4eb252a6f88de5d/href">https://medium.com/media/1881a4f213cf2484f4eb252a6f88de5d/href</a></iframe><p>The using file can then import the component using the following import statement.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3c605ecad66b5e7277d5547ffdbc7f87/href">https://medium.com/media/3c605ecad66b5e7277d5547ffdbc7f87/href</a></iframe><p>Now we extracted the component code from the html file. But the template is still in it and separated from the code. If we would continue to add components to the application we would end up with an enourmous html file that contains all the template definitions. Reusing components is not possible because we’d need to copy the template definition to every file that makes use of a component.</p><p>Source Code at <a href="https://github.com/chrsi/vue-journey/tree/03_extract-component">Github</a></p><h3>Single File Components to the rescue</h3><p>Single File Components contain the template, code and styles that are make up a component in ONE file. And thats a good thing, because it keeps the parts together that are tightly coupled and simplifies reusability.</p><p>All we need is a way for the client to interpret the files. And because adapting the client is not a feasable solution, we’re going to introduce a process that knows how to interpret each section and compiles it to browser friendly javascript. This compilation step is done with a combination of the following tools:</p><ul><li><a href="https://github.com/webpack/webpack">Webpack</a></li><li><a href="https://github.com/vuejs/vue-loader">vue-loader</a></li></ul><p>Webpacks job is to bundle a bunch of javascript files together. With the help of vue-loader it can interpret single file components and create a javascript representation that can be processed by all modern browsers.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d0a235b033c811474ba93ddf1b2f1165/href">https://medium.com/media/d0a235b033c811474ba93ddf1b2f1165/href</a></iframe><h4>Prepare the workspace for webpack</h4><p>To keep the configuration effort at a minimum, I’ll use the defaults as much as possible. This means we’ll have to reconfigure the workspace a bit. First we’re going to introduce two new folders: dist and src. The src folder contains all the source files that make up the <em>webpack</em>ed application. The result of the webpack process will be emitted to the dist folder by default.</p><p>Webpack will need an entry point for processing. This will be the file where we initialize our <em>ViewModel</em>. So we’ll extract the Vue initialization to its own file (src/index.js).</p><p>The changes can be viewed on this github diff: <a href="https://github.com/chrsi/vue-journey/commit/f322baedb6511ae1431bfe87ee1cef21f4103fb9">Prepare for webpack by extracting vue specific js code</a></p><h4>Setup Webpack</h4><p>Setting up webpack can simply be done by adding a configuration file that holds the entry point.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/16837208b4b55123f87e1ae2838413db/href">https://medium.com/media/16837208b4b55123f87e1ae2838413db/href</a></iframe><p>The changes can be viewed on this github diff: <a href="https://github.com/chrsi/vue-journey/commit/73a842922998e3b6c8713ded76bbe38560e8a86e">Add webpack</a></p><h4>Add the vue-loader</h4><p>To make the appHeader our first single file component, we’re going to rename the appHeader.component.js to appHeader.vue. A single file component consists of template, code and styles. Therefore we can remove the x-template from the index.html file and put it into the template tag of the new appHeader.vue.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/afc4d58c59be5a624f6f94f764e2ed23/href">https://medium.com/media/afc4d58c59be5a624f6f94f764e2ed23/href</a></iframe><p>The using vue component (in our case the root instance) must be adapted to retrieve the appHeaer from the new single file component.</p><p>The vue-loader module is required by webpack to be able to interpret single file components. It can be configured in the webpack configuration by adding</p><ul><li>the loader plugin to the plugins collection and</li><li>a rule that links the <em>vue</em> file extension with the loader.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/980/1*IQzYW37gPF2frsthb4ejWA.png" /></figure><p>By running npx webpack the application bundled and minified into a main.js file that is included in our index.html. Serving the dist folder will lead to the expected output.</p><p>The changes can be viewed on this github diff: <a href="https://github.com/chrsi/vue-journey/commit/651cdc103206c9fb7877c64b9b53ccc3afe9bfcb">Add vue-loader and make the app-header a sfc</a></p><p>Source Code at <a href="https://github.com/chrsi/vue-journey/tree/04_single-file-components">Github</a></p><h3>Scaffolding using the vue-cli</h3><p>Setting up the whole Vue environment with webpack might be exciting at first (who am i kidding?…) but gets tedious after a few times.</p><p>Thankfully Evan You and the open source community provided us with a cli tool for scaffolding those applications.</p><p>Running the command “vue create &lt;app-name&gt;” is all it needs to setup the environment with everything mentioned before (and more).</p><p>More information about the vue-cli can be found in the official docs [2].</p><h3>Conclusion</h3><p>We’ve seen that it’s fairly simple to creat a basic vue.js application. All we need is a index.html file containing a reference to a vue build and a few lines of javascript.</p><p>But applications that fit into a single html file are either quite rare or hard to maintain. The growing complexity of single page applications requires modularization to keep it maintainable. This can be done by extracting the components into independable modules. But since each component consists of a template, code and styles we’re faced with another challenge:</p><blockquote>How can we combine those thightly coupled parts?</blockquote><p>Vue.js provides us with Single File Components to anwer this question. With webpack and vue-loader those components can be compiled and bundled into a distributable package. This gives us the following benefits</p><ul><li>Enhanced maintainability for vue.js components during development.</li><li>Small bundles that can be easily distributed.</li></ul><p>What I really like about Vue.js is, that you don’t need tools like webpack. I would describe it as an approachable frontend framework, because it gives you the opportunity to setup a single page application in a failry simple way. But when you’re about to create a genuine application its best to stick with the already established tools.</p><p>So when setting up you’re next project enjoy the delightful development experience vue-cli is giving you. Having this amazing scaffolding process makes me forget the strenuous time we had with jQuery back in the days.</p><p>I hope you enjoyed my article about evolving a small index.html page to a Single Page Application with Vue.js. Thanks for reading this far … you deserve a great day!</p><p>The repository containing the examples can be found at: <a href="https://github.com/chrsi/vue-journey">Github</a></p><p>[1] Anthony Gore; 7 Ways To Define A Component Template in Vue.js; <a href="https://vuejsdevelopers.com/2017/03/24/vue-js-component-templates/">https://vuejsdevelopers.com/2017/03/24/vue-js-component-templates</a></p><p>[2] Official Documentation for Vue CLI; <a href="https://cli.vuejs.org/guide/">https://cli.vuejs.org/guide/</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d493c81525b8" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>