R
R
romaro2021-03-29 10:54:43
webpack
romaro, 2021-03-29 10:54:43

How to create multiple html pages from ejs templates in webpack?

I understand webpack and can't figure out which plugin(s) to use to generate html statics from ejs templates. I have in a project:

ejs/
L shared/
  L header.ejs
  L footer.ejs
L index.ejs
L 404.ejs
L contact.ejs


I need to build with one launch of webpack:
static/
L index.html
L 404.html
L contact.html


In all the examples that I found, only one file is being worked on. How to feed webpack the entire ejs directory, or at least a list of the necessary pages in it? For example, connect them through main.js, similar to how it is done to build sass from separate files:
import './ejs/index.ejs';
import './ejs/404.ejs';
import './ejs/contact.ejs';


I do not want to add Gulp to the project for the sake of one task.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sun_Day, 2021-03-29
@Sun_Day

Something like that, mb something needs to be added / corrected.
HtmlWebpackPlugin

import './ejs/index.ejs';
import './ejs/404.ejs';
import './ejs/contact.ejs';

const htmls = ["index", "404", "contact"].map(template => {
            new HtmlWebpackPlugin({
                template: `./static/${template}.html`,
                filename: `${template}.html`,
                chunks: [`${template}`]
            })
        };
        module.exports = {
            entry: {
                index: './ejs/index.ejs',
                404: './ejs/404.ejs',
                contact:  '. / ejs / contact.ejs'.
            },
            plugins: [htmls]
        };

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question