Coder Social home page Coder Social logo

swimmadude66 / html-webpack-skip-assets-plugin Goto Github PK

View Code? Open in Web Editor NEW
19.0 3.0 5.0 85 KB

Skip adding certain output files to the html file. Built as a drop-in replacement for html-webpack-exclude-assets-plugin and works with newer html-webpack-plugin versions

License: MIT License

TypeScript 98.96% JavaScript 0.26% HTML 0.51% CSS 0.27%
webpack plugin html-webpack-plugin html-webpack-exclude-assets-plugin typescript

html-webpack-skip-assets-plugin's Introduction

Html Webpack Skip Assets Plugin

Skip adding certain output files to the html file. Built as a drop-in replacement for html-webpack-exclude-assets-plugin and works with newer html-webpack-plugin versions

build-plugin

NPM

Configuration

  1. Install via npm i -D html-webpack-skip-assets-plugin
  2. Add to your webpack config AFTER HtmlWebpackPlugin
    var HtmlWebpackSkipAssetsPlugin = require('html-webpack-skip-assets-plugin').HtmlWebpackSkipAssetsPlugin;
    // OR for import style
    import {HtmlWebpackSkipAssetsPlugin} from 'html-webpack-skip-assets-plugin'
    ...
    plugins: [
        new HtmlWebpackPlugin({
            filename: join(OUTPUT_DIR, './dist/index.html'),
            // Skip Assets options can be added here
            excludeAssets: ['polyfill.**.js', /styles\..*js$/i, (asset) => (asset.attributes && asset.attributes['x-skip'])]
            // OR
            skipAssets: ['polyfill.**.js', /styles\..*js$/i, (asset) => (asset.attributes && asset.attributes['x-skip'])]
        }),
        new HtmlWebpackSkipAssetsPlugin({
            // or they can be passed in on the plugin. These 4 lists are combined before running
            excludeAssets: ['polyfill.**.js', /styles\..*js$/i, (asset) => (asset.attributes && asset.attributes['x-skip'])]
            // OR
            skipAssets: ['polyfill.**.js', /styles\..*js$/i, (asset) => (asset.attributes && asset.attributes['x-skip'])]
        })
    ]

The plugin takes a configuration argument with a key called skipAssets. This is an array of file globs (provided via minimatch), regex patterns, or functions which accept the asset and return a boolean representing wheter or not to skip adding to the output html. In order to ease migration from html-webpack-exclude-assets-plugin, the plugin also supports passing excludeAssets as the option key, as well as the ability to add either key to the HtmlWebpackPlugin options. All provided lists will be concatenated and used to filter the assets.

Custom insertion

This exclusion will also work for inject: false:

new HtmlWebpackPlugin({
  inject: false,
  excludeAssets: ['polyfill.**.js', /styles\..*js$/i, (asset) => (asset.attributes && asset.attributes['x-skip'])]
  templateContent: ({htmlWebpackPlugin}) => `
    <html>
      <head>
        ${htmlWebpackPlugin.tags.headTags}
      </head>
      <body>
        ${htmlWebpackPlugin.tags.bodyTags}
      </body>
    </html>
  `
})

Testing

Testing is done via ts-node and mocha. Test files can be found in /spec, and will be auto-discovered as long as the file ends in .spec.ts. Just run npm test after installing to see the tests run.

html-webpack-skip-assets-plugin's People

Contributors

erezrokah avatar jantimon avatar mbonaci avatar swimmadude66 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

html-webpack-skip-assets-plugin's Issues

[QUESTION] Can "garbage" files also be removed?

First of all: thank you very, very much for providing this plugin! I am really glad, I found it!

I would like to ask a question, if this is ok here (if not, please tell me, where I should do so; I tend to stay away from stackoverflow, but if it is a must... well... then I will abide...)

Not including js files that webpack generates because of css files, works great. Nevertheless those "garbage" files live on in the public or dist folder and need a manual cleanup.

Do you know of a way how to remove those empty js files automatically, too?
Thank you!

Feature: Support callbacks & full asset access

In order to skip assets based on data from other plugins (that are not available when configuring webpack)
I wold like to provide a callback instead of a pattern.

Currently due to the (pattern as RegExp).test(asset) i'm able to hack this by passing { constructor: { name: 'RegExp' }, test(asset) { /* my implementation */ } } as a pattern.

It would be really helpful to also get access on the full asset attributes in order to apply different filtering when it's a rel: 'preload' for example.

What I ideally would like to do is

type Asset = {
  tagName: string,
  closeTag: boolean,
  attributes: Record<string, string>
}
new HtmlWebpackPlugin({
  filename: join(OUTPUT_DIR, './dist/index.html'),
  excludeAssets: [(path: string, fullAsset: Asset): boolean => { /* my implementation */ }]
})

Same as #5: I'll happily provide PRs for this

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.