const fs = require("fs");
const path = require("path");
const handlebars = require("handlebars");

function localRequest(request) {
  return request.match(/src\/main\/webapp\/resources\/js/);
}

class i18nThymeleafWebpackPlugin {
  constructor(options) {
    this.options = options || {};
    this.functionName = this.options.functionName || "i18n";
    this.entries = {};
    this.i18nsByRequests = {};
  }

  apply(compiler) {
    compiler.hooks.emit.tap(
      "i18nPropertiesWebpackPlugin",
      compilation => {
        for ( const [ entrypointName, entrypoint ] of compilation.entrypoints.entries() ) {
          this.entries[entrypointName] = [];
          for ( const chunk of entrypoint.chunks ) {
            for ( const { userRequest } of chunk.modulesIterable ) {
              if ( userRequest != null && localRequest(userRequest) ) {
                this.entries[entrypointName].push(userRequest);
              }
            }
          }
        }
      }
    );

    compiler.hooks.normalModuleFactory.tap(
      "i18nPropertiesWebpackPlugin",
      factory => {
        factory.hooks.parser
          .for("javascript/auto")
          .tap("i18nPropertiesWebpackPlugin", parser => {
            parser.hooks.call
              .for(this.functionName)
              .tap("i18nPropertiesWebpackPlugin", expr => {
                /*
                Make sure an argument was passed to the function.
                 */
                if (expr.arguments.length) {
                  const key = expr.arguments[0].value;
                  this.i18nsByRequests[parser.state.module.userRequest] = this.i18nsByRequests[parser.state.module.userRequest] || {};
                  this.i18nsByRequests[parser.state.module.userRequest][key] = true;
                }
              });
          });
      }
    );

    /*
    Write the language files for each entry.
     */
    compiler.hooks.done.tap("i18nPropertiesWebpackPlugin", () => {
      const dir = path.join(compiler.options.output.path, "i18n");
      if (!fs.existsSync(dir)) {
        fs.mkdirSync(dir, { recursive: true });
      }

      fs.readFile(__dirname + "/i18n.html", "utf-8", (error, source) => {
        handlebars.registerHelper('tl', key => `/*[[#{${key}}]]*/ ""`);
        handlebars.registerHelper("id", bundle => `id="${bundle.replace("/", "-")}-translations"`);
        const template = handlebars.compile(source);

        Object.keys(this.entries).forEach(entry => {
          let keys = {};

          this.entries[entry].forEach(request => {
            if (request in this.i18nsByRequests) {
              Object.assign(keys, Object.keys(this.i18nsByRequests[request]));
            }
          });

          if (Object.keys(keys).length > 0) {
              const html = template({ keys, entry });
              fs.writeFileSync(path.join(dir, `${entry}.html`), html);
              const entryPath = path.join(dir, `${entry}.html`);
              if (!fs.existsSync(path.dirname(entryPath))) {
                fs.mkdirSync(path.dirname(entryPath), { recursive: true });
              }
              fs.writeFileSync(entryPath, html);
          }
        });
      });
    });
  }
}

module.exports = i18nThymeleafWebpackPlugin;

@@ -648,11 +648,12 @@
 %5D = 
-%5B%5D;
+%7B%7D;%0A
 %0A   
@@ -663,39 +663,134 @@
     
-for ( const chunk of entrypoint
+// get requests from lazy chunks%0A          entrypoint.getChildren().forEach( child =%3E %7B%0A            for ( const chunk of child
 .chu
@@ -813,35 +813,408 @@
     
+  
 for ( 
-const %7B userRequest %7D
+let issuer of chunk.modulesIterable ) %7B%0A                if ( issuer.userRequest != null && localRequest(issuer.userRequest) ) %7B%0A                  this.entries%5BentrypointName%5D%5Bissuer.userRequest%5D = true;%0A                %7D%0A              %7D%0A            %7D%0A          %7D);%0A%0A          // get requests from static chunks%0A          for ( const chunk of entrypoint.chunks ) %7B%0A            for ( let issuer
  of 
@@ -1258,16 +1258,23 @@
    if ( 
+issuer.
 userRequ
@@ -1301,16 +1301,23 @@
 Request(
+issuer.
 userRequ
@@ -1373,14 +1373,16 @@
 ame%5D
-.push(
+%5Bissuer.
 user
@@ -1388,17 +1388,24 @@
 rRequest
-)
+%5D = true
 ;%0A      
@@ -2964,18 +2964,18 @@
  keys = 
-%7B%7D
+%5B%5D
 ;%0A%0A     
@@ -2971,32 +2971,116 @@
  %5B%5D;%0A%0A          
+// gather the keys from all the dependencies of an entrypoint%0A          Object.keys(
 this.entries%5Bent
@@ -3082,16 +3082,17 @@
 s%5Bentry%5D
+)
 .forEach
@@ -3174,28 +3174,28 @@
     
-Object.assign(
+keys = %5B...
 keys, 
+...
 Obje
@@ -3232,18 +3232,17 @@
 equest%5D)
-);
+%5D
 %0A       
@@ -3281,25 +3281,12 @@
 if (
-Object.keys(keys)
+keys
 .len

Fix I18nThymeleafWebpackPlugin to work with lazy loaded chunks