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