Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

html-webpack-select-assets-plugin

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

html-webpack-select-assets-plugin

Select certain output files to the html file.

  • 0.1.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

html-webpack-select-assets-plugin

Select certain output files to the html file. Works with newer html-webpack-plugin versions.

Build Status

NPM

Only for webpack@5 and html-webpack-plugin@5 or higher.

Usage

  1. Install via npm i -D html-webpack-select-assets-plugin.
  2. Add to your webpack config AFTER HtmlWebpackPlugin.
const HtmlWebpackSelectAssetsPlugin = require('html-webpack-select-assets-plugin');
// OR
import HtmlWebpackSelectAssetsPlugin from 'html-webpack-select-assets-plugin';

// And for webpack config
{
    entry: {
        app: join(__dirname, './src/entry.js'),
        other: join(__dirname, './src/other.js'),
    },
    // ...
    plugins: [
        new HtmlWebpackPlugin({
            filename: (entry) => `${entry}.html`,
            template: join(__dirname, './src/index.html'),
        }),
        new HtmlWebpackSelectAssetsPlugin({
            selector: 'smart',
        }),
    ]
}

The plugin takes a configuration argument with a key called selector. It will help you to select assets for the specified output html.

The typical or most powerful usage case is when you deal with multiple html pages:

  • You have multiple entry for webpack entry config;
  • Only one HtmlWebpackPlugin instance and config with filename: (entry) => \${entry}.html`` to generate multiple html files (one-to-one correspond to each entry js file).

The problem is that HtmlWebpackPlugin will inject all (entry) assets files to each html file. What if you only want the corresponding entry's assets? This is why html-webpack-select-assets-plugin here -- select assets corresponding to entry.

Powerful selector option

  • "smart": only select assets corresponding to each entry automatically. This means if you want to generate app.html for entry:app.js, only app.js and assets imported by app.js will be injected.

  • function selector(asset:HtmlTagObject, context:SelectorContext, type:AssetType):boolean: you can write your custom selector function to choose assets flexibly.

    • HtmlTagObject: {attributes,meta,tagName}, the type is defined by html-webpack-plugin and represents the asset.
    • SelectorContext: {entry:string, outputName:string, entryFiles:string[]}, the context info for the asset. entry means entry name, outputName is current generated html file's name, and entryFiles means all assets associated with the entry.
    • AssetType: enum "script"|"style"|"meta", means the type of the asset.

Keywords

FAQs

Package last updated on 27 Feb 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc