├── .gitignore ├── package.json ├── LICENSE ├── lib ├── ember-addon.js └── manifest.js └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | 5 | # Runtime data 6 | pids 7 | *.pid 8 | *.seed 9 | 10 | # Directory for instrumented libs generated by jscoverage/JSCover 11 | lib-cov 12 | 13 | # Coverage directory used by tools like istanbul 14 | coverage 15 | 16 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 17 | .grunt 18 | 19 | # Compiled binary addons (http://nodejs.org/api/addons.html) 20 | build/Release 21 | 22 | # Dependency directory 23 | # Commenting this out is preferred by some people, see 24 | # https://npmjs.org/doc/faq.html#Should-I-check-my-node_modules-folder-into-git 25 | node_modules 26 | 27 | # Users Environment Variables 28 | .lock-wscript 29 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "broccoli-manifest", 3 | "version": "0.0.7", 4 | "description": "A broccoli plugin automating appcache manifest file creation", 5 | "main": "lib/manifest.js", 6 | "ember-addon": { 7 | "main": "lib/ember-addon.js", 8 | "after": "broccoli-asset-rev" 9 | }, 10 | "scripts": { 11 | "test": "echo \"Error: no test specified\" && exit 1" 12 | }, 13 | "repository": { 14 | "type": "git", 15 | "url": "https://github.com/racido/broccoli-manifest" 16 | }, 17 | "keywords": [ 18 | "broccoli", 19 | "broccoli-plugin", 20 | "appcache", 21 | "manifest", 22 | "ember-addon" 23 | ], 24 | "author": "Sjoerd de Jong", 25 | "license": "MIT", 26 | "bugs": { 27 | "url": "https://github.com/racido/broccoli-manifest/issues" 28 | }, 29 | "homepage": "https://github.com/racido/broccoli-manifest", 30 | "dependencies": { 31 | "broccoli-writer": "~0.1.1", 32 | "broccoli-kitchen-sink-helpers": "~0.2.2", 33 | "broccoli-merge-trees": "~0.1.4", 34 | "broccoli-funnel": "~0.2.3" 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 racido 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /lib/ember-addon.js: -------------------------------------------------------------------------------- 1 | var path = require('path'); 2 | var fs = require('fs'); 3 | var mergeTrees = require('broccoli-merge-trees'); 4 | var funnel = require('broccoli-funnel'); 5 | 6 | var manifest = require('./manifest'); 7 | 8 | module.exports = { 9 | name: 'broccoli-manifest', 10 | 11 | config: function (env, baseConfig) { 12 | var options = baseConfig.manifest || {}; 13 | 14 | var defaultOptions = { 15 | enabled: env === 'production', 16 | appcacheFile: "/manifest.appcache", 17 | excludePaths: ['index.html', 'tests/'], 18 | includePaths: [], 19 | network: ['*'], 20 | showCreateDate: true 21 | } 22 | 23 | for (var option in defaultOptions) { 24 | if (!options.hasOwnProperty(option)) { 25 | options[option] = defaultOptions[option]; 26 | } 27 | } 28 | 29 | this.manifestOptions = options; 30 | }, 31 | 32 | postprocessTree: function (type, tree) { 33 | var options = this.manifestOptions; 34 | 35 | if (type === 'all' && options.enabled) { 36 | manifestTree = funnel(tree, { 37 | exclude: options.excludePaths 38 | }); 39 | return mergeTrees([tree, manifest(manifestTree, options)]); 40 | } 41 | 42 | return tree; 43 | }, 44 | 45 | treeFor: function() {} 46 | } 47 | -------------------------------------------------------------------------------- /lib/manifest.js: -------------------------------------------------------------------------------- 1 | var fs = require("fs"); 2 | var path = require('path'); 3 | var brocWriter = require("broccoli-writer"); 4 | var helpers = require("broccoli-kitchen-sink-helpers"); 5 | 6 | var BroccoliManifest = function BroccoliManifest(inTree, options) { 7 | if (!(this instanceof BroccoliManifest)) { 8 | return new BroccoliManifest(inTree, options); 9 | } 10 | this.inTree = inTree; 11 | options = options || {}; 12 | this.appcacheFile = options.appcacheFile || "/manifest.appcache"; 13 | this.includePaths = options.includePaths || []; 14 | 15 | this.network = options.network || ['*']; 16 | this.fallback = options.fallback || []; 17 | this.showCreateDate = options.showCreateDate; 18 | }; 19 | 20 | BroccoliManifest.prototype = Object.create(brocWriter.prototype); 21 | BroccoliManifest.prototype.constructor = BroccoliManifest; 22 | 23 | BroccoliManifest.prototype.write = function(readTree, destDir) { 24 | var appcacheFile = this.appcacheFile; 25 | var includePaths = this.includePaths; 26 | var network = this.network; 27 | var fallback = this.fallback; 28 | var showCreateDate = this.showCreateDate; 29 | return readTree(this.inTree).then(function (srcDir) { 30 | var lines = ["CACHE MANIFEST"]; 31 | 32 | if (showCreateDate) { 33 | lines.push("# created " + (new Date()).toISOString()); 34 | } else { 35 | lines.push("# " + Math.random().toString(36).substr(2)); 36 | } 37 | 38 | lines.push("", "CACHE:"); 39 | 40 | getFilesRecursively(srcDir, [ "**/*" ]).forEach(function (file) { 41 | var srcFile = path.join(srcDir, file); 42 | var stat = fs.lstatSync(srcFile); 43 | 44 | if (!stat.isFile() && !stat.isSymbolicLink()) 45 | return; 46 | 47 | lines.push(file); 48 | }); 49 | 50 | includePaths.forEach(function (file) { 51 | lines.push(file); 52 | }); 53 | 54 | lines.push("","NETWORK:"); 55 | 56 | network.forEach(function (line) { 57 | lines.push(line); 58 | }); 59 | 60 | if (fallback.length) { 61 | lines.push("", "FALLBACK:"); 62 | lines.push.apply(lines, fallback); 63 | } 64 | 65 | fs.writeFileSync(path.join(destDir, appcacheFile), lines.join("\n")); 66 | }); 67 | }; 68 | 69 | BroccoliManifest.prototype.addExternalFile = function(file) { 70 | this.externalFiles.push(file); 71 | } 72 | 73 | function getFilesRecursively(dir, globPatterns) { 74 | return helpers.multiGlob(globPatterns, { cwd: dir }); 75 | } 76 | 77 | module.exports = BroccoliManifest; 78 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | broccoli-manifest 2 | ================= 3 | 4 | HTML5 cache-manifest compilation for broccoli 5 | 6 | A nice introduction on the subject: http://www.html5rocks.com/en/tutorials/appcache/beginner/ 7 | 8 | There's also a Wicked-Good-Ember talk on this subject, see http://confreaks.tv/videos/wickedgoodember2015-taking-ember-offline 9 | 10 | Usage for Ember Cli 11 | ------------------- 12 | 13 | `npm install --save-dev broccoli-manifest` 14 | `npm install --save broccoli-merge-trees` 15 | 16 | ```JavaScript 17 | //app/config/environment.js 18 | 19 | ENV.manifest = { 20 | enabled: true, 21 | appcacheFile: "/manifest.appcache", 22 | excludePaths: ['index.html', 'someother.html'], 23 | includePaths: ['/'], 24 | network: ['api/'], 25 | showCreateDate: true 26 | } 27 | ```` 28 | 29 | Upgrade your `index.html` (see below) and you are done. 30 | 31 | Usage for Broccoli.js 32 | --------------------- 33 | 34 | `npm install --save broccoli-manifest` 35 | 36 | Use `broccoli-manifest` as your last filter in the `Brocfile.js` like this 37 | 38 | ```JavaScript 39 | var writeManifest = require('broccoli-manifest'); 40 | 41 | ... 42 | 43 | var completeTree = mergeTrees([appJs, appCss, publicFiles]); 44 | 45 | module.exports = mergeTrees([completeTree, writeManifest(completeTree)]); 46 | ``` 47 | 48 | Options 49 | ------- 50 | 51 | You can pass some options as the second argument to `writeManifest`: 52 | 53 | ```JavaScript 54 | 55 | writeManifest(completeTree, { 56 | appcacheFile: '/manifest.appcache', // Name of the generated appcache file - default value shown 57 | fallback: ['assets/is-online.json assets/offline.json'] // Lines to add to the FALLBACK section of the generated manifest 58 | }); 59 | ``` 60 | 61 | `showCreateDate` toggles the inclusion of a Date object or a random string in your manifest. If you 62 | want to hide the build date from customers, this is your setting. 63 | 64 | Thanks to https://github.com/racido/broccoli-manifest/pull/9 files can be filtered using 65 | regular expressions: 66 | 67 | ```JavaScript 68 | { 69 | excludePaths: ['index.html', new RegExp(/.\.map$/)], 70 | includePaths: [''] 71 | } 72 | ``` 73 | 74 | ### External Files 75 | 76 | 77 | ```JavaScript 78 | var mergeTrees = require('broccoli-merge-trees'); 79 | var manifest = require('broccoli-manifest'); 80 | 81 | ... 82 | all app.import statements go here 83 | ... 84 | 85 | // Write a html5 manifest.appcache file with jquery external 86 | var completeTree = app.toTree(); 87 | var manifestTree = manifest(completeTree) 88 | manifestTree.includePaths(["https://code.jquery.com/jquery-2.1.1.min.js"]) 89 | 90 | module.exports = mergeTrees([completeTree, manifestTree]); 91 | ``` 92 | 93 | 94 | 95 | Upgrade your index.html 96 | ----------------------- 97 | 98 | Add `manifest="manifest.appcache"` to your `` tag. The extra ` 109 | 110 | ... 111 | ``` 112 | --------------------------------------------------------------------------------