<p>We love static websites. Why? Because they're fast. Really fast. Moreover, you don't have to take care of a database, and you like that a lot.</p> <p>You are probably building your site already with Jekyll, and while this is cool, sometimes you want to add some magic into the mix to have some fancy JS frameworks like VueJS or React for developing cool stuff. Also, you got so used to Babel and Webpack that you don't know how to write JS code anymore without this toolchain. Hence, you have a question: can I still use my Jekyll site and add as toppings VueJS + Babel + Webpack? Yes, you can! Let me explain how. Let's begin the hacking!</p> <div style="width:100%;height:0;padding-bottom:100%;position:relative;"><iframe src="https://giphy.com/embed/JIX9t2j0ZTN9S" width="100%" height="100%" style="position:absolute" frameBorder="0" class="giphy-embed" allowFullScreen></iframe></div><p><a href="https://giphy.com/gifs/JIX9t2j0ZTN9S">via GIPHY</a></p> <h2>Making Jekyll speak JSON</h2> <p>As you will be using a framework like <a href="https://vuejs.org/">VueJS</a> (we love love love love it so much, that from now on we will only talk about this framework), we need to instruct Jekyll to serve our content as JSON.</p> <p>There are several options for serving your content as JSON, but one that we like a lot is using the _data folder to store inside of it YAML (or JSON directly) that will be transformed into JSON in the HTML page that your VueJS app will be rendered.</p> <p>Wait, yes, you read that we will be writing content in YAML to transform it into JSON and then load it into VueJS. Why so much trouble? Well, because we want to allow non-coders to be able to add or edit content quickly, and YAML is simple on that (in other words, less curly braces 😉).</p> <p>Thus, go to your _data folder and create a file named <strong>mydata.yml</strong>. This file could have something like this:</p> <pre><code class="language-yaml">- title: How crowdsourcing can help beat cancer person: profile.jpg cover: logo.png person_name: John Doe person_position: Digital Solutions Architect </code></pre> <p>Then go to your Jekyll folder where you will be rendering your VueJS app. Let's see you want to build a hello world URL, so go to the folder helloworld and edit a file named index.html.</p> <p>This file will have the front matter as any other Jekyll file, but you will have to modify it to render your VueJS app:</p> <pre><code class="language-html">--- layout: default title: Hello World description: Jekyll and Vuejs --- &lt;section id=&quot;vuejs&quot; class=&quot;section&quot;&gt; &lt;App&gt;&lt;/App&gt; &lt;/section&gt; &lt;script&gt; window.mydata = {{ site.data.mydata | jsonify }}; &lt;/script&gt; &lt;script src=&quot;/assets/js/myvuejs.min.js&quot;&gt;&lt;/script&gt; </code></pre> <p>That's all! We provide the DOM for mounting the VueJS app. Then, we create a script section where we can load our mydata.yaml as a JSON object, and below it our minified version of our VueJS app (thanks Webpack!).</p> <p>Obviously, you will need to compile webpack and Jekyll commands to build everything correctly. As both command support a --watch flag, you can run both of them in parallel and forget about running the commands by hand while you develop your excellent new site.</p> <h2>VueJS, Bulma CSS, and Webpack</h2> <p>Well, this has been easy, right? But, how do we adequately integrate our webpack toolchain into the site as well? We just instruct webpack to do it properly.</p> <h3>Webpack</h3> <pre><code class="language-javascript">// webpack.config.js var htmlWebpackPlugin = require('html-webpack-plugin'); var webpack = require(&quot;webpack&quot;); module.exports = { // entry point of our application entry: './helloworld.js', // where to place the compiled bundle output: { path: '../', publicPath: '../', filename: 'helloworld.min.js' }, module: { // `loaders` is an array of loaders to use. // here we are only configuring vue-loader loaders: [ { test: /\.vue$/, // a regex for matching all files that end in `.vue` loader: 'vue-loader' // loader to use for matched files }, { test: /\.js$/, loader: 'babel-loader', exclude: /node_modules/ }, { test: /\.[sa|sc|c]ss$/, loader: &quot;style!css!sass&quot; }, { test: /\.(png|jpg|jpeg|gif|svg|woff|woff2|ttf|eot)$/, loader: 'file-loader?outputPath=../img/search/&amp;publicPath=../img/search/' } ], noParse: /dist\/ol.js/, }, vue: { loaders: { scss: 'style!css!sass' }}, resolve: { alias: { 'vue$': 'vue/dist/vue.common.js' } }, plugins: [ new webpack.optimize.DedupePlugin(), new webpack.optimize.UglifyJsPlugin({minimize: true}) ] } </code></pre> <p>NOTE: adapt the paths to your specific needs. These are just examples, so feel free to modify them to whatever you like.</p> <p>As you can see, we're telling webpack to compile our code, minimize it and put it in the right place that we want. Now, you can jump into your VueJS app.</p> <h3>VueJS</h3> <p>We only use VueJS components. Thus, our example will be using a component that will render our data. The first element that we have to add is our entry point: helloworld.js</p> <pre><code class="language-javascript">import Vue from &quot;vue&quot; import App from &quot;./components/App.vue&quot; Vue.config.debug = true Vue.config.devtools = true var app = new Vue({ data() { return {foo: 'all'} }, el: '#vuejs', components: { App }, }) </code></pre> <p>Then, create a components folder and create a file named App.vue:</p> <pre><code class="language-javascript">&lt;template&gt; &lt;h1 v-for=&quot;datum in mydata&quot;&gt;{{datum.title}}&lt;/h1&gt; &lt;/template&gt; &lt;script&gt; export default { data() { return {mydata: window.mydata} }, // your stuff } &lt;/script&gt; &lt;style&gt; &lt;/style&gt; </code></pre> <p>Done! Now you are loading your data created in mydata.yml file into your VueJS app. Now you are free to do whatever you want, as you are in the field of VueJS. Enjoy!</p> <p><strong>NOTE</strong>: Jekyll sometimes does not recompile the _data folder, so you will need to re-run it to be sure that your data is updated.</p> <h3>Bulma and Buefy</h3> <p>We use SaSS to style our Jekyll sites; I guess you do it too. If this is the case, you don't want to have separate sass folders to build your website and your VueJS apps. You can solve it by instructing your VueJS to re-use the Bulma CSS framework. How? Like this:</p> <pre><code>&lt;style lang=&quot;scss&quot;&gt; @import &quot;../../../_sass/_scifabric.scss&quot;; @import &quot;~buefy/src/scss/buefy&quot;; // your SCSS &lt;/style&gt; </code></pre> <h2>Cavebeats</h2> <p>While this is an excellent <strong>hack</strong> it's not the best solution. It would be much much better to use only a static website generator built with Node.JS or just something like <a href="https://nuxtjs.org/">nuxt.js</a>, but we needed to re-use our Jekyll infrastructure and therefore the hack.</p> <p>In any case, this hack has space for improvement. The most noticeable one would be to not include the CSS from Jekyll where we only use VueJS to avoid downloading the same stuff twice. If you like it, let us know and share this article with your colleagues and friends!</p> <h3>Final notes</h3> <p>This was originally posted in our <a href="https://scifabric.com">Scifabric</a> site.</p>