-
Notifications
You must be signed in to change notification settings - Fork 1
Adding optional argument to append fingerprint hash with a new name #3
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
Adding optional argument to append fingerprint hash with a new name #3
Conversation
|
|
||
| this.initializerDirectory = initializerDirectory; | ||
| this.needsFingerprint = needsFingerprint; | ||
| this.fingerprintName = fingerprintName || 'ASSET_FINGERPRINT'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm wondering if it might be beneficial to have some sort of validation/constant format enforcement here to ensure something valid is written to the Ruby file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I could add a check for names that have A-Z and _ in the middle and throw an error when it does not match.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On that note, do we want to lock them to something like this to enforce a convention, raise a meaningful error if not, and add something to the docs? Or leave off the "FINGERPRINT" part but still enforce the rest:
/^([A-Z]|_)*_FINGERPRINT$/
This would allow for something like "TEST_THIS_FINGERPRINT" as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I think having that as part of the name is a good idea so that it is always clear.
| let defaultRun = this.fingerprintName === 'ASSET_FINGERPRINT'; | ||
| let initializerPath = `${this.initializerDirectory}/asset_fingerprint.rb`; | ||
| const newline = defaultRun ? '' : '\r\n'; | ||
| const fsMethod = defaultRun ? 'writeFileSync' : 'appendFileSync'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When you run this script more than once, it's going to append the custom constant multiple times. Is there a way we might be able to prevent that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I could read the file prior to writing the value back out. If I find a string that matches the value for the name I could throw an exception.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is that what we'd want to do though? Or just no-op? The way it is (at least how it worked for me locally, which was what I was curious about when we talked last week) is that every subsequent build will just append a duplicate of the constant, or if files have changed, a new value for the same constant, similar to this:
ASSET_FINGERPRINT='lkasd7fdjassdk23'
CUSTOM_ASSET_FINGERPRINT='lkasd7fdjassdk23'
CUSTOM_ASSET_FINGERPRINT='hhgdks553l2343n'
CUSTOM_ASSET_FINGERPRINT='hhgdks553l2343n'There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ideally, the user should not call the plugin twice in the same build. I would not want multiple instances of the same constant with the same or different value.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But if it's appending, even on subsequent builds you would end up accumulating multiple instances of that appended constant, right? I was referring to if I run the build command once locally, then run it again a second time.
index.js
Outdated
| const fsMethod = defaultRun ? 'writeFileSync' : 'appendFileSync'; | ||
| let output = `${newline}${this.fingerprintName} = '${stats.hash}'`; | ||
| fs[fsMethod](initializerPath, output); | ||
| console.log(`asset-fingerprint-webpack-rails: updated file ${initializerPath} with ${this.fingerprintName} = ${stats.hash}`) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't reflect any secondary append to the file, does it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It will continue to append unless default arguments, or the value for fingerprintName === 'ASSET_FINGERPRINT' are passed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I was just referring to the logging.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, it should log with every run.
index.js
Outdated
|
|
||
| function _validateFingerprintName(fingerprintName) { | ||
| if(!/^([A-Z]|_)*_FINGERPRINT$/.test(fingerprintName)) { | ||
| throw new Error('Please supply a fingerprint name that is all caps separating words by underscores (i.e. CUSTOM_ASSET_FINGERPRINT).'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We may want to note something about the _FINGERPRINT being a part of the validation? Can we add something to the README about this requirement as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I'll add that.
spec/indexSpec.js
Outdated
| }); | ||
| }); | ||
|
|
||
| describe("fingerprint name", function() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Honestly just curious, but in the case where there are different setups for the same describe (like in this case where we have 3 "fingerprint name" blocks) is this an idiomatic pattern, vs having them nested under one block like below?
describe("fingerprint name", function(){
describe("valid", function(){
beforeEach(function() {
...
});
it("should use value when provided", function() {
...
});
});
describe("invalid", function(){
beforeEach(function() {
...
});
it("should validate for duplicate name", function() {
...
});
});
});There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The return values setup for the spyOn calls for fs are different and must be setup in a new describe section. There is one test I can move up with the others that has the same criteria.
| _validateFingerprintName(fingerprintName); | ||
|
|
||
| if(_checkForExistingFingerprint(fingerprintName, this.initializerPath)) { | ||
| throw new Error('The provided fingerprint name has already been used.'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is being thrown for me on secondary runs. I'm testing it by running two independent bundles. The first plugin is setup as a default: new AssetFingerprintPlugin('config/initializers', true) which writes ASSET_FINGERPRINT. The second build sets the plugin up with the optional argument: new AssetFingerprintPlugin('config/initializers', true, 'TEST_FINGERPRINT'). On the first run, it creates the file, adds the default and then the override as expected. But if I run that delegation script again it throws this error. Is there a more deterministic way to test/implement this that you're using?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Per our discussion, this was a local testing implementation issue. 👍
|
@gcscoder - one last thing is that we should bump the package.json version. I'm guessing a minor bump should be fine since it should be backwards compatible? |
coderbydesign
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me, thanks for this PR!
|
Glad I could help! |
No description provided.