定制 ttbooking/vite-manager 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

ttbooking/vite-manager

Composer 安装命令:

composer require ttbooking/vite-manager

包简介

Vite Application Manager for Laravel.

README 文档

README

This package extends Vite functionality built in Laravel, so you can manage multiple entry point (hereafter app) configurations without clogging your Blade templates with clumsy configuration code. It is especially useful if your project have multiple JS entry points, or if you develop a package which publishes its own Vite managed assets.

Installation

You can install this package using Composer:

composer require ttbooking/vite-manager

Then you'll probably need to publish configuration file:

artisan vendor:publish --tag=vite-manager

Configuration

There are 3 ways to configure Vite "apps".

Config file

The most basic way. Use it if you develop a project with few entry points. Example config/vite.php file:

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | Default Vite App
    |--------------------------------------------------------------------------
    */

    'app' => env('VITE_APP', 'default'),

    'apps' => [

        'default' => [
            'entry_points' => ['vite/legacy-polyfills-legacy', 'resources/js/app-legacy.js', 'resources/js/app.js'],
            'build_directory' => 'build',
        ],

        'myapp' => [
            'entry_points' => ['vite/legacy-polyfills-legacy', 'src/app-legacy.js', 'src/app.js'],
            'build_directory' => 'build/packages/myapp',
            'hot_file' => 'build/packages/myapp/hot',
        ],

    ],

];

You may find complete list of configurable options here.

Note VITE_APP environment variable, which you can use to customize default entry point per environment:

# Your .env file
VITE_APP=custom

Service provider's boot method

Advanced method. Use it if you develop a package with Vite assets, or if you need fine-grained control over your app configuration:

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Str;
use TTBooking\ViteManager\Facades\Vite;

class ViteServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     */
    public function boot(): void
    {
        Vite::withEntryPoints(['vite/legacy-polyfills-legacy', 'resources/js/app-legacy.js', 'resources/js/app.js'])
            ->useBuildDirectory('build');

        Vite::app('myapp')
            ->withEntryPoints(['vite/legacy-polyfills-legacy', 'src/app-legacy.js', 'src/app.js'])
            ->useBuildDirectory('build/packages/myapp')
            ->useHotFile(public_path('build/packages/myapp/hot'))
            ->useScriptTagAttributes(function (string $src) {
                return Str::contains($src, '-legacy') ? ['type' => 'text/javascript', 'nomodule'] : [];
            })
            ->usePreloadTagAttributes(function (string $src) {
                return Str::contains($src, '-legacy') ? false : [];
            });
    }
}

You can use your AppServiceProvider.php (or other of your choice) for this matter, but it is recommended to use separate provider, especially if you have many entry point configurations. Just don't forget to register this service provider in your config/app.php file.

App factory

The most advanced method. Use it if you have many similar apps/workspaces to be configured alike:

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Str;
use TTBooking\ViteManager\Contracts\Vite as ViteContract;
use TTBooking\ViteManager\Facades\Vite;

class ViteServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     */
    public function boot(): void
    {
        Vite::useAppFactory(fn (ViteContract $vite, string $app, array $config) => $vite
            ->withEntryPoints(['vite/legacy-polyfills-legacy', 'src/app-legacy.js', 'src/app.js'])
            ->useBuildDirectory("build/packages/$app")
            ->useHotFile(public_path("build/packages/$app/hot"))
            ->useScriptTagAttributes(function (string $src) {
                return Str::contains($src, '-legacy') ? ['type' => 'text/javascript', 'nomodule'] : [];
            })
            ->usePreloadTagAttributes(function (string $src) {
                return Str::contains($src, '-legacy') ? false : [];
            })
            ->configure($config)
        );
    }
}

Closure passed into useAppFactory retrieves 4 arguments: a Vite class instance to be configured by the factory, app name, corresponding configuration section (if exists), and application's container instance. You may (or may not) want to amend instance's configuration by the options from config file. To do this, call configure method on Vite instance with the given $config variable. This way, end user could change predefined options on per-app basis.

Usage

@viteApp directive should be used in Blade templates:

{{-- Default entry point --}}
@viteApp

{{-- Example app entry point --}}
@viteApp('myapp')

{{-- Example app with additional entry point --}}
@viteApp('myapp', "src/Pages/{$page['component']}.vue")

If you are using template engine other than Blade, you can use vite_app helper function:

{# Default entry point #}
{{ vite_app() }}

{# Example app entry point #}
{{ vite_app('myapp') }}

{# Example app with additional entry point #}
vite_app('myapp', 'src/Pages/' ~ page.component ~ '.vue')

If you are using TwigBridge, don't forget to register this function in config/twigbridge.php file under extensions.functions section.

ttbooking/vite-manager 适用场景与选型建议

ttbooking/vite-manager 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.27k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2023 年 12 月 20 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「manager」 「laravel」 「vite」 「vite-manager」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 ttbooking/vite-manager 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 ttbooking/vite-manager 我们能提供哪些服务?
定制开发 / 二次开发

基于 ttbooking/vite-manager 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-12-20