All Projects → oliviertassinari → Serviceworker Webpack Plugin

oliviertassinari / Serviceworker Webpack Plugin

Licence: mit
Simplifies creation of a service worker to serve your webpack bundles. ♻️

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Serviceworker Webpack Plugin

Webpack Plugin Hash Output
Plugin to replace webpack chunkhash with an md5 hash of the final file conent.
Stars: ✭ 128 (-71.81%)
Mutual labels:  webpack, plugin
Webpack Deadcode Plugin
Webpack plugin to detect unused files and unused exports in used files
Stars: ✭ 180 (-60.35%)
Mutual labels:  webpack, plugin
Webpack Internal Plugin Relation
🔎 a tiny tool to show the relation of webpack internal plugins & hooks
Stars: ✭ 135 (-70.26%)
Mutual labels:  webpack, plugin
Wordless
All the power of Pug, Sass, Coffeescript and WebPack in your WordPress theme. Stop writing themes like it's 1998.
Stars: ✭ 1,374 (+202.64%)
Mutual labels:  webpack, plugin
Webpack Pwa Manifest
Progressive Web App Manifest Generator for Webpack, with auto icon resizing and fingerprinting support.
Stars: ✭ 447 (-1.54%)
Mutual labels:  webpack, plugin
Sounds Webpack Plugin
🔊Notify or errors, warnings, etc with sounds
Stars: ✭ 125 (-72.47%)
Mutual labels:  webpack, plugin
Webpack Fast Refresh
React Fast Refresh plugin and loader for webpack
Stars: ✭ 155 (-65.86%)
Mutual labels:  webpack, plugin
Serverless Plugin Webpack
Serverless Plugin Webpack
Stars: ✭ 72 (-84.14%)
Mutual labels:  webpack, plugin
Grunt Webpack
integrate webpack into grunt build process
Stars: ✭ 249 (-45.15%)
Mutual labels:  webpack, plugin
Webpackmonitor
A tool for monitoring webpack optimization metrics through the development process
Stars: ✭ 2,432 (+435.68%)
Mutual labels:  webpack, plugin
Fork Ts Checker Webpack Plugin
Webpack plugin that runs typescript type checker on a separate process.
Stars: ✭ 1,343 (+195.81%)
Mutual labels:  webpack, plugin
Webpack Ops
📁 webpack bundle visualization // optimization // config tool
Stars: ✭ 251 (-44.71%)
Mutual labels:  webpack, plugin
Cypress Webpack Preprocessor
Cypress preprocessor for bundling JavaScript via webpack
Stars: ✭ 93 (-79.52%)
Mutual labels:  webpack, plugin
Webapp Webpack Plugin
[DEPRECATED] use favicons-webpack-plugin instead
Stars: ✭ 127 (-72.03%)
Mutual labels:  webpack, plugin
Award
⚙基于react的服务端渲染框架
Stars: ✭ 91 (-79.96%)
Mutual labels:  webpack, plugin
Speed Measure Webpack Plugin
⏱ See how fast (or not) your plugins and loaders are, so you can optimise your builds
Stars: ✭ 1,980 (+336.12%)
Mutual labels:  webpack, plugin
Prepack Webpack Plugin
A webpack plugin for prepack.
Stars: ✭ 1,054 (+132.16%)
Mutual labels:  webpack, plugin
Craco Alias
A craco plugin for automatic aliases generation for Webpack and Jest
Stars: ✭ 56 (-87.67%)
Mutual labels:  webpack, plugin
Vue Plugin Template
🚀 Solid foundation to start a Vue plugin with the best developer experience and a focus on performance
Stars: ✭ 189 (-58.37%)
Mutual labels:  webpack, plugin
Webpack Fix Style Only Entries
Webpack plugin to solve the problem of having a style only entry (css/sass/less) generating an extra js file.
Stars: ✭ 250 (-44.93%)
Mutual labels:  webpack, plugin

serviceworker-webpack-plugin

Simplifies creation of a service worker to serve your webpack bundles.

npm version npm downloads Build Status

Dependencies DevDependencies

Installation

npm install serviceworker-webpack-plugin

The problem solved

When building a service worker, you probably want to cache all your assets during the install phase. But in order to do so, you need their names. That's not simple when you are using Webpack:

  • The assets names are non-deterministic when taking advantage of the long term caching.
  • The assets list can even evolve over time as you add splitting points or more resources.
  • You want to be able to use your service worker with the dev-server mode of Webpack.
  • You want to keep the build process as simple as possible.

Setup

1. Add the plugin to your webpack config

import ServiceWorkerWebpackPlugin from 'serviceworker-webpack-plugin';

...

  plugins: [
    new ServiceWorkerWebpackPlugin({
      entry: path.join(__dirname, 'src/sw.js'),
    }),
  ],

2. Register the service worker in your main JS thread

import runtime from 'serviceworker-webpack-plugin/lib/runtime';

if ('serviceWorker' in navigator) {
  const registration = runtime.register();
}

3. Write your own sw.js

You can now use the global serviceWorkerOption variable in your sw.js. E.g. In our example this object looks like:

{
  assets: [
    './main.256334452761ef349e91.js',
  ],
}

Simple example

You can have a look at the /docs folder if you need a full working example.

API

ServiceWorkerWebpackPlugin(options)

  • options
  • entry, required, string: Path to the actual service worker implementation.
  • filename, string, default 'sw.js': Relative (from the webpack's config output.path) output path for emitted script.
  • excludes, array, default ['**/.*', '**/*.map']: Exclude matched assets from being added to the serviceWorkerOption.assets variable. (Blacklist)
  • includes, array, default ['**/*']: Include matched assets added to the serviceWorkerOption.assets variable. (Whitelist)
  • publicPath, string, default '/': Specifies the public URL address of the output files when referenced in a browser. We use this value to load the service worker over the network.
  • template, function, default noop: This callback function can be used to inject statically generated service worker. It's taking a serviceWorkerOption argument and must return a promise.
  • transformOptions, function: This callback function receives a raw serviceWorkerOption argument. The jsonStats key contains all the webpack build information.
  • minimize: Whether to minimize output. Defaults to process.env.NODE_ENV === 'production'

runtime(options)

Credit

Why simply not use the offline-plugin?

I wouldn't have been able to write this plugin without the offline-plugin project. Thanks @NekR for sharing it!

Still, soon after using it, I realized that it wasn't what I was looking for.

  • The abstraction provided was too high. (I needed to build some custom fetch logic.)
  • It was making me, even more, dependent on Webpack. (What if later, I want to switch to another build system?)

Hence, I decided to change the approach and created this thin layer on top of Webpack to solve the assets name issue. Nothing more.

If you don't care about my two issues with offline-plugin then you don't need to use this package, offline-plugin is great.

The specs

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].