├── .gitignore ├── composer.json ├── config └── tailwind.php ├── readme.md └── src ├── Facades └── Tailwind.php ├── LaravelTailwindConfigServiceProvider.php ├── Tailwind.php └── helpers.php /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor 2 | composer.phar 3 | composer.lock 4 | .DS_Store 5 | .idea/* -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "approvedio/laravel-tailwind-config", 3 | "description": "Get access to the tailwind config values in your Laravel Project", 4 | "license": "MIT", 5 | "authors": [ 6 | { 7 | "name": "Michael Boffey", 8 | "email": "michael@approved.io" 9 | } 10 | ], 11 | "require": { 12 | "php": ">=5.5.9", 13 | "illuminate/support": "5.0.* || 5.1.* || 5.2.* || 5.3.* || 5.4.* || 5.5.* || 5.6.* || 5.7.* || 5.8.*" 14 | }, 15 | "autoload": { 16 | "files": [ 17 | "src/helpers.php" 18 | ], 19 | "psr-4": { 20 | "ApprovedDigital\\LaravelTailwindConfig\\": "src/" 21 | } 22 | }, 23 | "extra": { 24 | "laravel": { 25 | "providers": [ 26 | "ApprovedDigital\\LaravelTailwindConfig\\LaravelTailwindConfigServiceProvider" 27 | ], 28 | "aliases": { 29 | "Tailwind": "ApprovedDigital\\LaravelTailwindConfig\\Facades\\Tailwind" 30 | } 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /config/tailwind.php: -------------------------------------------------------------------------------- 1 | base_path('tailwind.json'), 6 | ]; -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # Laravel Tailwind Config 2 | 3 | I've recently found myself using Tailwind more and more but have run into a few situations where I need to access tailwind config values within my blade templates. The most recent event occurred when building a admin section and i needed to access a color defined within the tailwind config file to pass to a charting library. Instead of hardcoding the value I decided to create this library 4 | 5 | ## Installation 6 | 7 | ```bash 8 | composer require approvedio/laravel-tailwind-config 9 | ``` 10 | 11 | ### Laravel 5.5+ 12 | 13 | The application service provider and facade will be automatically registered for you. 14 | 15 | ### Laravel 5.4 and Below 16 | 17 | Add the service provider to your app.php config file 18 | 19 | ``` 20 | ApprovedDigital\LaravelTailwindConfig\LaravelTailwindConfigServiceProvider::class, 21 | ``` 22 | 23 | Optionally you can add the facade to the Aliases section of your app.php config file 24 | 25 | ``` 26 | 'Tailwind' => ApprovedDigital\LaravelTailwindConfig\Facades\LaravelTailwindConfigFacade::class. 27 | ``` 28 | 29 | ## Usage 30 | 31 | You can use the facade 32 | 33 | ```php 34 | Tailwind::get('colors.red-light', '#FF0000'); 35 | ``` 36 | 37 | You can use the helper method 38 | 39 | ```php 40 | tailwind('colors.red-light', '#FF0000'); 41 | ``` 42 | 43 | ## Config 44 | 45 | By default we assume your tailwind config file is called tailwind.json in the root of your project. you can override this configuration by publishing the config and updating the path to your tailwind.json file. 46 | 47 | ```php 48 | 'cache_path' => base_path('tailwind.json'), 49 | ``` 50 | 51 | To generate the tailwind.json file from your config you will need to add the following Mix extension to your webpack.mix.js 52 | 53 | ```js 54 | mix.extend('exportTailwindConfig', function(webpackConfig, configPath = './tailwind.js') { 55 | let fs = require('fs'); 56 | let config = require(configPath); 57 | let json = JSON.stringify(config, null, 2); 58 | 59 | fs.writeFile('./tailwind.json', json); 60 | }); 61 | ``` 62 | 63 | And then call the following mix function to generate this file 64 | 65 | ```js 66 | mix.exportTailwindConfig('./tailwind.js'); 67 | ``` 68 | 69 | ##Future Development 70 | 71 | - Extract Tailwind Config Extractor into a dedicated package and less janky package 72 | -------------------------------------------------------------------------------- /src/Facades/Tailwind.php: -------------------------------------------------------------------------------- 1 | registerTailwindService(); 17 | 18 | if ($this->app->runningInConsole()) { 19 | $this->registerResources(); 20 | } 21 | } 22 | 23 | /** 24 | * Register currency provider. 25 | * 26 | * @return void 27 | */ 28 | public function registerTailwindService() 29 | { 30 | $this->app->singleton('tailwind', function ($app) { 31 | return new Tailwind( 32 | $app->config->get('tailwind', []) 33 | ); 34 | }); 35 | } 36 | 37 | /** 38 | * Register resources. 39 | * 40 | * @return void 41 | */ 42 | public function registerResources() 43 | { 44 | $this->mergeConfigFrom( 45 | __DIR__ . '/../config/tailwind.php', 'tailwind' 46 | ); 47 | 48 | $this->publishes([ 49 | __DIR__ . '/../config/tailwind.php' => config_path('tailwind.php'), 50 | ]); 51 | } 52 | } -------------------------------------------------------------------------------- /src/Tailwind.php: -------------------------------------------------------------------------------- 1 | config = $config; 15 | $this->tailwindValues = json_decode(file_get_contents($this->config('cache_path')), true); 16 | } 17 | 18 | public function get($key = null, $default = null) 19 | { 20 | return Arr::get($this->tailwindValues, $key, $default); 21 | } 22 | 23 | public function config($key, $default = null) 24 | { 25 | return Arr::get($this->config, $key, $default); 26 | } 27 | } -------------------------------------------------------------------------------- /src/helpers.php: -------------------------------------------------------------------------------- 1 | get($key, $default); 11 | } 12 | } 13 | 14 | --------------------------------------------------------------------------------