Skip to content

Commit 818c92f

Browse files
committed
Use vite-plugin-kirby, simplify helpers
1 parent 0579cbc commit 818c92f

File tree

12 files changed

+481
-470
lines changed

12 files changed

+481
-470
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
.DS_Store
22
node_modules/
33
.phpunit.result.cache
4+
.dev
45

56
# Frontend
67
/public/assets

README.md

Lines changed: 24 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ Then inside your template files (or anywhere else) you can use the helper functi
1818
```php
1919
<html>
2020
<head>
21-
<?= vite()->client() ?>
2221
<?= vite()->css() ?>
2322
</head>
2423
<body>
@@ -37,35 +36,30 @@ If you want use the plugin without one of the starter kits, you can add it to yo
3736
composer require arnoson/kirby-vite
3837
```
3938

39+
```
40+
npm install vite-plugin-kirby
41+
```
42+
4043
### Folder structure
4144

4245
Make sure you use a modern [public folder structure](https://getkirby.com/docs/guide/configuration#custom-folder-setup__public-folder-setup).
4346

4447
### Development VS Production modes
4548

46-
In development, files are loaded from Vite dev server. In production, files are injected based on the `manifest.json` file generated by Vite.
49+
In development, files are loaded from Vite's dev server. In production, files are injected based on the `manifest.json` file generated by Vite.
4750

48-
Kirby-Vite use a file named `.lock` in the `rootDir` (see [Options](#options) below) to dermine which mode to use:
51+
kirby-vite uses a file named `.dev` (created automatically by vite-plugin-kirby) to determine which mode to use:
4952

5053
- when the file exists, it will run in development mode
5154
- when the file doesn’t exists, it will run in production mode
5255

53-
For an example, have a look at the scripts in the `package.json`.
54-
55-
Alternatively, you can use the `dev` option below to alter this behavior.
56-
57-
### Static assets
58-
59-
Ensure that Vite loads assets like images and fonts from its own server in development by setting `server.origin` to match Vite server host and port:
60-
6156
```js
57+
import kirby from 'vite-plugin-kirby'
58+
6259
// vite.config.js
6360
export default {
6461
// ...
65-
server: {
66-
// ...
67-
origin: 'http://localhost:3000',
68-
}
62+
plugins: [kirby()]
6963
}
7064
```
7165

@@ -76,29 +70,20 @@ See `vite.config.js` for a complete example.
7670
```php
7771
'arnoson.kirby-vite' => [
7872
// The default entry that is used when calling `vite()->js()`
79-
// Note: this has to match vite config's `build.rollupOptions.input`
73+
// Note: this has to match Vite config's `build.rollupOptions.input`
8074
'entry' => 'index.js',
8175

82-
// The source directory for assets served by Vite
83-
// Note: this has to match vite config's `root`
84-
'rootDir' => '/src',
85-
8676
// Wether or not to output legacy bundles automatically (see: Legacy build)
8777
'legacy' => false,
8878

8979
// The output directory for the production assets (js, css, fonts, ...)
90-
// Note: this has to match vite config's `base` and `build.outDir`
80+
// Note: this has to match Vite config's `base` and `build.outDir`
9181
'outDir' => 'dist',
9282

9383
// Wether or not to use modern es modules
9484
// Note: if you want to support older browsers you can still enabled es
9585
// modules but enabled the `legacy` option
96-
'module' => true,
97-
98-
// Should be `true` in development to inject Vite client and load assets from
99-
// Vite server; and `false` in production to load them from the manifest.json
100-
// This will override the behavior based on the `.lock` file described above.
101-
'dev' => true,
86+
'module' => true
10287
]
10388
```
10489

@@ -111,37 +96,36 @@ Therefore add the [@vitejs/plugin-legacy](https://github.com/vitejs/vite/tree/ma
11196
'arnoson.kirby-vite.legacy' => true
11297
```
11398

114-
Now call kirby-vite's `js()` helper as usual and make sure to add the legacy polyfills:
99+
Now call kirby-vite's `js()` helper as usual.
115100

116101
```php
117102
<!-- your template -->
118103
<?= vite()->js() ?>
119-
<?= vite()->legacyPolyfills() ?>
120104
```
121105

122106
which will render:
123107

124108
```html
125109
<script
126-
src="http://your-website.org/dist/assets/index.[hash].js"
127-
type="module"
110+
src="https://your-website.org/dist/assets/polyfills-legacy.[hash].js"
111+
nomodule=""
128112
></script>
129113
<script
130-
src="http://your-website.org/dist/assets/index-legacy.[hash].js"
114+
src="https://your-website.org/dist/assets/index-legacy.[hash].js"
131115
nomodule=""
132116
></script>
133117
<script
134-
src="http://your-website.org/dist/assets/polyfills-legacy.[hash].js"
135-
nomodule=""
118+
src="https://your-website.org/dist/assets/index.[hash].js"
119+
type="module"
136120
></script>
137121
```
138122

139-
If you want to have more control over where the legacy files are rendered, disable `arnoson.kirby-vite.legacy` and use kirby-vite's legacy helpers manually:
123+
If you want to have more control over where the legacy files are rendered, disable `arnoson.kirby-vite.legacy` and use Kirby-Vite's legacy helpers manually:
140124

141125
```php
142-
<?= vite()->js() ?>
143-
<?= vite()->legacyJs() ?>
144126
<?= vite()->legacyPolyfills() ?>
127+
<?= vite()->legacyJs() ?>
128+
<?= vite()->js() ?>
145129
```
146130

147131
### Known issue
@@ -150,22 +134,9 @@ If you want to have more control over where the legacy files are rendered, disab
150134

151135
## Watch php files
152136

153-
If you also want to live reload your site in development mode whenever you change something in kirby, install [vite-plugin-live-reload](https://github.com/arnoson/vite-plugin-live-reload). Then adjust your `vite.config.js`:
154-
155-
```js
156-
// vite.config.js
157-
import liveReload from 'vite-plugin-live-reload'
158-
159-
export default {
160-
// ...
161-
plugins: [
162-
liveReload(
163-
'../content/**/*',
164-
'../public/site/(templates|snippets|controllers|models)/**/*.php'
165-
),
166-
],
167-
}
168-
```
137+
vite-plugin-kirby automatically uses vite-plugin-live-reload to watch the following paths:
138+
- `site/(templates|snippets|controllers|models|layouts)/**/*.php`
139+
- `content/**/*`
169140

170141
## Credits
171142

0 commit comments

Comments
 (0)