-
Notifications
You must be signed in to change notification settings - Fork 12k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Generate an AppCache/ServiceWorker manifest during the build step. #215
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
node_modules/ | ||
.idea | ||
jsconfig.json | ||
npm-debug.log | ||
typings/ | ||
tmp/ | ||
tmp/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
"use strict"; | ||
|
||
var diffingPlugin = require('./diffing-broccoli-plugin'); | ||
var path = require('path'); | ||
var fs = require('fs'); | ||
var crypto = require('crypto'); | ||
|
||
var FILE_ENCODING = { encoding: 'utf-8' }; | ||
var MANIFEST_FILE = 'manifest.appcache'; | ||
var FILE_HASH_PREFIX = '# sw.file.hash:'; | ||
|
||
class DiffingSWManifest { | ||
constructor(inputPath, cachePath, options) { | ||
this.inputPath = inputPath; | ||
this.cachePath = cachePath; | ||
this.options = options; | ||
this.firstBuild = true; | ||
} | ||
|
||
rebuild(diff) { | ||
var manifest = {}; | ||
if (this.firstBuild) { | ||
this.firstBuild = false; | ||
} else { | ||
// Read manifest from disk. | ||
manifest = this.readManifestFromCache(); | ||
} | ||
|
||
// Remove manifest entries for files that are no longer present. | ||
diff.removedPaths.forEach((file) => delete manifest[file]); | ||
|
||
// Merge the lists of added and changed paths and update their hashes in the manifest. | ||
[] | ||
.concat(diff.addedPaths) | ||
.concat(diff.changedPaths) | ||
.filter((file) => file !== MANIFEST_FILE) | ||
.forEach((file) => manifest[file] = this.computeFileHash(file)); | ||
var manifestPath = path.join(this.cachePath, MANIFEST_FILE); | ||
fs.writeFileSync(manifestPath, this.generateManifest(manifest)); | ||
} | ||
|
||
// Compute the hash of the given relative file. | ||
computeFileHash(file) { | ||
var contents = fs.readFileSync(path.join(this.inputPath, file)); | ||
return crypto | ||
.createHash('sha1') | ||
.update(contents) | ||
.digest('hex'); | ||
} | ||
|
||
// Compute the hash of the bundle from the names and hashes of all included files. | ||
computeBundleHash(files, manifest) { | ||
var hash = crypto.createHash('sha1'); | ||
files.forEach((file) => hash.update(manifest[file] + ':' + file)); | ||
return hash.digest('hex'); | ||
} | ||
|
||
// Generate the string contents of the manifest. | ||
generateManifest(manifest) { | ||
var files = Object.keys(manifest).sort(); | ||
var bundleHash = this.computeBundleHash(files, manifest); | ||
var contents = files | ||
.map((file) => `# sw.file.hash: ${this.computeFileHash(file)}\n/${file}`) | ||
.join('\n'); | ||
return `CACHE MANIFEST | ||
# sw.bundle: ng-cli | ||
# sw.version: ${bundleHash} | ||
${contents} | ||
`; | ||
} | ||
|
||
// Read the manifest from the cache and split it out into a dict of files to hashes. | ||
readManifestFromCache() { | ||
var contents = fs.readFileSync(path.join(this.cachePath, MANIFEST_FILE), FILE_ENCODING); | ||
var manifest = {}; | ||
var hash = null; | ||
contents | ||
.split('\n') | ||
.map((line) => line.trim()) | ||
.filter((line) => line !== 'CACHE MANIFEST') | ||
.filter((line) => line !== '') | ||
.filter((line) => !line.startsWith('#') || line.startsWith('# sw.')) | ||
.forEach((line) => { | ||
if (line.startsWith(FILE_HASH_PREFIX)) { | ||
// This is a hash prefix for the next file in the list. | ||
hash = line.substring(FILE_HASH_PREFIX.length).trim(); | ||
} else if (line.startsWith('/')) { | ||
// This is a file belonging to the application. | ||
manifest[line.substring(1)] = hash; | ||
hash = null; | ||
} | ||
}); | ||
return manifest; | ||
} | ||
} | ||
|
||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.default = diffingPlugin.wrapDiffingPlugin(DiffingSWManifest); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add some more detail about how to install the worker script?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done (in the README).