Skip to content
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

Switch to using broccoli-persistent-filter #93

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/asset-rev.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ function AssetRev(inputTree, options) {
this.prepend = options.prepend || defaults.prepend;
this.ignore = options.ignore;
this.description = options.description;
this.persist = options.persist;

var fingerprintTree = Fingerprint(inputTree, this);

Expand Down
54 changes: 28 additions & 26 deletions lib/fingerprint.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var fs = require('fs');
var path = require('path');
var crypto = require('crypto');
var Filter = require('broccoli-filter');
var Filter = require('broccoli-persistent-filter');
var Promise = require('rsvp').Promise;
// JSON.stringify is not stable
var stringify = require('json-stable-stringify');
Expand All @@ -24,10 +24,15 @@ function Fingerprint(inputNode, options) {

options = options || {};

if (options.persist === undefined) {
options.persist = true;
}

Filter.call(this, inputNode, {
extensions: options.extensions || [],
// We should drop support for `description` in the next major release
annotation: options.description || options.annotation
annotation: options.description || options.annotation,
persist: options.persist
})

this.assetMap = options.assetMap || {};
Expand Down Expand Up @@ -56,7 +61,11 @@ function Fingerprint(inputNode, options) {
Fingerprint.prototype = Object.create(Filter.prototype);
Fingerprint.prototype.constructor = Fingerprint;

Fingerprint.prototype.canProcessFile = function (relativePath) {
Fingerprint.prototype.canFingerprintFile = function (relativePath) {
if (this.customHash === null) {
return false;
}

for (var i = 0; i < this.exclude.matchers.length; i++) {
if (relativePath.indexOf(this.exclude.matchers[i].pattern) !== -1) {
return false;
Expand All @@ -67,47 +76,40 @@ Fingerprint.prototype.canProcessFile = function (relativePath) {
return false;
}

return Filter.prototype.getDestFilePath.apply(this, arguments) != null;
return true;
};

Fingerprint.prototype.processFile = function (srcDir, destDir, relativePath) {
var file = fs.readFileSync(srcDir + '/' + relativePath);
var self = this;

return Promise.resolve().then(function () {
var outputPath = self.getDestFilePath(relativePath);
fs.writeFileSync(destDir + '/' + outputPath, file);
});
Fingerprint.prototype.baseDir = function () {
return path.resolve(__dirname, '..');
};

Fingerprint.prototype.getDestFilePath = function (relativePath) {
var destFilePath = Filter.prototype.getDestFilePath.apply(this, arguments);
if (destFilePath) {
if (this.assetMap[relativePath]) {
return this.assetMap[relativePath];
}
if (this.customHash === null) {
return this.assetMap[relativePath] = destFilePath;
}
Fingerprint.prototype.processString = function (contents, relativePath) {
if (this.assetMap[relativePath]) {
return contents;
}

if (this.canFingerprintFile(relativePath)) {
var tmpPath = path.join(this.inputPaths[0], relativePath);
var file = fs.readFileSync(tmpPath, { encoding: null });
var hash;

if (this.customHash) {
hash = this.customHash;
} else {
hash = this.hashFn(file, tmpPath);
hash = this.hashFn(contents, tmpPath);
}

var ext = path.extname(relativePath);
var newPath = relativePath.replace(new RegExp(ext+'$'), '-' + hash + ext);
this.assetMap[relativePath] = newPath;

return newPath;
} else {
this.assetMap[relativePath] = relativePath;
}

return null;
return contents;
};

Fingerprint.prototype.getDestFilePath = function (relativePath) {
return this.assetMap[relativePath] || Filter.prototype.getDestFilePath.apply(this, arguments);
};

Fingerprint.prototype.writeAssetMap = function (destDir) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"homepage": "https://github.com/rickharrison/broccoli-asset-rev",
"dependencies": {
"broccoli-asset-rewrite": "^1.0.9",
"broccoli-filter": "^1.2.2",
"broccoli-persistent-filter": "^1.2.0",
"json-stable-stringify": "^1.0.0",
"matcher-collection": "^1.0.1",
"rsvp": "~3.0.6"
Expand Down
17 changes: 16 additions & 1 deletion tests/filter-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,25 @@ var assert = require('assert');
var walkSync = require('walk-sync');
var broccoli = require('broccoli');
var MergeTrees = require('broccoli-merge-trees');
var AssetRev = require('../lib/asset-rev');
var _AssetRev = require('../lib/asset-rev');
var sinon = require('sinon');
var builder;

// NOTE: don't persist broccoli-persistent-filter's cache during tests.
// Because many fixture dirs share the same file structure, and tests are setup
// in such a way that the parent of the fixture dir isn't passed to the filter,
// having one test populate the cache sometimes breaks it for subsequent tests
// that need to process a file with the same name and contents in a sibilng
// fixture directory. Besides, that the persistent cache works correctly,
// should be tested in broccoli-persistent-filter, not here.
function AssetRev(inputTree, options) {
if (options.persist === undefined) {
options.persist = false;
}

return new _AssetRev(inputTree, options);
}

function confirmOutput(actualPath, expectedPath) {
var actualFiles = walkSync(actualPath);
var expectedFiles = walkSync(expectedPath);
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/basic/output/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<title>Application</title>

<link rel="stylesheet" href="/styles-daff78e51cc77ea97e75a7fd9c5f6fae.css">
<link rel="icon" href="/images/icons/favicon-9243e67fccd0c5e08a3d2c158af6500d.png">
<link rel="icon" href="/images/icons/favicon-63c59ffade880971af3567086267e69a.png">
</head>
<body>
<script type="text/javascript" src="/assets/application-058eb02dfd08e347c40ae14e9f2e4600.js"></script>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
.sample-img {
width: 50px;
height: 50px;
background-image: url(images/sample-1f6b78f1b4667adc7e397f7bf94041ab.png);
background-image: url(images/sample-b7caf246e908cd00d8011f370e3dd992.png);
}

.sample-img2 {
width: 50px;
height: 50px;
background-image: url('images/sample-1f6b78f1b4667adc7e397f7bf94041ab.png');
background-image: url('images/sample-b7caf246e908cd00d8011f370e3dd992.png');
}
2 changes: 1 addition & 1 deletion tests/fixtures/customHash-function/output/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<title>Application</title>

<link rel="stylesheet" href="/styles-6c275657b4c8042d38718e75d8356f627bd58fe8.css">
<link rel="icon" href="/images/icons/favicon-aa86317c1876afeaced38d576edc58ab20d31a60.png">
<link rel="icon" href="/images/icons/favicon-5f44ed4e0d18a4212b41dafb9f1bf22b558d0a78.png">
</head>
<body>
<script type="text/javascript" src="/assets/application-a17063909b34051b8396d1034de6c246514f06eb.js"></script>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
.sample-img {
width: 50px;
height: 50px;
background-image: url(images/sample-2c57d0d404fa94fd22023242d1f36faaaa7ad792.png);
background-image: url(images/sample-a3eba6f18695684fa6df5d80f157d82277e93172.png);
}

.sample-img2 {
width: 50px;
height: 50px;
background-image: url('images/sample-2c57d0d404fa94fd22023242d1f36faaaa7ad792.png');
background-image: url('images/sample-a3eba6f18695684fa6df5d80f157d82277e93172.png');
}
2 changes: 1 addition & 1 deletion tests/fixtures/exclude/output/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<title>Application</title>

<link rel="stylesheet" href="/styles-daff78e51cc77ea97e75a7fd9c5f6fae.css">
<link rel="icon" href="/images/icons/favicon-9243e67fccd0c5e08a3d2c158af6500d.png">
<link rel="icon" href="/images/icons/favicon-63c59ffade880971af3567086267e69a.png">
</head>
<body>
<script type="text/javascript" src="/assets/application-058eb02dfd08e347c40ae14e9f2e4600.js"></script>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
.sample-img {
width: 50px;
height: 50px;
background-image: url(images/sample-1f6b78f1b4667adc7e397f7bf94041ab.png);
background-image: url(images/sample-b7caf246e908cd00d8011f370e3dd992.png);
}

.sample-img2 {
width: 50px;
height: 50px;
background-image: url('images/sample-1f6b78f1b4667adc7e397f7bf94041ab.png');
background-image: url('images/sample-b7caf246e908cd00d8011f370e3dd992.png');
}
2 changes: 1 addition & 1 deletion tests/fixtures/existing-manifest/output/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<title>Application</title>

<link rel="stylesheet" href="/styles-daff78e51cc77ea97e75a7fd9c5f6fae.css">
<link rel="icon" href="/images/icons/favicon-9243e67fccd0c5e08a3d2c158af6500d.png">
<link rel="icon" href="/images/icons/favicon-63c59ffade880971af3567086267e69a.png">
</head>
<body>
<script type="text/javascript" src="/assets/application-058eb02dfd08e347c40ae14e9f2e4600.js"></script>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
.sample-img {
width: 50px;
height: 50px;
background-image: url(images/sample-1f6b78f1b4667adc7e397f7bf94041ab.png);
background-image: url(images/sample-b7caf246e908cd00d8011f370e3dd992.png);
}

.sample-img2 {
width: 50px;
height: 50px;
background-image: url('images/sample-1f6b78f1b4667adc7e397f7bf94041ab.png');
background-image: url('images/sample-b7caf246e908cd00d8011f370e3dd992.png');
}
2 changes: 1 addition & 1 deletion tests/fixtures/extensions/output/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<title>Application</title>

<link rel="stylesheet" href="/styles-107bca768dc01cba6b2c0d102c977586.css">
<link rel="icon" href="/images/icons/favicon-9243e67fccd0c5e08a3d2c158af6500d.png">
<link rel="icon" href="/images/icons/favicon-63c59ffade880971af3567086267e69a.png">
</head>
<body>
<script type="text/javascript" src="/assets/application-058eb02dfd08e347c40ae14e9f2e4600.js"></script>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
.sample-img {
width: 50px;
height: 50px;
background-image: url(images/sample-1f6b78f1b4667adc7e397f7bf94041ab.png);
background-image: url(images/sample-b7caf246e908cd00d8011f370e3dd992.png);
}

.sample-img2 {
width: 50px;
height: 50px;
background-image: url('images/sample-1f6b78f1b4667adc7e397f7bf94041ab.png');
background-image: url('images/sample-b7caf246e908cd00d8011f370e3dd992.png');
}

@font-face {
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/manifest/output/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<title>Application</title>

<link rel="stylesheet" href="/styles-daff78e51cc77ea97e75a7fd9c5f6fae.css">
<link rel="icon" href="/images/icons/favicon-9243e67fccd0c5e08a3d2c158af6500d.png">
<link rel="icon" href="/images/icons/favicon-63c59ffade880971af3567086267e69a.png">
</head>
<body>
<script type="text/javascript" src="/assets/application-058eb02dfd08e347c40ae14e9f2e4600.js"></script>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
.sample-img {
width: 50px;
height: 50px;
background-image: url(images/sample-1f6b78f1b4667adc7e397f7bf94041ab.png);
background-image: url(images/sample-b7caf246e908cd00d8011f370e3dd992.png);
}

.sample-img2 {
width: 50px;
height: 50px;
background-image: url('images/sample-1f6b78f1b4667adc7e397f7bf94041ab.png');
background-image: url('images/sample-b7caf246e908cd00d8011f370e3dd992.png');
}
2 changes: 1 addition & 1 deletion tests/fixtures/prepend/output/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<title>Application</title>

<link rel="stylesheet" href="https://foobar.cloudfront.net/styles-daff78e51cc77ea97e75a7fd9c5f6fae.css">
<link rel="icon" href="https://foobar.cloudfront.net/images/icons/favicon-9243e67fccd0c5e08a3d2c158af6500d.png">
<link rel="icon" href="https://foobar.cloudfront.net/images/icons/favicon-63c59ffade880971af3567086267e69a.png">
</head>
<body>
<script type="text/javascript" src="https://foobar.cloudfront.net/assets/application-058eb02dfd08e347c40ae14e9f2e4600.js"></script>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
.sample-img {
width: 50px;
height: 50px;
background-image: url(https://foobar.cloudfront.net/images/sample-1f6b78f1b4667adc7e397f7bf94041ab.png);
background-image: url(https://foobar.cloudfront.net/images/sample-b7caf246e908cd00d8011f370e3dd992.png);
}

.sample-img2 {
width: 50px;
height: 50px;
background-image: url('https://foobar.cloudfront.net/images/sample-1f6b78f1b4667adc7e397f7bf94041ab.png');
background-image: url('https://foobar.cloudfront.net/images/sample-b7caf246e908cd00d8011f370e3dd992.png');
}