Skip to content

Commit

Permalink
version 0.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
jkleinsc committed Jun 13, 2015
1 parent b3793ed commit 9343c02
Show file tree
Hide file tree
Showing 6 changed files with 441 additions and 27 deletions.
28 changes: 1 addition & 27 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,27 +1 @@
# Logs
logs
*.log

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directory
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
node_modules
node_modules
3 changes: 3 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"node": true
}
101 changes: 101 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
broccoli-serviceworker
=================

ServiceWorker generator for Broccoli and Ember.js. Derived from [broccoli-manifest](https://github.com/racido/broccoli-manifest).

For more details on ServiceWorker check out the following:
* [ServiceWorker spec](https://slightlyoff.github.io/ServiceWorker/spec/service_worker/)
* https://github.com/slightlyoff/ServiceWorker
* [Is ServiceWorker ready?](https://jakearchibald.github.io/isserviceworkerready/)

Usage for Ember Cli
-------------------

`npm install --save-dev broccoli-serviceworker`

```JavaScript
//app/config/environment.js

ENV.serviceWorker = {
enabled: true,
serviceWorkerFile: "service-worker.js",
excludePaths: ['tests/', 'online.html',],
includePaths: ['/'],
fallback: [
'/online.html offline.html'
],
dynamicCache: [
'/api/todos'
]
};
```

Upgrade your `index.html` (see below) and you are done.

Usage for Broccoli.js
---------------------

`npm install --save broccoli-serviceworker`

Use `broccoli-serviceworker` as your last filter in the `Brocfile.js` like this

```JavaScript
var writeServiceWorker = require('broccoli-serviceworker');

...

var completeTree = mergeTrees([appJs, appCss, publicFiles]);

module.exports = mergeTrees([completeTree, writeServiceWorker((completeTree)]);
```
Options
-------
You can pass some options as the second argument to `writeServiceWorker`:
```JavaScript

writeServiceWorker(completeTree, {
serviceWorkerFile: "service-worker.js",
excludePaths: ['tests/', 'online.html',],
includePaths: ['/'],
fallback: [
'/online.html offline.html'
],
dynamicCache: [
'/api/todos'
]
});
```
Files can be filtered using regular expressions.
```JavaScript
{
excludePaths: ['index.html', new RegExp(/.\.map$/)],
includePaths: ['']
}
```
Upgrade your index.html
-----------------------
In order to use the generated serviceworker, you will need to register the serviceworker. You can do so with the following code:
```HTML
<!DOCTYPE html>
<html>
...
<script>
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('./service-worker.js', {scope: './'})
.catch(function(error) {
alert('Error registering service worker:'+error);
});
} else {
alert('service worker not supported');
}
</script>
</html>
```
48 changes: 48 additions & 0 deletions lib/ember-addon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
var path = require('path');
var fs = require('fs');
var mergeTrees = require('broccoli-merge-trees');
var funnel = require('broccoli-funnel');

var serviceWorker = require('./x');

module.exports = {
name: 'broccoli-serviceworker',

included: function (app) {
this.app = app;
this.initializeOptions();
},

initializeOptions: function () {
var appOptions = this.app.project.config(this.app.env);
var options = appOptions.serviceWorker || {};

var defaultOptions = {
enabled: this.app.env === 'production',
excludePaths: ['test.*'],
includePaths: ['/'],
};

for (var option in defaultOptions) {
if (!options.hasOwnProperty(option)) {
options[option] = defaultOptions[option];
}
}
this.serviceWorkerOptions = options;
},

postprocessTree: function (type, tree) {
var options = this.serviceWorkerOptions;

if (type === 'all' && options.enabled) {
var serviceWorkerTree = funnel(tree, {
exclude: options.excludePaths
});
return mergeTrees([tree, serviceWorker(serviceWorkerTree, options)]);
}

return tree;
},

treeFor: function() {}
};
Loading

0 comments on commit 9343c02

Please sign in to comment.