定制 millancore/vite-manifest 二次开发

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

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

millancore/vite-manifest

最新稳定版本:v0.1.2

Composer 安装命令:

composer require millancore/vite-manifest

包简介

PHP Vite Manifest Manager

README 文档

README

A framework-agnostic PHP library to integrate Vite with your PHP applications. It manages manifest parsing, tag generation, and asset preloading/prefetching.

Requirements

  • PHP 8.2+

Installation

composer require millancore/vite-manifest

Basic Usage

Setup

use Millancore\Vite\Vite;

$vite = new Vite();

// Configure build directory (default: 'build')
$vite->useBuildDirectory('dist');

// Configure manifest filename (default: 'manifest.json')
$vite->useManifestFilename('.vite/manifest.json');

Generating Tags

Use __invoke or toHtml() to generate <script> and <link> tags:

<!-- Single entry point -->
<?= $vite('resources/js/app.js') ?>

<!-- Multiple entry points -->
<?= $vite(['resources/js/app.js', 'resources/css/app.css']) ?>

<!-- Using withEntryPoints + toHtml -->
<?= $vite->withEntryPoints(['resources/js/app.js', 'resources/css/app.css'])->toHtml() ?>

Getting Asset URLs

// Get the URL for a specific asset
$url = $vite->asset('resources/js/app.js');

// Get the file contents of an asset
$content = $vite->content('resources/js/app.js');

Configuration

Hot Module Replacement (HMR)

The library automatically detects if Vite's HMR server is running by checking for a "hot" file.

// Set the path to the hot file (default: public_path('/hot'))
$vite->useHotFile(__DIR__ . '/public/hot');

// Check if HMR is active
if ($vite->isRunningHot()) {
    // ...
}

Content Security Policy (CSP) Nonce

Inject a nonce into all generated tags:

// Set a specific nonce
$vite->useCspNonce('your-random-nonce');

// Or generate a random one
$nonce = $vite->useCspNonce();

// Retrieve the current nonce
$vite->cspNonce();

Custom Asset Paths

If your assets are hosted on a CDN or a different path, define a custom resolver:

$vite->createAssetPathsUsing(function ($path, $secure) {
    return 'https://cdn.example.com/' . $path;
});

Subresource Integrity

By default the library looks for an integrity key in manifest chunks. You can change the key or disable it:

// Use a custom key
$vite->useIntegrityKey('sri-hash');

// Disable integrity checking
$vite->useIntegrityKey(false);

Entry Points

// Set entry points
$vite->withEntryPoints(['resources/js/app.js']);

// Merge additional entry points (deduplicates)
$vite->mergeEntryPoints(['resources/css/app.css']);

Manifest Hash

Get a unique hash of the current manifest, useful for cache busting:

$hash = $vite->manifestHash(); // returns null when hot or no manifest

Prefetching

Enable prefetching for dynamic imports to improve navigation performance.

Aggressive Strategy

Loads all prefetchable assets immediately:

$vite->useAggressivePrefetching();

Waterfall Strategy

Loads assets sequentially with a concurrency limit to avoid network congestion:

// Default concurrency of 3
$vite->useWaterfallPrefetching();

// Custom concurrency
$vite->useWaterfallPrefetching(concurrency: 5);

Shorthand

// Aggressive (no concurrency specified)
$vite->prefetch();

// Waterfall with concurrency
$vite->prefetch(concurrency: 3);

// Custom event trigger (default: 'load')
$vite->prefetch(event: 'DOMContentLoaded');

Tag Attributes

Add custom attributes to generated tags using arrays or callbacks:

// Script tags
$vite->useScriptTagAttributes(['defer' => true]);
$vite->useScriptTagAttributes(function ($src, $url, $chunk, $manifest) {
    return ['data-module' => 'true'];
});

// Style tags
$vite->useStyleTagAttributes(['data-theme' => 'dark']);

// Preload tags
$vite->usePreloadTagAttributes(['crossorigin' => 'anonymous']);

// Disable preloading for specific assets
$vite->usePreloadTagAttributes(function ($src, $url, $chunk, $manifest) {
    return str_contains($src, 'legacy') ? false : [];
});

React Refresh

Include the React Refresh runtime when using React with HMR:

<?= $vite->reactRefresh() ?>
<?= $vite('resources/js/app.jsx') ?>

Returns null when not in HMR mode.

License

MIT

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-09-23

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固