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

[10.x] Add content method to Vite #47968

Merged
merged 10 commits into from
Aug 8, 2023
Merged

[10.x] Add content method to Vite #47968

merged 10 commits into from
Aug 8, 2023

Conversation

michael-rubel
Copy link
Contributor

About

This PR adds a method to retrieve compiled content for the given file available in the Vite manifest. It allows us to pack our Vite assets inline when needed.

Example use case

You want to create an HTML page that needs to be open or sent as a single document (for example, as a mail attachment), and to work offline (without access to the internet) so the asset should be injected into the document for the browser to work on the local file without firing the HTTP requests.

Usage example

<style>{!! Vite::content('resources/scss/file.scss') !!}</style>

@michael-rubel michael-rubel changed the title Add content method to Vite [10.x] Add content method to Vite Aug 4, 2023
@jessarcher
Copy link
Member

This sounds pretty useful, and I've run into similar scenarios myself.

My only concern is that the local dev behaviour might be unexpected or confusing when running the Vite dev server. It will fail if you haven't run a build since adding the asset. Otherwise, it will return the contents from the last build, ignoring any changes you've made to the source file. You'd need to run Vite in "watch" mode instead of HMR mode if you wanted to automatically have the latest changes while developing.

michael-rubel and others added 2 commits August 6, 2023 02:50
Co-authored-by: Jess Archer <jess@jessarcher.com>
@michael-rubel
Copy link
Contributor Author

michael-rubel commented Aug 6, 2023

@jessarcher what can we do to address this? Maybe adding an exception when in HMR and the method is called?

By the way, developers can add a check like this:

@if (! Vite::isRunningHot())
    <style>{!! Vite::content('resources/scss/file.scss') !!}</style>
@else
    @vite('resources/scss/file.scss')
@endif

I thought also about adding the redirect to the Vite::asset() when the HMR mode is enabled, but that would require lots of additional work on the framework side, and not sure it's worth it or makes sense. The way it is now it can be kept simple.

@jessarcher
Copy link
Member

jessarcher commented Aug 6, 2023

@michael-rubel You could try calling file_get_contents on the dev server URL for the asset, but I'm not sure how well that would work. For something like CSS, it might be okay. But for JavaScript, it might cause more issues and confusion than it solves 😅

It's probably fine as-is. Just a caveat to be aware of when using it.

@taylorotwell taylorotwell merged commit 060faa6 into laravel:10.x Aug 8, 2023
20 checks passed
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.

None yet

4 participants