jantimon/html-webpack-plugin

htmlWebpackPlugin.options is undefined with twig-html-loader

Nkzq opened this issue ยท 1 comments

Nkzq commented

Current behaviour ๐Ÿ’ฃ

Hello, I'm trying to access htmlWebpackPlugin.options on a twig file, like so :
{{ htmlWebpackPlugin.options }}
But it's undefined. I don't know if it's related to twig-html-loader. There is also an issue on twig-html-loader repo https://github.com/radiocity/twig-html-loader/issues/47, no relevant answers since few months.

Expected behaviour โ˜€๏ธ

Be able to access htmlWebpackPlugin.options in my twig templates in order to have a nice translation support.

Reproduction Example ๐Ÿ‘พ

pages.json

[
  {
    "filename": "./fr/index.html",
    "template": "./src/templates/index.twig",
    "lang": "fr"
  },
  {
    "filename": "./en/index.html",
    "template": "./src/templates/index.twig",
    "lang": "en"
  }
]

webpack.config.js (not necessary part skipped)

const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const pages = require('./pages.json');

const webpackConfig = {
  module: {
    rules: [
      {
        test: /\.twig$/,
        type: 'asset/source',
        use: [
          {
            loader: 'twig-html-loader',
            options: {
              data: context => {
                const data = path.join(__dirname, './data/data.json');
                context.addDependency(data);
                return context.fs.readJsonSync(data, { throws: false }) || {};
              }
            }
          }
        ]
      }
    ]
  },
  plugins: []
};

pages.map(page => {
  const trans = require(`./translations/messages.${page.lang}.json`);

  webpackConfig.plugins.push(
    new HtmlWebpackPlugin({
      locale: page.lang,
      filename: page.filename,
      template: page.template,
      trans
    })
  );
});

module.exports = webpackConfig;

Environment ๐Ÿ–ฅ

Node.js v16.13.0
darwin 20.6.0
8.1.0
webpack@5.65.0
html-webpack-plugin@5.5.0

Thanks ๐Ÿ™

Nkzq commented

It seems to be an issue with twig-html-loader, I used twig-loader instead and it's working fine. I close this issue.