Skip to content

Commit 75a5698

Browse files
author
Keith Walsh
committed
Update README
1 parent 0ca51ed commit 75a5698

File tree

1 file changed

+46
-16
lines changed

1 file changed

+46
-16
lines changed

README.md

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,12 @@ npm install asset-fingerprint-webpack-rails --save-dev
99
## Usage
1010
**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)._
1111

12+
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).
13+
14+
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.
15+
1216
### Option 1
13-
Use a local variable in `webpack.config.js` to pass into the plugin to inform it whether not not it should fingerprint. **_defualts to true with no args_**
17+
Use a local variable in `webpack.config.js` to pass into the plugin to inform it whether not not it should fingerprint, calling `webpack` without any args. **_plugin defualts to fingerprint=true with no args_**
1418
```javascript
1519
// webpack.config.js
1620

@@ -22,13 +26,18 @@ const AssetFingerprintPlugin = require('asset-fingerprint-webpack-rails');
2226
const needsFingerprint = someLocalBoolean;
2327

2428
/**
25-
* add it to your plugins
29+
* add it to your plugins and conditionally use the hash in the filename
2630
*/
27-
...
28-
plugins: [
29-
new AssetFingerprintPlugin(needsFingerprint)
30-
]
31-
...
31+
module.exports = {
32+
entry: "./entry.js",
33+
output: {
34+
path: __dirname,
35+
filename: needsFingerprint ? "bundle-[hash].js" : "bundle.js"
36+
},
37+
plugins: [
38+
new AssetFingerprintPlugin(needsFingerprint)
39+
]
40+
};
3241
```
3342

3443
### Option 2
@@ -41,24 +50,25 @@ Then in `webpack.config.js`
4150
// webpack.config.js
4251

4352
/**
44-
* return a configuration object based on the option passed in, only using the plugin when it exists
53+
* require the plugin
4554
*/
46-
4755
const AssetFingerprintPlugin = require('asset-fingerprint-webpack-rails');
4856

57+
/**
58+
* return a configuration object based on the option passed in, only using the plugin when it exists
59+
* and conditionally using the hash in the filename
60+
*/
4961
module.exports = function(fingerprint) {
5062
if (fingerprint) {
5163
return {
5264
entry: "./entry.js",
5365
output: {
5466
path: __dirname,
55-
filename: "bundle.js"
67+
filename: "bundle-[hash].js"
5668
},
57-
module: {
58-
plugins: [
59-
new AssetFingerprintPlugin()
60-
]
61-
}
69+
plugins: [
70+
new AssetFingerprintPlugin()
71+
]
6272
}
6373
} else {
6474
return {
@@ -70,4 +80,24 @@ module.exports = function(fingerprint) {
7080
}
7181
}
7282
};
73-
```
83+
```
84+
85+
### Integration with Rails
86+
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`:
87+
88+
```ruby
89+
def asset_with_fingerprint(asset_name)
90+
Rails.env.production? ? "#{asset_name}-#{ASSET_FINGERPRINT}" : asset_name
91+
end
92+
```
93+
94+
And in your layout:
95+
96+
```html
97+
= javascript_include_tag asset_with_fingerprint('path_of_bundled_js_output/bundle')
98+
```
99+
100+
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.
101+
102+
### Credit
103+
Concept adapted from @samullen - http://samuelmullen.com/articles/replacing-the-rails-asset-pipeline-with-webpack-and-yarn/

0 commit comments

Comments
 (0)