├── phpstan.neon ├── CHANGELOG.md ├── ecs.php ├── src ├── translations │ └── en │ │ └── minify.php ├── config.php ├── twigextensions │ ├── MinifyTwigExtension.php │ ├── MinifyNode.php │ └── MinifyTokenParser.php ├── services │ ├── ServicesTrait.php │ └── MinifyService.php ├── models │ └── Settings.php ├── icon.svg └── Minify.php ├── LICENSE.txt ├── README.md └── composer.json /phpstan.neon: -------------------------------------------------------------------------------- 1 | includes: 2 | - %currentWorkingDirectory%/vendor/craftcms/phpstan/phpstan.neon 3 | 4 | parameters: 5 | level: 5 6 | paths: 7 | - src 8 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Minify Changelog 2 | 3 | ## 5.0.0 - 2024.04.15 4 | ### Added 5 | * Stable release for Craft CMS 5 6 | 7 | ## 5.0.0-beta.2 - 2024.02.10 8 | ### Added 9 | * Added `ServicesTrait` for the plugin service component registration 10 | 11 | ## 5.0.0-beta.1 - 2024.02.09 12 | ### Added 13 | * Initial beta version for Craft CMS 5 14 | -------------------------------------------------------------------------------- /ecs.php: -------------------------------------------------------------------------------- 1 | paths([ 8 | __DIR__ . '/src', 9 | __FILE__, 10 | ]); 11 | $ecsConfig->parallel(); 12 | $ecsConfig->sets([SetList::CRAFT_CMS_4]); 13 | }; 14 | -------------------------------------------------------------------------------- /src/translations/en/minify.php: -------------------------------------------------------------------------------- 1 | 'Minify plugin loaded', 17 | ]; 18 | -------------------------------------------------------------------------------- /src/config.php: -------------------------------------------------------------------------------- 1 | false, 28 | 29 | // if set to `true` then Minify will not minify anything if `devMode` is enabled 30 | "disableDevModeMinifying" => true, 31 | 32 | ]; 33 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | Copyright (c) nystudio107 3 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 5 | 6 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 7 | 8 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 9 | -------------------------------------------------------------------------------- /src/twigextensions/MinifyTwigExtension.php: -------------------------------------------------------------------------------- 1 | [ 33 | 'minify' => MinifyService::class, 34 | ], 35 | ]; 36 | } 37 | 38 | // Public Methods 39 | // ========================================================================= 40 | 41 | /** 42 | * Returns the helper service 43 | * 44 | * @return MinifyService The minify service 45 | * @throws InvalidConfigException 46 | */ 47 | public function getMinify(): MinifyService 48 | { 49 | return $this->get('minify'); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/nystudio107/craft-minify/badges/quality-score.png?b=v5)](https://scrutinizer-ci.com/g/nystudio107/craft-minify/?branch=v5) [![Code Coverage](https://scrutinizer-ci.com/g/nystudio107/craft-minify/badges/coverage.png?b=v5)](https://scrutinizer-ci.com/g/nystudio107/craft-minify/?branch=v5) [![Build Status](https://scrutinizer-ci.com/g/nystudio107/craft-minify/badges/build.png?b=v5)](https://scrutinizer-ci.com/g/nystudio107/craft-minify/build-status/v5) [![Code Intelligence Status](https://scrutinizer-ci.com/g/nystudio107/craft-minify/badges/code-intelligence.svg?b=v5)](https://scrutinizer-ci.com/code-intelligence) 2 | 3 | # Minify plugin for Craft CMS 5.x 4 | 5 | A simple plugin that allows you to minify blocks of HTML, CSS, and JS inline in Craft CMS templates 6 | 7 | ![Screenshot](./docs/docs/resources/img/plugin-logo.png) 8 | 9 | ## Requirements 10 | 11 | This plugin requires Craft CMS 5.0.0 or later. 12 | 13 | ## Installation 14 | 15 | 1. Install with Composer via `composer require nystudio107/craft-minify` from your project directory 16 | 2. Install the plugin via `./craft install/plugin minify` via the CLI -or- in the Craft Control Panel under Settings > Plugins 17 | 18 | You can also install Minify via the **Plugin Store** in the Craft Control Panel. 19 | 20 | ## Documentation 21 | 22 | Click here -> [Minify Documentation](https://nystudio107.com/plugins/minify/documentation) 23 | 24 | Brought to you by [nystudio107](https://nystudio107.com/) 25 | -------------------------------------------------------------------------------- /src/models/Settings.php: -------------------------------------------------------------------------------- 1 | false], 52 | ['disableDevModeMinifying', 'boolean'], 53 | ['disableDevModeMinifying', 'default', 'value' => true], 54 | ]; 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "nystudio107/craft-minify", 3 | "description": "A simple plugin that allows you to minify blocks of HTML, CSS, and JS inline in Craft CMS templates.", 4 | "type": "craft-plugin", 5 | "version": "5.0.0", 6 | "keywords": [ 7 | "craft", 8 | "cms", 9 | "craftcms", 10 | "craft-plugin", 11 | "minify" 12 | ], 13 | "support": { 14 | "docs": "https://nystudio107.com/docs/minify/", 15 | "issues": "https://nystudio107.com/plugins/minify/support", 16 | "source": "https://github.com/nystudio107/craft-minify" 17 | }, 18 | "license": "MIT", 19 | "authors": [ 20 | { 21 | "name": "nystudio107", 22 | "homepage": "https://nystudio107.com/" 23 | } 24 | ], 25 | "require": { 26 | "php": "^8.2", 27 | "craftcms/cms": "^5.0.0", 28 | "mrclay/minify": "^3.0.10" 29 | }, 30 | "require-dev": { 31 | "craftcms/ecs": "dev-main", 32 | "craftcms/phpstan": "dev-main", 33 | "craftcms/rector": "dev-main" 34 | }, 35 | "scripts": { 36 | "phpstan": "phpstan --ansi --memory-limit=1G", 37 | "check-cs": "ecs check --ansi", 38 | "fix-cs": "ecs check --fix --ansi" 39 | }, 40 | "config": { 41 | "allow-plugins": { 42 | "craftcms/plugin-installer": true, 43 | "yiisoft/yii2-composer": true 44 | }, 45 | "optimize-autoloader": true, 46 | "sort-packages": true 47 | }, 48 | "autoload": { 49 | "psr-4": { 50 | "nystudio107\\minify\\": "src/" 51 | } 52 | }, 53 | "extra": { 54 | "class": "nystudio107\\minify\\Minify", 55 | "handle": "minify", 56 | "name": "Minify" 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/twigextensions/MinifyNode.php: -------------------------------------------------------------------------------- 1 | getAttribute('html'); 34 | $css = $this->getAttribute('css'); 35 | $js = $this->getAttribute('js'); 36 | 37 | $compiler 38 | ->addDebugInfo($this); 39 | 40 | $compiler 41 | ->write("ob_start();\n") 42 | ->subcompile($this->getNode('body')) 43 | ->write("\$_compiledBody = ob_get_clean();\n"); 44 | 45 | if ($html) { 46 | $compiler 47 | ->write("echo " . Minify::class . "::\$plugin->minify->htmlMin(\$_compiledBody);\n"); 48 | } elseif ($css) { 49 | $compiler 50 | ->write("echo " . Minify::class . "::\$plugin->minify->cssMin(\$_compiledBody);\n"); 51 | } elseif ($js) { 52 | $compiler 53 | ->write("echo " . Minify::class . "::\$plugin->minify->jsMin(\$_compiledBody);\n"); 54 | } else { 55 | $compiler 56 | ->write("echo " . Minify::class . "::\$plugin->minify->minify(\$_compiledBody);\n"); 57 | } 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/icon.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 6 | 7 | 11 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /src/twigextensions/MinifyTokenParser.php: -------------------------------------------------------------------------------- 1 | getLine(); 39 | $stream = $this->parser->getStream(); 40 | 41 | $attributes = [ 42 | 'html' => false, 43 | 'css' => false, 44 | 'js' => false, 45 | ]; 46 | 47 | if ($stream->test(Token::NAME_TYPE, 'html')) { 48 | $attributes['html'] = true; 49 | $stream->next(); 50 | } 51 | 52 | if ($stream->test(Token::NAME_TYPE, 'css')) { 53 | $attributes['css'] = true; 54 | $stream->next(); 55 | } 56 | 57 | if ($stream->test(Token::NAME_TYPE, 'js')) { 58 | $attributes['js'] = true; 59 | $stream->next(); 60 | } 61 | 62 | $stream->expect(Token::BLOCK_END_TYPE); 63 | $nodes['body'] = $this->parser->subparse([$this, 'decideMinifyEnd'], true); 64 | $stream->expect(Token::BLOCK_END_TYPE); 65 | 66 | return new MinifyNode($nodes, $attributes, $lineNo, $this->getTag()); 67 | } 68 | 69 | /** 70 | * @return string 71 | */ 72 | public function getTag(): string 73 | { 74 | return 'minify'; 75 | } 76 | 77 | /** 78 | * @param Token $token 79 | * 80 | * @return bool 81 | */ 82 | public function decideMinifyEnd(Token $token): bool 83 | { 84 | return $token->test('endminify'); 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /src/Minify.php: -------------------------------------------------------------------------------- 1 | name = $this->getName(); 70 | 71 | // Add in our Twig extensions 72 | Craft::$app->view->registerTwigExtension(new MinifyTwigExtension()); 73 | 74 | Craft::info( 75 | Craft::t( 76 | 'minify', 77 | '{name} plugin loaded', 78 | ['name' => $this->name] 79 | ), 80 | __METHOD__ 81 | ); 82 | } 83 | 84 | /** 85 | * Returns the user-facing name of the plugin, which can override the name 86 | * in composer.json 87 | * 88 | * @return string 89 | */ 90 | public function getName(): string 91 | { 92 | return Craft::t('minify', 'Minify'); 93 | } 94 | 95 | // Protected Methods 96 | // ========================================================================= 97 | 98 | /** 99 | * @inerhitDoc 100 | */ 101 | protected function createSettingsModel(): ?Model 102 | { 103 | return new Settings(); 104 | } 105 | } 106 | -------------------------------------------------------------------------------- /src/services/MinifyService.php: -------------------------------------------------------------------------------- 1 | getSettings()->disableTemplateMinifying) { 36 | $this->shouldMinify = false; 37 | } 38 | 39 | if (Craft::$app->getConfig()->getGeneral()->devMode 40 | && Minify::$plugin->getSettings()->disableDevModeMinifying) { 41 | $this->shouldMinify = false; 42 | } 43 | } 44 | 45 | /** 46 | * Minify all the things 47 | * 48 | * @param string $htmlText 49 | * 50 | * @return string 51 | */ 52 | public function minify(string $htmlText = ""): string 53 | { 54 | if ($this->shouldMinify) { 55 | $options = [ 56 | 'cssMinifier' => '\Minify_CSSmin::minify', 57 | 'jsMinifier' => '\JSMin\JSMin::minify', 58 | ]; 59 | $htmlText = Minify_HTML::minify($htmlText, $options); 60 | } 61 | 62 | return $htmlText; 63 | } 64 | 65 | /** 66 | * Minify the passed in HTML 67 | * 68 | * @param string $htmlText 69 | * 70 | * @return string 71 | */ 72 | public function htmlMin(string $htmlText = ""): string 73 | { 74 | if ($this->shouldMinify) { 75 | $htmlText = Minify_HTML::minify($htmlText); 76 | } 77 | 78 | return $htmlText; 79 | } 80 | 81 | /** 82 | * Minify the passed in CSS 83 | * 84 | * @param string $cssText 85 | * 86 | * @return string 87 | */ 88 | public function cssMin(string $cssText = ""): string 89 | { 90 | if ($this->shouldMinify) { 91 | $cssText = Minify_CSSmin::minify($cssText); 92 | } 93 | 94 | return $cssText; 95 | } 96 | 97 | /** 98 | * Minify the passed in JS 99 | * 100 | * @param string $jsText 101 | * 102 | * @return string 103 | */ 104 | public function jsMin(string $jsText = ""): string 105 | { 106 | if ($this->shouldMinify) { 107 | $jsText = JSMin::minify($jsText); 108 | } 109 | 110 | return $jsText; 111 | } 112 | } 113 | --------------------------------------------------------------------------------