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!!!

Angular 9 Tutorial: Learn to Build a CRUD Angular App Quickly

What's new in Bootstrap 5 and when Bootstrap 5 release date?

Brave, Chrome, Firefox, Opera or Edge: Which is Better and Faster?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

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.

Mobile App Development Company India | Ecommerce Web Development Company India

Best Mobile App Development Company India, WebClues Global is one of the leading web and mobile app development company. Our team offers complete IT solutions including Cross-Platform App Development, CMS & E-Commerce, and UI/UX Design.

For World Class Web Development Services in India visit RB Genie

Do you want excellent and world class web development services for your valuable projects? Contact **RB Genie **now, we have more than 8 years experienced team of web developers, which specializes in overall web design and website development...