From d03d889024633938baf672ec09ea3cc8430a8055 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Wed, 29 Nov 2017 12:29:27 +0100 Subject: [PATCH] Webpack bundle management with hashes (core.router) : 'public_html/js/bundle@{hash}.js' + pre-script to remove bundles (packages.json) + min updates --- .gitignore | 4 ++-- build/router/controller/page.php | 25 ++++++++++++++++++++++++- config/routes.json | 6 ++++++ package.json | 12 +++++++----- view/home.php | 2 +- view/main.js | 27 +++++++++++++++++++++------ view/vue/main.vue | 6 +++--- webpack.config.js | 2 +- 8 files changed, 65 insertions(+), 19 deletions(-) diff --git a/.gitignore b/.gitignore index e531552..e00c249 100755 --- a/.gitignore +++ b/.gitignore @@ -4,8 +4,8 @@ node_modules/ dist/ npm-debug.log yarn-error.log -public_html/js/bundle.js -public_html/js/bundle.js* +public_html/js/bundle@*.js +public_html/js/bundle@*.js* package-lock.json # Editor directories and files diff --git a/build/router/controller/page.php b/build/router/controller/page.php index 59a056f..a2ff3ea 100755 --- a/build/router/controller/page.php +++ b/build/router/controller/page.php @@ -14,7 +14,8 @@ * */ public function __construct($url){ - $this->pagename = $url['page']; + $this->pagename = isset($url['page']) ? $url['page'] : null; + } @@ -29,6 +30,28 @@ echo "page not found"; } + + /* Manage bundle hash + * + */ + public function bundle(){ + + /* (1) Extract /public_html/js/ all .js files */ + $js_scripts = glob(__PUBLIC__.'/js/*.js'); + + /* (2) If match pattern 'bundle@*.js' */ + foreach($js_scripts as $fname){ + + $bname = basename($fname); + + // if match -> load it and exit + if( preg_match('/bundle@[\da-f]+\.js/', $bname) ) + header("Location: /js/$bname"); + + } + + } + /* POST-CALL * */ diff --git a/config/routes.json b/config/routes.json index 870be7b..9c83a1e 100755 --- a/config/routes.json +++ b/config/routes.json @@ -5,6 +5,12 @@ "routes": { + "/js/bundle.js": { + "methods": ["GET"], + "controller": "page:bundle", + "arguments": {} + }, + "/{page}/": { "methods": ["GET"], "controller": "page:load", diff --git a/package.json b/package.json index 03153e4..6e86390 100755 --- a/package.json +++ b/package.json @@ -7,10 +7,12 @@ "private": true, "scripts": { "dev": "cross-env NODE_ENV=development webpack-dev-server --open --hot", - "build": "cross-env NODE_ENV=production webpack --progress --hide-modules" + "build:clean": "rm ./public_html/js/bundle@*.js*", + "build:bundle": "cross-env NODE_ENV=production webpack --progress --hide-modules", + "build": "npm run build:clean && npm run build:bundle" }, "dependencies": { - "vue": "^2.4.4" + "vue": "^2.5.9" }, "browserslist": [ "> 1%", @@ -26,8 +28,8 @@ "css-loader": "^0.28.7", "file-loader": "^1.1.4", "vue-loader": "^13.0.5", - "vue-template-compiler": "^2.4.4", - "webpack": "^3.6.0", - "webpack-dev-server": "^2.9.1" + "vue-template-compiler": "^2.5.9", + "webpack": "^3.8.1", + "webpack-dev-server": "^2.9.5" } } diff --git a/view/home.php b/view/home.php index 6d9be51..a94f687 100755 --- a/view/home.php +++ b/view/home.php @@ -18,7 +18,7 @@ -
+
diff --git a/view/main.js b/view/main.js index c21d853..ee0ec81 100755 --- a/view/main.js +++ b/view/main.js @@ -1,11 +1,26 @@ +/* (1) Imports +---------------------------------------------------------*/ +/* (1) NPM libs */ import Vue from 'vue' -import {API} from './lib/api-es6' -// import App from './vue/main.vue' +/* (2) Internal libs */ +import {API} from './lib/api-es6' + +/* (3) Vues */ +import main_vue from './vue/main.vue' + + +/* (2) Initialisation +---------------------------------------------------------*/ +/* (1) API */ +window.api = new API("http://ndli1718/api/v/1.0/"); + +/* (2) wsclient */ +//todo: init websocket client + +/* (3) Render view */ new Vue({ - el: '#mainview', - render: h => h(require('./vue/main.vue')) + el: '#main-vue', + render: h => h(main_vue) }) - -window.api = new API("http://ndli1718/api/v/1.0/"); \ No newline at end of file diff --git a/view/vue/main.vue b/view/vue/main.vue index f741ca7..403e04c 100755 --- a/view/vue/main.vue +++ b/view/vue/main.vue @@ -1,5 +1,5 @@