|
| 1 | +# Managing and Compiling Assets |
| 2 | + |
| 3 | +## Intro |
| 4 | +Managing and compiling assets is a very common task in web development. Unfortunately, it's rarely fun. |
| 5 | + |
| 6 | +With hyde, **you don't have to do it**, in fact, you can skip this entire page if you are happy with how it is. |
| 7 | +But as always with Hyde, you can customize everything if you want to. |
| 8 | + |
| 9 | +Hyde ships with a complete frontend where base styles and scripts are included through [HydeFront](https://github.com/hydephp/hydefront) which adds accessibility and mobile support as well as interactions for dark mode switching and navigation and sidebar interactions. |
| 10 | + |
| 11 | +HydeFront is split into two files, `hyde.css` and `hyde.js`. These are loaded in the default Blade views using the [jsDelivr CDN](https://www.jsdelivr.com/package/npm/hydefront). This is the recommended way to load the base styles as the [Hyde Framework](https://github.com/hydephp/framework) automatically makes sure that the correct HydeFront version for the current version of Hyde is loaded. You can disable the CDN in the `config/hyde.php` file by setting `'loadHydeAssetsUsingCDN'` to `false`. |
| 12 | + |
| 13 | +The bulk of the frontend is built with [TailwindCSS](https://tailwindcss.com/). To get you started, when installing Hyde, all the Tailwind styles you need come precompiled and minified into `_media/app.css`. |
| 14 | + |
| 15 | +## Some extra information, and answers to possible questions |
| 16 | + |
| 17 | +### Do I have to use NPM to use Hyde? |
| 18 | +No. NPM is optional as all the compiled styles you need are already installed. You only need NPM if you want to compile your own styles. |
| 19 | + |
| 20 | +### When do I need to compile assets? |
| 21 | + |
| 22 | +#### When customizing |
| 23 | +If you want to customize the Tailwind settings or add custom styles, you will need to take care of compiling the styles yourself. |
| 24 | + |
| 25 | +#### When adding new classes |
| 26 | +The `_media/app.css` file that comes with Hyde contains TailwindCSS for all classes that are used in the default Blade views, but nothing more. |
| 27 | + |
| 28 | +If you customize the Blade views and add new classes, or if you add new classes in Blade-based pages, you may need to compile the assets yourself to get the new styles. |
| 29 | + |
| 30 | +If you stick to using Markdown based pages, you don't need to compile anything. |
| 31 | + |
| 32 | +## How are assets stored and managed? |
| 33 | + |
| 34 | +Currently, the frontend assets are separated into three places. |
| 35 | + |
| 36 | +The `resources/assets` contains **source** files, meaning files that will be compiled into something else. Here you will find the `app.css` file that bootstraps the TailwindCSS styles. This file is also an excellent place to add your custom styles. |
| 37 | + |
| 38 | +The `_media` folder contains **compiled** (and usually minified) files. When Hyde compiles your static site, all asset files here will get copied as they are into the `_site/media` folder. |
| 39 | + |
| 40 | +The `_site/media` folder contains the files that are served to the user. |
| 41 | + |
| 42 | +### What is the difference between `_media` and `_site/media`? |
| 43 | +It may seem weird to have two folders for storing the compiled assets, but it is quite useful. |
| 44 | + |
| 45 | +The `_site` directory is intended to be excluded from version control while the `_media` folder is included in the version control, though you may choose to exclude the compiled files from the `_media` folder if you want to. |
| 46 | + |
| 47 | +## How do I compile assets? |
| 48 | + |
| 49 | +First, make sure that you have installed all the NodeJS dependencies using `npm install`. |
| 50 | +Then run `npm run dev` to compile the assets. If you want to compile the assets for production, run `npm run prod`. |
| 51 | +You can also run `npm run watch` to watch for changes in the source files and recompile the assets automatically. |
| 52 | + |
| 53 | +### How does it work? |
| 54 | + |
| 55 | +Hyde uses [Laravel Mix](https://laravel-mix.com/) (which is a wrapper for [webpack](https://webpack.js.org/)) to compile the assets. |
| 56 | + |
| 57 | +When running the `npm run dev/prod` command, the following happens: |
| 58 | + |
| 59 | +1. Laravel Mix will compile the `resources/assets/app.css` file into `_media/app.css` using PostCSS with TailwindCSS and AutoPrefixer. |
| 60 | +2. Mix then copies the `_media` folder into `_site/media`, this is so that they are automatically accessible to your site without having to rerun `php hyde build`. |
| 61 | + |
| 62 | + |
| 63 | + |
| 64 | +## Managing images |
| 65 | +As mentioned above, assets stored in the _media folder are automatically copied to the _site/media folder, making it the recommended place to store images. You can then easily reference them in your Markdown files. |
0 commit comments