问题
I am unsure how I can hack the assetic less filter to output a sourcemap file. I am referring to the LessFilter here https://github.com/kriswallsmith/assetic/blob/master/src/Assetic/Filter/LessFilter.php
lines 145 and 146 is where the Symfony\Component\Process\Process
object is created
$proc = $pb->getProcess();
$code = $proc->run();
The trouble is this output gets placed into one single file. I am not sure how to generate a second sourcemap file.
How can I extend this filter or hack the Assetic core to make this work?
回答1:
Yes, this is the right spot. However, you don't need to hack it. Extend it!
I use this:
# Using less source maps with Symfony
namespace Acme\MyBundle\Assetic;
use Assetic\Asset\AssetInterface;
class LessFilter extends AsseticLessFilter
{
public function filterLoad(AssetInterface $asset)
{
$sourcemapRoot = realpath(dirname($asset->getSourceRoot() . '/' . $asset->getSourcePath()));
$this->addTreeOption('sourceMap', true);
$this->addTreeOption('sourceMapBasepath', $sourcemapRoot);
parent::filterLoad($asset);
}
}
// config.yml
assetic:
filters:
less:
class: Acme\MyBundle\Assetic\LessFilter
I found this snipped here: https://github.com/thomaswelton/blog/blob/master/articles/symfony/using-less-source-maps.md
It extends the Filters' filterLoad() method by adding two new tree parameters. All available tree parameters can be found here:
https://github.com/less/less.js/blob/master/bin/lessc#L361-L378
You gotta love dependency injection :)
回答2:
Another way I've found to show the origin less file contents without faffing around with paths is to use the outputSourceFiles flag which bundles the less files into the generated css file (adds bloat so use in dev only).
<?php
...
class LessFilter extends AsseticLessFilter
{
public function filterLoad(AssetInterface $asset)
{
$this->addTreeOption('sourceMap', true);
$this->addTreeOption('outputSourceFiles', true);
parent::filterLoad($asset);
}
}
来源:https://stackoverflow.com/questions/24255882/symfony2-assetic-and-less-sourcemaps