Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 0ca51ed

Browse files
author
Keith Walsh
committedSep 8, 2017
Initial commit
0 parents  commit 0ca51ed

File tree

5 files changed

+125
-0
lines changed

5 files changed

+125
-0
lines changed
 

‎.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

‎LICENSE.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
MIT License
2+
3+
Copyright (c) 2017 Keith Walsh
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6+
7+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8+
9+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

‎README.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# Asset Fingerprint Webpack Rails
2+
A webpack plugin to fingerprint your JS for consumption by Rails
3+
4+
## Install
5+
```
6+
npm install asset-fingerprint-webpack-rails --save-dev
7+
```
8+
9+
## Usage
10+
**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)._
11+
12+
### 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_**
14+
```javascript
15+
// webpack.config.js
16+
17+
...
18+
/**
19+
* require the plugin
20+
*/
21+
const AssetFingerprintPlugin = require('asset-fingerprint-webpack-rails');
22+
const needsFingerprint = someLocalBoolean;
23+
24+
/**
25+
* add it to your plugins
26+
*/
27+
...
28+
plugins: [
29+
new AssetFingerprintPlugin(needsFingerprint)
30+
]
31+
...
32+
```
33+
34+
### Option 2
35+
Setup your `webpack.config.js` to conditionally return an object based on an argument passed into the `webpack` command, like `webpack --env.fingerprint`
36+
37+
start webpack with an option: `webpack --env.fingerprint`
38+
39+
Then in `webpack.config.js`
40+
```javascript
41+
// webpack.config.js
42+
43+
/**
44+
* return a configuration object based on the option passed in, only using the plugin when it exists
45+
*/
46+
47+
const AssetFingerprintPlugin = require('asset-fingerprint-webpack-rails');
48+
49+
module.exports = function(fingerprint) {
50+
if (fingerprint) {
51+
return {
52+
entry: "./entry.js",
53+
output: {
54+
path: __dirname,
55+
filename: "bundle.js"
56+
},
57+
module: {
58+
plugins: [
59+
new AssetFingerprintPlugin()
60+
]
61+
}
62+
}
63+
} else {
64+
return {
65+
entry: "./entry.js",
66+
output: {
67+
path: __dirname,
68+
filename: "bundle.js"
69+
}
70+
}
71+
}
72+
};
73+
```

‎index.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const fs = require('fs');
2+
3+
function AssetFingerprint(needsFingerprint = true) {
4+
this.needsFingerprint = needsFingerprint;
5+
}
6+
7+
AssetFingerprint.prototype.apply = function(compiler) {
8+
compiler.plugin('done', function(stats) {
9+
if (this.needsFingerprint) {
10+
let output = `ASSET_FINGERPRINT = '${stats.hash}'`;
11+
fs.writeFileSync('config/initializers/asset_fingerprint.rb', output)
12+
}
13+
}.bind(this));
14+
}
15+
16+
module.exports = AssetFingerprint;

‎package.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "asset-fingerprint-webpack-rails",
3+
"version": "0.1.0",
4+
"description": "A webpack plugin to fingerprint your JS for consumption by Rails",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/coderbydesign/asset-fingerprint-webpack-rails.git"
12+
},
13+
"keywords": [
14+
"webpack",
15+
"plugin",
16+
"rails",
17+
"assets",
18+
"fingerprint"
19+
],
20+
"author": "Keith Walsh",
21+
"license": "MIT",
22+
"bugs": {
23+
"url": "https://github.com/coderbydesign/asset-fingerprint-webpack-rails/issues"
24+
},
25+
"homepage": "https://github.com/coderbydesign/asset-fingerprint-webpack-rails#readme"
26+
}

0 commit comments

Comments
 (0)
Please sign in to comment.