From b7ec06ffdc7e6bb889b267de68b8839af7c05fff Mon Sep 17 00:00:00 2001 From: Juan Ferrer Toribio Date: Thu, 2 Mar 2017 11:01:29 +0100 Subject: [PATCH] Webpack caching --- .eslintrc.yml | 3 + .gitignore | 2 + app.js | 20 ++++--- debian/changelog | 2 +- debian/control | 4 +- debian/rules | 2 +- env.php | 1 - forms/ecomerce/catalog/style.css | 1 - package.json | 12 ++-- pages/main/analytics.html | 9 --- pages/main/head.php | 17 ------ pages/main/ui.php | 57 ++++++++++++++++++- pages/tpv/locale/ca.json | 4 -- pages/tpv/locale/en.json | 4 -- pages/tpv/locale/es.json | 4 -- pages/tpv/locale/fr.json | 4 -- pages/tpv/locale/mn.json | 4 -- pages/tpv/locale/pt.json | 4 -- pages/tpv/style.css | 16 ------ pages/tpv/ui.php | 22 -------- rest/misc/mail.php | 1 + web/html-service.php | 36 +----------- web/html.php | 39 +++++++++++++ web/json-service.php | 10 +--- web/service.php | 68 +++++++++++++---------- webpack.config.js | 95 ++++++++++++++++++++------------ webpack.config.json | 8 +++ 27 files changed, 237 insertions(+), 212 deletions(-) create mode 100644 .eslintrc.yml delete mode 100755 pages/main/analytics.html delete mode 100755 pages/main/head.php delete mode 100755 pages/tpv/locale/ca.json delete mode 100755 pages/tpv/locale/en.json delete mode 100755 pages/tpv/locale/es.json delete mode 100755 pages/tpv/locale/fr.json delete mode 100755 pages/tpv/locale/mn.json delete mode 100644 pages/tpv/locale/pt.json delete mode 100755 pages/tpv/style.css delete mode 100755 pages/tpv/ui.php create mode 100644 web/html.php create mode 100644 webpack.config.json diff --git a/.eslintrc.yml b/.eslintrc.yml new file mode 100644 index 00000000..de984486 --- /dev/null +++ b/.eslintrc.yml @@ -0,0 +1,3 @@ +extends: eslint:recommended +rules: + no-undef: 0 \ No newline at end of file diff --git a/.gitignore b/.gitignore index 3c3629e6..ff7a2e19 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ node_modules +npm-debug.log +build diff --git a/app.js b/app.js index 5407c0a9..138ca36b 100644 --- a/app.js +++ b/app.js @@ -1,18 +1,21 @@ -var host = window.location.host.split(':')[0]; +var assetsPath; if (_DEV_MODE) - host = host +':8080'; +{ + var host = window.location.host.split(':')[0]; + assetsPath = 'http://'+ host +':'+ _DEV_SERVER_PORT +'/'+ _PUBLIC_PATH; +} +else + assetsPath = _PUBLIC_PATH; -__webpack_public_path__ = '//'+ host +'/build/'; +__webpack_public_path__ = assetsPath; require ('hedera/hedera'); window.onload = function () { - Vn.Locale.init (); - var lang = Vn.Locale.language; - loadLocale (lang, main); + loadLocale (main); } function main (req) @@ -26,8 +29,11 @@ function main (req) hederaWeb.run (); } -function loadLocale (lang, cb) +function loadLocale (cb) { + Vn.Locale.init (); + var lang = Vn.Locale.language; + switch (lang) { case 'ca': diff --git a/debian/changelog b/debian/changelog index 8df29a1b..45f05bdb 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,4 +1,4 @@ -hedera-web (1.390-deb8) stable; urgency=low +hedera-web (2.0.1) stable; urgency=low * Initial Release. diff --git a/debian/control b/debian/control index 3880d954..d3ccdba8 100644 --- a/debian/control +++ b/debian/control @@ -9,8 +9,8 @@ Vcs-Git: git://www.verdnatura.es/var/git/hedera-web Package: hedera-web Architecture: all -Depends: apache2, php5-mysql, php5-mcrypt, php5-ssh2, php-vn-lib, nodejs, npm -Suggests: php-text-captcha, php5-imap, tinymce +Depends: apache2, php5-mysql, php5-mcrypt, php5-ssh2, php-vn-lib, php-acpu, nodejs, npm +Suggests: php-text-captcha, php5-imap Section: misc Priority: optional Description: Verdnatura's web page diff --git a/debian/rules b/debian/rules index a8f9a7e2..18794ac0 100755 --- a/debian/rules +++ b/debian/rules @@ -6,7 +6,7 @@ dh $@ clean: - npm --production run clean + npm run clean dh_clean $@ build: diff --git a/env.php b/env.php index 3ddf9c0a..55bbbf13 100644 --- a/env.php +++ b/env.php @@ -11,4 +11,3 @@ const _DEV_MODE = TRUE; const _CONFIG_DIR = __DIR__.'/../../.config'; const _LOG_DIR = '/tmp'; const _DATA_DIR = '/tmp'; - diff --git a/forms/ecomerce/catalog/style.css b/forms/ecomerce/catalog/style.css index d9a93979..ff526bbb 100644 --- a/forms/ecomerce/catalog/style.css +++ b/forms/ecomerce/catalog/style.css @@ -189,7 +189,6 @@ .item-info .producer { text-transform: uppercase; - font-weight: bold; padding: 0; padding-bottom: .2em; font-size: 1em; diff --git a/package.json b/package.json index 051cdc02..8e77c567 100644 --- a/package.json +++ b/package.json @@ -1,17 +1,21 @@ { "name": "hedera-web", - "version": "1.0.0", + "version": "2.0.1", "description": "Verdnatura web page", "devDependencies": { + "assets-webpack-plugin": "^3.5.1", "bundle-loader": "^0.5.4", "css-loader": "^0.25.0", + "eslint": "^3.16.1", "file-loader": "^0.9.0", "json-loader": "^0.5.4", "raw-loader": "^0.5.1", "style-loader": "^0.13.1", "url-loader": "^0.5.7", - "webpack": "^1.13.3", - "webpack-dev-server": "^1.16.2" + "webpack": "^2.2.0", + "webpack-chunk-hash": "^0.4.0", + "webpack-dev-server": "^2.2.0", + "webpack-merge": "^3.0.0" }, "dependencies": { "mootools": "^1.5.2", @@ -19,7 +23,7 @@ }, "scripts": { "dev": "webpack-dev-server --progress --colors --hot", - "build": "rm -f build/* ; webpack --progress --colors", + "build": "rm -rf build/ ; webpack --progress --colors", "clean": "rm -rf build/" } } diff --git a/pages/main/analytics.html b/pages/main/analytics.html deleted file mode 100755 index 22fa2837..00000000 --- a/pages/main/analytics.html +++ /dev/null @@ -1,9 +0,0 @@ - diff --git a/pages/main/head.php b/pages/main/head.php deleted file mode 100755 index c25c13a1..00000000 --- a/pages/main/head.php +++ /dev/null @@ -1,17 +0,0 @@ -'."\n\t\t"; - -if ($result = $db->query ('SELECT name, content FROM metatag')) -{ - while ($row = $result->fetch_assoc ()) - echo "\n\t\t"; - - $result->free (); -} - -$url = _DEV_MODE ? "http://{$_SERVER['SERVER_NAME']}:8080/" : ''; -$this->includeJs ("{$url}build/hedera-web.js"); - diff --git a/pages/main/ui.php b/pages/main/ui.php index 65006b62..f081b50b 100755 --- a/pages/main/ui.php +++ b/pages/main/ui.php @@ -1,5 +1,50 @@ - +query('SELECT name, content FROM metatag'); + +$wpConfig = json_decode (file_get_contents ('webpack.config.json')); +$buildDir = $wpConfig->buildDir; +$devServerPort = $wpConfig->devServerPort; + +$host = $_SERVER['SERVER_NAME']; +$assets = new stdClass(); + +if (!_DEV_MODE) +{ + $wpAssets = json_decode (file_get_contents ("$buildDir/webpack-assets.json")); + + $manifestJs = $wpAssets->manifest->js; + $mainJs = $wpAssets->main->js; + unset ($wpAssets->manifest); + unset ($wpAssets->main); + + foreach ($wpAssets as $name => $asset) + if (property_exists ($asset, 'js')) + $assets->$name = $asset->js; +} +else +{ + $devServerPath = "http://$host:$devServerPort/$buildDir"; + $manifestJs = "$devServerPath/manifest.js"; + $mainJs = "$devServerPath/main.js"; + + unset ($wpConfig->entry->main); + + foreach ($wpConfig->entry as $asset => $files) + $assets->$asset = "$devServerPath/$asset.js"; +} + +$jsFiles = []; +$jsFiles[] = $manifestJs; + +foreach ($assets as $jsFile) + $jsFiles[] = $jsFile; + +$jsFiles[] = $mainJs; + +?> + @@ -11,7 +56,15 @@ - + + + fetch_object()): ?> + + + + + + Verdnatura diff --git a/pages/tpv/locale/ca.json b/pages/tpv/locale/ca.json deleted file mode 100755 index 58ac070e..00000000 --- a/pages/tpv/locale/ca.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "PaymentComplete": "Pagament acabat, ja pots tornar a la nostra pàgina web." - ,"ReturnToWeb": "Tornar a Verdnatura" -} diff --git a/pages/tpv/locale/en.json b/pages/tpv/locale/en.json deleted file mode 100755 index 4e04b791..00000000 --- a/pages/tpv/locale/en.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "PaymentComplete": "Payment completed, you can return to our website." - ,"ReturnToWeb": "Back to Verdnatura" -} diff --git a/pages/tpv/locale/es.json b/pages/tpv/locale/es.json deleted file mode 100755 index ec4c2a75..00000000 --- a/pages/tpv/locale/es.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "PaymentComplete": "Pago terminado, ya puedes volver a nuestra página web." - ,"ReturnToWeb": "Volver a Verdnatura" -} diff --git a/pages/tpv/locale/fr.json b/pages/tpv/locale/fr.json deleted file mode 100755 index 46c0940c..00000000 --- a/pages/tpv/locale/fr.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "PaymentComplete": "Paiement terminé, merci par votre confiance, maintenant vous pouvez revenir à notre site." - ,"ReturnToWeb": "Retour au Verdnatura" -} diff --git a/pages/tpv/locale/mn.json b/pages/tpv/locale/mn.json deleted file mode 100755 index 4e04b791..00000000 --- a/pages/tpv/locale/mn.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "PaymentComplete": "Payment completed, you can return to our website." - ,"ReturnToWeb": "Back to Verdnatura" -} diff --git a/pages/tpv/locale/pt.json b/pages/tpv/locale/pt.json deleted file mode 100644 index 621bd916..00000000 --- a/pages/tpv/locale/pt.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "PaymentComplete": "Pagamento terminado, já podes voltar a nossa página web." - ,"ReturnToWeb": "Voltar a Verdnatura" -} diff --git a/pages/tpv/style.css b/pages/tpv/style.css deleted file mode 100755 index b43a1ce8..00000000 --- a/pages/tpv/style.css +++ /dev/null @@ -1,16 +0,0 @@ -* -{ - font-family: 'Roboto'; - font-size: 10pt; -} -div -{ - position: absolute; - width: 48em; - height: 10em; - margin-left: -24em; - margin-top: -5em; - top: 50%; - left: 50%; - text-align: center; -} diff --git a/pages/tpv/ui.php b/pages/tpv/ui.php deleted file mode 100755 index 6ceea6a0..00000000 --- a/pages/tpv/ui.php +++ /dev/null @@ -1,22 +0,0 @@ - - - - - - - - - Verdnatura - - -
- Verdnatura -

- -

-

- -

-
- - diff --git a/rest/misc/mail.php b/rest/misc/mail.php index f816b5c8..b76a7409 100755 --- a/rest/misc/mail.php +++ b/rest/misc/mail.php @@ -58,6 +58,7 @@ class Mail extends Vn\Lib\Method foreach ($mailList as $mailTo) $mail->AddAddress ($mailTo); + $mail->IsHTML (TRUE); $mail->Subject = $row['subject']; $mail->Body = ' '. $row['text']; $mail->CharSet = 'UTF-8'; diff --git a/web/html-service.php b/web/html-service.php index 928d6745..dee901fe 100644 --- a/web/html-service.php +++ b/web/html-service.php @@ -2,6 +2,8 @@ namespace Vn\Web; +require_once __DIR__.'/html.php'; + use Vn\Lib\Locale; /** @@ -101,43 +103,9 @@ class HtmlService extends Service exit (0); } - function getUrl ($fileName) - { -/* if (file_exists ($fileName)) - $mTime = '?'. strftime ('%G%m%d%H%M%S', filemtime ($fileName)); - else -*/ $mTime = '?'. $this->getVersion (); - - return $fileName.$mTime; - } - - function includeJs ($fileName) - { - echo ''."\n\t\t"; - } - - function includeLib ($libName) - { - $args = func_get_args (); - - $localeJs = 'locale/'. Locale::get () .'/js/'. $libName .'.js'; - - if (file_exists ($localeJs)) - $this->includeJs ($localeJs); - - for ($i = 1; $i < count ($args); $i++) - $this->includeJs ("js/$libName/{$args[$i]}.js"); - } - - function includeCss ($fileName) - { - echo ''."\n\t\t"; - } - function isMobile () { $re = '/(Android|webOS|iPhone|iPad|iPod|BlackBerry|Windows Phone)/i'; return preg_match ($re, $_SERVER['HTTP_USER_AGENT']); } } - diff --git a/web/html.php b/web/html.php new file mode 100644 index 00000000..e1e7fae3 --- /dev/null +++ b/web/html.php @@ -0,0 +1,39 @@ +'."\n"; +} + +function css ($fileName) +{ + return ''."\n"; +} diff --git a/web/json-service.php b/web/json-service.php index e6994c6a..59ead94a 100644 --- a/web/json-service.php +++ b/web/json-service.php @@ -18,15 +18,7 @@ class JsonService extends RestService set_exception_handler ([$this, 'exceptionHandler']); $this->startSession (); - - // Checks the client version - - if (!empty ($_COOKIE['vnVersion'])) - $clientVersion = (int) $_COOKIE['vnVersion']; - - if (isset ($clientVersion) - && $clientVersion < $this->getVersion ()) - throw new OutdatedVersionException (); + $this->checkVersion (); $json = $this->loadMethod (__NAMESPACE__.'\JsonRequest'); $this->replyJson ($json); diff --git a/web/service.php b/web/service.php index ab8901c7..eac532a0 100755 --- a/web/service.php +++ b/web/service.php @@ -294,6 +294,46 @@ abstract class Service return $res; } + + /** + * Obtains the application version number. It is extracted and + * cached from package.json file. + * + * @return string The version number + */ + function getVersion () + { + $appName = $this->app->getName (); + $version = apc_fetch("$appName.version", $success); + + if (!$success) + { + if (file_exists ('package.json')) + { + $package = json_decode (file_get_contents ('package.json')); + $version = $package->version; + } + else + $version = "0.0.0"; + + apc_store ("$appName.version", $version); + } + + return $version; + } + + /** + * Checks the client version. + */ + function checkVersion () + { + if (!empty ($_COOKIE['vnVersion'])) + $clientVersion = $_COOKIE['vnVersion']; + + if (isset ($clientVersion) + && $clientVersion < $this->getVersion ()) + throw new OutdatedVersionException (); + } /** * Checks if the HTTP connection is secure. @@ -315,33 +355,5 @@ abstract class Service $proto = $this->isHttps () ? 'https' : 'http'; return "$proto://{$_SERVER['SERVER_NAME']}{$_SERVER['REQUEST_URI']}"; } - - /** - * Obtains the application version number. It is based on de last - * modification date of the main script. - * - * @return string The version number - */ - function getVersion () - { - return (int) strftime ('%G%m%d%H%M%S', - filectime ($_SERVER['SCRIPT_FILENAME'])); - } - - /** - * Obtains the relative path to document root from an absolute path. - * - * @return string The relative path - */ - function getDir ($absoluteDir) - { - error_log ("Absolute: $absoluteDir"); - error_log ("Root: {$_SERVER['DOCUMENT_ROOT']}"); - error_log ("Script: {$_SERVER['SCRIPT_FILENAME']}"); - error_log ("Self: {$_SERVER['PHP_SELF']}"); - - $rootLen = strlen ($_SERVER['DOCUMENT_ROOT']); - return substr ($absoluteDir, $rootLen); - } } diff --git a/webpack.config.js b/webpack.config.js index a6168619..69f283a8 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -1,53 +1,80 @@ - -var webpack = require ('webpack'); var path = require ('path'); +var webpack = require ('webpack'); +var AssetsWebpackPlugin = require ('assets-webpack-plugin'); +var WebpackChunkHash = require ('webpack-chunk-hash'); +var merge = require ('webpack-merge'); +var wpConfig = require ('./webpack.config.json'); var devMode = process.env.NODE_ENV !== 'production'; +var outputPath = path.join (__dirname, wpConfig.buildDir); +var publicPath = wpConfig.buildDir +'/'; -var config = { - entry: ['./app.js'], +var baseConfig = { + entry: wpConfig.entry, output: { - path: path.join (__dirname, 'build'), - filename: 'hedera-web.js', - chunkFilename: 'chunk.[id].[chunkhash].js', - publicPath: 'build/' + path: outputPath, + publicPath: publicPath, + filename: '[name].js', + chunkFilename: 'chunk.[id].js' }, module: { - loaders: [ - { test: /\.css$/, loader: 'style!css' }, - { test: /\.json$/, loader: 'json' }, - { test: /\.xml$/, loader: 'raw' }, - { test: /\.ttf$/, loader: 'file' } + rules: [ + { test: /\.css$/, loader: 'style-loader!css-loader' }, + { test: /\.xml$/, loader: 'raw-loader' }, + { test: /\.ttf$/, loader: 'file-loader' } ] }, resolve: { - modulesDirectories: [__dirname +'/js', __dirname, 'node_modules'], - fallback: process.env.NODE_PATH, - }, - resolveLoader: { - fallback: process.env.NODE_PATH + modules: [ + __dirname +'/js', + __dirname, + 'node_modules', + '/usr/lib/node_modules' + ] }, plugins: [ - new webpack.DefinePlugin ({ _DEV_MODE: devMode }) + new webpack.DefinePlugin ({ + _DEV_MODE: devMode, + _DEV_SERVER_PORT: wpConfig.devServerPort, + _PUBLIC_PATH: JSON.stringify (publicPath) + }), + new webpack.optimize.CommonsChunkPlugin ({ + names: ['vendor', 'manifest'] + }) + ] +}; + +var prodConfig = { + output: { + filename: '[name].[chunkhash].js', + chunkFilename: 'chunk.[id].[chunkhash].js' + }, + plugins: [ + new webpack.optimize.UglifyJsPlugin ({ + minimize: true, + compress: { warnings: false } + }), + new AssetsWebpackPlugin ({ + path: outputPath + }), + new webpack.HashedModuleIdsPlugin (), + new WebpackChunkHash () + ], + devtool: 'source-map' +}; + +var devConfig = { + plugins: [ + new webpack.NamedModulesPlugin () ], devServer: { - inline: true, host: '0.0.0.0', - headers: { "Access-Control-Allow-Origin": "*" } + port: wpConfig.devServerPort, + headers: { "Access-Control-Allow-Origin": "*" }, + stats: { chunks: false } }, devtool: 'eval-source-map' }; -if (!devMode) -{ - config.plugins.push ( - new webpack.optimize.UglifyJsPlugin ({ - minimize: true, - compress: { warnings: false } - }) - ); - config.devtool = 'source-map'; -} - -module.exports = config; - +var mrgConfig = devMode ? devConfig : prodConfig; +module.exports = merge (baseConfig, mrgConfig); diff --git a/webpack.config.json b/webpack.config.json new file mode 100644 index 00000000..127ce7a0 --- /dev/null +++ b/webpack.config.json @@ -0,0 +1,8 @@ +{ + "buildDir": "build", + "devServerPort": 9000, + "entry": { + "main": "./app.js", + "vendor": "mootools" + } +} \ No newline at end of file