Yet another Service Workers loader for webpack.
npm install --save-dev sw-loaderThis loader emits a standalone service worker file and exports the entry point.
import scriptURL from 'sw-loader!./sw.js';
// => 'scriptURL = {output.publicPath}/sw.js'
// => file location: {output.path}/sw.js
navigator.serviceWorker.register(scriptURL).then(...);Specify the name of output file:
import scriptURL from 'sw-loader?name=my-worker.js!./sw.js';
// => 'scriptURL = {output.publicPath}/my-worker.js'
// => file location: {output.path}/my-worker.jsNotice: this loader DOES NOT support filename interpolation, for the reason that a service worker is considered as a persistent resource.
By default the output path follows output.path option in webpack configurations, you can change it to anywhere you like.
The outputPath will be resolved to project root.
import scriptURL from 'sw-loader?outputPath=build/workers/!./sw.js';
// => 'scriptURL = {output.publicPath}/sw.js'
// => file location: build/workers/sw.jsThe outputPath modifies only the output destination, if you want to change the access URL address of service worker script, use the publicPath option below.
By default this loader uses output.publicPath to concatenate the public URL address of service worker script, you can modify it by:
import scriptURL from 'sw-loader?publicPath=/!./sw.js';
// => 'scriptURL = /sw.js'
// => file location: {output.path}/sw.js