Skip to content

Commit 5158b46

Browse files
committed
Vite
1 parent f8a78f4 commit 5158b46

File tree

17 files changed

+149
-108
lines changed

17 files changed

+149
-108
lines changed

Diff for: README.md

+9-10
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Only the latest major version of Laravel UI receives bug fixes. The table below
2121
| [1.x](https://github.com/laravel/ui/tree/1.x) | 5.8, 6.x |
2222
| [2.x](https://github.com/laravel/ui/tree/2.x) | 7.x |
2323
| [3.x](https://github.com/laravel/ui/tree/3.x) | 8.x |
24+
| [4.x](https://github.com/laravel/ui/tree/4.x) | 9.x |
2425

2526
### Installation
2627

@@ -46,11 +47,11 @@ php artisan ui react --auth
4647

4748
#### CSS
4849

49-
[Laravel Mix](https://laravel.com/docs/mix) provides a clean, expressive API over compiling SASS or Less, which are extensions of plain CSS that add variables, mixins, and other powerful features that make working with CSS much more enjoyable. In this document, we will briefly discuss CSS compilation in general; however, you should consult the full [Laravel Mix documentation](https://laravel.com/docs/mix) for more information on compiling SASS or Less.
50+
Laravel officially supports [Vite](https://laravel.com/docs/vite), a modern frontend build tool that provides an extremely fast development environment and bundles your code for production. Vite supports a variety of CSS preprocessor languages, including SASS and Less, which are extensions of plain CSS that add variables, mixins, and other powerful features that make working with CSS much more enjoyable. In this document, we will briefly discuss CSS compilation in general; however, you should consult the full [Vite documentation](https://laravel.com/docs/vite#working-with-stylesheets) for more information on compiling SASS or Less.
5051

5152
#### JavaScript
5253

53-
Laravel does not require you to use a specific JavaScript framework or library to build your applications. In fact, you don't have to use JavaScript at all. However, Laravel does include some basic scaffolding to make it easier to get started writing modern JavaScript using the [Vue](https://vuejs.org) library. Vue provides an expressive API for building robust JavaScript applications using components. As with CSS, we may use Laravel Mix to easily compile JavaScript components into a single, browser-ready JavaScript file.
54+
Laravel does not require you to use a specific JavaScript framework or library to build your applications. In fact, you don't have to use JavaScript at all. However, Laravel does include some basic scaffolding to make it easier to get started writing modern JavaScript using the [Vue](https://vuejs.org) library. Vue provides an expressive API for building robust JavaScript applications using components. As with CSS, we may use Vite to easily compile JavaScript components into a single, browser-ready JavaScript file.
5455

5556
### Writing CSS
5657

@@ -62,13 +63,13 @@ Before compiling your CSS, install your project's frontend dependencies using th
6263
npm install
6364
```
6465

65-
Once the dependencies have been installed using `npm install`, you can compile your SASS files to plain CSS using [Laravel Mix](https://laravel.com/docs/mix#working-with-stylesheets). The `npm run dev` command will process the instructions in your `webpack.mix.js` file. Typically, your compiled CSS will be placed in the `public/css` directory:
66+
Once the dependencies have been installed using `npm install`, you can compile your SASS files to plain CSS using [Vite](https://laravel.com/docs/vite#working-with-stylesheets). The `npm run dev` command will process the instructions in your `vite.config.js` file. Typically, your compiled CSS will be placed in the `public/build/assets` directory:
6667

6768
```bash
6869
npm run dev
6970
```
7071

71-
The `webpack.mix.js` file included with Laravel's frontend scaffolding will compile the `resources/sass/app.scss` SASS file. This `app.scss` file imports a file of SASS variables and loads Bootstrap, which provides a good starting point for most applications. Feel free to customize the `app.scss` file however you wish or even use an entirely different pre-processor by [configuring Laravel Mix](https://laravel.com/docs/mix).
72+
The `vite.config.js` file included with Laravel's frontend scaffolding will compile the `resources/sass/app.scss` SASS file. This `app.scss` file imports a file of SASS variables and loads Bootstrap, which provides a good starting point for most applications. Feel free to customize the `app.scss` file however you wish or even use an entirely different pre-processor by [configuring Vite](https://laravel.com/docs/vite#working-with-stylesheets).
7273

7374
### Writing JavaScript
7475

@@ -80,13 +81,13 @@ npm install
8081

8182
> By default, the Laravel `package.json` file includes a few packages such as `lodash` and `axios` to help you get started building your JavaScript application. Feel free to add or remove from the `package.json` file as needed for your own application.
8283
83-
Once the packages are installed, you can use the `npm run dev` command to [compile your assets](https://laravel.com/docs/mix). Webpack is a module bundler for modern JavaScript applications. When you run the `npm run dev` command, Webpack will execute the instructions in your `webpack.mix.js` file:
84+
Once the packages are installed, you can use the `npm run dev` command to [compile your assets](https://laravel.com/docs/vite). Vite is a module bundler for modern JavaScript applications. When you run the `npm run dev` command, Vite will execute the instructions in your `vite.config.js` file:
8485

8586
```bash
8687
npm run dev
8788
```
8889

89-
By default, the Laravel `webpack.mix.js` file compiles your SASS and the `resources/js/app.js` file. Within the `app.js` file you may register your Vue components or, if you prefer a different framework, configure your own JavaScript application. Your compiled JavaScript will typically be placed in the `public/js` directory.
90+
By default, the Laravel `vite.config.js` file compiles your SASS and the `resources/js/app.js` file. Within the `app.js` file you may register your Vue components or, if you prefer a different framework, configure your own JavaScript application. Your compiled JavaScript will typically be placed in the `public/build/assets` directory.
9091

9192
> The `app.js` file will load the `resources/js/bootstrap.js` file which bootstraps and configures Vue, Axios, jQuery, and all other JavaScript dependencies. If you have additional JavaScript dependencies to configure, you may do so in this file.
9293
@@ -95,10 +96,8 @@ By default, the Laravel `webpack.mix.js` file compiles your SASS and the `resour
9596
When using the `laravel/ui` package to scaffold your frontend, an `ExampleComponent.vue` Vue component will be placed in the `resources/js/components` directory. The `ExampleComponent.vue` file is an example of a [single file Vue component](https://vuejs.org/guide/single-file-components) which defines its JavaScript and HTML template in the same file. Single file components provide a very convenient approach to building JavaScript driven applications. The example component is registered in your `app.js` file:
9697

9798
```javascript
98-
Vue.component(
99-
'example-component',
100-
require('./components/ExampleComponent.vue').default
101-
);
99+
import ExampleComponent from './components/ExampleComponent.vue';
100+
Vue.component('example-component', ExampleComponent);
102101
```
103102

104103
To use the component in your application, you may drop it into one of your HTML templates. For example, after running the `php artisan ui vue --auth` Artisan command to scaffold your application's authentication and registration screens, you could drop the component into the `home.blade.php` Blade template:

Diff for: src/Auth/bootstrap-stubs/layouts/app.stub

+2-5
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,12 @@
99

1010
<title>{{ config('app.name', 'Laravel') }}</title>
1111

12-
<!-- Scripts -->
13-
<script src="{{ asset('js/app.js') }}" defer></script>
14-
1512
<!-- Fonts -->
1613
<link rel="dns-prefetch" href="//fonts.gstatic.com">
1714
<link href="https://fonts.googleapis.com/css?family=Nunito" rel="stylesheet">
1815

19-
<!-- Styles -->
20-
<link href="{{ asset('css/app.css') }}" rel="stylesheet">
16+
<!-- Scripts -->
17+
@vite(['resources/sass/app.scss', 'resources/js/app.js'])
2118
</head>
2219
<body>
2320
<div id="app">

Diff for: src/Presets/Bootstrap.php

+4-5
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class Bootstrap extends Preset
1414
public static function install()
1515
{
1616
static::updatePackages();
17-
static::updateWebpackConfiguration();
17+
static::updateViteConfiguration();
1818
static::updateSass();
1919
static::updateBootstrapping();
2020
static::removeNodeModules();
@@ -32,18 +32,17 @@ protected static function updatePackageArray(array $packages)
3232
'bootstrap' => '^5.1.3',
3333
'@popperjs/core' => '^2.10.2',
3434
'sass' => '^1.32.11',
35-
'sass-loader' => '^11.0.1',
3635
] + $packages;
3736
}
3837

3938
/**
40-
* Update the Webpack configuration.
39+
* Update the Vite configuration.
4140
*
4241
* @return void
4342
*/
44-
protected static function updateWebpackConfiguration()
43+
protected static function updateViteConfiguration()
4544
{
46-
copy(__DIR__.'/bootstrap-stubs/webpack.mix.js', base_path('webpack.mix.js'));
45+
copy(__DIR__.'/bootstrap-stubs/vite.config.js', base_path('vite.config.js'));
4746
}
4847

4948
/**

Diff for: src/Presets/React.php

+41-7
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ public static function install()
1616
{
1717
static::ensureComponentDirectoryExists();
1818
static::updatePackages();
19-
static::updateWebpackConfiguration();
19+
static::updateViteConfiguration();
2020
static::updateBootstrapping();
2121
static::updateComponent();
22+
static::addViteReactRefreshDirective();
2223
static::removeNodeModules();
2324
}
2425

@@ -32,19 +33,20 @@ protected static function updatePackageArray(array $packages)
3233
{
3334
return [
3435
'@babel/preset-react' => '^7.13.13',
36+
'@vitejs/plugin-react' => '^1.3.2',
3537
'react' => '^17.0.2',
3638
'react-dom' => '^17.0.2',
37-
] + Arr::except($packages, ['vue', 'vue-template-compiler']);
39+
] + Arr::except($packages, ['@vitejs/plugin-vue', 'vue']);
3840
}
3941

4042
/**
41-
* Update the Webpack configuration.
43+
* Update the Vite configuration.
4244
*
4345
* @return void
4446
*/
45-
protected static function updateWebpackConfiguration()
47+
protected static function updateViteConfiguration()
4648
{
47-
copy(__DIR__.'/react-stubs/webpack.mix.js', base_path('webpack.mix.js'));
49+
copy(__DIR__.'/react-stubs/vite.config.js', base_path('vite.config.js'));
4850
}
4951

5052
/**
@@ -59,8 +61,8 @@ protected static function updateComponent()
5961
);
6062

6163
copy(
62-
__DIR__.'/react-stubs/Example.js',
63-
resource_path('js/components/Example.js')
64+
__DIR__.'/react-stubs/Example.jsx',
65+
resource_path('js/components/Example.jsx')
6466
);
6567
}
6668

@@ -73,4 +75,36 @@ protected static function updateBootstrapping()
7375
{
7476
copy(__DIR__.'/react-stubs/app.js', resource_path('js/app.js'));
7577
}
78+
79+
/**
80+
* Add Vite's React Refresh Runtime
81+
*
82+
* @return void
83+
*/
84+
protected static function addViteReactRefreshDirective()
85+
{
86+
$view = static::getViewPath('layouts/app.blade.php');
87+
88+
if (! file_exists($view)) {
89+
return;
90+
}
91+
92+
file_put_contents(
93+
$view,
94+
str_replace('@vite(', '@viteReactRefresh'.PHP_EOL.' @vite(', file_get_contents($view))
95+
);
96+
}
97+
98+
/**
99+
* Get full view path relative to the application's configured view path.
100+
*
101+
* @param string $path
102+
* @return string
103+
*/
104+
protected static function getViewPath($path)
105+
{
106+
return implode(DIRECTORY_SEPARATOR, [
107+
config('view.paths')[0] ?? resource_path('views'), $path,
108+
]);
109+
}
76110
}

Diff for: src/Presets/Vue.php

+7-7
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public static function install()
1616
{
1717
static::ensureComponentDirectoryExists();
1818
static::updatePackages();
19-
static::updateWebpackConfiguration();
19+
static::updateViteConfiguration();
2020
static::updateBootstrapping();
2121
static::updateComponent();
2222
static::removeNodeModules();
@@ -31,26 +31,26 @@ public static function install()
3131
protected static function updatePackageArray(array $packages)
3232
{
3333
return [
34+
'@vitejs/plugin-vue' => '^2.3.3',
3435
'resolve-url-loader' => '^3.1.2',
3536
'sass' => '^1.32.11',
36-
'sass-loader' => '^11.0.1',
37-
'vue' => '^2.6.12',
38-
'vue-template-compiler' => '^2.6.12',
37+
'vue' => '^3.2.37',
3938
] + Arr::except($packages, [
4039
'@babel/preset-react',
40+
'@vitejs/plugin-react',
4141
'react',
4242
'react-dom',
4343
]);
4444
}
4545

4646
/**
47-
* Update the Webpack configuration.
47+
* Update the Vite configuration.
4848
*
4949
* @return void
5050
*/
51-
protected static function updateWebpackConfiguration()
51+
protected static function updateViteConfiguration()
5252
{
53-
copy(__DIR__.'/vue-stubs/webpack.mix.js', base_path('webpack.mix.js'));
53+
copy(__DIR__.'/vue-stubs/vite.config.js', base_path('vite.config.js'));
5454
}
5555

5656
/**

Diff for: src/Presets/bootstrap-stubs/app.scss

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55
@import 'variables';
66

77
// Bootstrap
8-
@import '~bootstrap/scss/bootstrap';
8+
@import 'bootstrap/scss/bootstrap';

Diff for: src/Presets/bootstrap-stubs/bootstrap.js

+13-9
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
window._ = require('lodash');
1+
import _ from 'lodash';
2+
window._ = _;
23

3-
try {
4-
require('bootstrap');
5-
} catch (e) {}
4+
import 'bootstrap';
65

76
/**
87
* We'll load the axios HTTP library which allows us to easily issue requests
98
* to our Laravel back-end. This library automatically handles sending the
109
* CSRF token as a header based on the value of the "XSRF" token cookie.
1110
*/
1211

13-
window.axios = require('axios');
12+
import axios from 'axios';
13+
window.axios = axios;
1414

1515
window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
1616

@@ -22,11 +22,15 @@ window.axios.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
2222

2323
// import Echo from 'laravel-echo';
2424

25-
// window.Pusher = require('pusher-js');
25+
// import Pusher from 'pusher-js';
26+
// window.Pusher = Pusher;
2627

2728
// window.Echo = new Echo({
2829
// broadcaster: 'pusher',
29-
// key: process.env.MIX_PUSHER_APP_KEY,
30-
// cluster: process.env.MIX_PUSHER_APP_CLUSTER,
31-
// forceTLS: true
30+
// key: import.meta.env.VITE_PUSHER_APP_KEY,
31+
// wsHost: import.meta.env.VITE_PUSHER_HOST ?? `ws-${import.meta.env.VITE_PUSHER_CLUSTER}.pusher.com`,
32+
// wsPort: import.meta.env.VITE_PUSHER_PORT ?? 80,
33+
// wssPort: import.meta.env.VITE_PUSHER_PORT ?? 443,
34+
// forceTLS: (import.meta.env.VITE_PUSHER_SCHEME ?? 'https') === 'https',
35+
// enabledTransports: ['ws', 'wss'],
3236
// });

Diff for: src/Presets/bootstrap-stubs/vite.config.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { defineConfig } from 'vite';
2+
import laravel from 'laravel-vite-plugin';
3+
4+
export default defineConfig({
5+
plugins: [
6+
laravel([
7+
'resources/sass/app.scss',
8+
'resources/js/app.js',
9+
]),
10+
],
11+
});

Diff for: src/Presets/bootstrap-stubs/webpack.mix.js

-16
This file was deleted.
File renamed without changes.

Diff for: src/Presets/react-stubs/app.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
* building robust, powerful web applications using React + Laravel.
55
*/
66

7-
require('./bootstrap');
7+
import './bootstrap';
88

99
/**
1010
* Next, we will create a fresh React component instance and attach it to
1111
* the page. Then, you may begin adding components to this application
1212
* or customize the JavaScript scaffolding to fit your unique needs.
1313
*/
1414

15-
require('./components/Example');
15+
import './components/Example';

Diff for: src/Presets/react-stubs/vite.config.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { defineConfig } from 'vite';
2+
import laravel from 'laravel-vite-plugin';
3+
import react from '@vitejs/plugin-react';
4+
5+
export default defineConfig({
6+
plugins: [
7+
laravel([
8+
'resources/sass/app.scss',
9+
'resources/js/app.js',
10+
]),
11+
react(),
12+
],
13+
});

Diff for: src/Presets/react-stubs/webpack.mix.js

-16
This file was deleted.

0 commit comments

Comments
 (0)