Skip to content

Commit b8c5108

Browse files
Merge pull request #1 from coderbydesign/feature/require-initializer-path-as-arg
Require initializer path
2 parents 1e270c3 + ce2ee97 commit b8c5108

File tree

4 files changed

+55
-22
lines changed

4 files changed

+55
-22
lines changed

README.md

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,14 @@ npm install asset-fingerprint-webpack-rails --save-dev
1111
## Usage
1212
**Note:** _You will likely want to avoid running the fingerprinting in anything but a production build. Otherwise, if you have file watching setup for instance, it will rebuild the fingerprinting each time a file is updated, requiring you to bounce the Rails server to pickup the new fingerprint. One way to avoid this is by passing an option to your command for dev vs prod: (`webpack` for dev without fingerprinting, and `webpack --env.fingerprint` for prod build)._
1313

14-
This plugin assumes you have a `config/initializers` directory in your project. It uses the `hash` values from the webpack `stats` object. This is the value that will be output into a new initializer called `asset_fingerprint.rb`, as: `ASSET_FINGERPRINT=XXXXXXXX`. You should setup your `webpack.config.js` to use this hash when building your output file (see sample config below).
14+
This plugin requires you to set your initializer directory path relative to your webpack config, such as: `config/initializers` as the first argument. The second optional argument determines whether or not the fingerprinting should take place. By default, this is set to `true`.
15+
16+
```javascript
17+
const AssetFingerprintPlugin = require('asset-fingerprint-webpack-rails');
18+
new AssetFingerprintPlugin('config/initializers', true);
19+
```
20+
21+
It uses the `hash` values from the webpack `stats` object. This is the value that will be output into a new initializer called `asset_fingerprint.rb`, as: `ASSET_FINGERPRINT=XXXXXXXX`. You should setup your `webpack.config.js` to use this hash when building your output file (see sample config below).
1522

1623
It is also recommended that you use a plugin such as [clean-webpack-plugin](https://github.com/johnagan/clean-webpack-plugin) in conjunction with this plugin to clean out your output directory on each build, if you wish to avoid a collection of old files.
1724

@@ -26,7 +33,7 @@ Use a local variable in `webpack.config.js` to pass into the plugin to inform it
2633
*/
2734
const AssetFingerprintPlugin = require('asset-fingerprint-webpack-rails');
2835
const needsFingerprint = someLocalBoolean;
29-
36+
const initializerDirectory = 'config/initializers';
3037
/**
3138
* add it to your plugins and conditionally use the hash in the filename
3239
*/
@@ -37,7 +44,7 @@ module.exports = {
3744
filename: needsFingerprint ? "bundle-[hash].js" : "bundle.js"
3845
},
3946
plugins: [
40-
new AssetFingerprintPlugin(needsFingerprint)
47+
new AssetFingerprintPlugin(initializerDirectory, needsFingerprint)
4148
]
4249
};
4350
```
@@ -69,7 +76,7 @@ module.exports = function(fingerprint) {
6976
filename: "bundle-[hash].js"
7077
},
7178
plugins: [
72-
new AssetFingerprintPlugin()
79+
new AssetFingerprintPlugin(initializerDirectory)
7380
]
7481
}
7582
} else {
@@ -85,7 +92,7 @@ module.exports = function(fingerprint) {
8592
```
8693

8794
### Integration with Rails
88-
Now that our `config/initializers/asset_fingerprint.rb` file is setup, we can conditionally use it in our views by setting up a helper method in `application_helper.rb`:
95+
Now that our `asset_fingerprint.rb` file is setup, we can conditionally use it in our views by setting up a helper method in `application_helper.rb`:
8996

9097
```ruby
9198
def asset_with_fingerprint(asset_name)

index.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,25 @@
11
const fs = require('fs');
22

3-
function AssetFingerprint(needsFingerprint = true) {
3+
function AssetFingerprint(initializerDirectory, needsFingerprint = true) {
4+
_validateInitializerDirectory();
5+
6+
this.initializerDirectory = initializerDirectory;
47
this.needsFingerprint = needsFingerprint;
8+
9+
function _validateInitializerDirectory() {
10+
if (initializerDirectory === undefined) {
11+
throw new Error('Please supply a directory path for your initializer, such as `config/initializers`.');
12+
}
13+
}
514
}
615

716
AssetFingerprint.prototype.apply = function(compiler) {
817
compiler.plugin('done', function(stats) {
918
if (this.needsFingerprint) {
1019
let output = `ASSET_FINGERPRINT = '${stats.hash}'`;
11-
fs.writeFileSync('config/initializers/asset_fingerprint.rb', output)
20+
let initializerPath = `${this.initializerDirectory}/asset_fingerprint.rb`;
21+
22+
fs.writeFileSync(initializerPath, output);
1223
}
1324
}.bind(this));
1425
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "asset-fingerprint-webpack-rails",
3-
"version": "0.1.1",
3+
"version": "1.1.1",
44
"description": "A webpack plugin to fingerprint your JS for consumption by Rails",
55
"main": "index.js",
66
"scripts": {

spec/indexSpec.js

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,39 @@ describe("AssetFingerprint", function() {
33
var fingerprint;
44

55
describe("initialization", function() {
6-
it("should default needsFingerprint = true when no args", function() {
7-
fingerprint = new AssetFingerprint();
8-
expect(fingerprint.needsFingerprint).toBeTruthy();
9-
});
6+
describe("needsFingerprint", function() {
7+
it("should default needsFingerprint = true when no args", function() {
8+
fingerprint = new AssetFingerprint('');
9+
expect(fingerprint.needsFingerprint).toBeTruthy();
10+
});
1011

11-
it("should accept true", function() {
12-
fingerprint = new AssetFingerprint(true);
13-
expect(fingerprint.needsFingerprint).toBeTruthy();
14-
});
12+
it("should accept true", function() {
13+
fingerprint = new AssetFingerprint('', true);
14+
expect(fingerprint.needsFingerprint).toBeTruthy();
15+
});
1516

16-
it("should accept false", function() {
17-
fingerprint = new AssetFingerprint(false);
18-
expect(fingerprint.needsFingerprint).toBeFalsy();
17+
it("should accept false", function() {
18+
fingerprint = new AssetFingerprint('', false);
19+
expect(fingerprint.needsFingerprint).toBeFalsy();
20+
});
21+
22+
it("should accept null", function() {
23+
fingerprint = new AssetFingerprint('', null);
24+
expect(fingerprint.needsFingerprint).toBeFalsy();
25+
});
1926
});
2027

21-
it("should accept null", function() {
22-
fingerprint = new AssetFingerprint(null);
23-
expect(fingerprint.needsFingerprint).toBeFalsy();
28+
describe("initializer path", function() {
29+
it("should set when supplied", function() {
30+
var dir = 'config/initializers';
31+
fingerprint = new AssetFingerprint(dir, true);
32+
expect(fingerprint.initializerDirectory).toEqual(dir);
33+
});
34+
35+
it("should fail when not supplied", function() {
36+
fingerprint = function() { new AssetFingerprint(); }
37+
expect(fingerprint).toThrowError('Please supply a directory path for your initializer, such as `config/initializers`.');
38+
});
2439
});
2540
});
2641
});

0 commit comments

Comments
 (0)