broccoli-asset-rev
Advanced tools
Comparing version 2.2.0 to 2.3.0
@@ -20,2 +20,4 @@ var Fingerprint = require('./fingerprint'); | ||
this.generateRailsManifest = options.generateRailsManifest; | ||
this.assetMapPath = options.assetMapPath || ''; | ||
this.railsManifestPath = options.railsManifestPath || ''; | ||
this.prepend = options.prepend || ''; | ||
@@ -22,0 +24,0 @@ this.ignore = options.ignore; |
@@ -30,2 +30,4 @@ var fs = require('fs'); | ||
this.generateRailsManifest = options.generateRailsManifest; | ||
this.assetMapPath = options.assetMapPath; | ||
this.railsManifestPath = options.railsManifestPath; | ||
this.prepend = options.prepend; | ||
@@ -101,17 +103,25 @@ | ||
var contents = new Buffer(stringify(toWrite)); | ||
var fileName = this.fingerprintAssetMap ? 'assetMap-' + this.hashFn(contents) + '.json' : 'assetMap.json'; | ||
var contents = new Buffer(stringify(toWrite, {space: 2})); | ||
var fileName = this.assetMapPath; | ||
var fileNameNoHash = fileName; | ||
if (!fs.existsSync(destDir + '/assets')) { | ||
fs.mkdirSync(destDir + '/assets'); | ||
if (!fileName) { | ||
fileName = 'assets/' + (this.fingerprintAssetMap ? 'assetMap-' + this.hashFn(contents) + '.json' : 'assetMap.json'); | ||
fileNameNoHash = 'assets/assetMap.json'; | ||
} | ||
fs.writeFileSync(destDir + '/assets/' + fileName, contents); | ||
this.assetMap['assets/assetMap.json'] = 'assets/' + fileName; | ||
this.safeWrite(destDir + '/' + fileName, contents); | ||
this.assetMap[fileNameNoHash] = fileName; | ||
}; | ||
Fingerprint.prototype.findExistingManifest = function(destDir) { | ||
var files = fs.readdirSync(destDir + '/assets'); | ||
var files = []; | ||
try{ | ||
files = fs.readdirSync(destDir + '/assets'); | ||
}catch(e){}; | ||
var userPath = this.railsManifestPath; | ||
var manifestFiles = files.filter(function(file) { | ||
return MANIFEST_REGEX.test(file); | ||
return file == userPath || MANIFEST_REGEX.test(file); | ||
}); | ||
@@ -150,3 +160,3 @@ if(manifestFiles.length > 1) { | ||
logical_path: assetlessKey, | ||
digest: fingerprintedPath.match(digestRegex)[1], | ||
digest: (fingerprintedPath.match(digestRegex) || [])[1], | ||
size: stats.size | ||
@@ -158,9 +168,15 @@ } | ||
var fileName = 'manifest'; | ||
var fileName = this.railsManifestPath; | ||
var assets = { assets: assetMap, files: files } | ||
var contents = new Buffer(stringify(assets)); | ||
var contents = new Buffer(stringify(assets, {space: 2})); | ||
if (this.exclude.indexOf('manifest.json') === -1) { | ||
var hash = this.hashFn(contents); | ||
fileName += '-' + hash; | ||
if (!fileName){ | ||
fileName = 'assets/manifest'; | ||
if (this.exclude.indexOf('manifest.json') === -1) { | ||
var hash = this.hashFn(contents); | ||
fileName += '-' + hash; | ||
} | ||
fileName += '.json'; | ||
} | ||
@@ -172,3 +188,3 @@ | ||
fs.writeFileSync(destDir + '/assets/' + fileName + '.json', contents); | ||
this.safeWrite(destDir + '/' + fileName, contents); | ||
}; | ||
@@ -190,2 +206,17 @@ | ||
Fingerprint.prototype.safeWrite = function(file, contents){ | ||
var dir = path.dirname(file); | ||
if (!fs.existsSync(dir)) { | ||
fs.mkdirSync(dir); | ||
} | ||
fs.writeFileSync(file, contents); | ||
}; | ||
/* | ||
* Does not use the second argument (tmpPath). | ||
* That argument is only there for custom hash functions. | ||
*/ | ||
function md5Hash(buf) { | ||
@@ -192,0 +223,0 @@ var md5 = crypto.createHash('md5'); |
{ | ||
"name": "broccoli-asset-rev", | ||
"version": "2.2.0", | ||
"version": "2.3.0", | ||
"description": "broccoli asset revisions (fingerprint)", | ||
@@ -5,0 +5,0 @@ "main": "lib/asset-rev.js", |
@@ -45,5 +45,7 @@ #broccoli-asset-rev | ||
- `generateRailsManifest` - Default: none - If true, will generate a `manifest.json` to be used by Sprockets for the Rails Asset Pipeline. The manifest will be fingerprinted by default but this can be avoided by adding `'manifest.json'` to the `exclude` list. | ||
- `railsManifestPath` - Default: `'assets/manifest-HASH.json'` - The path in the destination folder to store the Rails manifest. Only for the default value, `HASH` will be replace with the fingerprint of the file. | ||
- `customHash` - Default: none - If set, overrides the md5 checksum calculation with the result of calling `customHash(buffer, pathToFile)`. If it is not a `function`, `customHash` is used as the hash value. If it is set to `null`, fingerprinting is skipped and only prepending occurs. | ||
- `generateAssetMap` - Default: false. If true, will generate a `assetMap.json` file in a `assets` directory on the output node. This file contains a mapping of the original asset name to the fingerprinted asset, like the following: | ||
- `assetMapPath` - Default: `'assets/assetMap-HASH.json'` - The path in the destination folder to store the `assetMap.json` in. Only for the default value, `HASH` will be replace with the fingerprint of the file. | ||
```js | ||
@@ -57,2 +59,3 @@ { | ||
``` | ||
- `fingerprintAssetMap` - Default: false. If true, will fingerprint `assetMap.json`. | ||
- `ignore` - Default: `[]` - An array of strings. If a filename contains any item in the ignore array, the contents of the file will not be processed for fingerprinting. | ||
@@ -59,0 +62,0 @@ - `annotation` - Default: null. A human-readable description for this plugin instance. |
@@ -108,2 +108,23 @@ var fs = require('fs'); | ||
it("uses a custom path for the rails-style manifest", function () { | ||
var sourcePath = 'tests/fixtures/basic'; | ||
var node = new AssetRev(sourcePath + '/input', { | ||
extensions: ['js', 'css', 'png', 'jpg', 'gif'], | ||
generateRailsManifest: true, | ||
railsManifestPath: 'otherManifest.json', | ||
replaceExtensions: ['html', 'js', 'css'] | ||
}); | ||
builder = new broccoli.Builder(node); | ||
return builder.build().then(function(graph) { | ||
var actualFiles = walkSync(graph.directory); | ||
var defaultPathNotPresent = !confirmPathPresent(actualFiles, /manifest-[0-9a-f]{32}\.json/); | ||
var pathPresent = confirmPathPresent(actualFiles, /otherManifest\.json/); | ||
assert(pathPresent, "custom manifest file found"); | ||
assert(defaultPathNotPresent, "default fingerprinted manifest file not found"); | ||
}); | ||
}); | ||
it('generates an asset map if requested', function () { | ||
@@ -146,2 +167,24 @@ var sourcePath = 'tests/fixtures/basic'; | ||
it("uses a custom path for the asset map", function () { | ||
var sourcePath = 'tests/fixtures/basic'; | ||
var node = new AssetRev(sourcePath + '/input', { | ||
extensions: ['js', 'css', 'png', 'jpg', 'gif'], | ||
fingerprintAssetMap: true, | ||
generateAssetMap: true, | ||
assetMapPath: 'otherAssetMap.json', | ||
replaceExtensions: ['html', 'js', 'css'] | ||
}); | ||
builder = new broccoli.Builder(node); | ||
return builder.build().then(function(graph) { | ||
var actualFiles = walkSync(graph.directory); | ||
var defaultPathNotPresent = !confirmPathPresent(actualFiles, /assetMap-[0-9a-f]{32}\.json/); | ||
var pathPresent = confirmPathPresent(actualFiles, /otherAssetMap\.json/); | ||
assert(pathPresent, "custom asset map file found"); | ||
assert(defaultPathNotPresent, "default fingerprinted asset map file not found"); | ||
}); | ||
}); | ||
it('will prepend if set', function () { | ||
@@ -148,0 +191,0 @@ var sourcePath = 'tests/fixtures/prepend'; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
68823
733
80