Skip to content

Commit 8e615f6

Browse files
committed
Adding optional argument to append fingerprint hash with a new name
1 parent b8c5108 commit 8e615f6

File tree

3 files changed

+79
-5
lines changed

3 files changed

+79
-5
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,5 +108,24 @@ And in your layout:
108108

109109
That's it! Now if you use a cleaner as suggested, along with file watching, you'll be able to run this in `development` and have `bundle.js` rebuild without fingerprinting, and your Rails app with use that directly. Once you build for production, your Rails app and your `bundle.js` will use a fingerprinted version of the file.
110110

111+
### Managing multiple webpack builds
112+
113+
If your build setup launches webpack multiple times a new hash will be generated with each run. This can cause problems when using the default
114+
arguments. The last run will overwrite any previously written hash value. An additional parameter can be passed to tell the plugin to append a new named value that has the key from the subsequent run.
115+
116+
Passing a name in the third paramenter will generate a new line in `asset_fingerprint.rb` having the name assigned to the new hash. For example:
117+
118+
```
119+
AssetFingerprintPlugin(rubyConfigInitPath, true, 'CUSTOM_ASSET_FINGERPRINT')
120+
```
121+
122+
will generate a new line in the file like this:
123+
124+
```
125+
ASSET_FINGERPRINT = '0556f9ab7cbd607d10ed'
126+
CUSTOM_ASSET_FINGERPRINT = '921dcffe35e5f2740ef5'
127+
```
128+
129+
111130
### Credit
112131
Concept adapted from @samullen - http://samuelmullen.com/articles/replacing-the-rails-asset-pipeline-with-webpack-and-yarn/

index.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
const fs = require('fs');
22

3-
function AssetFingerprint(initializerDirectory, needsFingerprint = true) {
3+
function AssetFingerprint(initializerDirectory, needsFingerprint = true, fingerprintName) {
44
_validateInitializerDirectory();
55

66
this.initializerDirectory = initializerDirectory;
77
this.needsFingerprint = needsFingerprint;
8+
this.fingerprintName = fingerprintName || 'ASSET_FINGERPRINT';
89

910
function _validateInitializerDirectory() {
1011
if (initializerDirectory === undefined) {
@@ -16,10 +17,13 @@ function AssetFingerprint(initializerDirectory, needsFingerprint = true) {
1617
AssetFingerprint.prototype.apply = function(compiler) {
1718
compiler.plugin('done', function(stats) {
1819
if (this.needsFingerprint) {
19-
let output = `ASSET_FINGERPRINT = '${stats.hash}'`;
20-
let initializerPath = `${this.initializerDirectory}/asset_fingerprint.rb`;
21-
22-
fs.writeFileSync(initializerPath, output);
20+
let defaultRun = this.fingerprintName === 'ASSET_FINGERPRINT';
21+
let initializerPath = `${this.initializerDirectory}/asset_fingerprint.rb`;
22+
const newline = defaultRun ? '' : '\r\n';
23+
const fsMethod = defaultRun ? 'writeFileSync' : 'appendFileSync';
24+
let output = `${newline}${this.fingerprintName} = '${stats.hash}'`;
25+
fs[fsMethod](initializerPath, output);
26+
console.log(`asset-fingerprint-webpack-rails: updated file ${initializerPath} with ${this.fingerprintName} = ${stats.hash}`)
2327
}
2428
}.bind(this));
2529
}

spec/indexSpec.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
describe("AssetFingerprint", function() {
22
var AssetFingerprint = require('../index');
3+
var fs = require('fs');
34
var fingerprint;
45

56
describe("initialization", function() {
@@ -37,5 +38,55 @@ describe("AssetFingerprint", function() {
3738
expect(fingerprint).toThrowError('Please supply a directory path for your initializer, such as `config/initializers`.');
3839
});
3940
});
41+
42+
describe("fingerprint name", function() {
43+
it("should use ASSET_FINGERPRINT when no args are passed", function () {
44+
var dir = 'config/initializers';
45+
fingerprint = new AssetFingerprint(dir);
46+
expect(fingerprint.fingerprintName).toEqual("ASSET_FINGERPRINT");
47+
});
48+
49+
it("should use value when provided", function () {
50+
var dir = 'config/initializers';
51+
fingerprint = new AssetFingerprint(dir, true, "FINGERPRINT_VALUE");
52+
expect(fingerprint.fingerprintName).toEqual("FINGERPRINT_VALUE");
53+
});
54+
});
55+
56+
describe("apply", function() {
57+
beforeEach(function() {
58+
spyOn(fs, "writeFileSync");
59+
spyOn(fs, "appendFileSync");
60+
spyOn(console, "log");
61+
});
62+
63+
it("should call writeFileSync on default arguments", function() {
64+
var dir = 'config/initializers';
65+
fingerprint = new AssetFingerprint(dir);
66+
fingerprint.apply({
67+
plugin: function(status, callback) { callback({hash: "A1B2C3D4"}) }
68+
});
69+
70+
expect(fs.writeFileSync).toHaveBeenCalledWith(
71+
'config/initializers/asset_fingerprint.rb', "ASSET_FINGERPRINT = 'A1B2C3D4'");
72+
73+
expect(console.log).toHaveBeenCalledWith(
74+
"asset-fingerprint-webpack-rails: updated file config/initializers/asset_fingerprint.rb with ASSET_FINGERPRINT = A1B2C3D4");
75+
});
76+
77+
it("should call appendFileSync on provided fingerprint name argument", function() {
78+
var dir = 'config/initializers';
79+
fingerprint = new AssetFingerprint(dir, true, "FINGERPRINT_VALUE");
80+
fingerprint.apply({
81+
plugin: function(status, callback) { callback({hash: "Z1Y2X3W4"}) }
82+
});
83+
84+
expect(fs.appendFileSync).toHaveBeenCalledWith(
85+
'config/initializers/asset_fingerprint.rb', "\r\nFINGERPRINT_VALUE = 'Z1Y2X3W4'");
86+
87+
expect(console.log).toHaveBeenCalledWith(
88+
"asset-fingerprint-webpack-rails: updated file config/initializers/asset_fingerprint.rb with FINGERPRINT_VALUE = Z1Y2X3W4");
89+
});
90+
});
4091
});
4192
});

0 commit comments

Comments
 (0)