-
Notifications
You must be signed in to change notification settings - Fork 11k
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
[9.x] Vite #42785
[9.x] Vite #42785
Conversation
Blade apps and SPAs have different entry point requirements so there is no sensible default.
Will the Vite manifest SRI plugin be supported by this? That would be nice. |
It's on my list of things to look at post-launch and should be easy to implement 👍 |
- <link rel="stylesheet" href="{{ mix('css/app.css') }}">
- <script src="{{ mix('js/app.js') }}" defer></script>
+ @vite(['resources/css/app.css', 'resources/js/app.js']) How would you add nonces to this vite call? |
Co-authored-by: Lucas Michot <513603+lucasmichot@users.noreply.github.com>
* Add Vite helpers * Remove vite helper * Add @viteReactRefresh directive * Add docblock * Remove unused import * Automatically clean up after tests * Use ASSET_URL when resolving assets * support default entrypoint * update docblock * Linting * Support CSS entry points * Remove default entry points Blade apps and SPAs have different entry point requirements so there is no sensible default. * Fix test namespace Co-authored-by: Lucas Michot <513603+lucasmichot@users.noreply.github.com> * Add missing import Co-authored-by: Tim MacDonald <hello@timacdonald.me> Co-authored-by: Lucas Michot <513603+lucasmichot@users.noreply.github.com>
Hi @jessarcher
Is there a way to output the path like it was with <link rel="stylesheet" type="text/css" href="{{ mix('/css/light.css') }}"
media="(prefers-color-scheme: light)"> For <script src="{{ mix('/app/part/landing.js') }}"
data-turbo-eval="false" data-turbolinks-eval="false"></script> |
@tabuna there isn't a way to do this currently with Vite, however we are considering enabling the specification of arbitrary attributes on entry point tags. |
I was thinking about this also, about how I could add a CSP nonce to the script tag. What about a blade template that can be overridden? That way, instead of calling |
@nlvedwin it looks like you are visiting the Vite dev server. You still need to utilise For example if you are using Valet on your system, you should visit |
facing the same issue as @nlvedwin. I use Sail with localhost already being exposed and accessible. |
No, you load the normal artisan serve URL, `@vite` will take care of making it
able to hot reload from the development server.
|
ahh OK. Got it now. Thanks! 🙏 |
got it now, it feels like magic 🧙, thanks @timacdonald ! |
For others who are having a 404 when trying to check the assets (http://localhost:3000/build/css/app.css), run It would be great if the dev server didn't swallow compile errors, but I thnk that's something to bring up to Vite. |
Hi all, referred here by @tabuna. For those of you who are looking for a direct replacement of the mix() helper, please see my Vite helper package on Packagist - https://packagist.org/packages/motomedialab/laravel-vite-helper |
Hey folks, A lot of questions in this thread about adding a CSP nonce, Subresource integrity, or arbitrary attributes (turoblinks, media query stuff, etc) to the Vite generated tags. I've just opened a PR to support all of those use-cases. You can check it out here: #43442 Any feedback would be appreciated. |
Awesome stuff @timacdonald, thank you so much! |
Great work @jessarcher 👍 , |
After #43442 I get a workaround to inject legacy generated files Vite::useScriptTagAttributes(function (string $src, string $url, array|null $chunk, array|null $manifest) {
if (in_array(
$src,
[
'vite/legacy-polyfills',
'app/resources/js/app/app-legacy.js',
'app/resources/sass/app/app-legacy.scss',
]
)) {
return [
'type' => 'text/javascript',
'nomodule',
];
}
return [];
}); |
Late comment, but while the above works (thanks!) it is still loading the legacy script via preloads for modern browsers eg. So far I've been unable to find a workaround for this |
I was able to find an ondocumented callback for the preloads, in which returning false causes no preloads to be included. The following worked for us in a ViteServiceProvider we added to the project.
|
⚡ Vite is a modern frontend build tool that provides an extremely fast development environment and bundles your code for production.
This PR introduces a
@vite
Blade directive to the framework that will be used in combination with the official Laravel Vite Plugin. This PR does not remove or deprecate the existingmix()
helper. There are also PRs to laravel/laravel as well as laravel/breeze and laravel/jetstream that will make this the default for new Laravel applications. There is also, of course, documentation.The Laravel side of the implementation is similar to Mix and should feel quite familiar.
The main difference is that instead of using the
mix()
helper, you would now use the@vite
Blade directive. The reason for the directive is that Laravel needs to output complete<script>
and<link rel="stylesheet">
tags, rather than just a path as with Mix.For the best experience with a Blade app, you will want to specify both CSS and JavaScript entry points, which would look something like this in your Blade layout template:
In development mode, this will also load the Vite client, which will automatically reload your JavaScript and CSS when you make changes. In production mode, it will automatically load the versioned assets for you.
For the best experience with a SPA (e.g. Inertia), you will want to import your CSS via your JavaScript entry point(s) and then only specify the JavaScript entry point to the
@vite
directive, which would look something like this:import './bootstrap.js'; + import '../css/app.css';
When importing your CSS via JavaScript, Vite will automatically inject a
<style>
tag in development mode that it will keep up to date. When building for production, a compiledapp.{version}.css
file will be generated that the@vite
directive will automatically load via a<link rel="stylesheet">
tag.When using React, you will also need to place a
@viteReactRefresh
directive above your@vite
directive that will inject Vite's React Refresh Runtime.Everything will be configured for you with Laravel's updated starter kits.
As with Mix, The
@vite
directive will automatically detect when the Vite development server is running, otherwise, it will use the compiled and versioned assets from the manifest.The Laravel plugin also handles Vite SSR builds and works well with tools like Sail and Vapor.
The documentation will include full instructions, examples, and advanced configuration.
This implementation requires a specific Vite configuration that the official Laravel Vite Plugin handles for you. For existing applications, there are also some required changes documented in the upgrade guide.