From 0ade9b5b9b819697fdea33e9c7698e33e1d0057f Mon Sep 17 00:00:00 2001 From: Dan Brown Date: Sun, 1 Apr 2018 14:10:12 +0100 Subject: [PATCH] Refactored moment.js out of app Reduces bundle size by 25% --- .gitignore | 1 + app/Http/Controllers/PageController.php | 3 +-- package-lock.json | 19 ------------------- package.json | 2 +- resources/assets/js/services/dates.js | 15 +++++++++++++++ resources/assets/js/vues/page-editor.js | 7 ++----- resources/assets/js/vues/search.js | 4 ++-- 7 files changed, 22 insertions(+), 29 deletions(-) create mode 100644 resources/assets/js/services/dates.js diff --git a/.gitignore b/.gitignore index be86e5a03..1b53cbe7a 100644 --- a/.gitignore +++ b/.gitignore @@ -21,3 +21,4 @@ nbproject .buildpath .project .settings/ +webpack-stats.json \ No newline at end of file diff --git a/app/Http/Controllers/PageController.php b/app/Http/Controllers/PageController.php index 17bce7eba..9cc73ae15 100644 --- a/app/Http/Controllers/PageController.php +++ b/app/Http/Controllers/PageController.php @@ -275,11 +275,10 @@ class PageController extends Controller $draft = $this->entityRepo->updatePageDraft($page, $request->only(['name', 'html', 'markdown'])); $updateTime = $draft->updated_at->timestamp; - $utcUpdateTimestamp = $updateTime + Carbon::createFromTimestamp(0)->offset; return response()->json([ 'status' => 'success', 'message' => trans('entities.pages_edit_draft_save_at'), - 'timestamp' => $utcUpdateTimestamp + 'timestamp' => $updateTime ]); } diff --git a/package-lock.json b/package-lock.json index ddb40574e..c794a62da 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3645,15 +3645,6 @@ "is-extglob": "1.0.0" } }, - "extract-loader": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/extract-loader/-/extract-loader-1.0.2.tgz", - "integrity": "sha512-hwlXWGHwzBXRNQCkDnLJuNgSkRsmYOwNz7wG9pHfA2EAgQaBCuQR71az7qL3rQT1JAMujiKPc+laet0kddVXWQ==", - "dev": true, - "requires": { - "loader-utils": "1.1.0" - } - }, "extract-text-webpack-plugin": { "version": "4.0.0-beta.0", "resolved": "https://registry.npmjs.org/extract-text-webpack-plugin/-/extract-text-webpack-plugin-4.0.0-beta.0.tgz", @@ -3710,16 +3701,6 @@ "escape-string-regexp": "1.0.5" } }, - "file-loader": { - "version": "1.1.11", - "resolved": "https://registry.npmjs.org/file-loader/-/file-loader-1.1.11.tgz", - "integrity": "sha512-TGR4HU7HUsGg6GCOPJnFk06RhWgEWFLAGWiT6rcD+GRC2keU3s9RGJ+b3Z6/U73jwwNb2gKLJ7YCrp+jvU4ALg==", - "dev": true, - "requires": { - "loader-utils": "1.1.0", - "schema-utils": "0.4.5" - } - }, "filename-regex": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/filename-regex/-/filename-regex-2.0.1.tgz", diff --git a/package.json b/package.json index eebda1fd6..2c2d8e370 100644 --- a/package.json +++ b/package.json @@ -3,6 +3,7 @@ "scripts": { "build": "webpack", "production": "NODE_ENV=production webpack && rm -f ./public/dist/*styles.js", + "build-profile": "NODE_ENV=production webpack --profile --json > webpack-stats.json && rm -f ./public/dist/*styles.js", "dev": "npm-run-all --parallel watch livereload", "watch": "webpack --watch", "livereload": "livereload ./public/dist/", @@ -34,7 +35,6 @@ "jquery": "^3.3.1", "markdown-it": "^8.3.1", "markdown-it-task-lists": "^2.0.0", - "moment": "^2.21.0", "vue": "^2.2.6", "vuedraggable": "^2.14.1" }, diff --git a/resources/assets/js/services/dates.js b/resources/assets/js/services/dates.js new file mode 100644 index 000000000..d0ecc207d --- /dev/null +++ b/resources/assets/js/services/dates.js @@ -0,0 +1,15 @@ + +export function getCurrentDay() { + let date = new Date(); + let month = date.getMonth() + 1; + let day = date.getDate(); + + return `${date.getFullYear()}-${(month>9?'':'0') + month}-${(day>9?'':'0') + day}`; +} + +export function utcTimeStampToLocalTime(timestamp) { + let date = new Date(timestamp * 1000); + let hours = date.getHours(); + let mins = date.getMinutes(); + return `${(hours>9?'':'0') + hours}:${(mins>9?'':'0') + mins}`; +} \ No newline at end of file diff --git a/resources/assets/js/vues/page-editor.js b/resources/assets/js/vues/page-editor.js index d33739b30..bb8e14c4c 100644 --- a/resources/assets/js/vues/page-editor.js +++ b/resources/assets/js/vues/page-editor.js @@ -1,6 +1,4 @@ -const moment = require('moment'); -require('moment/locale/en-gb'); -moment.locale('en-gb'); +import * as Dates from "../services/dates"; let autoSaveFrequency = 30; @@ -96,9 +94,8 @@ let methods = { let url = window.baseUrl(`/ajax/page/${this.pageId}/save-draft`); window.$http.put(url, data).then(response => { draftErroring = false; - let updateTime = moment.utc(moment.unix(response.data.timestamp)).toDate(); if (!this.isNewDraft) this.isUpdateDraft = true; - this.draftNotifyChange(response.data.message + moment(updateTime).format('HH:mm')); + this.draftNotifyChange(`${response.data.message } ${Dates.utcTimeStampToLocalTime(response.data.timestamp)}`); lastSave = Date.now(); }, errorRes => { if (draftErroring) return; diff --git a/resources/assets/js/vues/search.js b/resources/assets/js/vues/search.js index 8cb790d24..c04104633 100644 --- a/resources/assets/js/vues/search.js +++ b/resources/assets/js/vues/search.js @@ -1,4 +1,4 @@ -const moment = require('moment'); +import * as Dates from "../services/dates"; let data = { terms: '', @@ -153,7 +153,7 @@ let methods = { }, enableDate(optionName) { - this.search.dates[optionName.toLowerCase()] = moment().format('YYYY-MM-DD'); + this.search.dates[optionName.toLowerCase()] = Dates.getCurrentDay(); this.dateChange(optionName); },