Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

workbox-build

Package Overview
Dependencies
Maintainers
3
Versions
101
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

workbox-build - npm Package Compare versions

Comparing version 0.0.2 to 0.0.3

67

build/index.js

@@ -10,26 +10,34 @@ // This will be the logic that powers both module and CLI

*
* **Install:** `npm install --save-dev workbox-build`
* This Node module can be used to generate a list of assets that should be
* precached in a service worker, generating a hash that can be used to
* intelligently update a cache when the service worker is updated.
*
* To get a list of url's and a corresponding revision details, call either
* `generateFileManifest` or `getFileManifestEntries()`.
* This module will use glob patterns to find assets in a given directory
* and use the resulting URL and hash data for one of the follow uses:
*
* `generateFileManifest()` will run globs over a directory of static assets
* and write a JavaScript file containing URL's and revision details for those
* files.
* 1. Generate a complete service worker with precaching and some basic
* configurable options. See
* [generateSW()]{@link module:workbox-build.generateSW}.
* 1. Inject a manifest into an existing service worker. This allows you
* to control your own service worker while still taking advantage of
* [workboxSW.precache()]{@link module:workbox-sw.WorkboxSW#precache} logic.
* See [injectManifest()]{@link module:workbox-build.injectManifest}.
* 1. Generate a manifest file. This is useful if you want to read in the
* urls and revision details via an import script or ES2015 module import.
* See [generateFileManifest()]{@link
* module:workbox-build.generateFileManifest}.
* 1. Get a JS object of the manifest details. Can be used in a build process
* if you want to inject the manifest into a file or template yourself.
* See [getFileManifestEntries()]{@link
* module:workbox-build.getFileManifestEntries}.
*
* If you'd rather receive the data as a JavaScript Array, use
* `getFileManifestEntries()` instead.
*
* If you only need precaching of your static assets in your service
* worker and nothing else, you can generate complete service worker
* with `generateSW()`.
*
* @example <caption>Generate a build manifest file.</caption>
* @example <caption>Generate a complete service worker that will precache
* the discovered assets.</caption>
* const swBuild = require('workbox-build');
*
* swBuild.generateFileManifest({
* swBuild.generateSW({
* globDirectory: './build/',
* staticFileGlobs: ['**\/*.{html,js,css}'],
* globPatterns: ['**\/*.{html,js,css}'],
* globIgnores: ['service-worker.js','admin.html'],
* mainfestDest: './build/scripts/manifest.js',
* swDest: './build/sw.js',
* templatedUrls: {

@@ -40,12 +48,14 @@ * '/shell': ['shell.hbs', 'main.css', 'shell.css'],

* .then(() => {
* console.log('Build file has been created.');
* console.log('Service worker generated.');
* });
*
* @example <caption>Get a list of files with revision details.</caption>
* @example <caption>Generate a file containing the assets to precache.
* </caption>
* const swBuild = require('workbox-build');
*
* swBuild.getFileManifestEntries({
* swBuild.generateFileManifest({
* globDirectory: './build/',
* staticFileGlobs: ['**\/*.{html,js,css}'],
* globPatterns: ['**\/*.{html,js,css}'],
* globIgnores: ['service-worker.js','admin.html'],
* mainfestDest: './build/scripts/manifest.js',
* templatedUrls: {

@@ -55,14 +65,13 @@ * '/shell': ['shell.hbs', 'main.css', 'shell.css'],

* })
* .then((fileDetails) => {
* // An array of file details include a `url` and `revision` parameter.
* .then(() => {
* console.log('Build file has been created.');
* });
*
* @example <caption>Generate a service worker for a project.</caption>
* @example <caption>Get an Array of files with revision details.</caption>
* const swBuild = require('workbox-build');
*
* swBuild.generateSW({
* swBuild.getFileManifestEntries({
* globDirectory: './build/',
* staticFileGlobs: ['**\/*.{html,js,css}'],
* globPatterns: ['**\/*.{html,js,css}'],
* globIgnores: ['service-worker.js','admin.html'],
* swDest: './build/sw.js',
* templatedUrls: {

@@ -72,4 +81,4 @@ * '/shell': ['shell.hbs', 'main.css', 'shell.css'],

* })
* .then(() => {
* console.log('Service worker generated.');
* .then((fileDetails) => {
* // An array of file details include a `url` and `revision` parameter.
* });

@@ -76,0 +85,0 @@ *

@@ -45,3 +45,3 @@ module.exports = {

generateFileManifest() must be either 'iife' (the default) or 'es'.`,
'invalid-static-file-globs': `The 'staticFileGlobs' value must be an array ` +
'invalid-static-file-globs': `The 'globPatterns' value must be an array ` +
`of strings.`,

@@ -52,3 +52,3 @@ 'invalid-templated-urls': `The 'templatedUrls' value should be ` +

'templated-url-matches-glob': `One of the 'templatedUrls' URL s` +
`is already being tracked via staticFileGlobs: `,
`is already being tracked via 'globPatterns': `,
'invalid-glob-ignores': `The 'globIgnore' parameter must be an ` +

@@ -76,2 +76,6 @@ `array string glob patterns.`,

`array of objects with at least a 'urlPattern' and 'handler'.`,
'both-glob-patterns-static-file-globs': `Both globPatterns and ` +
`staticFileGlobs are set. Please fully migrate to globPatterns.`,
'both-templated-urls-dynamic-urls': `Both templatedUrls and ` +
`dynamicUrlToDependencies are set. Please fully migrate to templatedUrls.`,
};

@@ -6,4 +6,42 @@ const getFileManifestEntries = require('./get-file-manifest-entries');

/**
* @example <caption>Generate a build manifest of static assets, which could
* then be used with a service worker.</caption>
* This method will generate a file manifest that can be used in a service
* worker to precache assets.
*
* @param {Object} input
* @param {String} [input.format] There are some options for how the file
* manifest is formatted in the final output. The format can be one of the
* following values:
* - **'iife'** - Output the manifest as an
* [immediately invoked function](https://en.wikipedia.org/wiki/Immediately-invoked_function_expression).
* - **'es'** - Output as an ES2015 module.
*
* Default value is 'iife'.
* @param {String} input.manifestDest The file path and name where the file
* manifest should be written (i.e. `./build/precache-manifest.js`).
* @param {String} input.globDirectory The directory you wish to run the
* `globPatterns` against.
* @param {Array<String>} input.globPatterns Files matching against any of
* these glob patterns will be included in the file manifest.
* @param {String|Array<String>} [input.globIgnores] Files matching against any
* of these glob patterns will be excluded from the file manifest, even if the
* file matches against a `globPatterns` pattern.
* @param {Object<String,Array|String>} [input.templatedUrls]
* If a URL is rendered with templates on the server, its contents may
* depend on multiple files. This maps URLs to an array of file names, or to a
* string value, that uniquely determines the URL's contents.
* @param {String} [input.modifyUrlPrefix] An object of key value pairs
* where URL's starting with the key value will be replaced with the
* corresponding value.
* @param {number} [input.maximumFileSizeToCacheInBytes] This value can be used
* to determine the maximum size of files that will be precached.
*
* Defaults to 2MB.
* @param {RegExp} [input.dontCacheBustUrlsMatching] Assets that match this
* regex will not have their revision details included in the precache. This
* is useful for assets that have revisioning details in the filename.
* @return {Promise} The returned promise resolves once the manifest file has
* been generated.
*
* @example <caption>Generate a build manifest of static assets, which can
* used with a service worker.</caption>
* const swBuild = require('workbox-build');

@@ -14,3 +52,3 @@ *

* globDirectory: './build/',
* staticFileGlobs: ['**\/*.{html,js,css}'],
* globPatterns: ['**\/*.{html,js,css}'],
* globIgnores: ['admin.html'],

@@ -23,23 +61,2 @@ * format: 'iife', // alternatively, use 'es'

*
* This method will generate a file manifest that can be used in a service
* worker for caching assets offline.
* @param {Object} input
* @param {String} input.manifestDest The name and path you wish to write your
* manifest file to.
* @param {String} input.globDirectory The root of the files you wish to
* be cached. This will also be the directory the service worker and library
* files are written to.
* @param {Array<String>} input.staticFileGlobs Patterns to glob for when
* generating the build manifest.
* @param {String|Array<String>} [input.globIgnores] Patterns to exclude when
* generating the build manifest.
* @param {Object<String,Array|String>} [input.templatedUrls]
* If a URL is rendered/templated on the server, its contents may not depend on
* a single file. This maps URLs to a list of file names, or to a string
* value, that uniquely determines each URL's contents.
* @param {String} [input.format] Default format is [`'iife'`](https://en.wikipedia.org/wiki/Immediately-invoked_function_expression), but also
* accepts `'es'`, which outputs an ES2015 module.
* @return {Promise} Resolves once the service worker has been generated
* with a precache list.
*
* @memberof module:workbox-build

@@ -46,0 +63,0 @@ */

const path = require('path');
const copySWLib = require('./utils/copy-workbox-sw');
const copyWorkboxSW = require('./utils/copy-workbox-sw');
const getFileManifestEntries = require('./get-file-manifest-entries');

@@ -8,9 +8,74 @@ const writeServiceWorker = require('./write-sw');

/**
* @example <caption>Generate a service worker for a project.</caption>
* This method will generate a working service worker with an inlined
* file manifest.
*
* @param {Object} input
* @param {String} input.swDest The file path and name you wish to writh the
* service worker file to.
* @param {String} input.globDirectory The directory you wish to run the
* `globPatterns` against.
* @param {Array<String>} input.globPatterns Files matching against any of
* these glob patterns will be included in the file manifest.
* @param {String|Array<String>} [input.globIgnores] Files matching against any
* of these glob patterns will be excluded from the file manifest, even if the
* file matches against a `globPatterns` pattern.
* @param {Object<String,Array|String>} [input.templatedUrls]
* If a URL is rendered with templates on the server, its contents may
* depend on multiple files. This maps URLs to an array of file names, or to a
* string value, that uniquely determines the URL's contents.
* @param {string} [input.navigateFallback] This URL will be used as a fallback
* if a navigation request can't be fulfilled. Normally this URL would be
* precached so it's always available. This is particularly useful for single
* page apps where requests should go to a single URL.
* @param {Array<Regexp>} [input.navigateFallbackWhitelist] An optional Array
* of regexs to restrict which URL's use the `navigateFallback` URL.
* @param {String} [input.cacheId] An optional ID to be prepended to caches
* used by workbox-build. This is primarily useful for local development where
* multiple sites may be served from the same `http://localhost` origin.
* @param {Boolean} [input.skipWaiting] When set to true the generated service
* worker activate immediately.
*
* Defaults to false.
* @param {Boolean} [input.clientsClaim] When set to true the generated service
* worker will claim any currently open pages.
*
* Defaults to false.
* @param {string} [input.directoryIndex] If a request for a URL ending in '/'
* fails, this value will be appended to the URL and a second request will be
* made.
*
* Defaults to 'index.html'.
* @param {Array<Object>} [input.runtimeCaching] Passing in an array of objects
* containing a `urlPattern` and a `handler` parameter will add the appropriate
* code to the service work to handle run time caching for URL's matching the
* pattern with the associated handler behavior.
* @param {String} [input.modifyUrlPrefix] An object of key value pairs
* where URL's starting with the key value will be replaced with the
* corresponding value.
* @param {Array<RegExp>} [input.ignoreUrlParametersMatching] Any search
* parameters matching against one of the regex's in this array will be removed
* before looking for a cache match.
* @param {Boolean} [input.handleFetch] When set to false all requests will
* go to the network. This is useful during development if you don't want the
* service worker from preventing updates.
*
* Defaults to true.
* @param {number} [input.maximumFileSizeToCacheInBytes] This value can be used
* to determine the maximum size of files that will be precached.
*
* Defaults to 2MB.
* @param {RegExp} [input.dontCacheBustUrlsMatching] Assets that match this
* regex will not have their revision details included in the precache. This
* is useful for assets that have revisioning details in the filename.
* @return {Promise} Resolves once the service worker has been generated
* with a precache list.
*
* @example <caption>Generate a service worker with precaching support.
* </caption>
* const swBuild = require('workbox-build');
*
* swBuild.generateSW({
* swDest: './build/sw.js',
* globDirectory: './build/',
* swDest: './build/sw.js',
* staticFileGlobs: ['**\/*.{html,js,css}'],
* globPatterns: ['**\/*.{html,js,css}'],
* globIgnores: ['admin.html'],

@@ -25,50 +90,2 @@ * templatedUrls: {

*
* This method will generate a working service worker with an inlined
* file manifest.
* @param {Object} input
* @param {String} input.globDirectory The root of the files you wish to
* be cached. This will also be the directory the service worker and library
* files are written to.
* @param {Array<String>} input.staticFileGlobs Patterns to glob for when
* generating the build manifest.
* @param {String|Array<String>} [input.globIgnores] Patterns to exclude when
* generating the build manifest.
* @param {String} input.swDest The name you wish to give to your
* service worker file.
* @param {Object<String,Array|String>} [input.templatedUrls]
* If a URL is rendered/templated on the server, its contents may not depend on
* a single file. This maps URLs to a list of file names, or to a string
* value, that uniquely determines each URL's contents.
* @param {String} [input.modifyUrlPrefix] An optional object of key value pairs
* where the key will be replaced at the start of a url with the corresponding
* value.
* @param {String} [input.cacheId] An optional ID to be prepended to caches
* used by workbox-build. This is primarily useful for local development where
* multiple sites may be served from `http://localhost`.
* @param {Boolean} [input.handleFetch] Stops the generated service worker
* from handling fetch events, i.e. everything goes to the network.
* (Defaults to true.)
* @param {Boolean} [input.skipWaiting] An optional boolean that indicates if
* the new service worker should activate immediately (Defaults to false).
* @param {Boolean} [input.clientsClaim] An optional boolean that indicates if
* the new service worker should claim current pages (Defaults to false).
* @param {string} [input.directoryIndex] An optional string that will
* append this string to urls ending with '/' (Defaults to 'index.html').
* @param {number} [input.maximumFileSizeToCacheInBytes] An optional number to
* define the maximum file size to consider whether the file should be
* precached. (Defaults to 2MB).
* @param {RegExp} [input.dontCacheBustUrlsMatching] An optional regex that will
* return a URL string and exclude the revision details for urls matching this
* regex. Useful if you have assets with file revisions in the URL.
* @param {string} [input.navigateFallback] An optional string that will
* attempt to serve the response for the URL defined as this option from cache.
* @param {Array<Regexp>} [input.navigateFallbackWhitelist] An optional Array
* of regexs to restrict which URL's use the navigateFallback cached response.
* @param {Array<Object>} [input.runtimeCaching] An optional Array
* of objects to define run time caching strategies.
* @param {Array<RegExp>} [input.ignoreUrlParametersMatching] An array of
* regex's to remove search params when looking for a cache match.
* @return {Promise} Resolves once the service worker has been generated
* with a precache list.
*
* @memberof module:workbox-build

@@ -109,3 +126,3 @@ */

let destDirectory = path.dirname(swDest);
return copySWLib(destDirectory)
return copyWorkboxSW(destDirectory)
.then((libPath) => {

@@ -112,0 +129,0 @@ // If sw file is in build/sw.js, the workboxSW file will be

@@ -21,19 +21,26 @@ const errors = require('./errors');

* @param {Object} input
* @param {Array<String>} input.staticFileGlobs Patterns used to select files to
* include in the file entries.
* @param {Array<String>} [input.globIgnores] Patterns used to exclude files
* from the file entries.
* @param {String} input.globDirectory The directory run the glob patterns over.
* @param {String} input.globDirectory The directory you wish to run the
* `globPatterns` against.
* @param {Array<String>} input.globPatterns Files matching against any of
* these glob patterns will be included in the file manifest.
* @param {String|Array<String>} [input.globIgnores] Files matching against any
* of these glob patterns will be excluded from the file manifest, even if the
* file matches against a `globPatterns` pattern.
* @param {Object<String,Array|String>} [input.templatedUrls]
* If a URL is rendered/templated on the server, its contents may not depend on
* a single file. This maps URLs to a list of file names, or to a string
* value, that uniquely determines each URL's contents.
* @param {number} [input.maximumFileSizeToCacheInBytes] An optional number to
* define the maximum file size to consider whether the file should be
* precached. (Defaults to 2MB).
* If a URL is rendered with templates on the server, its contents may
* depend on multiple files. This maps URLs to an array of file names, or to a
* string value, that uniquely determines the URL's contents.
* @param {String} [input.modifyUrlPrefix] An object of key value pairs
* where URL's starting with the key value will be replaced with the
* corresponding value.
* @param {number} [input.maximumFileSizeToCacheInBytes] This value can be used
* to determine the maximum size of files that will be precached.
*
* Defaults to 2MB.
* @param {RegExp} [input.dontCacheBustUrlsMatching] An optional regex that will
* return a URL string and exclude the revision details for urls matching this
* regex. Useful if you have assets with file revisions in the URL.
* @return {Array<ManifestEntry>} An array of ManifestEntries will include
* a url and revision details for each file found.
* @return {Array<ManifestEntry>}
* An array of {@link module:workbox-build#ManifestEntry|ManifestEntries}
* which will include a url and revision parameter.
* @memberof module:workbox-build

@@ -46,6 +53,15 @@ */

const staticFileGlobs = input.staticFileGlobs;
// staticFileGlobs is to ease workbox to sw-precache migration.
if (input.globPatterns && input.staticFileGlobs) {
throw new Error(errors['both-glob-patterns-static-file-globs']);
}
const globPatterns = input.globPatterns || input.staticFileGlobs;
const globIgnores = input.globIgnores ? input.globIgnores : [];
const globDirectory = input.globDirectory;
// dynamicUrlToDependencies is for workbox-precaching parity / migration.
// dynamicUrlToDependencies is to ease workbox to sw-precache migration.
if (input.templatedUrls && input.dynamicUrlToDependencies) {
throw new Error(errors['both-templated-urls-dynamic-urls']);
}
const templatedUrls = input.templatedUrls || input.dynamicUrlToDependencies;

@@ -58,3 +74,3 @@

if (!staticFileGlobs || !Array.isArray(staticFileGlobs)) {
if (!globPatterns || !Array.isArray(globPatterns)) {
return Promise.reject(

@@ -88,3 +104,3 @@ new Error(errors['invalid-static-file-globs']));

const fileDetails = staticFileGlobs.reduce((accumulated, globPattern) => {
const fileDetails = globPatterns.reduce((accumulated, globPattern) => {
const globbedFileDetails = getFileDetails(

@@ -91,0 +107,0 @@ globDirectory, globPattern, globIgnores);

@@ -9,2 +9,36 @@ const fs = require('fs');

/**
* This method will read an existing service worker file and replace an empty
* precache() call, like: `.precache([])`, and replace the array with
* an array of assets to precache. This allows the service worker
* to efficiently cache assets for offline use.
*
* @param {Object} input
* @param {String} input.swSrc File path and name of the service worker file
* to read and inject the manifest into before writing to `swDest`.
* @param {String} input.swDest The file path and name you wish to writh the
* service worker file to.
* @param {String} input.globDirectory The directory you wish to run the
* `globPatterns` against.
* @param {Array<String>} input.globPatterns Files matching against any of
* these glob patterns will be included in the file manifest.
* @param {String|Array<String>} [input.globIgnores] Files matching against any
* of these glob patterns will be excluded from the file manifest, even if the
* file matches against a `globPatterns` pattern.
* @param {Object<String,Array|String>} [input.templatedUrls]
* If a URL is rendered with templates on the server, its contents may
* depend on multiple files. This maps URLs to an array of file names, or to a
* string value, that uniquely determines the URL's contents.
* @param {String} [input.modifyUrlPrefix] An object of key value pairs
* where URL's starting with the key value will be replaced with the
* corresponding value.
* @param {number} [input.maximumFileSizeToCacheInBytes] This value can be used
* to determine the maximum size of files that will be precached.
*
* Defaults to 2MB.
* @param {RegExp} [input.dontCacheBustUrlsMatching] An optional regex that will
* return a URL string and exclude the revision details for urls matching this
* regex. Useful if you have assets with file revisions in the URL.
* @return {Promise} Resolves once the service worker has been written
* with the injected precache list.
*
* @example <caption>Generate a build manifest of static assets, which could

@@ -16,3 +50,3 @@ * then be used with a service worker.</caption>

* globDirectory: './build/',
* staticFileGlobs: ['**\/*.{html,js,css}'],
* globPatterns: ['**\/*.{html,js,css}'],
* globIgnores: ['admin.html'],

@@ -26,25 +60,2 @@ * swSrc: './src/sw.js',

*
* This method will read in an existing service worker file and replace an empty
* array in a call like so: `.precache([])`, to an array of files
* with up to date array revision details. This allows the service worker
* to efficiently cache assets that will be available offline.
* @param {Object} input
* @param {String} input.swDest The name and path you wish to write your
* manifest file to.
* @param {String} input.globDirectory The root of the files you wish to
* be cached. This will also be the directory the service worker and library
* files are written to.
* @param {Array<String>} input.staticFileGlobs Patterns to glob for when
* generating the build manifest.
* @param {String|Array<String>} [input.globIgnores] Patterns to exclude when
* generating the build manifest.
* @param {Object<String,Array|String>} [input.templatedUrls]
* If a URL is rendered/templated on the server, its contents may not depend on
* a single file. This maps URLs to a list of file names, or to a string
* value, that uniquely determines each URL's contents.
* @param {String} input.swSrc File name for service worker file to read in
* and alter.
* @return {Promise} Resolves once the service worker has been generated
* with a precache list.
*
* @memberof module:workbox-build

@@ -51,0 +62,0 @@ */

{
"name": "workbox-build",
"version": "0.0.2",
"description": "This module can be used to generate a file manifest or service worker, that can be used with workbox-sw.",
"version": "0.0.3",
"description": "A module that integrates into your build process, helping you generate a manifest of local files that workbox-sw should precache.",
"keywords": [

@@ -27,4 +27,4 @@ "workbox",

"repository": "googlechrome/sw-helpers",
"bugs": "https://github.com/googlechrome/sw-helpers/issues",
"homepage": "https://github.com/GoogleChrome/sw-helpers/tree/master/packages/workbox-build",
"bugs": "https://github.com/GoogleChrome/workbox/issues",
"homepage": "https://github.com/GoogleChrome/workbox/tree/master/packages/workbox-build",
"dependencies": {

@@ -36,4 +36,4 @@ "chalk": "^1.1.3",

"mkdirp": "^0.5.1",
"workbox-sw": "^0.0.2"
"workbox-sw": "^0.0.3"
}
}

@@ -1,18 +0,32 @@

<!-- DO NOT EDIT. This page is autogenerated. -->
<!-- To make changes, edit templates/Project-README.hbs, not this file. -->
# workbox-build
This module can be used to generate a file manifest or service worker, that can be used with workbox-sw.
A module that integrates into your build process, helping you generate a
manifest of local files that [`workbox-sw`](../workbox-sw) should precache.
It can also scaffold a full service worker implementation for you.
While you can use this module directly, there are both
[command-line](../workbox-cli) and [Webpack](../workbox-webpack-plugin)
wrappers.
## Installation
`npm install --save-dev workbox-build`
```sh
npm install --save-dev workbox-build
```
## Demo
## Documentation
Browse sample source code in the [demo directory](https://github.com/GoogleChrome/workbox/tree/master/packages/workbox-build/demo).
Read more at this module's [documentation page](https://workboxjs.org/reference-docs/latest/module-workbox-build.html).
## Reference Docs
## Sample Code and Examples
You can find [documentation for this module here](https://googlechrome.github.io/workbox/reference-docs/stable/latest/module-workbox-build.html#main).
View the
[sample code](https://github.com/GoogleChrome/workbox/tree/master/packages/workbox-build/demo)
to see this module put to use.
# What's Workbox?
This module is a part of Workbox, which is a collection of JavaScript libraries
for [Progressive Web Apps](https://developers.google.com/web/progressive-web-apps/).
Visit https://workboxjs.org/ to learn more about what Workbox can do for you.

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc