sagold / handlebars-webpack-plugin

Renders your html-template at build time
161 stars 45 forks source link

Bug with html-webpack-plugin ? #79

Open tchapi opened 3 years ago

tchapi commented 3 years ago

Hello

webpack version: 4.44.2

I'm using this plugin along with html-webpack-plugin to generate a head partial before feeding it to Handlebars. I'm basically following https://github.com/sagold/handlebars-webpack-plugin#html-webpack-plugin exactly, but I have the following error:

/assets/pages/index.hbs: The partial html/_head could not be found
Error: The partial html/_head could not be found

My index.hbs template calls the partial with {{> html/_head }} which I think is what it should be.

My configuration is strictly what is proposed in the README:

new HtmlWebpackPlugin({
          scriptLoading: 'defer',
          template: path.join(process.cwd(), "assets", "components", '_head.hbs'),
          filename: path.join(process.cwd(), "dist", '_head.hbs'),
          publicPath: './build/',
          inject: "head"
})
new HandlebarsPlugin({
      htmlWebpackPlugin: {
        enabled: true, // register all partials from html-webpack-plugin, defaults to `false`
        prefix: 'html', // where to look for htmlWebpackPlugin output. default is "html"
        HtmlWebpackPlugin // optionally: pass in HtmlWebpackPlugin if it cannot be resolved
      },

      // path to hbs entry file(s). Also supports nested directories if write path.join(process.cwd(), "app", "src", "**", "*.hbs"),
      entry: path.join(process.cwd(), "assets", "pages", "**", "*.hbs"),
      // you can also add a [path] variable, which will emit the files with their relative path, like
      output: path.join(process.cwd(), "dist", "[name].html"),

      // globbed path to partials, where folder/filename is unique
      partials: [
        path.join(process.cwd(), 'html', '*', '*.hbs'),
        path.join(process.cwd(), "assets", "components", "*.hbs")
      ],
})

Am I missing something ? It looks like HandlebarsPlugin is not being fed the files from the HtmlWebpackPlugin correctly. Thanks a lot for your suggestions !

Best regards

sagold commented 3 years ago

I am currently having the same issue. My blind guess is, that there is a breaking change between html-webpack-plugin v4.4 and v4.5. I am looking into it.

tchapi commented 3 years ago

Hi @sagold

I think I found it. The culprit is process.cwd() that introduces problems in the path for this specific feature. In fact, it changes the path where we expect the generated partial to be.

I'm doing this right now in the HtmlWebpackPlugin:

filename: path.join("dist", '_head.hbs')

and in the HandlebarsPlugin:

// ...
partials: [
   path.join('html', '*', '*.hbs'), // No process.cwd() here also
    path.join(process.cwd(), "assets", "components", "*.hbs")
],
// ...

... and it seems to work !

Maybe you could just update the example to reflect that ?

Best regards