andreinocenti/cakephp-inocenti_shrink

编译、合并和压缩javascript、coffee、less、scss和css

安装: 21

依赖者: 0

建议者: 0

安全性: 0

星星: 0

关注者: 0

分支: 9

类型:cakephp-plugin

3.0.7.2 2019-02-25 22:52 UTC

This package is auto-updated.

Last update: 2024-09-17 05:24:03 UTC


README

关于

Shrink 插件用于编译、合并和压缩javascript和css。目前支持原生javascript和css,Less(php和node版本),Sass(php和ruby版本),CoffeeScript和TypeScript。

Shrink 是一个最小化配置的插件。如果你需要一个功能强大的、可配置的资产压缩器,请查看 Mark Story 的 asset_compress

安装

此版本适用于 CakePHP 3,对于其他版本的 CakePHP,请检查 Github 上的分支。

建议使用 composer 安装: "trentrichardson/cakephp-shrink": "~3.0"。然后在 bootstrap.php 中启用插件 Plugin::load('Shrink');Plugin::loadAll();

你也可以手动下载 Shrink 并将其放置在 app/Plugin 文件夹中

使用 Plugin::load('Shrink',['autoload'=>true]);Plugin::loadAll(); 在 bootstrap.php 中启用插件。手动安装时,需要将自动加载标志设置为 true。

将 "Shrink.Shrink" 添加到控制器中的 $helpers 属性。可能是 AppController.php。

使用方法

您可以使用 Shrink 的方式与您已经使用 Html helper 的方式相同。略有不同的是,您必须包括文件扩展名(因为您现在可以处理 less,sass,coffee),以及文件后的参数。

/**
* Adds a css file to the file queue
* @param array/string files - string name of a file or array containing multiple string of files
* @param bool immediate - true to immediately process and print the file, false to merge with others
* @param string how - 'link' to print <link>, 'embed' to use <style>...css code...</style>
* @return string - when $immediate=true the tag will be printed, "" otherwise
*/
public function css($files, $immediate=false, $how='link')

/**
* Adds a js file to the file queue
* @param array/string files - string name of a file or array containing multiple string of files
* @param bool immediate - true to immediately process and print the file, false to merge with others
* @param string how - 'link' for <script src="">, 'async' for <script src="" async>, 'embed' for <script>...js code...</script>
* @return string - when $immediate=true the tag will be printed, "" otherwise
*/
public function js($files, $immediate=false, $how='link')

/**
* Processes/minify/combines queued files of the requested type.
* @param string type - 'js' or 'css'. This should be the end result type
* @param string how - 'link' for <script src="">, 'async' for <script src="" async>, 'embed' for <script>...js code...</script>
* @param array files - string name of a file or array containing multiple string of files
* @return string - the <script> or <link>
*/
$this->Shrink->fetch($type, $how='link')

假设您有一个布局和一个用户控制器的视图。视图顶部可能如下所示

<?php
	// View/Users/edit.ctp
	$this->Shrink->css(['users/edit.less']);
	$this->Shrink->js(['users/edit.coffee']);
?>

然后在布局中您有

<?php
	// View/Layouts/default.ctp
	$this->Shrink->css(['bootstrap.css', 'site.less']);
	echo $this->Shrink->fetch('css');

	$this->Shrink->js(['jquery.js', 'site.coffee']);
	echo $this->Shrink->fetch('js');
?>

Shrink 将看到有传递 js 和 css 的视图以及布局,并将它们合并在一起(布局的 css 和 js,然后是视图的 css 和 js)

一旦调用 fetch,该类型的队列将被清除,您可以再次排队。

选项

选项的传递方式与其他助手相同。

// in your AppController.php
$helpers = array('Form','Html',
	'Shrink.Shrink'=>array(
		'url'=>'http://assets.trentrichardson.com'
	)
);

您还可以使用 Configure 传递选项,但是直接传递给助手的任何选项(如上所示)将覆盖使用 Configure 设置的选项

Configure::write('Shrink.url', 'http://assets.trentrichardson.com');

可传递的选项如下

public $settings = array(
	'js'=>array(
			'path'=>'js/',        // folder to find src js files
			'cachePath'=>'js/',   // folder to create cache files
			'minifier'=>'jsmin'   // minifier to minify, false to leave as is
		),
	'css'=>array(
			'path'=>'css/',       // folder to find src css files
			'cachePath'=>'css/',  // folder to create cache files
			'minifier'=>'cssmin', // minifier name to minify, false to leave as is
			'charset'=>'utf-8'    // charset to use
		),
	'url'=>'',                     // url without ending /, incase you access from another domain
	'prefix'=>'shrink_',           // prefix the beginning of cache files
	'debugLevel'=>1                // compared against Core.debug, eq will recompile, > will not minify
);

扩展

扩展 Shrink 非常简单,可能只需几行代码即可完成。有两种抽象:编译器和压缩器。

编译器用于文件类型,如 Coffee、Less 等。这些可以在 app/Plugin/Shrink/src/Lib/ShrinkCompiler 文件夹中找到。文件和类名按文件扩展名命名。Less 文件具有 .less 扩展名,因此将创建 ShrinkCompilerLess.php。每个编译器都必须设置 $resultType 变量为 'js' 或 'css'(最终结果类型),并实现 compile 方法。

压缩器用于在代码编译为 js 或 css 后压缩代码。这些可以在 app/Plugin/Shrink/src/Lib/ShrinkCompressor 中找到。文件和类名按您在设置中设置的选项命名(js 或 css 的最小化器选项)。例如,如果您将 css 的最小化器选项传递给 cssmin,则文件名将为 ShrinkCompressorCssmin.php。每个压缩器类都必须实现 compress 方法。

扩展编译器或压缩程序的最简单方法是直接复制现有的某个版本。您还会注意到编译器和压缩程序扩展了ShrinkBase。这是一个提供“cmd”方法以执行命令的实用程序类。

测试

如果您正在做出更改,创建测试并确保您的新代码处于通过状态是一个好主意。测试通过phpunit运行。由于cakephp-shrink提供了对命令行实用程序和composer包的支持,您必须在测试运行之前安装它们。

composer install
npm install -g less
npm install -g coffee-script
npm install -g typescript
gem install sass

根据您的设置,您可能需要使用sudo。要运行测试,只需调用phpunitvendor/bin/phpunit

Build Status

许可证

版权所有 2015 Trent Richardson

您可以在MIT或GPL许可证下使用此项目。

http://trentrichardson.com/Impromptu/GPL-LICENSE.txt

http://trentrichardson.com/Impromptu/MIT-LICENSE.txt