Symfony2 Assetic and Less Sourcemaps

时光毁灭记忆、已成空白 提交于 2020-01-03 17:01:52

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!