frc/wp-assets

资源助手。

安装量: 1,290

依赖: 0

建议者: 0

安全: 0

星标: 0

关注者: 7

分支: 0

公开问题: 0

类型:wordpress-muplugin

dev-master 2020-05-04 08:34 UTC

This package is auto-updated.

Last update: 2024-09-17 14:20:24 UTC


README

这是一个必须使用的插件,通过composer使用,无需插件激活。

使用方法

调用函数 frc_asset($file)。例如

wp_enqueue_style('styles', frc_asset('styles/main.css'));

可用方法

  • frc_asset($file)->uri()
  • frc_asset($file)->path()
  • frc_asset($file)->exists()
  • frc_asset($file)->notExists()
  • frc_asset($file)->contents()

frc_asset($file) 强制转换为字符串将返回资产的URI。

使用 add_theme_support 函数进行可选配置。

add_theme_support('frc-assets', [
    'root' => 'asset', // Root folder name for distributed assets
    'file' => 'assets.json', // Assetmanifest's file name
    'path' => get_theme_file_path('<root>'), // Full path to root folder
    'uri' => get_theme_file_uri("<roo>"), // Full URI to root folder
    'manifest' => '<path>/<file>', // Full path to manifest file
    'addHashToFilename' => false, // If using laravel mix you can opt to use the hash from query string in filename and get better caching from cdn's and nginx
]);

如果使用 'addHashToFilename',则清单文件应采用以下格式,哈希长度为20。

{
    "/scripts/main.js": "/scripts/main.js?id=a91004e635bd357e16e6"
}

并且你应该在nginx.conf中添加以下内容,这将从请求的文件名中移除添加的 "hash.a91004e635bd357e16e6":

location ~ "^(.+)(hash\.[0-9a-f]{20})\.(js|css|ttf|eot|woff2?|png|jpe?g|gif|svg|ico)$" {
    add_header Cache-Control "public";
    expires 10y;
    try_files $uri $1$3 =404;
    access_log off;
}

所有这些都可以正常工作。
/GET /scripts/main.hash.a91004e635bd357e16e6.js => /scripts/main.js
/GET /scripts/main.a91004e635bd357e16e6.js => /scripts/main.a91004e635bd357e16e6.js
/GET /scripts/main.hash.js => /scripts/main.hash.js
/GET /scripts/main.js => /scripts/main.js