承接 userfrosting/vite-php-twig 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

userfrosting/vite-php-twig

Composer 安装命令:

composer require userfrosting/vite-php-twig

包简介

Vite function for Twig templates

关键字:

README 文档

README

Version PHP Version License Build Codecov StyleCI PHPStan Donate

Vite Manifest function for PHP & Twig Templates. Allows Vite manifest integration in PHP & Twig Templates without Symfony. Optimized for PHP-DI style containers.

Inspired by kellerkinderDE/vite-encore-bundle & PHP-Vite.

Installation

composer require userfrosting/vite-php-twig

Documentation & Usage

Using standalone

$manifest = new ViteManifest('.vite/manifest.json');

// Get files for `views/foo.js` entry
$manifest->getScripts('views/foo.js'); // Scripts
$manifest->getStyles('views/foo.js'); // Styles
$manifest->getImports('views/foo.js'); // Preloads

// Render HTML tags for `views/foo.js` entry
$manifest->renderScripts('views/foo.js');      // Scripts
$manifest->renderStyles('views/foo.js');       // Styles
$manifest->renderPreloads('views/foo.js');     // JS module preloads
$manifest->renderStylePreloads('views/foo.js'); // CSS preload hints

// If you have multiple entry point scripts on the same page, you should pass them in a single call to avoid duplicates - for example:
$manifest->getScripts('views/foo.js', 'views/bar.js');

Tip

ViteManifest implements \UserFrosting\ViteTwig\ViteManifestInterface if you prefer to type-hint against interfaces, for use with dependency injection.

Using with Twig

Important

Requires Twig 3 or newer

Vite writes a manifest.json file that contains all of the files needed for each Rollup Input or "entry". To reference entries in Twig, you need to add the ViteTwigExtension extension to the Twig Environment. This accepts a ViteManifest, which itself accepts the path to the manifest.json.

use UserFrosting\ViteTwig\ViteManifest;
use UserFrosting\ViteTwig\ViteTwigExtension;
use Twig\Environment;
use Twig\Loader\FilesystemLoader;

$manifest = new ViteManifest('.vite/manifest.json');
$extension = new ViteTwigExtension($manifest);

// Create Twig Environment and add extension
$loader = new FilesystemLoader('./path/to/templates');
$twig = new Environment($loader);
$twig->addExtension($extension);

Now, to render all of the script and link tags for a specific "entry" (e.g. views/foo.js), you can:

{{ vite_js('views/foo.js') }}
{{ vite_css('views/foo.js') }}
{{ vite_preload('views/foo.js') }}
{{ vite_css_preload('views/foo.js') }}
  • vite_js — renders <script type="module"> tags for the entry's JS files.
  • vite_css — renders <link rel="stylesheet"> tags for the entry's CSS files.
  • vite_preload — renders <link rel="modulepreload"> hints for eagerly fetching imported JS chunks.
  • vite_css_preload — renders <link rel="preload" as="style"> hints so the browser discovers CSS files earlier, even though vite_css already adds the <link rel="stylesheet"> tags.

If you have multiple entry point scripts on the same page, you should pass them in a single call to avoid duplicates - for example:

{{ vite_js('views/foo.js', 'views/bar.js') }}

Standalone CSS, SCSS, SASS, and LESS files can also be rendered directly, as long as they are defined in your Vite config as a rollup input.

{{ vite_css('my_theme.less') }}

Vite Default Port

By default, Vite will use port 5173. However, if the port is already being used, Vite will automatically try the next available port so this may not be the actual port the server ends up listening on. Since a PHP application doesn't know which port is being used by Vite, the port can be forced in the vite.config.js file inside your project's root directory using server.strictPort and server.port:

server: {
    strictPort: true,
    port: 3000,
},

For more information on how to configure Vite, see the official documentation.

ViteManifest Options

$manifest = new ViteManifest(
    manifestPath: '.vite/manifest.json',
    basePath: 'dist/',
    serverUrl: 'http://[::1]:5173/',
    devEnabled: true,
);
  • manifestPath - string: Points to the Vite manifest.json file created for the production build. Optional if you're using the dev server.
  • basePath - string: Public base path from which Vite's published assets are served. The assets paths will be relative to the outDir in your Vite configuration. It could also point to a CDN or other asset server if you are serving assets from a different domain.
  • serverUrl - string: The Vite server URL, including port. Can be used to specify a non-default port if used.
  • devEnabled - bool: Indicates whether the application is running in development mode (i.e. using Vite server). Defaults to false.

See Also

References

userfrosting/vite-php-twig 适用场景与选型建议

userfrosting/vite-php-twig 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 14.84k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2024 年 06 月 29 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-06-29