wp-strap / vite
使用 ViteJS 开发 WordPress 后端工具集。
Requires
- psr/container: ^2.0
Requires (Dev)
- captainhook/captainhook: ^5.16
- dealerdirect/phpcodesniffer-composer-installer: ^1.0.0
- php-parallel-lint/php-parallel-lint: ^1.3
- php-stubs/wordpress-stubs: ^6.2.1
- phpcompatibility/phpcompatibility-wp: *
- phpcsstandards/phpcsextra: ^1.0.4
- phpcsstandards/phpcsutils: ^1.0.6
- roave/security-advisories: dev-latest
- slevomat/coding-standard: ~8.13.1
- szepeviktor/phpstan-wordpress: ^1.3.0
- wp-coding-standards/wpcs: *
README
Vite PHP
使用 ViteJS 开发 WordPress 后端/PHP 工具库。
你可以在 vitejs.dev 上了解更多关于 ViteJS 的信息
用法
将依赖项安装到您的项目中。
composer require wp-strap/vite
这暴露了一些类,它们负责从 Vite 的 manifest.json 生成资产 URL,您可以对其进行注册或排队。它还负责在 ViteJS 开发服务器运行时启用 HMR。
此包是为以下用途而制作的
但也可以与任何 ViteJS 设置一起使用。
类遵循 PSR 实践,使用接口,因此可以通过依赖注入和 IoC 容器进行面向对象编程。它还提供了一个 Facade 类,允许您使用静态方法而不是在您喜欢的任何地方调用方法。
使用 Facade 的示例
use WPStrap\Vite\Assets; // Resolves instance and registers project configurations Assets::register([ 'dir' => plugin_dir_path(__FILE__), // or get_stylesheet_directory() for themes 'url' => plugins_url(\basename(__DIR__)) // or get_stylesheet_directory_uri() for themes 'version' => '1.0.2', // Set a global version (optional) 'deps' => [ 'scripts' => [], 'styles' => [] ] // Set global dependencies (optional) ]); // Listens to ViteJS dev server and makes adjustment to make HMR work Assets::devServer()->start(); // returns: https://your-site.com/wp-content/plugins/your-plugin/build/js/main.oi4h32d.js Assets::get('js/main.js') // Alternatively you can use these as well which will be more targeted to specific folders // and for some of the methods you don't need to write the file extension Assets::js('main') Assets::css('main') Assets::image('bird-on-black.jpg') Assets::svg('instagram') Assets::font('SourceSerif4Variable-Italic.ttf.woff2') // Example of enqueuing the scripts add_action('wp_enqueue_scripts', function () { // You can enqueue & register the tradtional way using global data wp_enqueue_script('my-handle', Assets::js('main'), Assets::deps('scripts'), Assets::version()); wp_enqueue_style('my-handle', Assets::css('main'), Assets::deps('styles'), Assets::version()); // Or use a more simple method that includes the global deps & version Assets::enqueueStyle('my-handle', 'main'); // Which also comes with some handy chained methods Assets::enqueueScript('my-handle', 'main', ['another-dep']) ->useAsync() ->useAttribute('key', 'value') ->localize('object_name', ['data' => 'data']) ->appendInline('<script>console.log("hello");</script>'); });
使用实例的示例
use WPStrap\Vite\Assets; use WPStrap\Vite\AssetsService; use WPStrap\Vite\DevServer; // Instantiates the Asset service and registers project configurations $assets = new AssetsService(); $assets->register([ 'dir' => plugin_dir_path(__FILE__), // or get_stylesheet_directory() for themes 'url' => plugins_url(\basename(__DIR__)) // or get_stylesheet_directory_uri() for themes ]); // Listens to ViteJS dev server and makes adjustment to make HMR work (new DevServer($assets))->start(); $assets->get('js/main.js'); $assets->js('main') $assets->css('main') $assets->image('bird-on-black.jpg') $assets->svg('instagram') $assets->font('SourceSerif4Variable-Italic.ttf.woff2') // Traditional wp_enqueue_script('my-handle', $this->assets->js('main'), $this->assets->deps('scripts'), $this->assets->version()); wp_enqueue_style('my-handle', $this->assets->css('main'), $this->assets->deps('styles'), $this->assets->version()); // Custom methods $this->assets->enqueueStyle('my-handle', 'main'); $this->assets->enqueueScript('my-handle', 'main', ['another-dep']) ->useAsync() ->useAttribute('key', 'value') ->localize('object_name', ['data' => 'data']) ->appendInline('<script>console.log("hello");</script>'); // You can also use the facade based on this instance. Assets::setFacade($assets); Assets::get('css/main.css');
使用函数实例的示例
use WPStrap\Vite\AssetsInterface; use WPStrap\Vite\AssetsService; use WPStrap\Vite\DevServer; function assets(): AssetsInterface { static $assets; if(!isset($assets)) { $assets = (new AssetsService())->register([ 'dir' => plugin_dir_path(__FILE__), 'url' => plugins_url(\basename(__DIR__)), 'version' => '1.0.0' ]); } return $assets; } (new DevServer(assets()))->start(); add_action('wp_enqueue_scripts', function () { // Traditional wp_enqueue_script('my-handle', assets()->js('main'), assets()->deps('scripts'), assets()->version()); wp_enqueue_style('my-handle', assets()->css('main'), assets()->deps('styles'), assets()->version()); // Using custom methods assets()->enqueueStyle('my-handle', 'main'); assets()->enqueueScript('my-handle', ['Main', 'main'], ['another-dep']) ->useAsync() ->useAttribute('key', 'value') ->localize('object_name', ['data' => 'data']) ->appendInline('<script>console.log("hello");</script>'); });
使用 League 容器的示例
use League\Container\Container; use WPStrap\Vite\Assets; use WPStrap\Vite\AssetsInterface; use WPStrap\Vite\AssetsService; use WPStrap\Vite\DevServer; use WPStrap\Vite\DevServerInterface; $container = new Container(); $container->add(AssetsInterface::class)->setConcrete(AssetsService::class)->addMethodCall('register', [ 'dir' => plugin_dir_path(__FILE__), 'url' => plugins_url(\basename(__DIR__)) ]); $container->add(DevServerInterface::class)->setConcrete(DevServer::class)->addArgument(AssetsInterface::class); $assets = $container->get(AssetsInterface::class); $devServer = $container->get(DevServerInterface::class); $devServer->start(); $assets->get('main/main.css'); // You can also set a PSR container as a facade accessor Assets::setFacadeAccessor($container); Assets::get('main/main.css')
开发服务器
Assets::devServer()->start(3000);
或 (new DevServer($assets))->start('3000');
开发服务器类负责通过 CURL 监听 ViteJS 开发服务器,检查它是否在本地的 3000 端口上运行,您可以使用 start() 方法中的可选参数进行调整,如上所示。
如果它可以验证开发服务器正在运行,它将注入开发服务器提供的 viteJS 脚本,过滤所有资产 URL,并加载源文件(从 assets::get(),assets:css(),assets::js() 等方法中获取),并修改脚本标签以确保源文件可以作为模块加载以启用 HMR。
此操作仅在本地/开发环境中运行。 因为它在每次请求上都使用 CURL,所以您不希望在生产环境中运行此操作。
项目示例
您可以在以下位置找到更多信息和一个项目示例: https://github.com/wp-strap/wp-vite-starter