Skip to content

Commit

Permalink
Add warning when using filepath option
Browse files Browse the repository at this point in the history
  • Loading branch information
goldhand committed Jan 18, 2017
1 parent 6c587bb commit a38ce9a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
8 changes: 4 additions & 4 deletions README.md
@@ -1,8 +1,8 @@
SW Precache Webpack Plugin
==========================
[![NPM version][npm-img]][npm-url]
[![NPM downloads][npm-downloads-img]][npm-url]
[![Dependency Status][daviddm-img]][daviddm-url]
[![devDependency Status][daviddmdev-img]][daviddmdev-url]
[![CircleCI][circleci-img]][circleci-url]

__`SWPrecacheWebpackPlugin`__ is a [webpack][webpack] plugin for using [service workers][sw-guide] to cache your external project dependencies. It will generate a service worker file using [sw-precache][sw-precache] and add it to your build directory.
Expand Down Expand Up @@ -68,8 +68,9 @@ You can pass a hash of configuration options to `SWPrecacheWebpackPlugin`:

__plugin options__:
* `filename`: `[String]` - Service worker filename, default is `service-worker.js`
* `filepath`: `[String]` - Service worker path and name, default is to use `webpack.output.path` + `options.filename`.
* `filepath`: `[String]` - Service worker path and name, default is to use `webpack.output.path` + `options.filename`. This will overried `filename`. *Warning: Make the service worker available in the same directory it will be needed. This is because the scope of the service worker is defined by the directory the worker exists.*
* `staticFileGlobsIgnorePatterns`: `[RegExp]` - Define an optional array of regex patterns to filter out of staticFileGlobs (see below)
* `forceDelete`: `[boolean]` - Pass force option to del, default is false.

[__`sw-precache` options__][sw-precache-options]:
* `cacheId`: `[String]` - Not required but you should include this, it will give your service worker cache a unique name
Expand Down Expand Up @@ -148,9 +149,8 @@ Run tests:

[npm-url]: https://npmjs.org/package/sw-precache-webpack-plugin
[npm-img]: https://badge.fury.io/js/sw-precache-webpack-plugin.svg
[npm-downloads-img]: https://img.shields.io/npm/dm/sw-precache-webpack-plugin.svg?style=flat-square
[daviddm-img]: https://david-dm.org/goldhand/sw-precache-webpack-plugin.svg
[daviddm-url]: https://david-dm.org/goldhand/sw-precache-webpack-plugin
[daviddmdev-img]: https://david-dm.org/goldhand/sw-precache-webpack-plugin/dev-status.svg
[daviddmdev-url]: https://david-dm.org/goldhand/sw-precache-webpack-plugin#info=devDependencies
[circleci-img]: https://circleci.com/gh/goldhand/sw-precache-webpack-plugin.svg?style=svg
[circleci-url]: https://circleci.com/gh/goldhand/sw-precache-webpack-plugin
15 changes: 11 additions & 4 deletions src/index.js
Expand Up @@ -4,6 +4,8 @@ import del from 'del';
import swPrecache from 'sw-precache';


const FILEPATH_WARNING = 'sw-prechache-webpack-plugin filepath: You are using a custom path for your service worker, this may prevent the service worker from working correctly if it is not available in the same path as your application.';

const
DEFAULT_CACHE_ID = 'sw-precache-webpack-plugin',
DEFAULT_WORKER_FILENAME = 'service-worker.js',
Expand Down Expand Up @@ -42,7 +44,7 @@ const DEFAULT_OPTIONS = {
* @param {string} [options.filename] - Service worker filename, default is 'service-worker.js'
* @param {string} [options.filepath] - Service worker path and name, default is to use webpack.output.path + options.filename
* @param {RegExp} [options.staticFileGlobsIgnorePatterns[]] - Define an optional array of regex patterns to filter out of staticFileGlobs
* @param {boolean} [options.forceDelete=false] - pass force option to del
* @param {boolean} [options.forceDelete=false] - Pass force option to del
*/
class SWPrecacheWebpackPlugin {

Expand All @@ -62,10 +64,15 @@ class SWPrecacheWebpackPlugin {
const outputPath = compiler.options.output.path || DEFAULT_OUTPUT_PATH;

// get the public path specified in webpack config
const publicPath = compiler.options.output.publicPath || DEFAULT_PUBLIC_PATH;
const {publicPath = DEFAULT_PUBLIC_PATH} = compiler.options.output;

// get the importScripts value specified in the sw-precache config
const importScripts = this.options.importScripts || DEFAULT_IMPORT_SCRIPTS;
const {importScripts = DEFAULT_IMPORT_SCRIPTS} = this.options;

if (this.options.filepath) {
// warn about changing filepath
compilation.warnings.push(new Error(FILEPATH_WARNING));
}

// get all assets outputted by webpack
const assetGlobs = Object
Expand Down Expand Up @@ -111,7 +118,7 @@ class SWPrecacheWebpackPlugin {
const
fileDir = compiler.options.output.path || DEFAULT_OUTPUT_PATH,
// default to options.filepath for writing service worker location
filepath = this.options.filepath || path.join(fileDir, this.options.filename),
{filepath = path.join(fileDir, this.options.filename)} = this.options,
workerOptions = {
...config,
...this.options,
Expand Down

0 comments on commit a38ce9a

Please sign in to comment.