diff --git a/CHANGELOG.md b/CHANGELOG.md index 24db6c7..48ad036 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # Change Log All notable changes to this project will be documented in this file. +## [1.2.0] - 2016-08-05 +### Added +- added autorequire option ## [1.1.0] - 2016-07-29 ### Changed @@ -9,4 +12,5 @@ All notable changes to this project will be documented in this file. ## 1.0.0 - 2016-07-29 * Initial release -[1.1.0]: https://github.com/denar90/sw-precache-brunch/compare/v1.0.0...v1.1.0 \ No newline at end of file +[1.1.0]: https://github.com/denar90/sw-precache-brunch/compare/v1.0.0...v1.1.0 +[1.2.0]: https://github.com/denar90/sw-precache-brunch/compare/v1.1.0...v1.2.0 \ No newline at end of file diff --git a/README.md b/README.md index 496e709..baa8a7d 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,31 @@ Filename for service worker. *Default:* `'sw.js'` + +##### autorequire [Boolean, Array] + +This option gives possibility to add and register +service worker into `html` asset files. +> If value set as `true` service worker will be added and registered to ALL `html` assets automatically. + +Config example: + +```js +swPrecache: { + 'swFileName': 'service-worker.js', + 'autorequire': true +} +``` + +```js +swPrecache: { + 'autorequire': ['index.html'] +} +``` + +*Default:* `false` + + ##### options [Object] Options for `sw-precache`. @@ -28,11 +53,13 @@ Look at all `sw-precache` available [options](https://github.com/GoogleChrome/sw *Default:* `{staticFileGlobs: ['public/**/*.*']}` + > If you don't pass any configuration properties it will cache all files in your `public` folder and create `sw.js`. All what you need is [register service worker](https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerRegistration) for your app. + ###Config example -```javascript +```js module.exports = { files: { diff --git a/index.js b/index.js index 1baa973..1835c09 100644 --- a/index.js +++ b/index.js @@ -3,6 +3,9 @@ const swPrecache = require('sw-precache'); const sysPath = require('path'); const defaultSWOptions = {staticFileGlobs: []}; +const fs = require('fs'); +const async = require('async'); +const cheerio = require('cheerio'); let swFileName = 'sw.js'; class SWCompiler { @@ -16,14 +19,61 @@ class SWCompiler { const publicPath = cfg.paths.public; const config = cfg.plugins && cfg.plugins.swPrecache; - swFileName = config.swFileName || swFileName; - - this.swFilePath = sysPath.join(publicPath, swFileName); + this.swFileName = config.swFileName || swFileName; + this.swFilePath = sysPath.join(publicPath, this.swFileName); + this.autorequire = (Array.isArray(config.autorequire) || config.autorequire === true) ? config.autorequire : false; this.options = config.options || defaultSWOptions; + if (!this.options.staticFileGlobs.length) this.options.staticFileGlobs.push(`${publicPath}/**/*.*`); } - onCompile() { + _getAssetsList(originalAssets) { + originalAssets = originalAssets.map((data) => { + return data.destinationPath; + }); + + return Array.isArray(this.autorequire) ? this.autorequire : originalAssets; + } + + _includeSWIntoAsset(assetsList) { + async.each(assetsList, this._openFile.bind(this)); + } + + _openFile(file, cb) { + fs.readFile(file, 'utf8', (err, fileContent) => { + if (err) cb(err); + + try { + this._writeFile({fileContent: this._changeFileContent(fileContent), filePath: file}, cb); + } catch (e) { + cb(e); + } + }); + } + + _changeFileContent(fileContent) { + const $ = cheerio.load(fileContent); + const swSource = `\