stumpart/stumpjs-compiler

为ZF2的JavaScript编译器

dev-master 2013-04-06 16:49 UTC

This package is not auto-updated.

Last update: 2024-09-14 14:34:48 UTC


README

版本 0.0.1 由 Barrington Henry 创建

简介

Zend Framework 2模块,可以将JavaScript文件合并和压缩。后续请求从缓存中检索(仍在开发中)

安装

首先,将以下行添加到您的 composer.json 文件中

"require": {
    "stumpart/stumpjs-compiler": "dev-master"
}

然后运行

    php composer.phar update

然后,通过在您的 application.config.php 文件中添加 StumpJsCompiler 来启用该模块。

<?php
return array(
    'modules' => array(
        'Application',
        'StumpJsCompiler',
    ),
);

需求

  • 确保web服务器用户有写入应用程序数据文件夹的权限。
  • 请确保您的Web服务器上已安装Java虚拟机。编译器可执行文件是jar文件,需要此环境来执行

配置

需要配置以使StumpjsCompiler启动和运行。此文件是模块配置文件 config/module.config.php

<?php
return array(
    /**
     * The javscript minifier executables and corresponsing classes/adapters
     * that wraps these executables. You can add your own executables and classes/adapters
     */
    'executables' => array(
            'yuicompressor'  => array(
                                 'file'    =>   'yuicompressor-2.4.7.jar',
                                 'class'   =>   'StumpJsCompiler\Compilers\YUICompressor'
                                ),
            'closure'        => array(
                                 'file'    =>   'compiler.jar',
                                 'class'   =>   'StumpJsCompiler\Compilers\GClosure'
                                ),
            'jsmin'          => array(
                                 'file'    =>   '',
                                 'class'   =>   ''
                                )
    ),
        
    /**
     * Sets of actions to be taken on the javascript file
     * You can add additional actions, but make sure the selected compiler executable
     * supports it or that you add that functionality in your adapter class
     */
    'actions' => array(
        //minify each javascript file
        'minifier'   => 'StumpJsCompiler\Channels\Minifier',
        
        //combine a list of javascript files into one
        'combiner'   => 'StumpJsCompiler\Channels\Combiner'
    ),
        
    'compiler' => array(
         //The current compiler to use for compilation
        'current'       => 'closure',
         
        //can use the various caching mechanisms provided by the ZF2
        'storageAdapter'=> 'filesystem',
        
        //work area for the jscompiler module, will create a StumpJsCompiler directory
        //where it stores temporary files for updating. So the web server needs to have write permissions
        //if no permissions, will use the /tmp/ directory and leave a notice in the log files
        'workareaDir'   => __DIR__ . '/../../../data'
    ),
        
    /**
     * A mapping of the builds that can be compiled.
     * Customize and replace to your own liking. Javascript files and values are
     * just place holders
     */
    'builds'=>array(
        'jstest1'=>array(
            'files'=>array(
                'js/prototype.js',
                'js/jquery-1.9.1.js',
                'js/foo.js'
             ),
            'cache-lifetime'=>31356000 //
        ),
        'jstest2'=>array(
            'files'=>array(
               'js/helloworld.js',
               'js/bar.js'         
             ),
            'headers'=>array(
                'X-Foo-Debug'=>md5("some hash"),
                'X-JS-Bar'=>'somebar',
                'X-Content-Type-Options'=>'nosniff'
            )
        )
    )
);

Definitions
cache-lifetime - The time in seconds that states how long the browser should cache the compiled javascript file
files          - The list of javascript files to include in the compilation
headers        - Additional headers that you may wish to send in the response. This will be added to the 
                 default list of headers.

用法

安装和配置此模块后,在视图中使用StumpjsCompiler很容易:'compiledScript'是一个视图辅助器,允许用户输入一个构建名称,例如'jstest1',然后映射到模块.config.php文件中的'builds'下的'jstest1'。

<?php echo $this->compiledScript('jstest1');?>

此视图辅助器调用可以放置在视图脚本中的任何位置,在head或body标签中。