radiocity / twig-html-loader

Twig to html loader for webpack
MIT License
27 stars 11 forks source link

htmlWebpackPlugin.options not available #47

Open italodr opened 3 years ago

italodr commented 3 years ago

Hello,

I'm trying to use data from htmlWebpackPlugin options as describe in Writing Your Own Templates section.

I tried to access the object but it's empty:

<h1>{{ htmlWebpackPlugin.options.title }}</h1>

Probably related to #26

Really appreciate your help

radiocity commented 3 years ago

Greetings, would you mind to show config?

italodr commented 3 years ago

No, not at all. Here's my config, I have omitted irrelevant part of the code

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;

And in my twig file:

{% extends "./layouts/default.twig" %}

{% block content %}
  <h1>{{ htmlWebpackPlugin.options.trans.title }}</h1>
  <div>{{ htmlWebpackPlugin.options.locale }}</div>
{% endblock %}
Nkzq commented 2 years ago

Hello, I'm facing the same issue with a close configuration. Any update ? @italodr Have you solved your problem ? Thanks.