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?
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.
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.
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.
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 %>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With