Created
March 31, 2021 17:36
-
-
Save woss/e10650ea45f289194c64bfdb0b877f09 to your computer and use it in GitHub Desktop.
Simple plugin for injecting the precache files and setting up the local workbox files.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { injectManifest, InjectManifestConfig, copyWorkboxLibraries } = require('workbox-build') | |
const rimraf = require('rimraf') | |
const { convertCompilerOptionsFromJson } = require('typescript') | |
const prependFile = require('prepend-file') | |
const { isNil } = require('ramda') | |
const { isEmpty } = require('ramda') | |
const { map } = require('ramda') | |
/** | |
* Injects the Manifest for the Service worker | |
* @param {*} snowpackConfig | |
* @param {{workbox: InjectManifestConfig, exclude: string[] }} pluginOptions | |
* @returns | |
*/ | |
module.exports = function (snowpackConfig, { exclude, workbox }) { | |
return { | |
name: 'workbox-plugin', | |
async optimize({ buildDirectory, log }) { | |
try { | |
const libPath = await copyWorkboxLibraries(buildDirectory) | |
const importLibsTemplate = `// import ${libPath} | |
importScripts('/${libPath}/workbox-sw.js') | |
workbox.setConfig({ | |
modulePathPrefix: '/${libPath}/', | |
debug: false | |
}) | |
` | |
log('Removing the *.dev.* files from the workbox source libraries') | |
rimraf.sync(`${buildDirectory}/${libPath}/*.dev.*`) | |
if (!isNil(exclude) || !isEmpty(exclude)) { | |
map((e) => { | |
log(`Removing the ${e} files from the workbox source libraries`) | |
rimraf.sync(`${buildDirectory}/${libPath}/workbox-${e}*`) | |
}, exclude) | |
} | |
const { count, size } = await injectManifest({ | |
...workbox, | |
}) | |
await prependFile(workbox.swDest, importLibsTemplate) | |
log( | |
`Generated ${workbox.swDest}, which will precache ${count} files, totaling ${( | |
size / 1000 | |
).toFixed(2)} KB.`, | |
) | |
} catch (error) { | |
throw new Error(error) | |
} | |
log('Now you have service-worker 🎉') | |
}, | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Snowpack Configuration File | |
// See all supported options: https://www.snowpack.dev/reference/configuration | |
const path = require("path"); | |
/** @type {import("snowpack").SnowpackUserConfig } */ | |
module.exports = { | |
mount: { | |
public: { url: "/", static: true, resolve: false }, | |
src: { url: "/dist" }, | |
}, | |
plugins: [ | |
[ | |
"./plugins/workbox.js", | |
{ | |
workbox: { | |
globDirectory: "build/", | |
globPatterns: ["**/*.{html,json,js,css}"], | |
swDest: path.resolve(__dirname, "build/service-worker.js"), | |
swSrc: path.resolve(__dirname, "build/service-worker.js"), | |
}, | |
exclude: ["offline-ga"], | |
}, | |
], | |
], | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment