From a7d4490b4f1fe60ff5ba25ab6552c5c1f7be76ce Mon Sep 17 00:00:00 2001
From: Marcin Kurczewski
Date: Thu, 11 Sep 2014 11:42:30 +0200
Subject: [PATCH] Changed minification engine (closed #4)
---
composer.json | 2 -
gruntfile.js | 103 +++++++++++++++++++++++++++++--
package.json | 4 ++
public_html/.gitignore | 5 +-
public_html/.htaccess | 2 +-
public_html/compile.php | 133 ----------------------------------------
public_html/index.html | 25 +++++++-
7 files changed, 131 insertions(+), 143 deletions(-)
delete mode 100644 public_html/compile.php
diff --git a/composer.json b/composer.json
index 439f238c..afd49f7b 100644
--- a/composer.json
+++ b/composer.json
@@ -1,8 +1,6 @@
{
"require": {
"mnapoli/php-di": "~4.0",
- "linkorb/jsmin-php": "1.*",
- "natxet/CssMin": "3.*",
"phpcheckstyle/phpcheckstyle": "~0.14"
}
}
diff --git a/gruntfile.js b/gruntfile.js
index e7680267..d308186c 100644
--- a/gruntfile.js
+++ b/gruntfile.js
@@ -1,16 +1,54 @@
var path = require('path');
+var fs = require('fs');
+var ini = require('ini');
module.exports = function(grunt) {
+ var phpCheckStyleConfigPath = path.join(path.resolve(), 'phpcheckstyle.cfg');
+ var phpSourcesDir = path.join(path.resolve(), 'src');
+ var jsSourcesDir = path.join(path.resolve(), 'public_html/js');
+ var cssSourcesDir = path.join(path.resolve(), 'public_html/css');
+ var templatesDir = path.join(path.resolve(), 'public_html/templates');
+
+ var config = readConfig([
+ path.join(path.resolve(), 'data/config.ini'),
+ path.join(path.resolve(), 'data/local.ini')
+ ]);
+
+ function readConfig(configPaths) {
+ var iniContent = '';
+ for (var i = 0; i < configPaths.length; i ++) {
+ var configPath = configPaths[i];
+ if (fs.existsSync(configPath)) {
+ iniContent += fs.readFileSync(configPath, 'utf-8');
+ }
+ }
+ var config = ini.parse(iniContent);
+ return config;
+ }
+
+ function readTemplates() {
+ var templatePaths = grunt.file.expand(templatesDir + '/**/*.tpl');
+ var templates = {};
+ for (var i = 0; i < templatePaths.length; i ++) {
+ var templatePath = templatePaths[i];
+ templates[path.basename(templatePath)] = fs.readFileSync(templatePath);
+ }
+ return templates;
+ }
+
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
- phpCheckStyleConfigPath: path.join(path.resolve(), 'phpcheckstyle.cfg'),
- phpSourcesDir: path.join(path.resolve(), 'src'),
- jsSourcesDir: path.join(path.resolve(), 'public_html'),
+ phpCheckStyleConfigPath: phpCheckStyleConfigPath,
+ phpSourcesDir: phpSourcesDir,
+ jsSourcesDir: jsSourcesDir,
+ cssSourcesDir: cssSourcesDir,
+
+ config: config,
jshint: {
- files: ['<%= jsSourcesDir %>/**/*.js'],
+ files: [jsSourcesDir + '/**/*.js'],
options: {
globals: {
console: true,
@@ -45,11 +83,66 @@ module.exports = function(grunt) {
command: 'phpunit --strict --bootstrap src/AutoLoader.php tests/',
},
},
+
+ cssmin: {
+ combine: {
+ files: {
+ 'public_html/app.min.css': [cssSourcesDir + '/**/*.css'],
+ },
+ },
+ },
+
+ uglify: {
+ dist: {
+ options: {
+ mangle: false, //breaks dependency injection
+ sourceMap: true,
+ },
+ files: {
+ 'public_html/app.min.js': [].concat(
+ [jsSourcesDir + '/DI.js'],
+ grunt.file.expand({
+ filter: function(src) {
+ return !src.match(/(DI|Bootstrap)\.js/);
+ }
+ }, jsSourcesDir + '/**/*.js'),
+ [jsSourcesDir + '/Bootstrap.js']),
+ },
+ },
+ },
+
+ processhtml: {
+ options: {
+ data: {
+ serviceName: config.basic.serviceName,
+ templates: readTemplates(),
+ timestamp: grunt.template.today('isoDateTime'),
+ }
+ },
+ dist: {
+ files: {
+ 'public_html/app.min.html': ['public_html/index.html']
+ }
+ }
+ },
});
grunt.loadNpmTasks('grunt-contrib-jshint');
+ grunt.loadNpmTasks('grunt-processhtml');
grunt.loadNpmTasks('grunt-shell');
- grunt.registerTask('default', ['jshint', 'shell']);
+ grunt.loadNpmTasks('grunt-contrib-uglify');
+ grunt.loadNpmTasks('grunt-contrib-cssmin');
+
+ grunt.registerTask('default', ['checkstyle', 'tests']);
+ grunt.registerTask('checkstyle', ['jshint', 'shell:phpcheckstyle']);
grunt.registerTask('tests', ['shell:tests']);
+ grunt.registerTask('clean', function() {
+ fs.unlink('public_html/app.min.html');
+ fs.unlink('public_html/app.min.js');
+ fs.unlink('public_html/app.min.js.map');
+ fs.unlink('public_html/app.min.css');
+ });
+ grunt.registerTask('build', ['clean', 'uglify', 'cssmin', 'processhtml']);
+
};
diff --git a/package.json b/package.json
index e131fd46..abd06fe3 100644
--- a/package.json
+++ b/package.json
@@ -3,7 +3,11 @@
"version": "0.0.0",
"devDependencies": {
"requirejs": "*",
+ "ini": "*",
"grunt": "~0.4.5",
+ "grunt-processhtml": "*",
+ "grunt-contrib-uglify": "*",
+ "grunt-contrib-cssmin": "*",
"grunt-contrib-jshint": "~0.10.0",
"grunt-shell": "~1.1.1"
}
diff --git a/public_html/.gitignore b/public_html/.gitignore
index 24320287..db7b7253 100644
--- a/public_html/.gitignore
+++ b/public_html/.gitignore
@@ -1 +1,4 @@
-index-compiled.html
+app.min.html
+app.min.js
+app.min.js.map
+app.min.css
diff --git a/public_html/.htaccess b/public_html/.htaccess
index b69f8bae..7568c3df 100644
--- a/public_html/.htaccess
+++ b/public_html/.htaccess
@@ -1,4 +1,4 @@
-DirectoryIndex index-compiled.html
+DirectoryIndex app.min.html
DirectoryIndex index.html
RewriteEngine On
diff --git a/public_html/compile.php b/public_html/compile.php
deleted file mode 100644
index eb86646a..00000000
--- a/public_html/compile.php
+++ /dev/null
@@ -1,133 +0,0 @@
-)/ms', $html, -1,
- PREG_SPLIT_DELIM_CAPTURE);
- $buffer = '';
- foreach ($chunks as $chunk)
- {
- if (in_array($chunk, $illegalTags))
- continue;
-
- if (preg_match('/^<(' . join('|', $illegalTags) . ')/', $chunk))
- {
- $buffer .= $chunk;
- continue;
- }
-
- # remove new lines & tabs
- $chunk = preg_replace( '/[\\n\\r\\t]+/', ' ', $chunk);
- # remove extra whitespace
- $chunk = preg_replace( '/\\s{2,}/', ' ', $chunk);
- # remove inter-tag whitespace
- $chunk = preg_replace( '/>\\s', '><', $chunk);
- # remove CSS & JS comments
- $chunk = preg_replace( '/\\/\\*.*?\\*\\//i', '', $chunk);
- $buffer .= $chunk;
- }
- return $buffer;
- }
-}
-
-class IndexBuilder
-{
- public static function build()
- {
- $html = file_get_contents(__DIR__ . DS . 'index.html');
- self::includeTemplates($html);
- self::minifyScripts($html);
- self::minifyStylesheets($html);
- return $html;
- }
-
- private static function injectBody(&$html, $text)
- {
- $html = str_replace('
@@ -28,6 +38,18 @@
+
+
+
+
@@ -56,5 +78,6 @@
+
', $text . '', $html);
- }
-
- private static function injectHead(&$html, $text)
- {
- $html = str_replace('', $text . '', $html);
- }
-
- private static function minifyScripts(&$html)
- {
- $scriptsToMinify = [];
-
- $html = preg_replace_callback(
- '/';
- self::injectBody($html, $out);
- }
-
- private static function minifyStylesheets(&$html)
- {
- $html = preg_replace_callback(
- '/]*href="([^"]+)"[^>]*>/',
- function($matches) use (&$stylesToMinify)
- {
- $stylePath = $matches[1];
- if (substr($stylePath, 0, 2) == '//' or strpos($stylePath, 'http') !== false)
- return $matches[0];
- if (strpos($matches[0], 'css') === false)
- return $matches[0];
- $stylesToMinify []= __DIR__ . DS . $stylePath;
- return '';
- }, $html);
-
- $out = '';
- self::injectHead($html, $out);
- }
-
- private static function includeTemplates(&$html)
- {
- $templatesToInclude = [];
- foreach (glob(__DIR__ . DS . 'templates' . DS . '*.tpl') as $templatePath)
- $templatesToInclude []= $templatePath;
-
- $out = '';
- foreach ($templatesToInclude as $templatePath)
- {
- $out .= '';
- }
- self::injectBody($html, $out);
- }
-}
-
-$compiledPath = __DIR__ . DS . 'index-compiled.html';
-$html = IndexBuilder::build();
-$html = Compressor::html($html);
-file_put_contents($compiledPath, $html);
diff --git a/public_html/index.html b/public_html/index.html
index e307c765..97c7795f 100644
--- a/public_html/index.html
+++ b/public_html/index.html
@@ -2,11 +2,20 @@