
<% include ../components/head %>
<% include ../components/header %>
<main>
<section class="section--blog">
<div class="container--info">
<p class="date">Updated: 11/20/2020</p>
<p class="estimate">Length: 23 minutes</p>
</div>
 <h1 id="how-i-made-this-blog">How I Made This Blog</h1>
<p><img src="imgs/daniel-olah-gEta6dbzFy0-unsplash.jpg" alt="Alt text" title="Photo by Daniel Olah"></p>
<figcaption>Photo by Daniel Olah</figcaption>
<p>First, this is a custom solution that's probably only applicable for small blogs (less than a few hundred posts). Anything more will take a long time to build, for reasons I will explain below.</p>
<p>Second, This blog was built just for fun. While webpack supports building multiple pages with multiple entry points, it was probably never intended for full-blown blog building. There are other tools better suited for the job, like Next.js, Huge, Jekyll, Eleventy, and the like.</p>
<p>That said, if you want to do something similar just for fun, read on!</p>
<h2 id="the-tech">The Tech</h2>
<p>I tried to make a blog using the SIMPLEST technlogies available, with as few tools as possible while still allowing for website optimizations.</p>
<ul>
<li><a href="https://ejs.co/">EJS</a> templates to reuse components.</li>
<li><a href="https://webpack.js.org/">Webpack v4</a> for the dev server, building, and optimizations.</li>
<li><a href="https://sass-lang.com/documentation/syntax">SASS</a> for reusing CSS.</li>
<li><a href="https://vanillajstoolkit.com/">Vanilla JS</a>, because I don't like dependencies. Webpack is terrible enough.</li>
<li><a href="https://greensock.com/docs/">GSAP</a> For animations. This website is actually my first time using GSAP.</li>
</ul>
<h3 id="ejs">EJS</h3>
<p>Why? Because I'm already very familiar with the syntax. And when building projects that you expect to last a while, it's probably best to use an 80/20 stack mix of tech you know well with new tech you want to learn, which is why...</p>
<h3 id="i-am-using-webpack-version-4">I am using Webpack version 4</h3>
<p>Webpack 5 just came out, and its release is a hot debate, because, well, it's like they didn't even try to see what would and wouldn't break. I know and love (and kind of hate) Webpack 4, so I'm stickin' with the old guns for now. Also, it's in plain Javascript, so even when I have to learn something new--like making a blog--it's not too much of a stretch figuring things out.</p>
<h3 id="sass-scss">SASS (scss)</h3>
<p>I love it. It's actually mostly unnecessary with the power of the current CSS3 api, but, again, I know it well and I enjoy it.</p>
<h3 id="vanilla-javascript">Vanilla Javascript</h3>
<p>This was a no-brainer. At the time of this writing, I'm only familiar with React. I'm no expert, but I'm certainly no novice at this point. I would love to learn Vue, Svelte, Preact, and even Angular, but I also want this site to be as fast as possible and rely on as few dependencies as possible. And that's not a realistic expection when using those Javascript libraries or frameworks. Vanilla Javascript is good. It's performant, and you really don't need more.</p>
<h3 id="gsap">GSAP</h3>
<p>Okay, GSAP is my 20%. I had never worked with GSAP before and had only heard good things. So I'm giving it a shot, and so far, it's wonderful. Very fun, very intuitive, and VERY powerful. Highly recommended.</p>
<h3 id="project-setup">Project Setup</h3>
<pre><code>root    |- configs...
        |- src  |- components
                |- fonts
                |- images
                |- pages
                |- posts
                |- scripts
                |- styles
                |- views
