Skip to content

Require initializer path #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 20, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,14 @@ npm install asset-fingerprint-webpack-rails --save-dev
## Usage
**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)._

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).
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`.

```javascript
const AssetFingerprintPlugin = require('asset-fingerprint-webpack-rails');
new AssetFingerprintPlugin('config/initializers', true);
```

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).

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.

Expand All @@ -26,7 +33,7 @@ Use a local variable in `webpack.config.js` to pass into the plugin to inform it
*/
const AssetFingerprintPlugin = require('asset-fingerprint-webpack-rails');
const needsFingerprint = someLocalBoolean;

const initializerDirectory = 'config/initializers';
/**
* add it to your plugins and conditionally use the hash in the filename
*/
Expand All @@ -37,7 +44,7 @@ module.exports = {
filename: needsFingerprint ? "bundle-[hash].js" : "bundle.js"
},
plugins: [
new AssetFingerprintPlugin(needsFingerprint)
new AssetFingerprintPlugin(initializerDirectory, needsFingerprint)
]
};
```
Expand Down Expand Up @@ -69,7 +76,7 @@ module.exports = function(fingerprint) {
filename: "bundle-[hash].js"
},
plugins: [
new AssetFingerprintPlugin()
new AssetFingerprintPlugin(initializerDirectory)
]
}
} else {
Expand All @@ -85,7 +92,7 @@ module.exports = function(fingerprint) {
```

### Integration with Rails
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`:
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`:

```ruby
def asset_with_fingerprint(asset_name)
Expand Down
15 changes: 13 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
const fs = require('fs');

function AssetFingerprint(needsFingerprint = true) {
function AssetFingerprint(initializerDirectory, needsFingerprint = true) {
_validateInitializerDirectory();

this.initializerDirectory = initializerDirectory;
this.needsFingerprint = needsFingerprint;

function _validateInitializerDirectory() {
if (initializerDirectory === undefined) {
throw new Error('Please supply a directory path for your initializer, such as `config/initializers`.');
}
}
}

AssetFingerprint.prototype.apply = function(compiler) {
compiler.plugin('done', function(stats) {
if (this.needsFingerprint) {
let output = `ASSET_FINGERPRINT = '${stats.hash}'`;
fs.writeFileSync('config/initializers/asset_fingerprint.rb', output)
let initializerPath = `${this.initializerDirectory}/asset_fingerprint.rb`;

fs.writeFileSync(initializerPath, output);
}
}.bind(this));
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "asset-fingerprint-webpack-rails",
"version": "0.1.1",
"version": "0.1.2",
"description": "A webpack plugin to fingerprint your JS for consumption by Rails",
"main": "index.js",
"scripts": {
Expand Down
43 changes: 29 additions & 14 deletions spec/indexSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,39 @@ describe("AssetFingerprint", function() {
var fingerprint;

describe("initialization", function() {
it("should default needsFingerprint = true when no args", function() {
fingerprint = new AssetFingerprint();
expect(fingerprint.needsFingerprint).toBeTruthy();
});
describe("needsFingerprint", function() {
it("should default needsFingerprint = true when no args", function() {
fingerprint = new AssetFingerprint('');
expect(fingerprint.needsFingerprint).toBeTruthy();
});

it("should accept true", function() {
fingerprint = new AssetFingerprint(true);
expect(fingerprint.needsFingerprint).toBeTruthy();
});
it("should accept true", function() {
fingerprint = new AssetFingerprint('', true);
expect(fingerprint.needsFingerprint).toBeTruthy();
});

it("should accept false", function() {
fingerprint = new AssetFingerprint(false);
expect(fingerprint.needsFingerprint).toBeFalsy();
it("should accept false", function() {
fingerprint = new AssetFingerprint('', false);
expect(fingerprint.needsFingerprint).toBeFalsy();
});

it("should accept null", function() {
fingerprint = new AssetFingerprint('', null);
expect(fingerprint.needsFingerprint).toBeFalsy();
});
});

it("should accept null", function() {
fingerprint = new AssetFingerprint(null);
expect(fingerprint.needsFingerprint).toBeFalsy();
describe("initializer path", function() {
it("should set when supplied", function() {
var dir = 'config/initializers';
fingerprint = new AssetFingerprint(dir, true);
expect(fingerprint.initializerDirectory).toEqual(dir);
});

it("should fail when not supplied", function() {
fingerprint = function() { new AssetFingerprint(); }
expect(fingerprint).toThrowError('Please supply a directory path for your initializer, such as `config/initializers`.');
});
});
});
});