如何在CSS和JS之前添加资产版本? [英] How to prepend assets version to css and js?
问题描述
我希望资产输出压缩的js和css到这样的东西:
v2.3.1 / css / whatever.css
I want assetic to output compressed js and css to something like this:
v2.3.1/css/whatever.css
当前,这是我转储CSS和js进行生产的方式: $ php app / console资产:dump --env = prod-没有调试
。但是它们会被丢弃到css /和js /中,而没有版本。
Currently this is how I dump my css and js for production: $ php app/console assetic:dump --env=prod --no-debug
. But they get dumped into css/ and js/, without the version.
我已阅读此,但它似乎仅指图像,而不是css / js。
I have read this but it seems to refer to images only, not css/js.
这样做的一个重要原因是缓存无效/无效。
An important reason for doing this is for cache busting/invalidation.
推荐答案
是的,已知问题...在我们的生产中我们在 bin / vendors
脚本中以这样的块结束了工作流程:
Yes, known issue... In our production workflow we ended up with such block in bin/vendors
script:
if (in_array('--env=dev', $argv)) {
system(sprintf('%s %s assets:install --symlink %s', $interpreter, escapeshellarg($rootDir . '/app/console'), escapeshellarg($rootDir . '/web/')));
system(sprintf('%s %s assetic:dump --env=dev', $interpreter, escapeshellarg($rootDir . '/app/console')));
system(sprintf('%s %s myVendor:assets:install --symlink ', $interpreter, escapeshellarg($rootDir . '/app/console')));
} else {
system(sprintf('%s %s assets:install %s', $interpreter, escapeshellarg($rootDir . '/app/console'), escapeshellarg($rootDir . '/web/')));
system(sprintf('%s %s assetic:dump --env=prod --no-debug', $interpreter, escapeshellarg($rootDir . '/app/console')));
system(sprintf('%s %s myVendor:assets:install ', $interpreter, escapeshellarg($rootDir . '/app/console')));
}
如您所见,我们定义了控制台命令,该命令将资产安装到Web安装和转储Symfony资产后的文件夹。在 MyVendorCommand
脚本中,我们执行以下操作:
As you can see, we defined our console command, which installs assets into web folder after installing and dumping of Symfony's assets. In the MyVendorCommand
script we do something like this:
$version = $this->getContainer()->getParameter('your_version_parameter');
$assetsInstallCommand = $this->getApplication()->find('assets:install');
$commandOptions = $input->getOptions();
$assetsInstallArguments = array(
'command' => 'assets:install',
'target' => 'web/version-' . $version,
'--symlink' => $commandOptions['symlink']
);
$assetsInstallInput = new ArrayInput($assetsInstallArguments);
$returnCode = $assetsInstallCommand->run($assetsInstallInput, $output);
这篇关于如何在CSS和JS之前添加资产版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!