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('', $text . '', $html); - } - - private static function injectHead(&$html, $text) - { - $html = str_replace('', $text . '', $html); - } - - private static function minifyScripts(&$html) - { - $scriptsToMinify = []; - - $html = preg_replace_callback( - '/]*src="([^"]+)"[^>]*><\/script>/', - function($matches) use (&$scriptsToMinify) - { - $scriptPath = $matches[1]; - if (substr($scriptPath, 0, 2) == '//' or strpos($scriptPath, 'http') !== false) - return $matches[0]; - $scriptsToMinify []= __DIR__ . DS . $scriptPath; - return ''; - }, $html); - - $out = ''; - 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 @@ - szurubooru + + szuru2 + + + + @@ -17,6 +26,7 @@ + @@ -28,6 +38,18 @@ + + + + @@ -56,5 +78,6 @@ +