Skip to content

Conversation

@gcscoder
Copy link
Contributor

No description provided.


this.initializerDirectory = initializerDirectory;
this.needsFingerprint = needsFingerprint;
this.fingerprintName = fingerprintName || 'ASSET_FINGERPRINT';
Copy link
Owner

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.

Copy link
Contributor Author

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.

Copy link
Owner

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.

Copy link
Contributor Author

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';
Copy link
Owner

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?

Copy link
Contributor Author

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.

Copy link
Owner

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'

Copy link
Contributor Author

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.

Copy link
Owner

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}`)
Copy link
Owner

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?

Copy link
Contributor Author

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.

Copy link
Owner

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.

Copy link
Contributor Author

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).');
Copy link
Owner

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?

Copy link
Contributor Author

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.

});
});

describe("fingerprint name", function() {
Copy link
Owner

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() {
      ...
    });
  });
});

Copy link
Contributor Author

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.');
Copy link
Owner

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?

Copy link
Owner

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

@coderbydesign
Copy link
Owner

coderbydesign commented May 31, 2018

@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?

Copy link
Owner

@coderbydesign coderbydesign left a 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!

@gcscoder
Copy link
Contributor Author

Glad I could help!

@coderbydesign coderbydesign merged commit 0788866 into coderbydesign:master Jun 1, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants