├── .gitignore ├── .prettierrc ├── resources └── views │ └── font-face.blade.php ├── .editorconfig ├── src ├── Facades │ └── Webfonts.php ├── WebfontsServiceProvider.php ├── PreloadWebfonts.php ├── Webfonts.php └── Console │ └── Commands │ └── WebfontsAddCommand.php ├── composer.json ├── LICENSE.md └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor 2 | composer.lock 3 | -------------------------------------------------------------------------------- /.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "semi": false, 3 | "singleQuote": true, 4 | "trailingComma": "es5" 5 | } 6 | -------------------------------------------------------------------------------- /resources/views/font-face.blade.php: -------------------------------------------------------------------------------- 1 | @props(['name', 'weight', 'style', 'path']) 2 | 3 | @font-face { 4 | font-display: swap; 5 | font-family: '{{ $name }}'; 6 | font-style: {{ $style }}; 7 | font-weight: {{ $weight }}; 8 | src: url('{{ $path }}') format('woff2'); 9 | } 10 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | charset = utf-8 5 | end_of_line = lf 6 | insert_final_newline = true 7 | indent_style = space 8 | indent_size = 2 9 | trim_trailing_whitespace = true 10 | 11 | [*.md] 12 | trim_trailing_whitespace = false 13 | 14 | [*.php] 15 | indent_size = 4 16 | -------------------------------------------------------------------------------- /src/Facades/Webfonts.php: -------------------------------------------------------------------------------- 1 | =8.1", 20 | "laravel/prompts": "^0.1|^0.2|^0.3", 21 | "guzzlehttp/guzzle": "^7.8" 22 | }, 23 | "require-dev": { 24 | "laravel/pint": "^1.13", 25 | "illuminate/support": "^10.0|^11.0|^12.0", 26 | "illuminate/console": "^10.0|^11.0|^12.0", 27 | "illuminate/http": "^10.0|^11.0|^12.0" 28 | }, 29 | "extra": { 30 | "laravel": { 31 | "providers": [ 32 | "Log1x\\LaravelWebfonts\\WebfontsServiceProvider" 33 | ] 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/WebfontsServiceProvider.php: -------------------------------------------------------------------------------- 1 | app->singleton('laravel-webfonts', fn () => Webfonts::make($this->app)); 19 | } 20 | 21 | /** 22 | * Bootstrap any application services. 23 | * 24 | * @return void 25 | */ 26 | public function boot() 27 | { 28 | if ($this->app->runningInConsole()) { 29 | $this->commands(WebfontsAddCommand::class); 30 | } 31 | 32 | $this->loadViewsFrom(__DIR__.'/../resources/views', 'laravel-webfonts'); 33 | 34 | Blade::directive('preloadFonts', fn () => "preload()->build(); ?>"); 35 | 36 | $this->app->make('laravel-webfonts')->handle(); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Brandon Nifong 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /src/PreloadWebfonts.php: -------------------------------------------------------------------------------- 1 | webfonts = $webfonts; 23 | } 24 | 25 | /** 26 | * Make a new instance of Preload Fonts. 27 | */ 28 | public static function make(Webfonts $webfonts): self 29 | { 30 | return new static($webfonts); 31 | } 32 | 33 | /** 34 | * Build the font preload markup. 35 | */ 36 | public function build(): ?string 37 | { 38 | if ($this->markup) { 39 | return $this->markup; 40 | } 41 | 42 | if (! $fonts = $this->webfonts()->fonts()) { 43 | return null; 44 | } 45 | 46 | return $this->markup = collect($fonts) 47 | ->map(fn ($font) => $this->asset($font)) 48 | ->map(fn ($font) => "") 49 | ->implode("\n"); 50 | } 51 | 52 | /** 53 | * Retrieve the asset URL. 54 | */ 55 | protected function asset(string $font): string 56 | { 57 | return function_exists('\Roots\asset') 58 | ? \Roots\asset($font) 59 | : asset("build/{$font}"); 60 | } 61 | 62 | /** 63 | * Retrieve the Webfonts instance. 64 | */ 65 | protected function webfonts(): Webfonts 66 | { 67 | return $this->webfonts; 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/Webfonts.php: -------------------------------------------------------------------------------- 1 | fonts = $this->fonts(); 45 | $this->preload = PreloadWebfonts::make($this); 46 | } 47 | 48 | /** 49 | * Make a new instance of Webfonts. 50 | */ 51 | public static function make(): self 52 | { 53 | return new static; 54 | } 55 | 56 | /** 57 | * Run the Webfonts handlers. 58 | */ 59 | public function handle(): self 60 | { 61 | $this->handleWordPress(); 62 | 63 | return $this; 64 | } 65 | 66 | /** 67 | * Retrieve the Preload Webfonts instance. 68 | */ 69 | public function preload(): PreloadWebfonts 70 | { 71 | return $this->preload; 72 | } 73 | 74 | /** 75 | * Set the fonts to exclude. 76 | */ 77 | public static function except(array $except): void 78 | { 79 | static::$except = [...static::$except, ...$except]; 80 | } 81 | 82 | /** 83 | * Set the fonts to allow. 84 | */ 85 | public static function only(array $only): void 86 | { 87 | static::$only = [...static::$only, ...$only]; 88 | } 89 | 90 | /** 91 | * Retrieve the fonts from the manifest. 92 | */ 93 | public function fonts(array $except = [], array $only = []): array 94 | { 95 | if ($this->fonts) { 96 | return $this->fonts; 97 | } 98 | 99 | $except = [...static::$except, ...$except]; 100 | $only = [...static::$only, ...$only]; 101 | 102 | return collect($this->manifest()) 103 | ->filter(fn ($value, $key) => Str::endsWith($key, '.woff2')) 104 | ->filter(fn ($value, $key) => blank($only) || in_array(basename($key), $only) || in_array(basename($key, '.woff2'), $only)) 105 | ->reject(fn ($value, $key) => in_array(basename($key), $except) || in_array(basename($key, '.woff2'), $except)) 106 | ->all(); 107 | } 108 | 109 | /** 110 | * Handle the font preload markup for WordPress. 111 | */ 112 | protected function handleWordPress(): void 113 | { 114 | if ($this->wordpress || ! $this->isWordPress() || ! $this->fonts()) { 115 | return; 116 | } 117 | 118 | add_filter('wp_head', function () { 119 | if (! $markup = $this->preload()->build()) { 120 | return; 121 | } 122 | 123 | echo "{$markup}\n"; 124 | }, 5); 125 | 126 | $this->wordpress = true; 127 | } 128 | 129 | /** 130 | * Retrieve the asset manifest. 131 | */ 132 | protected function manifest(): array 133 | { 134 | return $this->manifest ??= $this->viteManifest(); 135 | } 136 | 137 | /** 138 | * Retrieve the Vite manifest. 139 | */ 140 | protected function viteManifest(): array 141 | { 142 | if (! file_exists($manifest = public_path('build/manifest.json'))) { 143 | return []; 144 | } 145 | 146 | $manifest = json_decode(file_get_contents($manifest), true); 147 | 148 | return collect($manifest) 149 | ->map(fn ($value, $key) => $value['file']) 150 | ->all(); 151 | } 152 | 153 | /** 154 | * Determine if the application is running WordPress. 155 | */ 156 | protected function isWordPress(): bool 157 | { 158 | return class_exists('\WP') && function_exists('\add_filter'); 159 | } 160 | } 161 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Laravel Webfonts 2 | 3 |  4 |  5 |  6 | 7 | Laravel Webfonts allows you to easily download, install, and preload over 1500 Google fonts locally in your Laravel project. 8 | 9 |  10 | 11 | ## Features 12 | 13 | - 🔍️ Search and install over 1500 Google fonts from the public [google-webfonts-helper](https://github.com/majodev/google-webfonts-helper) API. 14 | - ⚡️ Automatically generate `@font-face` CSS `at-rules` when installing fonts using CLI. 15 | - 🧑💻 Supports [Vite](https://vitejs.dev/) out of the box with zero configuration. 16 | - ⚡️ Provides an easy-to-use `@preloadFonts` Blade directive to preload fonts found in the Vite manifest. 17 | - 🚀 Automatically injects font preload markup into `wp_head` on WordPress sites running [Acorn](https://github.com/roots/acorn). 18 | 19 | ## Requirements 20 | 21 | - [PHP](https://secure.php.net/manual/en/install.php) >= 8.1 22 | - [Composer](https://getcomposer.org/download/) 23 | - [Laravel](https://github.com/laravel/laravel) >= 10.0 24 | 25 | ## Installation 26 | 27 | Install via Composer: 28 | 29 | ```sh 30 | $ composer require log1x/laravel-webfonts 31 | ``` 32 | 33 | ## Usage 34 | 35 | If you already have fonts locally installed in your project, skip to [Preloading Fonts](#preloading-fonts). 36 | 37 | ### Adding Fonts 38 | 39 | Laravel Webfonts provides a very easy way to install new webfonts to your project using command line: 40 | 41 | ```sh 42 | artisan webfonts:add 43 | ``` 44 | 45 | By default, installing a font will trigger the following things to happen: 46 | 47 | - Download the font archive to a temporary directory in local storage. 48 | - Extract the font archive. 49 | - Move downloaded fonts to `resources/fonts`. 50 | - Clean up the temporary directory. 51 | - Generate and prepend `@font-face` at-rules to a `fonts` stylesheet. 52 | 53 | The fonts stylesheet will reside at the root of your stylesheet directory located in `resources/`. If the font stylesheet does not already exist, it will be created using the most common stylesheet extension (css, scss, ...) found among your styles. 54 | 55 | By default, the `resources/css` and `resources/styles` directories are automatically scanned for existing files to find the appropriate place to write the fonts stylesheet. 56 | 57 | The generated `@font-face` at-rules will look like this: 58 | 59 | ```css 60 | @font-face { 61 | font-display: swap; 62 | font-family: 'Roboto'; 63 | font-style: normal; 64 | font-weight: 400; 65 | src: url('../fonts/roboto-v30-latin-regular.woff2') format('woff2'); 66 | } 67 | 68 | @font-face { 69 | font-display: swap; 70 | font-family: 'Roboto'; 71 | font-style: italic; 72 | font-weight: 400; 73 | src: url('../fonts/roboto-v30-latin-italic.woff2') format('woff2'); 74 | } 75 | ``` 76 | 77 | Adding additional fonts will cause them to be prepended to the existing `fonts` stylesheet. 78 | 79 | ### Importing Fonts 80 | 81 | When fonts are installed for the first time, a `fonts` stylesheet is created in your project's stylesheet folder. In a vanilla Laravel project, this is typically `resources/css/fonts.css`. 82 | 83 | You must import the generated `fonts` file into your project's primary stylesheet (e.g. `app.css`). If you're using Tailwind, it would look something like: 84 | 85 | ```css 86 | @import 'fonts'; 87 | 88 | @tailwind base; 89 | @tailwind components; 90 | @tailwind utilities; 91 | ``` 92 | 93 | ### Preloading Fonts 94 | 95 | > [!NOTE] 96 | > If you are using WordPress alongside [Acorn](https://github.com/roots/acorn), you can ignore this section as preloading is automatically handled for you inside of `wp_head` if an asset manifest containing valid fonts is detected. 97 | 98 | Laravel Webfonts primary functionality while in production is to provide a simple way to preload your locally hosted webfonts. 99 | 100 | This is done by reading the compiled `woff2` fonts from your Vite manifest and generating the appropriate markup for you to place inside of `
`. 101 | 102 | In most cases, you can simply use the `@preloadFonts` Blade directive to handle building and echoing the font preload HTML markup. 103 | 104 | Alternatively to the Blade directive, you can access the `PreloadFonts` class directly using the `Webfonts` Facade: 105 | 106 | ```php 107 | use Log1x\LaravelWebfonts\Facades\Webfonts; 108 | 109 | // Retrieve an array of compiled font paths. 110 | $fonts = Webfonts::fonts(); 111 | 112 | // Build the font preload HTML markup. 113 | $html = Webfonts::preload()->build(); 114 | ``` 115 | 116 | Allowing/excluding certain fonts from being preloaded can be done inside `register()` of a service provider: 117 | 118 | ```php 119 | use Log1x\LaravelWebfonts\Webfonts; 120 | 121 | // Allow specific fonts. 122 | Webfonts::only(['inter-v13-latin-regular']); 123 | 124 | // Exclude specific fonts. 125 | Webfonts::except(['inter-v13-latin-500']); 126 | ``` 127 | 128 | ## Bug Reports 129 | 130 | If you discover a bug in Laravel Webfonts, please [open an issue](https://github.com/log1x/laravel-webfonts/issues). 131 | 132 | ## Contributing 133 | 134 | Contributing whether it be through PRs, reporting an issue, or suggesting an idea is encouraged and appreciated. 135 | 136 | ## License 137 | 138 | Laravel Webfonts is provided under the [MIT License](LICENSE.md). 139 | -------------------------------------------------------------------------------- /src/Console/Commands/WebfontsAddCommand.php: -------------------------------------------------------------------------------- 1 | option('clear-cache')) { 101 | cache()->forget($this->cache); 102 | } 103 | 104 | $this->fonts = spin( 105 | fn () => $this->fonts(), 106 | 'Fetching fonts from the