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

@mightyplow/inject-assets-webpack-plugin

v1.1.1

Published

webpack plugin to inject built assets into a file

Downloads

4

Readme

inject-assets-webpack-plugin

A plugin for webpack 4 which replaces assets with it's output filenames in other assets. It was created to be used for caching in service workers.

The plugin comes in handy when you add hashes to your assets during the webpack build.

It can also be used in watch mode.

installation

npm i -D @mightyplow/inject-assets-webpack-plugin

usage

The assets you want to be replaced have to be wrapped in double curly braces. The assets can only be replaced if the target is also a webpack asset.

So for example if you want to replace the assets in a service worker, it has either be built by webpack or at least run through another plugin. Personally I like to use the copy-webpack-plugin to copy the service worker to the target directory.

If there are multiple files for the same asset name, for example when you have a css file and a js file with the same asset name, you can specify the wanted asset by adding a pipe symbol and the file extension.

[
    '{{vendor|js}}',
    '{{vendor|css}}'
]

options

targets: string[] - required

These are the asset names in which you want the placeholders to be replaced.

examples

Let's assume, our service worker has the following content

// serviceWorker.js
const FILES_TO_CACHE = [
    '/',
    '{{vendor}}',
    '{{app|js}}'
    '{{app|css}}'
]

copied with webpack-copy-plugin

// webpack.config.js
const CopyPlugin = require('copy-webpack-plugin');
const InjectAssetsPlugin = require('@mightyplow/inject-assets-webpack-plugin');

...

plugins: [
    new CopyPlugin([
        {
            from: './src/serviceWorker.js',
            to: './'
        }
    ]),
    
    new InjectAssetsPlugin({
        targets: ['serviceWorker.js']
    })
]
...

build service worker with webpack

// webpack.config.js
const InjectAssetsPlugin = require('@mightyplow/inject-assets-webpack-plugin');

...

entry: {
    // build the app and the service worker with webpack
    app: path.join(__dirname, 'src', 'index.js'),
    serviceWorker: path.join(__dirname, 'src', 'serviceWorker.js')
},

plugins: [
    new InjectAssetsPlugin({
        // replace assets in the service worker and in the app script;
        // since there would also be an app css asset, we have to specify the type here
        targets: ['serviceWorker', 'app|js']
    })
]
...
// index.js

if ('serviceWorker' in navigator) {
    // {{serviceWorker}} will be replaced with the built file name 
    navigator.serviceWorker.register('{{serviceWorker}}')
        .then((registration) => {
            console.info('service worker registered');
        });
}