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

inject-assets-list-webpack-plugin

v1.0.3

Published

Inject assets list into your HTML template

Downloads

5

Readme

npm node npm

Install

  npm i --save-dev inject-assets-list-webpack-plugin
  yarn add --dev inject-assets-list-webpack-plugin

Usage

The plugin will generate an JS array for you that includes all your webpack assets(RawSource) in the <head> using <script> tags. Just add the plugin to your webpack config as follows:

webpack.config.js

// !! HtmlWebpackPlugin required
const HtmlWebpackPlugin = require('html-webpack-plugin');
const InjectAssetsListWebpackPlugin = require('inject-assets-list-webpack-plugin');

module.exports = {
  entry: 'index.js',
  publicPath: '/',
  output: {
    path: __dirname + '/dist',
    filename: 'bundle.js',
  },
  plugins: [new HtmlWebpackPlugin(), new InjectAssetsListWebpackPlugin()],
};

This will generate a file dist/index.html containing the following

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>Webpack App</title>
    <script type="text/javascript">
      var __assets = [
        '/img/apple.707709ec.png',
        '/img/banana.51a48343.png',
        /* Webpack assets */
      ];
    </script>
  </head>
  <body>
    <script src="bundle.js"></script>
  </body>
</html>

Assets list value format: <publicPath>name.ext (eg. /img/banana.51a48343.png)

Options

You can pass a hash of configuration options to inject-assets-list-webpack-plugin. Allowed values are as follows:

| Name | Type | Default | Description | | :-----------------: | :--------: | :--------: | :--------------------------------------- | | name | {String} | __assets | The name to use for the global variable | | allowPattern | {RegExp} | undefined | Regular expression for allow assets name | | ignorePattern | {RegExp} | undefined | Regular expression for ignoring assets |

Here's an example webpack config illustrating how to use these options

webpack.config.js

module.exports = {
  entry: 'index.js',
  publicPath: '/',
  output: {
    path: __dirname + '/dist',
    filename: 'index.js',
  },
  plugins: [
    new HtmlWebpackPlugin(),
    new InjectAssetsListWebpackPlugin({
      name: 'myAssets',
      allowPattern: /[png|jpg]/, // Allow `png`, `jpg`
      ignorePattern: /[gif|ttf]/, // ignoring `gif`, `ttf` files
    }),
  ],
};

Sample assets

assets
  ├─ img
  │   ├── apple.png
  │   ├── animation.gif
  │   └── banana.png
  │
  ├─ font
  │   └── my-font.ttf
  │
  └─ content
      ├── post_1.jpg
      ├── post_2.jpg
      └── post_3.jpg

Result

// in <script>
var myAssets = [
  '/img/apple.707709ec.png',
  '/img/banana.51a48343.png',
  '/content/post_1.6b60786f.jpg',
  '/content/post_2.26053162.jpg',
  '/content/post_3.a416371c.jpg',
];

You can uses assets list list like this.

function preload() {
  Promise.allSettled(myAssets.map((uri) => fetch(uri))).then(() => {
    console.log(`${myAssets.length} resource(s) loaded.`);
  });
}

preload(); // 5 resource(s) loaded.

Development

# Install dependencies
npm i

# Build module
npm run build

Changelog

  • 1.0.3 (2020.11.09)
    • Remove RawSource filtering logic
  • 1.0.2 (2020.11.09)
    • FIXED: Combine default options at initialization
    • Update README.md
  • 1.0.1 (2020.11.09)
    • Add allowPattern option
    • Change name property to optional
  • 1.0.0 (2020.11.09)
    • First Release!

Contributors

This project exists thanks to all the people who contribute.

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