Skip to content

Commit

Permalink
Refactor to use broccoli-persistent-filter public APIs
Browse files Browse the repository at this point in the history
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
  • Loading branch information
elucid committed Apr 2, 2016
1 parent 9bae601 commit 3f51d43
Show file tree
Hide file tree
Showing 30 changed files with 56 additions and 48 deletions.
45 changes: 19 additions & 26 deletions lib/fingerprint.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,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 @@ -72,51 +76,40 @@ Fingerprint.prototype.canProcessFile = function (relativePath) {
return false;
}

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

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

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.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
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');
}

0 comments on commit 3f51d43

Please sign in to comment.