webmasterapp / assets
支持开发机器上资源的mtime。
0.5.0
2023-12-14 11:44 UTC
Requires
- php: >= 8.3
- latte/latte: ^3.0.12
- nette/application: ^3.1.14
- nette/bootstrap: ^3.2.1
- nette/safe: ^0.9
- nette/utils: ^4.0.3
Requires (Dev)
This package is not auto-updated.
Last update: 2024-09-19 15:14:59 UTC
README
如果您需要浏览器自动清除缓存,请使用此扩展。
通过composer安装
$ composer require h4kuna/assets
变更日志
- 1.1.0 无需要求,更新nette 3.0
- 1.0.0 支持 PHP 7.1+ (严格类型)
- 0.1.4 0.1.5 更新版本支持 PHP 5.6 及以上版本
- 0.1.3 支持 PHP 5.3
如何使用
第一步只需注册扩展,其他参数是可选的。您可以使用新的过滤器 asset。
extensions:
assets: h4kuna\Assets\DI\AssetsExtension
assets:
# required nothing
# optional
wwwDir: %wwwDir%
debugMode: %debugMode%
tempDir: %tempDir%
wwwTempDir: %wwwDir%/temp # here is place where move assets from 3rd library (from vendor/ etc.)
externalAssets:
- %appDir%/../vendor/nette/nette.js # save to %wwwTempDir%/nette.js
'ext/nette2.4.js': %appDir%/../vendor/nette/nette.js # save to %wwwTempDir%/ext/nette2.4.js
# download from external source, this is experimental!
- http://example.com/foo.js # save to %wwwTempDir%/foo.js
'sha256-secure-token': http://example.com/foo.js # check if is right file
优点
- $basePath 不需要
- 路径相对于您的 wwwDir
- 当发现新文件或删除 %tempDir%/cache/_assets 时,将构建缓存
- 生产环境和开发环境的行为相同
<link rel="stylesheet" href="{='css/main.css'|asset}">
<script src="{='js/main.js'|asset}"></script>
示例输出: `?file mtime`
。
<link rel="stylesheet" href="/css/main.css?123456">
<script src="/js/main.js?456789"></script>
使用双斜杠可以启用打印模板的绝对路径
<link rel="stylesheet" href="{='//css/main.css'|asset}">
资源
这是一个可以依赖任何内容并收集 css 和 js 文件以渲染到模板的对象。
/* @var $assets \h4kuna\Assets\Assets */
$assets->addJs('ext/nette2.4.js', ['async' => TRUE]);
echo (string) $assets->renderJs();
渲染此内容
<script src="/temp/ext/nette2.4.js?456789" async></script>
自定义缓存构建器 - 高级使用
这将在编译时创建缓存。默认情况下,资源缓存是在运行时构建的
assetsExtension:
cacheBuilder: \CacheBuilder
使用准备好的接口
class CacheBuilder implements \h4kuna\Assets\DI\ICacheBuilder
{
public function create(\h4kuna\Assets\CacheAssets $cache, $wwwDir)
{
$finder = Nette\Utils\Finder::findFiles('*')->in($wwwDir . '/config');
foreach ($finder as $file) {
/* @var $file \SplFileInfo */
$cache->load(self::replaceSlashOnWindows($file));
}
}
private static function replaceSlashOnWindows(SplFileInfo $file)
{
static $isWindows;
if ($isWindows === NULL) {
$isWindows = strtoupper(substr(PHP_OS, 0, 3)) === 'WIN';
}
if ($isWindows) {
return str_replace('\\', '/', $file->getPathname());
}
return $file->getPathname();
}
}