<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Ashlin Aronin</title>
    <description>The latest articles on DEV Community by Ashlin Aronin (@ashlincascade).</description>
    <link>https://dev.to/ashlincascade</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F182634%2F81b98e63-f0ac-45e5-8d84-30df34eccecf.jpeg</url>
      <title>DEV Community: Ashlin Aronin</title>
      <link>https://dev.to/ashlincascade</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ashlincascade"/>
    <language>en</language>
    <item>
      <title>Tangible software development</title>
      <dc:creator>Ashlin Aronin</dc:creator>
      <pubDate>Wed, 17 Jul 2019 22:39:28 +0000</pubDate>
      <link>https://dev.to/cascade-energy/tangible-software-development-5dkn</link>
      <guid>https://dev.to/cascade-energy/tangible-software-development-5dkn</guid>
      <description>&lt;p&gt;When I first started writing software professionally, I was puzzled by the fixation on tools. A few years later, I’ve come to realize that the tools we use drastically affect both the efficiency of our work and how fulfilling it is. For comparison, imagine a carpenter framing a house without power tools. It can be done, but requires much more time and effort.&lt;/p&gt;

&lt;p&gt;At Cascade Energy, we’re a small dev-ops team with a lot of work to do. This makes our choice of tools important. Over the past year, we’ve introduced automated deployments, static analysis and re-formatting (with &lt;code&gt;CodePipeline&lt;/code&gt;, &lt;code&gt;eslint&lt;/code&gt; and &lt;code&gt;prettier&lt;/code&gt;, respectively). This post will focus on one slightly more experimental tool that is redefining our workflow: &lt;strong&gt;hot reloading&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Recently I was tasked with adding a new feature to our customer-facing React application. Here is the process I went through, repeatedly:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Decide on a small unit of change I could do at a time&lt;/li&gt;
&lt;li&gt;Read through the code and click through the production application to figure out how it currently works&lt;/li&gt;
&lt;li&gt;Read tests related to this component&lt;/li&gt;
&lt;li&gt;Make a small change to the code itself&lt;/li&gt;
&lt;li&gt;Save the file&lt;/li&gt;
&lt;li&gt;Switch to my web browser&lt;/li&gt;
&lt;li&gt;Refresh the page&lt;/li&gt;
&lt;li&gt;Select a sample customer from a dropdown menu to display data on the page&lt;/li&gt;
&lt;li&gt;Scroll down the page to the component I'm working on&lt;/li&gt;
&lt;li&gt;Click on the component&lt;/li&gt;
&lt;li&gt;See if my change worked&lt;/li&gt;
&lt;li&gt;If not, repeat&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Many of these steps are unavoidable. For instance, most developers will tell you they spend more time reading code than writing it. However, we can consolidate steps 6-10 using hot reloading. With hot reloading configured, every small change I make automatically registers in the web browser, with the surrounding context preserved. There’s a lot of plumbing to make this happen, but once it’s set up, it’s magical.&lt;/p&gt;

&lt;p&gt;These days, when creating a new frontend application, you can use a pre-configured starter pack that already has hot reloading and other productivity features out of the box (&lt;code&gt;vue-cli&lt;/code&gt;, &lt;code&gt;create-react-app&lt;/code&gt;, etc). In this case, we couldn't lean on these tools since this was an existing application with some custom configuration.&lt;/p&gt;

&lt;p&gt;Our setup is a Node backend layer which handles connections to our other services and serves up our frontend React application. We use &lt;code&gt;webpack&lt;/code&gt; as our frontend build system.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;webpack&lt;/code&gt; team maintains the pre-packaged &lt;code&gt;webpack-dev-server&lt;/code&gt; module, but it wouldn’t work for our purposes, since the backend and frontend of our application are intertwined. If the backend of our application were built using &lt;code&gt;express&lt;/code&gt;, then we could configure the server to use &lt;code&gt;webpack-dev-middleware&lt;/code&gt; (used by &lt;code&gt;webpack-dev-server&lt;/code&gt; under the hood) directly. However, we’re using &lt;code&gt;hapi&lt;/code&gt;, which doesn’t support Express-style middleware.&lt;/p&gt;

