theokbokki/blade-sfc
Composer 安装命令:
composer require theokbokki/blade-sfc
包简介
A laravel package that adds blade directives for writing css, scss, javascript and typescript directly in your blade views.
README 文档
README
I really like the way javascript frameworks like Vue or Svelte structure their templates, it looks sort of like this:
<script> document.querySelector(".my-component").innerText = "Maybe star this repo ?" </script> <div class="my-component"></div> <style> .my-component { background: red; } </style>
But I hate using javascript for dealing with backend stuff so I use Laravel.
And that's why I created blade directives to use Blade components like you would Svelte or Vue ones. (You can read more about it here)
@javascript() <script> document.querySelector(".my-component").innerText = "Maybe star this repo ?" </script> @endjavascript <div class="my-component"></div> @css() <style> .my-component { background: red; } </style> @endcss
And you could also use SCSS or Typescript
@javascript('/js/my-file.ts') <script> let starRepo: boolean; starRepo = true; console.log(starRepo); </script> @endjavascript @css('/css/my-file.scss') <style> $color: red; .profile-picture { color: $color; } </style> @endcss
Requirements
The blade-sfc package requires PHP 8.0+, Laravel 9+.
Installing
You can install the package via composer:
composer require theokbokki/blade-sfc
Then add BladeSfcServiceProvider to your list of service providers in bootstrap/app.php:
return [ App\Providers\AppServiceProvider::class, Theokbokki\BladeSfc\BladeSfcServiceProvider::class, ];
Using
Css rules
The @css()...@endcss rules work as follow:
@css('/optional/pathname.css') <style> // Your CSS </style> @endcss
If a pathname is provided, the code will try to find or create the file in the resource_path() directory.
If no pathname is provided, the code will be added to the file defined in the config (/resources/css/generated.css by default).
You can then import it in your main CSS entry point.
The <style> tags are optional, you can add them for better syntax highlighting.
JS rules
The @javascript()...@endjavascript rules work as follow:
@javascript('/optional/pathname.js') <script> // Your JS </script> @endjavascript
If a pathname is provided, the code will try to find or create the file in the resource_path() directory.
If no pathname is provided, the code will be added to the file defined in the config (/resources/js/generated.js by default).
You can then import it in your main JS entry point.
The <script> tags are optional, you can add them for better syntax highlighting.
The blade-sfc:compile command
This command is used to parse the blade files and put the JS and CSS content into the correct files. It's used like so:
php artisan blade-sfc:compile npm run build
If you want to avoid running it manually, you can use vite-plugin-run. Here's how to modify your vite config:
import { defineConfig } from 'vite'; import laravel from 'laravel-vite-plugin'; import { run } from 'vite-plugin-run' export default defineConfig({ plugins: [ laravel({ input: ['resources/css/app.css', 'resources/js/app.js'], // Or whatever your CSS and JS files are refresh: true, }), run([ { , name: 'compile views', run: ['php', 'artisan', 'blade-sfc:compile'], condition: (file) => file.includes('.blade.php'), }, ]), ] });
Configuration
You can publish the package's configuration like so:
php artisan vendor:publish --tag=blade-sfc-config
The configuration file allows you to choose where you want to output your JS and CSS by default.
Future improvements
- Allow for blade statements to be used inside JS and CSS. (Is currently a problem because of unknown variables at render time).
theokbokki/blade-sfc 适用场景与选型建议
theokbokki/blade-sfc 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 14 次下载、GitHub Stars 达 4, 最近一次更新时间为 2024 年 09 月 19 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 theokbokki/blade-sfc 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 theokbokki/blade-sfc 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 14
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 21
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-09-19