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

custom-pwa-webpack-plugin

v0.0.28

Published

Make You PWA with You scripts

Downloads

37

Readme

custom-pwa-webpack-plugin

webpack plugin for development and customization service worker

webpack

A plugin to help you create your Progressive Web Apps.

It will suit you if you know what you are doing, and you just need a tool for this.

Usage:

Add this code to the script of your service worker. It will be replaced by the same variables. Only with actual values.

my-service-worker.js

// <version_template
const VERSION = '0.1';
const files = [];
// end_version_template>

// ...

You can use the files to create a query cache. They will be collected relative to the pattern specified in the options.

Next, connect the plugin and specify the desired options.

webpack.config.js

var path = require('path');
var CustomPwaWebpackPlugin = require('custom-pwa-webpack-plugin');

module.exports = {
    // You config
    entry: path.join(__dirname, './src/index.js'),
    output: {
        filename: 'bundle.js'
    },
    // ...

    plugins: [
        new CustomPwaWebpackPlugin({
            entry: path.join(__dirname, 'my-service-worker.js'),
            dist: path.join(__dirname, 'dist'), 
            version: require('./package').version,
            name: 'service-worker.js',
            file_pattern: /\.(js|css|html)$/
        })
    ]
}


| Parameters | Default | Required | Description | |---------------|:------------------------|:-------------:|--------------| | entry | | true | The file from which to start the build script (absolute path) | run | true | false | If you don't need create service worker | dist | | false | The folder to save the script (absolute path) | version | | false | Version to create the cache | name | 'service-worker.js' | false | File name | file_pattern | /\.(js|css|html)$/i | false | Regular expression for file caching | file_prefix | '/' | false | Prefix path for files | add | [] | false | The list of paths added manually | replace_names | {} | false | Replace full path name for files, | mode | 'production' | false | Service Worker build file mode | num_runned | 1 | false | HACK for webpack <4 and Nuxt | dev | false | false | true - if watch mode

num_runned - Need if you start multiple processes, and only one need to run. Then specify the number. For Nuxt is 1.


Options

replace_names

webpack.config.js

var path = require('path');
var CustomPwaWebpackPlugin = require('custom-pwa-webpack-plugin');

module.exports = {
    // You config
    entry: path.join(__dirname, './src/index.js'),
    output: {
        filename: 'bundle.js'
    },
    // ...

    plugins: [
        new CustomPwaWebpackPlugin({
            entry: path.join(__dirname, 'my-service-worker.js'),
            replace_names: {
                // key is will be replaced with value
                '/_nuxt/index.spa.html': '/'
            }
        })
    ]
}

Tips for NuxtJs

// nuxt.config.js

module.exports = {
    build: {
        plugins: [
            new CustomPwaWebpackPlugin({
                entry: path.join(__dirname, 'sw/src/index.js'),
                dist: path.join(__dirname, 'static'),
                version: require('./package').version,
                file_pattern: /\.(html|js|css|woff|woff2|ttf|eot|svg)$/i,
                file_prefix: '/_nuxt/',
                num_runned: 1 // for "npm run generate"
            })
        ]
    }
}

In layout/default.vue add

<script type="text/javascript">
(function() {
    'use strict';
    // Check to make sure service workers are supported in the current browser,
    // and that the current page is accessed from a secure origin. Using a
    // service worker from an insecure origin will trigger JS console errors.
    const isLocalhost = Boolean(window.location.hostname === 'localhost' ||
        // [::1] is the IPv6 localhost address.
        window.location.hostname === '[::1]' ||
        // 127.0.0.1/8 is considered localhost for IPv4.
        window.location.hostname.match(
            /^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/
        )
    );

if ('serviceWorker' in navigator &&
    (window.location.protocol === 'https:' || isLocalhost)) {
    navigator.serviceWorker.register('/service-worker.js')
        .then(function(registration) {
            // updatefound is fired if service-worker.js changes.
            registration.onupdatefound = function() {
                // updatefound is also fired the very first time the SW is installed,
                // and there's no need to prompt for a reload at that point.
                // So check here to see if the page is already controlled,
                // i.e. whether there's an existing service worker.
                if (navigator.serviceWorker.controller) {
                    // The updatefound event implies that registration.installing is set
                    const installingWorker = registration.installing;
                    installingWorker.onstatechange = function() {
                        switch (installingWorker.state) {
                            case 'installed':
                                // At this point, the old content will have been purged and the
                                // fresh content will have been added to the cache.
                                // It's the perfect time to display a "New content is
                                // available; please refresh." message in the page's interface.
                                break;
                            case 'redundant':
                                throw new Error('The installing ' +
                                    'service worker became redundant.');
                            default:
                                // Ignore
                        }
                    };
                }
            };
        }).catch(function(e) {
            console.error('Error during service worker registration:', e);
        });
    }
})();
</script>