Skip to content Skip to sidebar Skip to footer

Webpack: Compile Folder But Keep Separate Files?

I have Webpack config that compiles nunjuck files into html files. However, I have to specify every single file input and output manually. I can't figure out how to 1) read all fil

Solution 1:

According to the HtmlWebpackPlugin docs what you are doing is actually a recommended approach (which obviously sucks).

But what you can do instead of listing them one by one manually like that, is write a helper function that will take in the list of files to convert (via glob wildcard lets say) and output the array of HtmlWebpackPlugin instructions, that you can feed directly to webpack config.

It's just JS. Webpack config is just a NodeJS script. You can do anything you like.

Solution 2:

Something like this should do the trick:

const glob = require('glob');


 const pages = glob.sync('**/*.njk', {
    cwd: path.join(__dirname, 'src/pages/'),
    root: '/',
  }).map(page =>newHtmlWebpackPlugin({
      filename: page.replace('njk', 'html'),
      template: `src/pages/${page}.njk`,
    }));

Then to use it:

plugins: [
  ...pages,

Post a Comment for "Webpack: Compile Folder But Keep Separate Files?"