New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

html-bundler-webpack-plugin

Package Overview
Dependencies
Maintainers
1
Versions
192
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

html-bundler-webpack-plugin

HTML bundler plugin for webpack, extracts CSS and JS from their sources specified in HTML.

  • 0.1.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
3K
decreased by-29.5%
Maintainers
1
Weekly downloads
 
Created
Source

HTML5 Powered
HTML bundler Webpack Plugin

The plugin generates HTML files contained output CSS and JS files extracted from their sources used in HTML

npm node node

Warning

This is a brand new unique modern plugin that does exactly what you have wanted for a long time.
This plugin has come to replace an outdated, inconvenient html-webpack-plugin and others.

The plugin automatically extracts JS, CSS, images, fonts, etc. from their sources loaded directly in HTML via <link> <script> <img> tags.

Note

The purpose of this plugin is to make the developer's life much easier than it was with other plugins such as html-webpack-plugin and mini-css-extract-plugin.

Note

The plugin is under continuous development. Many new feature will be implemented soon.

Кeep your finger on the pulse.

html-bundler-webpack-plugin

This plugin will work like the pug-plugin but the entry point is a HTML file.

The plugin enable to use a HTML file as entry-point in Webpack, extracts JS and CSS files from their sources specified in HTML. The plugin generates HTML files containing hashed output JS and CSS filenames whose source files are specified in the HTML file.

The plugin automatically extracts the CSS from source style (e.g *.scss, *.styl) loaded via a <link> tag and generates a separate file with hashed filename for it.
The plugin automatically extracts the JavaScript from source script (e.g. *.js, *.ts) loaded via a <script> tag and generates a separate file with hashed filename for it.
The plugin automatically processes the images, fonts from sources loaded via <link>, <img> or <source> tags and generates a separate file with hashed filename for it.

💡 Highlights:

  • The HTML file is the entry-point for all source scripts and styles.
  • Source scripts and styles should be specified directly in HTML.
  • All JS and CSS files will be extracted from their sources specified in HTML.

Specify the HTML files in the Webpack entry:

const HtmlBundlerPlugin = require('html-bundler-webpack-plugin');
module.exports = {
  entry: {
    // define your HTML files here
    index: './src/views/home/index.html',  // output dist/index.html
    about: './src/views/about/index.html', // output dist/about.html
  },
  plugins: [
    // enable processing of HTML files defined in Webpack entry
    new HtmlBundlerPlugin({
      js: {
        // output filename of extracted JS file from source script
        filename: 'assets/js/[name].[contenthash:8].js',
      },
      css: {
        // output filename of extracted CSS file from source style
        filename: 'assets/css/[name].[contenthash:8].css',
      },
    }),
  ],
  module: {
    rules: [
      {
        test: /.html/,
        loader: HtmlBundlerPlugin.loader, // HTML loader
      },
    ],
  },
};

Add source scripts and styles directly to HTML using relative path or Webpack alias:

<html>
  <head>
    <link href="./styles.scss" rel="stylesheet">
    <script src="./main.js" defer="defer"></script>
  </head>
  <body>
    <h1>Hello World!</h1>
  </body>
</html>

The generated HTML contains hashed output CSS and JS filenames:

<html>
  <head>
    <link href="/assets/css/styles.05e4dd86.css" rel="stylesheet">
    <script src="/assets/js/main.f4b855d8.js" defer="defer"></script>
  </head>
  <body>
    <h1>Hello World!</h1>
  </body>
</html>

The <link> and <script> tags stay where they are written. This can be very important in some cases, as opposed to using other plugins like html-webpack-plugin and mini-css-extract-plugin that injects these tags somewhere in the HTML.

Just one HTML bundler plugin replaces the functionality of the plugins and loaders:

PackageFeatures
html-webpack-pluginextract HTML and save in a file
mini-css-extract-pluginextract CSS and save in a file
webpack-remove-empty-scriptsremove empty JS files generated by the mini-css-extract-plugin
resolve-url-loaderresolve url in CSS
svg-url-loaderencode SVG data-URL as utf8
posthtml-inline-svginline SVG icons in HTML

How to use source images in HTML

It's very easy! Add to Webpack config the rule:

 module: {
  rules: [
    {
      test: /\.(png|jpe?g|ico)/,
      type: 'asset/resource',
      generator: {
        filename: 'assets/img/[name].[hash:8][ext]',
      },
    },
  ],

Now you can write source file (relative path or a Webpack alias) in HTML:

<html>
  <head>
    <link href="./favicon.ico" rel="icon" />
  </head>
  <body>
    <img src="./apple.png" srcset="./apple1.png 320w, ./apple2.png 640w" alt="apple">
    <picture>
      <source srcset="./fig1.jpg, ./fig2.jpg 320w, ./fig3.jpg 640w">
    </picture>
  </body>
</html>

The generated HTML contains hashed output images filenames:

<html>
  <head>
    <link href="/assets/img/favicon.05e4dd86.ico" rel="icon" />
  </head>
  <body>
    <img src="/assets/img/apple.f4b855d8.png" srcset="/assets/img/apple1.855f4bd8.png 320w, /assets/img/apple2.d8f4b855.png 640w" alt="apple">
    <picture>
      <source srcset="/assets/img/fig1.605e4dd8.jpg, /assets/img/fig2.8605e4dd.jpg 320w, /assets/img/fig3.e4605dd8.jpg 640w">
    </picture>
  </body>
</html>

How to preload source fonts in HTML

It's very easy! Add to Webpack config the rule:

 module: {
  rules: [
    {
      test: /\.(eot|ttf|woff|woff2)/,
      type: 'asset/resource',
      generator: {
        filename: 'assets/fonts/[name][ext]',
      },
    },
  ],

Now you can write source file (relative path or a Webpack alias) in HTML:

<html>
  <head>
    <link href="./open-sans-regular.woff2" rel="preload" as="font" type="font/woff2" />
  </head>
  <img src="./apple.png" alt="apple">
</html>

The generated HTML contains output fonts filenames:

<html>
  <head>
    <link href="/assets/fonts/open-sans-regular.woff2" rel="preload" as="font" type="font/woff2" />
  </head>
  <body>
    <h1>Hello World!</h1>
  </body>
</html>

Note

Now you don't need a plugin to copy files from source directory to public.

Also See

  • ansis - The Node.js lib for ANSI color styling of text in terminal
  • pug-loader The Pug loader for Webpack
  • pug-plugin The Pug plugin for Webpack

License

ISC

Keywords

FAQs

Package last updated on 12 Jan 2023

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