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

Inline CSS Loading Fails with Vite 5 #24

Closed
bavodeclercq opened this issue Feb 14, 2024 · 3 comments
Closed

Inline CSS Loading Fails with Vite 5 #24

bavodeclercq opened this issue Feb 14, 2024 · 3 comments
Labels
bug Something isn't working

Comments

@bavodeclercq
Copy link

Describe the bug

When using Vite 5, the function {{ craft.vite.inline(craft.vite.entry('emails.css')) }} fails to return any output, indicating a significant issue with inline CSS loading. The core of the problem lies within the extractEntry function in manifestHelper.php, which is required to delve deeper due to changes in how CSS files are represented in manifest.json in Vite 5. The malfunction occurs because the styleKey is not correctly retrieved. This is due to the filenameWithoutHash function not functioning correctly, as it only searches for dots in filenames, ignoring dashes. This specific issue traces back to the transition from Vite 3 to Vite 4, where the separator in filenames changed from dots to dashes. However, the repercussions of this change only became apparent with the introduction of Vite 5, due to adjustments in the manifest file structure and the handling of CSS files. A workaround involves modifying line 300 in ManifestHelper.php from $filenameParts = explode('.', $filename); to $filenameParts = preg_split("/[.-]/", $filename);, which addresses the issue and allows the CSS from email.css to be correctly inlined.

To reproduce

To reproduce the behavior, follow these steps:

  1. Upgrade to Vite 5.
  2. Attempt to inline CSS in a Craft CMS template using {{ craft.vite.inline(craft.vite.entry('emails.css')) }}.
  3. Observe that the expected inline CSS is not rendered, indicating the function returns nothing.

Expected behaviour

The expected behavior is for the CSS from emails.css to be correctly inlined when using the {{ craft.vite.inline(craft.vite.entry('emails.css')) }} function in Craft CMS with Vite 5.

Screenshots

N/A

Versions

  • Plugin version: Vite 4.0.8
  • Craft version: Craft 4.7.1
@bavodeclercq bavodeclercq added the bug Something isn't working label Feb 14, 2024
@khalwat
Copy link
Contributor

khalwat commented Feb 28, 2024

Appreciate the issue! I think:

$filenameParts = preg_split("/[.-]/", $filename);

Might be problematic if the file name has a - in it however.

@bavodeclercq
Copy link
Author

bavodeclercq commented Feb 28, 2024

Yeah, you're right. The quick and dirty fix was a stopgap for my scenario. Ideally, if possible, checking which version of Vite is being used and then deciding whether to split file names at dots, or just at the last dash (for Vite 5) based on that information, could be a more stable solution. Alternatively, instead of checking the version, checking if there are dots in the filename and, if not, splitting at the last dash could be another approach.

khalwat added a commit that referenced this issue Mar 2, 2024
… using Vite 5 or later, due to the `ManifestHelper::fileNameWithoutHash()` function not working correctly ([#24](#24))
khalwat added a commit that referenced this issue Mar 2, 2024
… using Vite 5 or later, due to the `ManifestHelper::fileNameWithoutHash()` function not working correctly ([#24](#24))
khalwat added a commit that referenced this issue Mar 2, 2024
… using Vite 5 or later, due to the `ManifestHelper::fileNameWithoutHash()` function not working correctly ([#24](#24))
@khalwat
Copy link
Contributor

khalwat commented Mar 2, 2024

Addressed in the above commits.

@khalwat khalwat closed this as completed Mar 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants