Skip to content

Commit

Permalink
Minify all the things
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Welch committed Mar 12, 2016
1 parent 6cadcc5 commit 12afa08
Show file tree
Hide file tree
Showing 7 changed files with 185 additions and 143 deletions.
6 changes: 3 additions & 3 deletions MinifyPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@ public function getDescription()
{
return 'A simple plugin that allows you to minify blocks of HTML, CSS, and JS inline in Craft CMS templates.';
}

public function getDocumentationUrl()
{
return 'https://github.com/khalwat/minify/blob/master/README.md';
}

public function getReleaseFeedUrl()
{
return 'https://raw.githubusercontent.com/khalwat/minify/master/releases.json';
}

public function getVersion()
{
return '1.0.4';
Expand Down
34 changes: 24 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ A simple plugin that allows you to minify blocks of HTML, CSS, and JS inline in

1. Download & unzip the file and place the `minify` directory into your `craft/plugins` directory
2. -OR- do a `git clone https://github.com/khalwat/minify.git` directly into your `craft/plugins` folder. You can then update it with `git pull`
3. Install plugin in the Craft Control Panel under Settings > Plugins
4. The plugin folder should be named `minify` for Craft to see it. GitHub recently started appending `-master` (the branch name) to the name of the folder for zip file downloads.
3. -OR- install with Composer via `composer require nystudio107/minify`
4. Install plugin in the Craft Control Panel under Settings > Plugins
5. The plugin folder should be named `minify` for Craft to see it. GitHub recently started appending `-master` (the branch name) to the name of the folder for zip file downloads.

## Configuring Minify

Expand All @@ -31,20 +32,29 @@ Secondly, not all CSS/JS can or should be in static asset files. Sometimes you

Finally, if you minify any code you wrap in `{% cache %}` tags, that means it will be stored minified in the database, reducing db size and (marginally) transactional overhead.

### Minifying HTML
### Minifying Everything

You can wrap any arbitrary HTML/Twig code in the following block tags to minify it:

{% minify html %}
{% minify %}
(HTML/Twig code here)
{% endminify %}

...and the resulting HTML output will be stripped of comments, empty space, etc. A shortcut for HTML minification block tags is simply:
...and the resulting HTML output will be stripped of comments, empty space, etc.

{% minify %}
Using the `{% minify %}` on its own with no parameters (see below) will minify HTML, as well as any inline CSS (in `<style>` tag pairs) and any inline Javascript (in `<script>` tag pairs).


### Minifying HTML

You can wrap any arbitrary HTML/Twig code in the following block tags to minify it:

{% minify html %}
(HTML/Twig code here)
{% endminify %}


...and the resulting HTML output will be stripped of comments, empty space, etc.

It will ignore `<script>` and `<style>` tag pairs in the minification. You should specifically wrap your inline CSS/JS in `{% minify css %}` and `{% minify js}` tag blocks if you want those minimized as well; see below.

### Minifying CSS
Expand Down Expand Up @@ -79,8 +89,6 @@ If you want to minify your entire HTML on the frontend, you can simply wrap your
(Entire base HTML/Twig template here)
{% endminify %}

Then also wrap any inline `<style>` and `<script>` tags in `{% minify css %}` and `{% minify js %}` tags respectively (nested `{% minify %}` tags are fine).

However, understand the potential performance implications. On large HTML/CSS/JS blocks minification is not cheap, and if you do it this way, every single HTTP request will spend extra cycles minimizing your entire template.

It works fine for HTML/CSS/JS templates that aren't too huge, but measure any performance impact by profiling your website before and after wrapping the entire `_layout.twig` template to ensure you're not introducing additional latency in the http requests.
Expand All @@ -92,7 +100,7 @@ On most sites, the overhead that spinning up PHP and Craft takes is the majority
A great way to use the `{% minify %}` tags is to wrap them in `{% cache %}` tags:

{% cache %}
{% minify html %}
{% minify %}
(HTML/Twig code here)
{% endminify %}
{% endcache %}
Expand Down Expand Up @@ -126,6 +134,12 @@ If you can think of another safe & efficient way to limit lines in these two too

## Changelog

### 1.1.0 -- 2016.03.12

* [Added] The default minify tag now minifies all the things (html, css, js)
* [Added] Added Composer support
* [Improved] Updated the README.md

### 1.0.4 -- 2015.12.28

* Fixed the `releases.json` feed URL
Expand Down
10 changes: 10 additions & 0 deletions releases.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
[
{
"version": "1.1.0",
"downloadUrl": "https://github.com/khalwat/minify/archive/master.zip",
"date": "2016-03-12T11:00:00-05:00",
"notes": [
"[Added] The default minify tag now minifies all the things (html, css, js)",
"[Added] Added Composer support",
"[Improved] Updated the README.md"
]
},
{
"version": "1.0.4",
"downloadUrl": "https://github.com/khalwat/minify/archive/master.zip",
Expand Down
53 changes: 35 additions & 18 deletions services/MinifyService.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,56 +9,73 @@ class MinifyService extends BaseApplicationComponent

public function init()
{
$disableTemplateMinifying = craft()->config->get('disableTemplateMinifying');
$disableDevmodeMinifying = craft()->config->get('disableDevmodeMinifying');
$disableTemplateMinifying = craft()->config->get('disableTemplateMinifying');
$disableDevmodeMinifying = craft()->config->get('disableDevmodeMinifying');

if ($disableTemplateMinifying)
{
$this->shouldMinify = false;
$this->shouldMinify = false;
}

if (craft()->config->get('devMode') && $disableDevmodeMinifying)
{
$this->shouldMinify = false;
$this->shouldMinify = false;
}
}

/* --------------------------------------------------------------------------------
Minify the passed in HTML
Minify all the things
-------------------------------------------------------------------------------- */

public function minify($htmlText="")
{
if ($this->shouldMinify)
{
$options = array(
'cssMinifier' => \Minify_CSSmin::minify,
'jsMinifier' => \JSMin\JSMin::minify,
);
$htmlText = \Minify_HTML::minify($htmlText, $options);
}
return $htmlText;
} /* -- minify */

/* --------------------------------------------------------------------------------
Minify the passed in HTML
-------------------------------------------------------------------------------- */

public function htmlMin($htmlText="")
{
if ($this->shouldMinify)
{
$htmlText = \Minify_HTML::minify($htmlText);
if ($this->shouldMinify)
{
$htmlText = \Minify_HTML::minify($htmlText);
}
return $htmlText;
} /* -- htmlMin */

/* --------------------------------------------------------------------------------
Minify the passed in CSS
Minify the passed in CSS
-------------------------------------------------------------------------------- */

public function cssMin($cssText="")
{
if ($this->shouldMinify)
{
$cssText = \Minify_CSSmin::minify($cssText);
if ($this->shouldMinify)
{
$cssText = \Minify_CSSmin::minify($cssText);
}
return $cssText;
} /* -- cssMin */

/* --------------------------------------------------------------------------------
Minify the passed in JS
Minify the passed in JS
-------------------------------------------------------------------------------- */

public function jsMin($jsText="")
{
if ($this->shouldMinify)
{
$jsText = \JSMin\JSMin::minify($jsText);
}
if ($this->shouldMinify)
{
$jsText = \JSMin\JSMin::minify($jsText);
}
return $jsText;
} /* -- jsMin */

Expand Down
44 changes: 22 additions & 22 deletions twigextensions/MinifyTwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,28 @@
*/
class MinifyTwigExtension extends \Twig_Extension
{
// Public Methods
// =========================================================================
// Public Methods
// =========================================================================

/**
* Returns the name of the extension.
*
* @return string The extension name
*/
public function getName()
{
return 'minify';
}
/**
* Returns the name of the extension.
*
* @return string The extension name
*/
public function getName()
{
return 'minify';
}

/**
* Returns the token parser instances to add to the existing list.
*
* @return array An array of Twig_TokenParserInterface or Twig_TokenParserBrokerInterface instances
*/
public function getTokenParsers()
{
return array(
new Minify_TokenParser(),
);
}
/**
* Returns the token parser instances to add to the existing list.
*
* @return array An array of Twig_TokenParserInterface or Twig_TokenParserBrokerInterface instances
*/
public function getTokenParsers()
{
return array(
new Minify_TokenParser(),
);
}
}
60 changes: 33 additions & 27 deletions twigextensions/Minify_Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,46 +6,52 @@
*/
class Minify_Node extends \Twig_Node
{
// Properties
// =========================================================================
// Properties
// =========================================================================


// Public Methods
// =========================================================================
// Public Methods
// =========================================================================

/**
* @param \Twig_Compiler $compiler
*
* @return null
*/
public function compile(\Twig_Compiler $compiler)
{
$html = $this->getAttribute('html');
$css = $this->getAttribute('css');
$js = $this->getAttribute('js');
/**
* @param \Twig_Compiler $compiler
*
* @return null
*/
public function compile(\Twig_Compiler $compiler)
{
$html = $this->getAttribute('html');
$css = $this->getAttribute('css');
$js = $this->getAttribute('js');

$compiler
->addDebugInfo($this);

$compiler
$compiler
->write("ob_start();\n")
->subcompile($this->getNode('body'))
->write("\$_compiledBody = ob_get_clean();\n");

if ($html) {
$compiler
->write("echo \Craft\craft()->minify->htmlMin(\$_compiledBody);\n");
if ($html)
{
$compiler
->write("echo \Craft\craft()->minify->htmlMin(\$_compiledBody);\n");
}

if ($css) {
$compiler
->write("echo \Craft\craft()->minify->cssMin(\$_compiledBody);\n");
elseif ($css)
{
$compiler
->write("echo \Craft\craft()->minify->cssMin(\$_compiledBody);\n");
}

if ($js) {
$compiler
->write("echo \Craft\craft()->minify->jsMin(\$_compiledBody);\n");
elseif ($js)
{
$compiler
->write("echo \Craft\craft()->minify->jsMin(\$_compiledBody);\n");
}
else
{
$compiler
->write("echo \Craft\craft()->minify->minify(\$_compiledBody);\n");
}

}
}
}
Loading

0 comments on commit 12afa08

Please sign in to comment.