humanmade/asset-loader

提供在WordPress主题和插件中无缝消费Webpack打包资源的功能。

安装数: 209,149

依赖者: 6

建议者: 0

安全: 0

星星: 26

关注者: 18

分支: 4

开放问题: 12

类型:wordpress-plugin


README

此插件暴露了可以在其他WordPress主题或插件中使用的一些函数,以帮助检测和加载Webpack生成的资源,包括从本地 webpack-dev-server 实例提供的服务。

Build Status

用法

此库旨在与Webpack配置(例如使用@humanmade/webpack-helpers中的预设创建的配置)一起工作,该配置生成一个资源清单文件。该清单将资源包名称与指向运行中的DevServer实例上的资源包的URI或磁盘上的本地文件路径相关联。

Asset_Loader提供了一套方法来读取此清单文件并在其中注册特定的资源以在您的WordPress网站中加载。此插件提供的公共接口主要是两个方法,Asset_Loader\register_asset()Asset_Loader\enqueue_asset()。要注册清单资产,请在类似 wp_enqueue_scriptsenqueue_block_editor_assets 的动作中调用这些方法,就像您会调用标准的WordPress wp_register_scriptwp_enqueue_style 函数一样。

<?php
namespace My_Theme\Scripts;

use Asset_Loader;

add_action( 'enqueue_block_editor_assets', __NAMESPACE__ . '\\enqueue_block_editor_assets' );

/**
 * Enqueue the JS and CSS for blocks in the editor.
 *
 * @return void
 */
function enqueue_block_editor_assets() {
  Asset_Loader\enqueue_asset(
    // In a plugin, this would be `plugin_dir_path( __FILE__ )` or similar.
    get_stylesheet_directory() . '/build/asset-manifest.json',
    // The handle of a resource within the manifest. For static file fallbacks,
    // this should also match the filename on disk of a build production asset.
    'editor.js',
    [
      'handle'       => 'optional-custom-script-handle',
      'dependencies' => [ 'wp-element', 'wp-editor' ],
    ]
  );

  Asset_Loader\enqueue_asset(
    // In a plugin, this would be `plugin_dir_path( __FILE__ )` or similar.
    get_stylesheet_directory() . '/build/asset-manifest.json',
    // Enqueue CSS for the editor.
    'editor.css',
    [
      'handle'       => 'custom-style-handle',
      'dependencies' => [ 'some-style-dependency' ],
    ]
  );
}

文档

有关完整文档,包括贡献过程,请访问文档网站

许可证

此插件是免费软件。您可以在自由软件基金会的GNU通用公共许可证(第2版或任何更新版本)的条款下重新分发或修改它。