How to use HTML components with webpack

The HTML loader allows us to inject HTML code into JavaScript which can then dynamically inject HTML components into the main DOM.

A powerful feature of PHP is its <?include?> feature which allows you to import HTML files into the DOM on the server itself. However, using PHP is not a viable solution for many people — especially front-end developers. GitHub Pages does not support server-side languages like PHP; hence, we have to find a better solution.

We want to use JavaScript on the client-side to use HTML modules in a single-page application. webpack comes to the rescue by providing the html-loader . The HTML loader allows us to inject HTML code into JavaScript which can then dynamically inject HTML components into the main DOM.

I refer to these HTML modules as “static-components”. They do not have an intrinsic state and do not contain any JavaScript code like React components. This makes them as fast as regular HTML+vanilla JS with added the convenience of modularity.

Installing the HTML loader

Assuming you have webpack installed,

npm install --save-dev html-loader
# or
yarn add -D html-loader

Now you have to add it in your configuration file:

// webpack.config.js
module.exports = {
  module: {
    rules: [
        test: /\.html$/,
        use: [
            loader: 'html-loader',
            options: {
              minimize: true,
              interpolation: false

Adding static components to your project

I like to create a separate folder for my HTML modules.


Injecting your HTML into JavaScript and then into the DOM

You can now import HTML modules in JavaScript files as strings. These string can then be set as the innerHTML property of your containers.

// main.html
  <div id="root"></div>
  <script src="index.js"></script>

Now, in your code:

// index.js
import header from './static-components/header.html'
import footer from './static-components/footer.html'
document.onload = function() {
  document.getElementById("root").innerHTML = header + footer;

Analogy to React

HTML modules are like React components except they are “static”. They do not change over time or have a state.

However, you could make controllers that get loaded and unloaded whenever you add and remove HTML from the DOM. These controllers could add DOM event handlers to make interactive applications.

// StaticController.js
window.staticControllers = {};
// HeaderController.js
import header from 'path/to/header.html'
window.staticControllers.headerController = {
  initialRender: function() {
    return header;
  onLoad: function() {
    document.getElementById('id').onclick = ...;

However, static components will have almost no overhead compared to React since it doesn’t ever render again. In addition, you don’t have load your site with 100s of KBs worth of data.

Combining JSX with HTML modules

A really sweet feature would be able to separate long JSX from the actual JavaScript file, right. The only obstacle would be evaluating the object notation of JSX at runtime. Of course, babel developers will have to add that feature.

However, if we get that one day, you might writing JSX in separate markup files!!!

#javascript #web-development #html

How to use HTML components with webpack
1 Likes18.40 GEEK