&lt;p&gt;Only slightly discouraged, I took a deep breath and pushed on. It was still possible to write a wrapper around &lt;code&gt;webpack-dev-middleware&lt;/code&gt;. Fortunately, I found an article that got me started-- a &lt;a href="https://medium.com/@tkh44/setting-up-your-front-end-dev-environment-with-webpack-with-hapi-b352ab8b2f9c"&gt;tutorial for writing a &lt;code&gt;hapi&lt;/code&gt; middleware adaptor for &lt;code&gt;webpack&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I borrowed the basic premise of hooking into &lt;code&gt;hapi&lt;/code&gt;’s onRequest and onPreResponse lifecycle extension points to intercept requests and pipe them to &lt;code&gt;webpack&lt;/code&gt; so it can handle hot reloading. However, I didn’t find the author’s suggestion of &lt;code&gt;webpack-dashboard&lt;/code&gt; to be any more helpful than &lt;code&gt;webpack&lt;/code&gt;’s built-in logging capabilities, and it obscured our API logs which normally get routed to the same console.&lt;/p&gt;

&lt;p&gt;With a bit more tinkering, I was able to get &lt;code&gt;webpack-dev-middleware&lt;/code&gt; hooked up to &lt;code&gt;hapi&lt;/code&gt;. Here’s roughly where that got us.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Install dev middleware
server.ext("onRequest", (request, reply) =&amp;gt; {
  if (passThroughRequest(request)) {
    return reply.continue();
  }

  devMiddleware(request.raw.req, request.raw.res, err =&amp;gt; {
    if (err) {
      return reply(err);
    }
    return reply.continue();
  });
});

// Install hot middleware (for module reloading without reloading the page)
  server.ext("onPostAuth", (request, reply) =&amp;gt; {
    if (passThroughRequest(request)) {
      return reply.continue();
    }

    hotMiddleware(request.raw.req, request.raw.res, err =&amp;gt; {
      if (err) {
        return reply(err);
      }
      return reply.continue();
    });
  });

  // Make sure react-router can handle our actual routing
  server.ext("onPreResponse", (request, reply) =&amp;gt; {
    if (passThroughRequest(request)) {
      return reply.continue();
    }

    return reply.file("public/index.html");
  });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;(&lt;code&gt;passThroughRequest&lt;/code&gt; ignores a few paths that need to skip &lt;code&gt;webpack&lt;/code&gt; and go straight to the backend.)&lt;/p&gt;

&lt;p&gt;With this set up, I tried saving a change to a module. However, instead of a hot reload, I got a warning in the console:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Ignored an update to unaccepted module ./client/components/project/tileView/ProjectTile.js -

…

process-update.js?e135:104 [HMR] The following modules couldn't be hot updated: (Full reload needed)
This is usually because the modules which have changed (and their parents) do not know how to hot reload themselves. See https://webpack.js.org/concepts/hot-module-replacement/ for more details.
…
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It turns out that not only do we need to wire up &lt;code&gt;webpack&lt;/code&gt; for hot reloading, we also have to teach our frontend components to hot reload themselves. This way, when &lt;code&gt;webpack&lt;/code&gt;’s watch process notices the components have changed, it can inject just the changed bit of code and not reload the whole page. Each framework has a different approach to this. React has &lt;code&gt;react-hot-loader&lt;/code&gt;, a pet project of Dan Abramov that, despite being quite experimental, is well supported and active. Abramov has written extensively about the concepts behind it, and &lt;a href="https://overreacted.io/my-wishlist-for-hot-reloading/"&gt;this article&lt;/a&gt; is well worth a read. Essentially, you have to mark your top-level App component as hot-exported:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;App.js
import React from "react";
import { hot } from "react-hot-loader/root";

import Routes from "./Routes";
import CascadeTheme from "./theme/Cascade";
import { AppContainer } from "./sharedComponents";

const App = () =&amp;gt; (
  &amp;lt;CascadeTheme&amp;gt;
    &amp;lt;&amp;gt;
      &amp;lt;AppContainer&amp;gt;
        &amp;lt;Routes /&amp;gt;
      &amp;lt;/AppContainer&amp;gt;
    &amp;lt;/&amp;gt;
  &amp;lt;/CascadeTheme&amp;gt;
);

