Skip to content

Commit e2e0700

Browse files
committed
Refactor to use broccoli-persistent-filter public APIs
NOTE: * overrides fewer `Filter` functions * simplifies implementation by making use of public APIs * avoids an extra readFileSync for every resource because hashing is done within `processString` (which has contents passed in as a param) rather than in `getDestFilePath` * unfortunately all image files (and files which reference them) in text fixtures had to be renamed because now the file contents as string rather than a Buffer are passed to the hash function and consequently produce different digests
1 parent 64ff29b commit e2e0700

30 files changed

+56
-48
lines changed

lib/fingerprint.js

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,11 @@ function Fingerprint(inputNode, options) {
6161
Fingerprint.prototype = Object.create(Filter.prototype);
6262
Fingerprint.prototype.constructor = Fingerprint;
6363

64-
Fingerprint.prototype.canProcessFile = function (relativePath) {
64+
Fingerprint.prototype.canFingerprintFile = function (relativePath) {
65+
if (this.customHash === null) {
66+
return false;
67+
}
68+
6569
for (var i = 0; i < this.exclude.matchers.length; i++) {
6670
if (relativePath.indexOf(this.exclude.matchers[i].pattern) !== -1) {
6771
return false;
@@ -72,51 +76,40 @@ Fingerprint.prototype.canProcessFile = function (relativePath) {
7276
return false;
7377
}
7478

75-
return Filter.prototype.getDestFilePath.apply(this, arguments) != null;
79+
return true;
7680
};
7781

7882
Fingerprint.prototype.baseDir = function () {
7983
return path.resolve(__dirname, '..');
8084
};
8185

82-
Fingerprint.prototype.processFile = function (srcDir, destDir, relativePath) {
83-
var file = fs.readFileSync(srcDir + '/' + relativePath);
84-
var self = this;
85-
86-
return Promise.resolve().then(function () {
87-
var outputPath = self.getDestFilePath(relativePath);
88-
fs.writeFileSync(destDir + '/' + outputPath, file);
89-
});
90-
};
91-
92-
Fingerprint.prototype.getDestFilePath = function (relativePath) {
93-
var destFilePath = Filter.prototype.getDestFilePath.apply(this, arguments);
94-
if (destFilePath) {
95-
if (this.assetMap[relativePath]) {
96-
return this.assetMap[relativePath];
97-
}
98-
if (this.customHash === null) {
99-
return this.assetMap[relativePath] = destFilePath;
100-
}
86+
Fingerprint.prototype.processString = function (contents, relativePath) {
87+
if (this.assetMap[relativePath]) {
88+
return contents;
89+
}
10190

91+
if (this.canFingerprintFile(relativePath)) {
10292
var tmpPath = path.join(this.inputPaths[0], relativePath);
103-
var file = fs.readFileSync(tmpPath, { encoding: null });
10493
var hash;
10594

10695
if (this.customHash) {
10796
hash = this.customHash;
10897
} else {
109-
hash = this.hashFn(file, tmpPath);
98+
hash = this.hashFn(contents, tmpPath);
11099
}
111100

112101
var ext = path.extname(relativePath);
113102
var newPath = relativePath.replace(new RegExp(ext+'$'), '-' + hash + ext);
114103
this.assetMap[relativePath] = newPath;
115-
116-
return newPath;
104+
} else {
105+
this.assetMap[relativePath] = relativePath;
117106
}
118107

119-
return null;
108+
return contents;
109+
};
110+
111+
Fingerprint.prototype.getDestFilePath = function (relativePath) {
112+
return this.assetMap[relativePath] || Filter.prototype.getDestFilePath.apply(this, arguments);
120113
};
121114

122115
Fingerprint.prototype.writeAssetMap = function (destDir) {

tests/filter-tests.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,25 @@ var assert = require('assert');
55
var walkSync = require('walk-sync');
66
var broccoli = require('broccoli');
77
var MergeTrees = require('broccoli-merge-trees');
8-
var AssetRev = require('../lib/asset-rev');
8+
var _AssetRev = require('../lib/asset-rev');
99
var sinon = require('sinon');
1010
var builder;
1111

12+
// NOTE: don't persist broccoli-persistent-filter's cache during tests.
13+
// Because many fixture dirs share the same file structure, and tests are setup
14+
// in such a way that the parent of the fixture dir isn't passed to the filter,
15+
// having one test populate the cache sometimes breaks it for subsequent tests
16+
// that need to process a file with the same name and contents in a sibilng
17+
// fixture directory. Besides, that the persistent cache works correctly,
18+
// should be tested in broccoli-persistent-filter, not here.
19+
function AssetRev(inputTree, options) {
20+
if (options.persist === undefined) {
21+
options.persist = false;
22+
}
23+
24+
return new _AssetRev(inputTree, options);
25+
}
26+
1227
function confirmOutput(actualPath, expectedPath) {
1328
var actualFiles = walkSync(actualPath);
1429
var expectedFiles = walkSync(expectedPath);

tests/fixtures/basic/output/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<title>Application</title>
77

88
<link rel="stylesheet" href="/styles-daff78e51cc77ea97e75a7fd9c5f6fae.css">
9-
<link rel="icon" href="/images/icons/favicon-9243e67fccd0c5e08a3d2c158af6500d.png">
9+
<link rel="icon" href="/images/icons/favicon-63c59ffade880971af3567086267e69a.png">
1010
</head>
1111
<body>
1212
<script type="text/javascript" src="/assets/application-058eb02dfd08e347c40ae14e9f2e4600.js"></script>
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
.sample-img {
22
width: 50px;
33
height: 50px;
4-
background-image: url(images/sample-1f6b78f1b4667adc7e397f7bf94041ab.png);
4+
background-image: url(images/sample-b7caf246e908cd00d8011f370e3dd992.png);
55
}
66

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

tests/fixtures/customHash-function/output/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<title>Application</title>
77

88
<link rel="stylesheet" href="/styles-6c275657b4c8042d38718e75d8356f627bd58fe8.css">
9-
<link rel="icon" href="/images/icons/favicon-aa86317c1876afeaced38d576edc58ab20d31a60.png">
9+
<link rel="icon" href="/images/icons/favicon-5f44ed4e0d18a4212b41dafb9f1bf22b558d0a78.png">
1010
</head>
1111
<body>
1212
<script type="text/javascript" src="/assets/application-a17063909b34051b8396d1034de6c246514f06eb.js"></script>
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
.sample-img {
22
width: 50px;
33
height: 50px;
4-
background-image: url(images/sample-2c57d0d404fa94fd22023242d1f36faaaa7ad792.png);
4+
background-image: url(images/sample-a3eba6f18695684fa6df5d80f157d82277e93172.png);
55
}
66

77
.sample-img2 {
88
width: 50px;
99
height: 50px;
10-
background-image: url('images/sample-2c57d0d404fa94fd22023242d1f36faaaa7ad792.png');
10+
background-image: url('images/sample-a3eba6f18695684fa6df5d80f157d82277e93172.png');
1111
}

0 commit comments

Comments
 (0)