From e925b2dc46e5f1ef2905f3272765ae351299f616 Mon Sep 17 00:00:00 2001 From: Roman Volosovskyi Date: Sat, 8 Jun 2019 13:06:28 +0300 Subject: [PATCH] [perf] prod version of slurp All resources loaded by slurp are moved to status-modules/resources dir in release builds and are loaded only by demand instead of being bundled into index.*.js. --- .gitignore | 1 + prepare-modules.js | 39 +++++++++++-------- src/status_im/extensions/capacities/map.cljs | 23 +++++------ src/status_im/fleet/core.cljs | 4 +- .../screens/add_new/new_public_chat/view.cljs | 10 ++++- src/status_im/ui/screens/browser/views.cljs | 13 +++++-- src/status_im/utils/js_resources.cljs | 19 ++++++--- src/status_im/utils/slurp.clj | 38 ++++++++++++------ 8 files changed, 94 insertions(+), 53 deletions(-) diff --git a/.gitignore b/.gitignore index 2e61a4e02c..83f801ac49 100644 --- a/.gitignore +++ b/.gitignore @@ -165,3 +165,4 @@ conan.cmake # modules status-modules/translations status-modules/cljs +status-modules/resources diff --git a/prepare-modules.js b/prepare-modules.js index 053a9eff04..805ac51ac7 100644 --- a/prepare-modules.js +++ b/prepare-modules.js @@ -1,20 +1,25 @@ var fs = require("fs"); +var path = require('path'); +var dirs = ["status-modules/cljs", "status-modules/resources"]; -var modules = [ - "i18n" -]; - -modules.forEach( - function (moduleName) { - fs.readFile(`status-modules/cljs/${moduleName}-raw.js`, "utf8", function (err, data) { - if (err) throw err; - fs.writeFile(`status-modules/cljs/${moduleName}.js`, - ("module.exports=`" + data.replace(/[\\$'"]/g, "\\$&") + "`;"), - function (err) { - if (err) { - return console.log(err); - } - }); - }); +dirs.forEach(dir => { + fs.readdir(dir, (err, files) => { + if (files) { + files.forEach(file => { + if (file.endsWith("-raw.js")) { + const filePath = path.resolve(dir, file); + fs.readFile(filePath, "utf8", function (err, data) { + if (err) throw err; + fs.writeFile(filePath.replace("-raw.js", ".js"), + ("module.exports=`" + data.replace(/[\\$'"]/g, "\\$&") + "`;"), + function (err) { + if (err) { + return console.log(err); + } + }); + }); + } + }); + } }); - +}); diff --git a/src/status_im/extensions/capacities/map.cljs b/src/status_im/extensions/capacities/map.cljs index 1d4064dd7b..9905bbaeac 100644 --- a/src/status_im/extensions/capacities/map.cljs +++ b/src/status_im/extensions/capacities/map.cljs @@ -55,17 +55,18 @@ (defview map-webview [{:keys [interactive fly style marker on-change]}] (letsubs [webview (atom nil)] [map-component - {:style style - :origin-whitelist ["*"] - :source {:html mapview-html :base-url (cond - platform/ios? "./mapview/" - platform/android? "file:///android_asset/" - :else nil)} - :java-script-enabled true - :bounces false - :over-scroll-mode "never" - :local-storage-enabled true - :render-error web-view-error + {:style style + :origin-whitelist ["*"] + :source {:html (mapview-html) + :base-url (cond + platform/ios? "./mapview/" + platform/android? "file:///android_asset/" + :else nil)} + :java-script-enabled true + :bounces false + :over-scroll-mode "never" + :local-storage-enabled true + :render-error web-view-error ;; load only local resources, for non-local resources open external browser :on-should-start-load-with-request #(let [url (.-url %)] diff --git a/src/status_im/fleet/core.cljs b/src/status_im/fleet/core.cljs index 5274f99d2d..b59f69f812 100644 --- a/src/status_im/fleet/core.cljs +++ b/src/status_im/fleet/core.cljs @@ -31,8 +31,8 @@ (def default-les-fleets (slurp "resources/config/fleets-les.json")) (defn fleets [{:keys [custom-fleets]}] - (as-> [default-fleets - default-les-fleets] $ + (as-> [(default-fleets) + (default-les-fleets)] $ (mapv #(:fleets (types/json->clj %)) $) (conj $ custom-fleets) (reduce merge $))) diff --git a/src/status_im/ui/screens/add_new/new_public_chat/view.cljs b/src/status_im/ui/screens/add_new/new_public_chat/view.cljs index b85e5957cc..d4805ea246 100644 --- a/src/status_im/ui/screens/add_new/new_public_chat/view.cljs +++ b/src/status_im/ui/screens/add_new/new_public_chat/view.cljs @@ -55,7 +55,13 @@ [list/item-icon {:icon :main-icons/next :icon-opts {:color :gray}}]]]]) -(def default-public-chats (types/json->clj (slurp "resources/default_public_chats.json"))) +(def default-public-chats-json + (slurp "resources/default_public_chats.json")) + +(def default-public-chats + (memoize + (fn [] + (types/json->clj (default-public-chats-json))))) (views/defview new-public-chat [] (views/letsubs [topic [:public-group-topic] @@ -71,7 +77,7 @@ [react/view styles/chat-name-container [react/text {:style styles/section-title} (i18n/label :t/selected)]] - [list/flat-list {:data default-public-chats + [list/flat-list {:data (default-public-chats) :key-fn identity :render-fn render-topic :keyboard-should-persist-taps :always diff --git a/src/status_im/ui/screens/browser/views.cljs b/src/status_im/ui/screens/browser/views.cljs index f4e513e394..2967e90ecc 100644 --- a/src/status_im/ui/screens/browser/views.cljs +++ b/src/status_im/ui/screens/browser/views.cljs @@ -24,8 +24,13 @@ [status-im.utils.slurp :refer [slurp]] [status-im.utils.views :as views])) +(def browser-config-edn + (slurp "./src/status_im/utils/browser_config.edn")) + (def browser-config - (edn/read-string (slurp "./src/status_im/utils/browser_config.edn"))) + (memoize + (fn [] + (edn/read-string (browser-config-edn))))) (defn toolbar-content [url url-original {:keys [secure?]} url-editing?] (let [url-text (atom url)] @@ -71,7 +76,7 @@ (defn get-inject-js [url] (when url (let [domain-name (nth (re-find #"^\w+://(www\.)?([^/:]+)" url) 2)] - (get (:inject-js browser-config) domain-name)))) + (get (:inject-js (browser-config)) domain-name)))) (defn navigation [browser-id url webview can-go-back? can-go-forward?] [react/view styles/navbar @@ -129,14 +134,14 @@ :on-bridge-message #(re-frame/dispatch [:browser/bridge-message-received %]) :on-load #(re-frame/dispatch [:browser/loading-started]) :on-error #(re-frame/dispatch [:browser/error-occured]) - :injected-on-start-loading-java-script (str (when-not opt-in? js-res/web3) + :injected-on-start-loading-java-script (str (when-not opt-in? (js-res/web3)) (if opt-in? (js-res/web3-opt-in-init (str network-id)) (js-res/web3-init (ethereum/normalized-address address) (str network-id))) (get-inject-js url)) - :injected-java-script js-res/webview-js}])] + :injected-java-script (js-res/webview-js)}])] [navigation browser-id url-original webview can-go-back? can-go-forward?] [permissions.views/permissions-panel [(:dapp? browser) (:dapp browser)] show-permission] (when show-tooltip diff --git a/src/status_im/utils/js_resources.cljs b/src/status_im/utils/js_resources.cljs index 69c01737e7..def986affb 100644 --- a/src/status_im/utils/js_resources.cljs +++ b/src/status_im/utils/js_resources.cljs @@ -1,5 +1,5 @@ (ns status-im.utils.js-resources - (:require-macros [status-im.utils.slurp :refer [slurp slurp-bot]]) + (:require-macros [status-im.utils.slurp :refer [slurp]]) (:require [status-im.utils.types :refer [json->clj]] [clojure.string :as s])) @@ -9,17 +9,24 @@ (and (string? url) (s/starts-with? url local-protocol))) (def webview-js (slurp "resources/js/webview.js")) -(def web3 (str "; if (typeof Web3 == 'undefined') {" - (slurp "node_modules/web3/dist/web3.min.js") - "}")) +(def web3-file (slurp "node_modules/web3/dist/web3.min.js")) +(def web3 + (memoize + (fn [] + (str "; if (typeof Web3 == 'undefined') {" + (web3-file) + "}")))) + +(def web3-init-file (slurp "resources/js/web3_init.js")) (defn web3-init [current-account-address network-id] (str "var currentAccountAddress = \"" current-account-address "\";" "var networkId = \"" network-id "\";" - (slurp "resources/js/web3_init.js"))) + (web3-init-file))) +(def web3-opt-in-init-file (slurp "resources/js/web3_opt_in.js")) (defn web3-opt-in-init [network-id] (str "var networkId = \"" network-id "\";" - (slurp "resources/js/web3_opt_in.js"))) + (web3-opt-in-init-file))) (defn local-storage-data [data] (str "var localStorageData = " (or data "{}") ";")) diff --git a/src/status_im/utils/slurp.clj b/src/status_im/utils/slurp.clj index 0b1516b31d..6c8b56d9d0 100644 --- a/src/status_im/utils/slurp.clj +++ b/src/status_im/utils/slurp.clj @@ -1,15 +1,31 @@ (ns status-im.utils.slurp (:refer-clojure :exclude [slurp]) - (:require [clojure.string :as string])) + (:require [clojure.java.io :as io] + [clojure.string :as str]) + (:import (java.io File))) + +(def prod? (= "prod" (System/getenv "BUILD_ENV"))) + +(defn copy-file [source-path dest-path] + (io/copy (io/file source-path) (io/file dest-path))) + +(def resources-dir "status-modules/resources/") + +(defn check-resources-dir [] + (let [resources (File. resources-dir)] + (when-not (.exists resources) + (.mkdir resources)))) (defmacro slurp [file] - (clojure.core/slurp file)) - -(defmacro slurp-bot [bot-name & files] - (->> (concat files ["translations.js" "bot.js"]) - (map (fn [file-name] - (try - (clojure.core/slurp - (string/join "/" ["resources/js/bots" (name bot-name) file-name])) - (catch Exception _ "")))) - (apply str))) \ No newline at end of file + (if prod? + (let [name (str/replace file #"[\/\.]" "_") + file-name (str resources-dir name)] + (check-resources-dir) + (copy-file file (str file-name "-raw.js")) + (let [res (gensym "res")] + `(let [~res (atom nil)] + (fn [] + (or @~res + (reset! ~res (js/require ~(str file-name ".js")))))))) + `(fn [] + ~(clojure.core/slurp file))))