Is it possible to resolve template outside from the root folder? (HTMLWebpackPlugin)

48 Views Asked by At

I have a monorepo with apps, which should use a shared template but this template is outside of the project.

The folder structure is the following:

/
  /apps
    /app1(this should import the shared index.html)
    /app2(this should import the shared index.html)
  /libs
    /shared (this folder contains the index.html)

If I resolve the path with node like.: "/libs/shared/index.html" it will resolve in app1 "/apps/app1/libs/shared/index.html" because it is using process.cwd() to resolve as it can see in the source of the plugin. (code is from: https://github.com/jantimon/html-webpack-plugin/blob/main/index.js)

  /**
   * Helper to return the absolute template path with a fallback loader
   * @param {string} template
   * The path to the template e.g. './index.html'
   * @param {string} context
   * The webpack base resolution path for relative paths e.g. process.cwd()
   */
  function getFullTemplatePath (template, context) {
    if (template === 'auto') {
      template = path.resolve(context, 'src/index.ejs');
      if (!fs.existsSync(template)) {
        template = path.join(__dirname, 'default_index.ejs');
      }
    }
    // If the template doesn't use a loader use the lodash template loader
    if (template.indexOf('!') === -1) {
      template = require.resolve('./lib/loader.js') + '!' + path.resolve(context, template);
    }
    // Resolve template path
    return template.replace(
      /([!])([^/\\][^!?]+|[^/\\!?])($|\?[^!?\n]+$)/,
      (match, prefix, filepath, postfix) => prefix + path.resolve(filepath) + postfix);
  }

Is there any way to solve this issue?

I tried to search a solution in the HTMLWebpackPlugin source code.

0

There are 0 best solutions below