Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Html Webpack Plugin - How do you import headers/footer html partials into body template

I'm trying to figure out a way where you can have an index file that when compiled loads the global header and footer into the index.html file so I don't have to add this every time.

At the moment this is what creates my index.html file:

    new HtmlWebpackPlugin({
        title: 'Typescript Webpack Starter',
        template: '!!ejs-loader!src/index.html'
    }),  

It loads the JS in the body and the css in the head fine but I'm wondering if I can do something like the below (just like a template engine would)

  <body>
       {{HEADER GETS IMPORTED HERE}}
       <p>Success your page has loaded</p>
       <button class="button">Button</button>
       {{FOOTER GETS IMPORTED HERE}}
  </body>

Let me know your thoughts

like image 824
Max Lynn Avatar asked Oct 29 '22 13:10

Max Lynn


2 Answers

As per the official document : YOU CAN ACHIEVE it.

If you already have a template loader, you can use it to parse the template. Please note that this will also happen if you specifiy the html-loader and use .html file as template.

 module: {
   loaders: [
     { test: /\.hbs$/, loader: "handlebars" }
   ]
 },
 plugins: [
   new HtmlWebpackPlugin({
     title: 'Custom template using Handlebars',
     template: 'my-index.hbs'
   })
 ]
like image 138
Ravi Roshan Avatar answered Nov 15 '22 06:11

Ravi Roshan


Just in case anybody needs a working example. I did it with an ejs-loader

<div id="page-wrapper">
   <%- include partials/header.ejs %>
   <div class="content"></div>
   <%- include partials/footer.ejs %>
</div>

Also put evth. into a small webpack boilerplate there are some more extras inside ;) feel free to use it.

like image 37
web-designed Avatar answered Nov 15 '22 04:11

web-designed