Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use webpack to concat html pieces?

Tags:

webpack

For example, I have a main entry html file:

<div>
   <!-- I'd like to include a html partial here -->
</div>

And I have a partial html

<span>I'm a partial html piece</span>

I hope that I can use webpack to generate a final html like below:

<div>
  <span>I'm a partial html piece</span>
</div>

Is it possible to do this with webpack plugin/loaders?

like image 957
Chris Avatar asked Mar 25 '16 08:03

Chris


People also ask

Can webpack bundle HTML?

The HtmlWebpackPlugin simplifies creation of HTML files to serve your webpack bundles. This is especially useful for webpack bundles that include a hash in the filename which changes every compilation.

Does webpack generate HTML?

Use webpack to build your JS assets and it will create a . html file with all assets imported in. Run yarn dev , and hopefully after a few sections you will see a dist directory was created with your JS files included.

What is HTML loader?

The html-loader will parse the URLs, require the images and everything you expect. The extract loader will parse the javascript back into a proper html file, ensuring images are required and point to proper path, and the asset modules will write the .html file for you. Example: webpack.config.js module.


1 Answers

There is a couple of loaders to achieve this: https://webpack.github.io/docs/list-of-loaders.html#templating


For example:

html-loader

<div>${require('./partials/gallery.html')}</div>

ejs-html-loader

<% include some/file %>
like image 110
Bob Sponge Avatar answered Sep 27 '22 17:09

Bob Sponge