Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle templating with webpack and html webpack text plugin?

I have a project that uses Twig for templating. All the data in it is static, but I have separated out parts of the page within other twig files for clarity sake (otherwise there would be hundreds of lines of markup in one file).

I'm using webpack as a build tool, along with HTML Webpack Plugin.

For example, let's say I have the following:

views/index.html

<h1>This is my main index page</h1>    
{% includes './welcome/hello.html' %}

views/welcome/hello.html

<p>Hello from the templated page.</p>

Now that I'd like to do is use webpack to bundle any js, css and also use HTML Webpack Plugin to create an HTML page and inject those styles and scripts into:

dist/index.html
dist/bundle.js
dist/styles.css

In my dist/index.html, I was expecting to get something like this where the html page generated is shown as HTML:

dist/index.html

<h1>This is my main index page</h1>
<p>Hello from the templated page.</p>

However, what I am getting is something like this where it still shows my 'includes' templating:

dist/index.html

<h1>This is my main index page</h1>
{% includes './welcome/hello.html' %}

If it's not possible to compile the templates, is it possible to use Html Webpack Plugin to copy over all the templates (entire directory of all template files to /dist so that they maintain their paths)?

Thanks!

like image 396
hidace Avatar asked Oct 10 '16 14:10

hidace


People also ask

What is the use of HTML webpack plugin?

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.

What is clean webpack plugin?

GitHub - johnagan/clean-webpack-plugin: A webpack plugin to remove your build folder(s) before building. Skip to content Toggle navigation. Product. Actions. Automate any workflow.

What is the use of HTML loader?

The html-loader defination says that it exports html as String (What does it mean). it also says that every loadable attributes (for example <img src="image. png" is imported as require('./image. png') ,and you may need to specify loader for images in your configuration ( file-loader or url-loader ), What does it mean.

What is copy webpack plugin?

copy-webpack-plugin is not designed to copy files generated from the build process; rather, it is to copy files that already exist in the source tree, as part of the build process.


1 Answers

You can use ejs-render-loader. see package.

 // webpack
         new HtmlWebpackPlugin({
                filename: 'a.html',
                hash: true,
                template: 'ejs-render?a.ejs',
         })
 // a.ejs
    <%- include components/menu -%>

 // components/menu.ejs
    <p>hei hei hei hei</p>

I think you can change your .html to .ejs file. And use plugin like above.

like image 57
deju Avatar answered Jan 03 '23 01:01

deju