webdiscus / html-bundler-webpack-plugin

Alternative to html-webpack-plugin ✅ Renders Eta, EJS, Handlebars, Nunjucks, Pug, Twig templates "out of the box" ✅ Resolves source files of scripts, styles, images in HTML ✅ Uses a template as entry point
ISC License
146 stars 14 forks source link

How can I keep the original `.php` file extension? #4

Closed webdiscus closed 1 year ago

webdiscus commented 1 year ago

Discussed in https://github.com/webdiscus/html-bundler-webpack-plugin/discussions/3

Originally posted by **Raxel21** March 21, 2023 Hi, I hope everything is going well for you, today I discovered your plugin and I find it very useful. Well, I am having a problem working with `.php`, with `.html` it works perfectly. I tried to adapt the code but I did not succeed. I tried 2 ways It works, but it changes the extension to `.html`, so it breaks the PHP code. ```javascript new HtmlBundlerPlugin({ test: /\.php$/i, verbose: true, entry: { index: path.join(__dirname, 'src', 'public', 'index.php'), }, js: { filename: '[name].[contenthash:8].js', }, css: { filename: '[name].[contenthash:8].css', }, }) ``` So I tried this other way It also works, but it keeps changing the extension to `.html`, but this time I get an error: `html-bundler-webpack-plugin: Failed to execute the template function` I thought that just returning the template in the `preprocessor` line would work but I get the above error. ```javascript new HtmlBundlerPlugin({ test: /\.php$/i, verbose: true, entry: { home: { import: path.join(__dirname, 'src', 'public', 'index.php'), filename: 'index.php', }, }, js: { filename: '[name].[contenthash:8].js', }, css: { filename: '[name].[contenthash:8].css', }, loaderOptions: { preprocessor: (template, { data }) => template, }, }) ``` Any suggestions please. Or maybe I forgot something from the documentation? **Thank you in advance**
webdiscus commented 1 year ago

@Raxel21

yes, I can reproduce the problem and will fix it soon. Thanks!

webdiscus commented 1 year ago

The plugin expects an HTML output file, but not PHP. I will add support for a custom output file.

Raxel21 commented 1 year ago

Thank you in advance for your time

webdiscus commented 1 year ago

@Raxel21

in the new release v1.9.0 the issue is fixed. Now a PHP template can be processed.

new HtmlBundlerPlugin({
  test: /\.php$/i,
  verbose: true,
  filename: '[name].php', // define output filename for all templates defined in entry 
  entry: {
    home: path.join(__dirname, 'src/public/index.php'),
  },
  js: {
    filename: '[name].[contenthash:8].js',
  },
  css: {
    filename: '[name].[contenthash:8].css',
  },
  loaderOptions: {
    preprocessor: false, // disable template compilation to HTML
  },
})

Please see How to process a PHP template in readme.

Raxel21 commented 1 year ago

Thank you so much, It works so good! Thank you for taking the time to add this new functionality.

webdiscus commented 1 year ago

@Raxel21

is the problem solved? Can be the issue closed?

Raxel21 commented 1 year ago

Yes, it works very well for me, thank you very much!