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

Exclude dir from minifing #38

Closed 5ulo closed 8 months ago

5ulo commented 8 months ago

Feature request

What is motivation or use case for adding/changing the behavior?

Included 3rd party libs (js, css) are minified already or one wont minify them at all, but there's no way to exclude dir containing files like that eg. /libs. Maybe this is already implemented and I am blind or incapable.

Describe the solution you'd like

Have an option to exclude directory from minifyng and resolving paths inside them, just keep watching files for changes.

Describe alternatives you've considered

Right now I use CopyPlugin to copy libs to it's place on build but every file included in src or link is compiled and at the end replaced. Used it together with TerserPlugin but somehow it is ignored or overriden with bundler plugin.

webdiscus commented 8 months ago

Hi @5ulo

Right now I use CopyPlugin to copy libs to it's place on build but every file included in src or link is compiled and at the end replaced.

To disable resolving and processing of attributes such as src, href etc, you can use the loaderOptions.sources[].filter() option.

For example:

<!-- must be processed via Bundler Plugin -->
<script src="./scripts/main.js"></script>
<!-- ignore the processing via Bundler Plugin, because is coped into `dist/` using CopyPlugin-->
<script src="myLib/lib.js"></script>
new HtmlBundlerPlugin({
  entry: {
    index: 'src/views/index.html',
  },
  loaderOptions: {
    sources: [
      // exclude JS dir
      {
        tag: 'script', // <= tag name
        filter: ({ attribute, value }) => { // attribute name and attribute value
          const libPath = 'myLib/'; // <= path to lib files coped in the `dist/` directory
          if (attribute === 'src' && value.includes(libPath) ) return false; // => disable processing
        },
      },
      // exclude CSS dir
       {
        tag: 'link', // <= tag name
        filter: ({ attribute, value }) => { // attribute name and attribute value
          const libPath = 'myLib/'; // <= path to lib files coped in the `dist/` directory
          if (attribute === 'href' && value.includes(libPath) ) return false; // => disable processing
        },
      },
    ],
  },
});
5ulo commented 8 months ago

Oh.. I use sources but missed that nifty filter. Perfect, thanks!