How to use HTML components with webpack

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.

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.

static-components/
  registration.html
  header.html
  footer.html

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
<html><head>...</head>
<body>
  <div id="root"></div>
  <script src="index.js"></script>
</body>
</html>

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

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Hire Web Developer

Looking for an attractive & user-friendly web developer? HourlyDeveloper.io, a leading web, and mobile app development company, offers web developers for hire through flexible engagement models. You can **[Hire Web...

Why Web Development is Important for your Business

With the rapid development in technology, the old ways to do business have changed completely. A lot more advanced and developed ways are ...

Important Reasons to Hire a Professional Web Development Company

    You name the business and I will tell you how web development can help you promote your business. If it is a startup or you seeking some...

10 Web Development Blogs in 2020

On account of the technological advances, the web has become a converging point for the entire world. Creating a digital presence today is…

Hire Dedicated eCommerce Web Developers | Top eCommerce Web Designers

Build your eCommerce project by hiring our expert eCommerce Website developers. Our Dedicated Web Designers develop powerful & robust website in a short span of time.