heimrichhannot/contao-encore-contracts
Composer 安装命令:
composer require heimrichhannot/contao-encore-contracts
包简介
A set of abstractions needed for encore bundle preparation.
README 文档
README
This package contains abstractions to add loose Encore bundle support.
Usage
Register entrypoints
To register encore entrypoints create an EncoreExtension class implementing EncoreExtensionInterface.
namespace HeimrichHannot\ExampleBundle\Asset; use HeimrichHannot\EncoreContracts\EncoreEntry; use HeimrichHannot\EncoreContracts\EncoreExtensionInterface; use HeimrichHannot\ExampleBundle\HeimrichHannotExampleBundle; class EncoreExtension implements EncoreExtensionInterface { public function getBundle(): string { // Return the bundle class return HeimrichHannotExampleBundle::class; } public function getEntries(): array { // Return the bundle entries return [ EncoreEntry::create('main-theme', 'assets/main/js/main-theme.js') ->setRequiresCss(true) ->setIsHeadScript(false), EncoreEntry::create('one-pager', 'assets/one-pager/js/one-pager.js') ->setRequiresCss(true), EncoreEntry::create('custom-head-js', 'assets/main/js/head.js') ->setIsHeadScript(true) // Define entries that will be removed from the global asset array ->addJsEntryToRemoveFromGlobals('colorbox') ->addCssEntryToRemoveFromGlobals('css-to-replace'), ->setDefer(true) ]; } }
Add entrypoints for current page
To add entrypoints (must be registered beforehand) from your code, you can use the PageAssetsTrait.
It checks if encore bundle is installed and add the entry, if this is the case.
Otherwise, it adds the fallback assets to the contao global asset array.
Make your class implement ServiceSubscriberInterface and use PageAssetsTrait (it already implements the needed methods for the ServiceSubscriberInterface).
Afterwards just call $this->addPageEntrypoint(string $name, array $fallbackAssets = []).
use HeimrichHannot\EncoreContracts\PageAssetsTrait; use Symfony\Contracts\Service\ServiceSubscriberInterface; class FrontendController implements ServiceSubscriberInterface { use PageAssetsTrait; public function __invoke() { $this->addPageEntrypoint( // Encore entry point name 'contao-example-bundle', // Optional: define fallback assets to use if encore bundle is not installed [ 'TL_CSS' => ['main-theme' => 'assets/main/dist/main-theme.min.css|static'], 'TL_JAVASCRIPT' => [ 'main-theme' => 'assets/main/dist/main-theme.min.js|static', 'some-dependency' => 'assets/some-dependency/some-dependency.min.js|static', ], ] ); } }
If implementing the ServiceSubscriberInterface is not possible, you can use the AddPageEntrypointTrait, which
also provides the addPageEntrypoint method, but requires you to implement the getFrontendAsset method, which should
return the FrontendAsset service object.
use HeimrichHannot\EncoreBundle\Asset\FrontendAsset; use HeimrichHannot\EncoreContracts\AddPageEntrypointTrait; class MyService { use AddPageEntrypointTrait; public function __construct(private readonly FrontendAsset $frontendAsset) {} protected function getFrontendAsset(): FrontendAsset { return $this->frontendAsset; } public function myAction() { $this->addPageEntrypoint( // Encore entry point name 'contao-example-bundle' ); } }
heimrichhannot/contao-encore-contracts 适用场景与选型建议
heimrichhannot/contao-encore-contracts 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 30.32k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 07 月 20 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 heimrichhannot/contao-encore-contracts 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 heimrichhannot/contao-encore-contracts 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 30.32k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 13
- 依赖项目数: 30
- 推荐数: 0
其他信息
- 授权协议: LGPL-3.0-or-later
- 更新时间: 2022-07-20