export default hot(App);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We also had to make some changes to &lt;code&gt;webpack&lt;/code&gt; config to load both &lt;code&gt;react-hot-loader&lt;/code&gt; and the &lt;code&gt;webpack-hot-middleware&lt;/code&gt; client. This is the relevant section:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if (process.env.npm_lifecycle_event
 === "start") {
  config = merge(common, {
    devtool: "cheap-module-eval-source-map",
    plugins: [new webpack.HotModuleReplacementPlugin()],
    module: {
      rules: [
        {
          // Mark everything matching this rule as "cold" (e.g. not hot exported)
          // This will allow hot reloading to work as expected for the rest of the
          // application
          test: /\.js?$/,
          include: /node_modules/,
          exclude: /node_modules\/@sensei\/shared-components\/src/,
          use: ["react-hot-loader/webpack"],
        },
      ],
    },
  });

  config.entry.app = ["webpack-hot-middleware/client", ...common.entry.app];
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see, this config only applies when the app is run via npm start (aka, in development).&lt;/p&gt;

&lt;p&gt;So I got that working, PR’d and merged. Then one of our other frontend developers noticed a bizarre occurrence—logout functionality was broken while using hot reloading in development. The app was still visible to logged-out users but in a broken state, with all calls to the backend failing. I realized that all of our &lt;code&gt;webpack&lt;/code&gt; dev/hot middleware calls were getting through, regardless if the user was authenticated or not.&lt;/p&gt;

&lt;p&gt;I had nagging feeling that there was a fundamental security flaw in my hot reloading implementation, and that I'd have to just make peace with manually refreshing the page every time I made a change. Regardless, I pressed on.&lt;/p&gt;

&lt;p&gt;I tried debugging the extension points and checking the auth object, but it seemed that cookies had not been parsed yet. My first instinct was not a helpful one— I tried importing our &lt;code&gt;hapi&lt;/code&gt; authorization plugin and re-injecting it into the new server request extension event methods. This led to numerous forays into the internals of &lt;code&gt;hapi&lt;/code&gt; which started to develop a bad code smell. &lt;/p&gt;

&lt;p&gt;So I took a deep breath and a step back and re-read &lt;code&gt;hapi&lt;/code&gt;’s documentation. The most useful bit was the &lt;a href="https://hapijs.com/api/16.1.1#request-lifecycle"&gt;request lifecycle&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;It turns out that in the example posted above, the author relied on the onRequest extension points, which come before authentication in the request lifecycle. The solution was to use the onPostAuth extension point to hook up &lt;code&gt;webpack-dev-middleware&lt;/code&gt; and &lt;code&gt;webpack-hot-middleware&lt;/code&gt;, so that our normal authentication middleware still processed each request. However, I still needed the onPreResponse handler to serve the HTML file, so we needed to check authorization at that point, too.&lt;/p&gt;

&lt;p&gt;Here is what we ended up with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/* eslint-disable consistent-return */
const config = require("config");
const webpack = require("webpack");
const webpackDevMiddleware = require("webpack-dev-middleware");
const webpackHotMiddleware = require("webpack-hot-middleware");

const webpackConfig = require("./webpack.config");

const IGNORE_PATTERN = /^\/(api|styles|img)\//;

function installWebpackHapiMiddleware(server) {
  // If we're not running under `npm start`, bail
  if (process.env.npm_lifecycle_event !== "start") {
    return server;
  }

  console.log(
    "You appear to be running a development server. Initializing webpack dev/hot middleware..."
  );

  const compiler = webpack(webpackConfig);

  const devMiddleware = webpackDevMiddleware(compiler, {
    port: config.port,
    historyApiFallback: true,
    publicPath: webpackConfig.output.publicPath,
  });

  const hotMiddleware = webpackHotMiddleware(compiler);

  // Install dev middleware
  server.ext("onPostAuth", (request, reply) =&amp;gt; {
    if (passThroughRequest(request)) {
      return reply.continue();
    }

    devMiddleware(request.raw.req, request.raw.res, err =&amp;gt; {
      if (err) {
        return reply(err);
      }
      return reply.continue();
    });
  });

  // Install hot middleware (for module reloading without reloading the page)
  server.ext("onPostAuth", (request, reply) =&amp;gt; {
    if (passThroughRequest(request)) {
      return reply.continue();
    }

    hotMiddleware(request.raw.req, request.raw.res, err =&amp;gt; {
      if (err) {
        return reply(err);
      }
      return reply.continue();
    });
  });

  // Make sure react-router can handle our actual routing
  server.ext("onPreResponse", (request, reply) =&amp;gt; {
    if (passThroughRequest(request)) {
      return reply.continue();
    }

    return reply.file("public/index.html");
  });

  return server;
}

function passThroughRequest(request) {
  const isNotAuthenticated = request.auth.mode === "required" &amp;amp;&amp;amp; !request.auth.isAuthenticated;
  return isNotAuthenticated || IGNORE_PATTERN.test(request.path);
}

module.exports = installWebpackHapiMiddleware;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Software development can often feel intangible, since the code we write looks very different from the direct machine instructions it eventually becomes, through complex processes that few of us fully understand. Ultimately, the immediacy of hot reloading brings our daily workflow closer to that of a carpenter working with physical tools-- make a change, and see it reflected immediately. I celebrate tangible software development and the tools that make it possible!&lt;/p&gt;

</description>
      <category>webpack</category>
      <category>react</category>
      <category>carpentry</category>
      <category>sculpture</category>
    </item>
  </channel>
</rss>
