npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

html-webpack-alter-asset-plugin

v1.0.0

Published

Allow plugins to make changes to the assets before invoking the template when using html-webpack-plugin

Downloads

159

Readme

html-webpack-alter-asset-plugin

Allow plugins to make changes to the assets before invoking the template when using html-webpack-plugin

Introduce

In some case, we want to make some changes to static resources(js and css) before inserting it into the output file, such as:

  • spectify the order of js and css, it cann't meet our expectations to use the option chunksSortMode from html-webpack-plugin
  • Insert additional static resources, which not introduced in the webpack
    ...

Based on the above requirements, I finished it!

Installation

You must be running webpack on node 4.3 or higher

Install the plugin with npm:

$ npm install --save-dev html-webpack-alter-asset-plugin

Basic Usage

webpack.config.js :

"use strict";
let HtmlWebpackPlugin = require('html-webpack-plugin');
let htmlWebpackAlterAssetPlugin = require('html-webpack-alter-asset-plugin');

...

module.exports = {
    entry: {
        "vendor": path.resolve(BASE_PATH, 'vendor.browser.js'),
        "polyfills": path.resolve(BASE_PATH, 'polyfills.browser.js'),
        "app": path.resolve(BASE_PATH, 'app.js'),
        "style": path.resolve(BASE_PATH, 'style.js'),
        "vnc": path.resolve(BASE_PATH, 'vnc.js'),
        "register": path.resolve(BASE_PATH, 'register.js'),
    },
    ...
    plugins: {
        new HtmlWebpackPlugin({
            template: './src/app/app.html',
            filename: 'tpl/index.html',
            injectAlter: {
                js: { 
                    keys: [ '~/system.json', 'commons', 'vendor', 'style', 'polyfills', 'app' ] 
                }
            }
        }),
        new htmlWebpackAlterAssetPlugin()
    }
    ...
}

After run webpack, <script> tag is below in the ouput html tpl/index.html:

<html>
<head>
    ...
</head>
<body>
  <app></app>
<script type="text/javascript" src="/system.json"></script><script type="text/javascript" src="/js/commons.js"></script><script type="text/javascript" src="/js/vendor.74c00f920eb335778ae2.bundle.js"></script><script type="text/javascript" src="/js/style.c3f0123e2b41041085f1.bundle.js"></script><script type="text/javascript" src="/js/polyfills.9c2975cfe87c0377bcb4.bundle.js"></script><script type="text/javascript" src="/js/app.b8673988fc2be4df9059.bundle.js"></script></body>

</html>

changes :

  1. ~/system.json into /system.json, so <script> tag is:

     <script type="text/javascript" src="/system.json">

2.the order of js is based on injectAlter.js.keys

Options

html-webpack-plugin

injectAlter for html-webpack-plugin

  1. the default : sort static assets and insert additional static resources (such as ~/system.json)

  2. the orders of js and css will depend on injectAlter.js.keys and injectAlter.css.keys separately

  3. if define injectAlter.js.func, so call injectAlter.js.func() deal js, the same as css
    injectAlter.js.func refer to HtmlWebpackAlterAssetPlugin.prototype.alterAsset

  4. html-webpack-alter-asset-plugin deal assets after chunks, excludeChunks from html-webpack-plugin, which means assets include chunks and skip some chunks from excludeChunks

the whole injectAlter :

    injectAlter : {
        js : {
            keys: ['~/system.json', 'common', 'polyfills', 'app'],
            func: jsInjectAlterFunc
        }
    }

Contribution

You're free to contribute to this project by submitting issues and/or pull requests.

License

This project is licensed under MIT.