pug-php / pug-minify
一个关键词即可压缩所有(资产:JS、CSS、Stylus、minify、Coffee、React)在你的pug-php模板中。
资助包维护!
kylekatarnls
Open Collective
Tidelift
Requires
- php: >=5.3
- nodejs-php-fallback/coffeescript: ^1.0
- nodejs-php-fallback/less: ^1.0
- nodejs-php-fallback/react: ^1.0
- nodejs-php-fallback/stylus: ^1.0
- nodejs-php-fallback/uglify: ^1.0.2
- pug-php/pug: ^2.0 || ^3.0
Requires (Dev)
- composer/composer: ^1.10.23 || ^2.1.9
- phpunit/phpunit: ^4.8.36 || ^5.7.27 || ^8.5.31
README
一个关键词即可压缩所有(资产:JS、CSS、Stylus、Less、Coffee、React)在你的pug-php模板中。
安装
composer require pug-php/pug-minify
使用方法
<?php use Pug\Keyword\Minify; use Pug\Pug; // Create a new Pug instance: $pug = new Pug(array( 'assetDirectory' => 'path/to/the/asset/sources', 'outputDirectory' => 'web', )); // Or if you already instanciate it, just set the options: $pug->setOptions(array( 'assetDirectory' => 'path/to/the/asset/sources', 'outputDirectory' => 'web', )); $minify = new Minify($pug); $pug->addKeyword('minify', $minify); $pug->addKeyword('concat', $minify); $pug->render('my/template.pug');
您可以将Minify实例链接到任何关键词。只需记住,如果您使用concat
或concat-to
,文件将仅进行连接而不会压缩,对于其他任何关键词,它们都将进行连接和压缩。
默认情况下,连接和压缩未启用,以便您更容易调试,在生产环境中,您应该设置environment
选项。
$pug->setOption('environment', 'production');
如果您仍在使用pug-php 2,则production
是默认值,您必须在开发环境中以这种方式设置。
$pug->setCustomOption('environment', 'development');
请注意,在pug-php 2中,您必须使用->setCustomOption
和->setCustomOptions
为assetDirectory
、outputDirectory
和environment
选项。使用pug-php 3,您现在可以使用->setOption
和->setOptions
设置任何选项。
这将仅转换(对于stylus、less、coffee等)并将您的资产复制到输出目录。
现在让我们看看您的模板应该是什么样子
doctype 5 html head title Foo minify top script(src="foo/test.js") script(src="coffee/test.coffee") script(src="react-pug/test.jsxp" type="text/babel") link(rel="stylesheet" href="foo/test.css") link(rel="stylesheet" href="less/test.less") link(rel="stylesheet" href="stylus/test.styl") meta(name="foo" content="bar") body h1 Foobar minify bottom script(src="react/test.jsx" type="text/babel") script(src="coffee-pug/test.cofp") //- some comment
在生产环境中,每个minify
块的每个script
和link
(具有样式表rel)标签都将合并为一个指向所有这些压缩版本的单个标签,如下所示
doctype 5 html head title Foo script(src="js/top.min.js") link(rel="stylesheet" href="css/top.min.css") meta(name="foo" content="bar") body h1 Foobar script(src="js/bottom.js") //- some comment
生成的文件js/top.min.js
、css/top.min.css
和js/bottom.js
存储在您通过选项指定的outputDirectory
中。因此,您只需确保src="foo/bar.js"
将针对{outputDirectory}/foo/bar.js
。
重要:为了在生产环境中提高性能,请通过将cache
选项设置为一个可写目录来启用Pug缓存,例如
$pug->setOption('cache', 'var/cache/pug'); $pug->setOption('cache', sys_get_temp_dir());
当您的资产更改或部署新资产时,请清除此缓存目录。
由于Pug缓存功能允许只渲染一次pug代码和资产,因此我们在Minify关键词中未包含特定的缓存选项。
支持的资产
.coffee
文件从CoffeeScript编译成JS.cofp
处理带有html = ::"""h1#title Hello"""
标签的Pug内部的CoffeeScript.jsx
文件从JSX编译成JS,也用于React.jsxp
处理带有html = ::`h1#title Hello`;
标签的Pug内部的JSX.styl
文件从Stylus编译成CSS.less
文件从Less编译成CSS
嵌入式Pug代码可以是多行的
.cofp
html = ::""" section article div:p.text Bla bla """
.jsxp
let html = ::` section article div:p.text Bla bla `;