</code></pre>
<p>I have all the code in the root/src folder with configs for postcss and webpack under the root directory. I use separate webpack configs for dev and production and use webpack-merge to merge them with common configurations.</p>
<p>I am also hosting on Netlify, which is its own topic.</p>
<p>In my src folder, I have directories hosting my assets.</p>
<p>I have styles in its own folder, but you can host them any way you like, including next to their pages (or their views or their components or wherever. It doesn't matter).</p>
<p>Of particular importance are my pages, posts, and views folders.</p>
<h4 id="multi-page-sites-with-webpack">Multi-page sites with Webpack</h4>
<p>My Webpack entry points are pointing the pages directory. Each file there there is its own entrypoint, except for blog posts. There is 1 entrypoint for each post (in the posts directory), which all use the same styles and javscript. This is not ideal, but I couldn't get webpack to reuse my entrypoints with the splutChunks options. I might try it again later, but it was extremely frustrating.</p>
<h4 id="transform-markdown-to-ejs-and-then-to-plain-html">Transform Markdown to EJS and then to plain HTML</h4>
<p>I am using <a href="https://github.com/cenguidanos/node-markdown-parser">@cenguidanos/node-markdown-parser</a> to return an array of all my posts with their meta content such as title, description and keywords in a json object. To do that, I make use of <code>fs.readdirSync</code> and <code>fs.readFileSync</code> to grab the all the posts and their data.</p>
<p>With that data, I construct an ejs template that reuses header and footer components. I then write the template to a corresponding ejs file in the views directory using <code>fs.writeFile</code>.</p>
<h4 id="updating-posts-and-preventing-full-rewrites">Updating Posts and Preventing Full Rewrites</h4>
<p>The tools provided don't provide a simple way of checking if the date being written or read is new or old. In particular the markdown parser will simply create the file with a new updatedAt date, which can be problematic when you didn't update the post.</p>
<p>The way I dealt with this problem is actually be keeping the list of blog data in a simple database file called db.json, to which I compare the new blog data. We construct a new array of the blog data using old, unchanged entries with new and updated entries and write that to the 'database'. This way, the keep the meta data -- especially the updatedAt date -- in tact.</p>
<h4 id="dynamic-entry-object-for-webpack">Dynamic entry object for Webpack</h4>
<p>Before I create any html files, though, I have to create my entry points for Webpack. Using the pages directory, I construct an object with the name of the page file and its path. For posts, I begin with the name 'posts1', since I will need to create 1 entry for each blog post (ugh. Annoying. And VERY non-performant).</p>
<h4 id="the-html-files">The HTML files</h4>
<p>Now, in order to create all those html files with their css and js files embedded, I get all the ejs files from the views directory and use the <a href="https://www.npmjs.com/package/glob">glob</a> package to get their paths. It's also possible with the fs module, but glob is easier.</p>
<p>I then use <code>Array.reduce()</code> to construct a new array of HtmlWebpackPlugin instances of ALL the ejs files, injecting their meta data and associating the appropriate chunk (css and js file) with their names. if they are posts, I have to handle them slightly differently.</p>
<p>Here, I also check if the ejs file is associated with a post that was deleted (no longer in the posts directory). If it's not there, posts is undefined, so I use <code>fs.unlink</code> to delete it from the views.</p>
<p>In the Webpack Plugins array, I concat the HtmlWebpackPlugins array with MiniCssExtractPlugin, Critters to inline the css, and CleanWebpackPlugin.</p>
<p>The rest is of my webpack config is just configuring scss, ejs, injecting my fonts and images, and setting node rules for different modules.</p>
<p>And there you have it!</p>
<h4 id="caveat">Caveat</h4>
<p>With ejs templates in webpack, the dev server will <em>not</em> reload the browser if there are nested ejs templates. That's annoying when we are reusing components defined in .ejs files.</p>
<p>The dev server only checks if the entrypoint files or their indexes (the root ejs pages) have changed, not their nested templates.</p>
<h2 id="summary">Summary</h2>
<p>This was a lot of setup for a small blog. But it's pretty lightweight, and a lot easier to set up than a full-blown blog CRUD app.</p>
<p>That set, it's probably easier to use Huge, Jekyll, or Eleventy. I'll experiment with those later.</p> 
</section>
</main>
<% include ../components/footer %>
</html>
