Skip to content
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

Refactor file name options #696

Merged
merged 5 commits into from
Jan 11, 2023
Merged

Refactor file name options #696

merged 5 commits into from
Jan 11, 2023

Conversation

bluwy
Copy link
Member

@bluwy bluwy commented Jan 11, 2023

Changes

  • Merge sourcefile and moduleId options as a single filename option. (Possible since Simplify HMR handling astro#5811)
  • Add a new normalizedFilename option to generate stable hashes instead.

The new scope hashes are derived like this:

  1. If normalizedFilename is define, use it only to generate the hash.
  2. Else use the entire source to generate the hash.

I don't think we need to combined both the file path and source to generate the hash, the normalizedFilename itself should be unique to generate off if it's define, which Astro core will always pass. The value would be like /src/pages/index.astro etc.


Should also pave the way for withastro/astro#5136 once we are able to use the normalizedFilename option in core.

Testing

  • Updated the tests with the option name changes.
  • Updated printer_test to also handle case where filename option is passed and used in the generated code.
  • Remove "same file, same html but different style tag, but still same hash" test. I think the hash should derive from the source entirely (no excluding style) by default. This shouldn't affect us as if normalizedFilename is passed (which we will), the hash will derive from it instead.

Docs

n/a

@changeset-bot
Copy link

changeset-bot bot commented Jan 11, 2023

🦋 Changeset detected

Latest commit: cde40dc

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@astrojs/compiler Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Comment on lines +547 to +548
escapedPatharg := strings.ReplaceAll(patharg, "'", "\\'")
patharg = fmt.Sprintf("\"%s\"", escapedPatharg)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is needed to get test pass as we now test filename in the tests.

Comment on lines +2421 to +2426
patharg := "import.meta.url"
if tt.filename != "" {
escapedFilename := strings.ReplaceAll(tt.filename, "'", "\\'")
patharg = fmt.Sprintf("\"%s\"", escapedFilename)
}
toMatch += "\n\n" + fmt.Sprintf("export const %s = %s(%s, %s);\n\n", METADATA, CREATE_METADATA, patharg, metadata)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This takes care of the filename value too, instead of import.meta.url

if len(tt.filename) > 0 {
escapedFilename := strings.ReplaceAll(tt.filename, "'", "\\'")
toMatch += suffixWithFilename(escapedFilename)
toMatch = strings.Replace(toMatch, "$$Component", getComponentName((tt.filename)), -1)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If filename is passed, the generated code uses a different component name based on it. Replace here too.

Comment on lines -40 to -43
test('hash is stable when styles change', () => {
const [a, b] = scopes;
assert.equal(a, b, 'Expected scopes to be equal');
});
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was removed as explained in the PR description.

@matthewp
Copy link
Contributor

Regarding the hash, the biggest thing here is that astro build should produce the same result on different machines, so that a build doesn't unnecessarily invalidate caching. It sounds like we are achieving that by using a root relative normalizedFilename, is that right?

@bluwy
Copy link
Member Author

bluwy commented Jan 11, 2023

Yup, "normalized" should also mean "root-relative" so the hash should be equal on different machines. To also add to the data point, vite-plugin-svelte does it too (though only for dev it can expand to build too).

Copy link
Member

@natemoo-re natemoo-re left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great! Much easier to reason about.

@matthewp matthewp merged commit edd3e0e into main Jan 11, 2023
@matthewp matthewp deleted the normalized-filename branch January 11, 2023 17:23
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.

3 participants