如何使用uglifyjs将所有js文件合并为一个资产库[symfony]

时间:2013-07-22 17:12:58

标签: symfony assetic uglifyjs

代码中的错误

我想在加载时将a.js和b.js合并为一个

config.yml

# Assetic Configuration
assetic:
    debug:          %kernel.debug%
    use_controller: false
    bundles:        ['AcmeDemoBundle']
    #java: /usr/bin/java
    filters:
        cssrewrite: ~
        #closure:
        #    jar: %kernel.root_dir%/Resources/java/compiler.jar
        #yui_css:
        #    jar: %kernel.root_dir%/Resources/java/yuicompressor-2.4.7.jar
        uglifyjs2: %kernel.root_dir%

查看文件

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title></title>
<meta name="" content="">
<link href="<?php echo $view['assets']->getUrl('bundles/acmedemo/css/a.css') ?>" rel="stylesheet" type="text/css" />
<!--<script src="<?php echo $view['assets']->getUrl('bundles/acmedemo/js/a.js') ?>" type="text/javascript" /></script>-->
<?php foreach ($view['assetic']->javascripts(
    array('@AcmeDemoBundle/Resources/public/js/a.js',
            '@AcmeDemoBundle/Resources/public/js/b.js'
    ),
    array('uglifyjs2')
) as $url): ?>
    <script type="text/javascript" src="<?php echo $view->escape($url) ?>"></script>
<?php endforeach; ?>
</head>
<body>

<div class="c2">Hello</div>

</body>
</html>

控制器

/**
 * @Route("/test", name="_demo_contact")
 * @Template()
 */
public function testAction(){
   return $this->render('AcmeDemoBundle:Demo:a.html.php');
}

1 个答案:

答案 0 :(得分:0)

请看这段http://symfony.com/doc/current/cookbook/assetic/asset_management.html#controlling-the-url-used

当你打开php版本时,你会看到你应该作为第三个参数传递一些output值的数组。

E.g。

<?php foreach ($view['assetic']->javascripts(
        array('@AcmeDemoBundle/Resources/public/js/a.js',
              '@AcmeDemoBundle/Resources/public/js/b.js'
        ),
        array('uglifyjs2'),
        array('output' => 'js/compiled/main.js')
      ) as $url): ?>
    <script type="text/javascript" src="<?php echo $view->escape($url) ?>"></script>
<?php endforeach; ?>