leuverink/asset-injector 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

leuverink/asset-injector

最新稳定版本:3.0.1

Composer 安装命令:

composer require leuverink/asset-injector

包简介

Automatically inject your package assets onto the page

README 文档

README

codestyle tests

Simplify your Laravel package development with automatic asset injection.

This package allows you to seamlessly insert JavaScript and CSS into web responses without requiring manual inclusion by your package users 🚀

Installation

Install the package via Composer:

composer require leuverink/asset-injector

Usage

  1. After installing, you'll need to create a AssetInjector implementation.
namespace YourPackage;

use Leuverink\AssetInjector\Contracts\AssetInjector;


class InjectAssets implements AssetInjector
{
    // Used to identify your assets in the HTML response
    public function identifier(): string
    {
        return 'MY_PACKAGE';
    }

    // You can opt in to asset injection by implementing your own checks.
    // For example if a package user can control this via config file, or when your end user hits a middleware
    public function enabled(): bool
    {
        return true;
    }

    // Will inject return value in head tag or before html close if no head is present
    public function inject(): string
    {
        $js = file_get_contents(__DIR__ . '/../build/my-package.js');
        $css = file_get_contents(__DIR__ . '/../build/my-package.css');

        return <<< HTML
        <script type="module">{$js}</script>
        <style>{$css}</style>
        HTML;
    }
}
  1. Register the implementation in your package's Service Provider.
namespace YourPackage;

use Illuminate\Support\ServiceProvider as BaseServiceProvider;
use Leuverink\AssetInjector\AssetManager;
use YourPackage\InjectAssets;

class ServiceProvider extends BaseServiceProvider
{
    public function boot()
    {
        AssetManager::register(new InjectAssets);
    }
}

How It Works

  • Assets are automatically included in full-page responses (not partial HTML responses).
  • If a <head> tag is present, assets are injected there. Otherwise, they're inserted before the closing </html> tag.
  • The identifier() method helps prevent duplicate asset injection.
  • Use the enabled() method to implement conditional asset injection based on your package's configuration.
  • Customize the injected content by modifying the inject() method.

Development

Use these commands for development:

composer lint # run all linters
composer fix # run all fixers

composer analyze # run static analysis
composer baseline # generate static analysis baseline

composer test # run test suite

License

This package is open-source software licensed under the MIT license.

统计信息

  • 总下载量: 4.15k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 6
  • 点击次数: 1
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 6
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-08-19