mindkomm / theme-lib-mix
Laravel Mix 在 WordPress 项目中的函数
Requires
- php: >=7.0.0
This package is auto-updated.
Last update: 2024-09-17 13:42:27 UTC
README
A Laravel Mix function for WordPress themes.
The mix()
function is useful if you want to enable cache busting for your theme asset files (CSS, JavaScript, images, icon sprites). Laravel Mix allows you to create a mix-manifest.json
file, which might look like this
{ "/css/styles.css": "/css/styles.css?id=6ed48b0b831e80bd7549", "/js/scripts.js": "/js/scripts.js?id=1bdd07b944e933aa88aa", }
The ID parameter is a hash of the file contents that changes every time that you make a change to a file. The mix()
, mix_child()
and mix_any()
functions provided in this package allow you to use these hashed URLs for enqueueing your assets in your WordPress theme.
安装
You can install the package via Composer
composer require mindkomm/theme-lib-mix
使用方法
The mix()
function assumes that you have a mix-manifest.json
(generated by the version function of Laravel Mix) in the build
folder of your theme.
add_action( 'wp_enqueue_scripts', function() { wp_enqueue_style( 'styles', mix( 'build/css/styles.css' ) ); } );
If the mix function can’t find your asset file in the manifest file, it will return the asset URL through get_theme_file_uri
as a fallback.
函数
mix()
获取主题中版本化 Mix 文件的路径。
如果您想加载主题依赖项,请使用此函数。此函数将为您缓存清单文件的文件内容。
- 如果您想在子主题中使用 mix,请使用
mix_child()
。 - 如果您想在主题文件夹外使用 mix,您可以使用
mix_any()
。
since 1.0.0
mix( string $path, array $args = [] )
返回值: string
版本化文件 URL。
add_action( 'wp_enqueue_scripts', function() { wp_enqueue_style( 'styles', mix( 'build/css/styles.css' ) ); } );
mix_child()
获取子主题中版本化 Mix 文件的路径。
类似于 mix()
,但首先尝试从子主题加载文件。
since 1.2.0
mix_child( string $path, array $args = [] )
add_action( 'wp_enqueue_scripts', function() { wp_enqueue_style( 'theme-styles-child', mix_child( 'build/css/styles-child.css' ), [], null ); } );
mix_any()
获取主题文件夹外版本化 Mix 文件的路径。
与 mix()
函数的不同之处在于,对于此函数,您需要提供文件和清单目录的绝对路径。好处是它更灵活,您可以用它来实现可能不在主题中,而是在插件、供应商包或符号链接包中的功能。
since 1.1.0
mix_any( string $path, string $manifest_directory, string $manifest_name = mix-manifest.json )
返回值: string
版本化文件 URL。
支持
This is a library that we use at MIND to develop WordPress themes. You’re free to use it, but currently, we don’t provide any support.