pug-php/pug-minify

一个关键词即可压缩所有(资产:JS、CSS、Stylus、minify、Coffee、React)在你的pug-php模板中。

1.3.0 2023-01-21 21:58 UTC

This package is auto-updated.

Last update: 2024-09-22 01:23:33 UTC


README

Latest Stable Version Build Status StyleCI Test Coverage Code Climate

一个关键词即可压缩所有(资产: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实例链接到任何关键词。只需记住,如果您使用concatconcat-to,文件将仅进行连接而不会压缩,对于其他任何关键词,它们都将进行连接和压缩。

默认情况下,连接和压缩未启用,以便您更容易调试,在生产环境中,您应该设置environment选项。

$pug->setOption('environment', 'production');

如果您仍在使用pug-php 2,则production是默认值,您必须在开发环境中以这种方式设置。

$pug->setCustomOption('environment', 'development');

请注意,在pug-php 2中,您必须使用->setCustomOption->setCustomOptionsassetDirectoryoutputDirectoryenvironment选项。使用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块的每个scriptlink(具有样式表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.jscss/top.min.cssjs/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
    `;