webdiscus / html-bundler-webpack-plugin

Renders Eta, EJS, Handlebars, Nunjucks, Pug, Twig templates "out of the box". Uses HTML template as entry point. Resolves source files of scripts, styles, images in HTML. Supports for Vue.
ISC License
119 stars 12 forks source link

Disable url resolving #44

Closed andreivictor closed 8 months ago

andreivictor commented 8 months ago

Let's consider that in the html entry file there are some resources that should not be resolved by webpack - these resources will be placed in the public folder and copied in the dist folder with webpack-copy-plugin.

To be more clear, the behaviour is similar with the html-loader feature - https://github.com/webpack-contrib/html-loader#examples.

Is there any built-in option in this plugin?

Thank you!

webdiscus commented 8 months ago

Hello @andreivictor,

to disable the resolving of URLs in all attributes, set the sources option as false.

Or use the filter of sources option to disable resolving an attribute by tag/attribute/value.

For example, the source script and style files in HTML should be resolved and processed, but all images are already coped via copy-plugin into dist/ directory, then disable resolving only src attribute of image tag:

new HtmlBundlerPlugin({
  entry: {
    index: 'src/views/index.html',
  },
  loaderOptions: {
    sources: [
      {
         tag: 'img',
         filter: ({ attribute }) => attribute !== 'src',
      }
    ],
  },
}),

If you will disable resolving both the src and srcset attributes, then use the filter:

 filter: ({ attribute }) => !['src', 'srcset'].includes(attribute),

The details see please in the documentation.

webdiscus commented 8 months ago

I don't use the comment like <!-- webpackIgnore: true --> to disable sources handling, because it's a bad idea.

andreivictor commented 8 months ago

Excellent! The sources.filter option is exactly what I needed.

Thank you for your response.