[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.
This commit is contained in:
Roman Volosovskyi 2019-06-08 13:06:28 +03:00
parent 7c1df4f3cd
commit e925b2dc46
No known key found for this signature in database
GPG Key ID: 0238A4B5ECEE70DE
8 changed files with 94 additions and 53 deletions

1
.gitignore vendored
View File

@ -165,3 +165,4 @@ conan.cmake
# modules # modules
status-modules/translations status-modules/translations
status-modules/cljs status-modules/cljs
status-modules/resources

View File

@ -1,20 +1,25 @@
var fs = require("fs"); var fs = require("fs");
var path = require('path');
var dirs = ["status-modules/cljs", "status-modules/resources"];
var modules = [ dirs.forEach(dir => {
"i18n" fs.readdir(dir, (err, files) => {
]; if (files) {
files.forEach(file => {
modules.forEach( if (file.endsWith("-raw.js")) {
function (moduleName) { const filePath = path.resolve(dir, file);
fs.readFile(`status-modules/cljs/${moduleName}-raw.js`, "utf8", function (err, data) { fs.readFile(filePath, "utf8", function (err, data) {
if (err) throw err; if (err) throw err;
fs.writeFile(`status-modules/cljs/${moduleName}.js`, fs.writeFile(filePath.replace("-raw.js", ".js"),
("module.exports=`" + data.replace(/[\\$'"]/g, "\\$&") + "`;"), ("module.exports=`" + data.replace(/[\\$'"]/g, "\\$&") + "`;"),
function (err) { function (err) {
if (err) { if (err) {
return console.log(err); return console.log(err);
} }
}); });
}); });
}
});
}
}); });
});

View File

@ -55,17 +55,18 @@
(defview map-webview [{:keys [interactive fly style marker on-change]}] (defview map-webview [{:keys [interactive fly style marker on-change]}]
(letsubs [webview (atom nil)] (letsubs [webview (atom nil)]
[map-component [map-component
{:style style {:style style
:origin-whitelist ["*"] :origin-whitelist ["*"]
:source {:html mapview-html :base-url (cond :source {:html (mapview-html)
platform/ios? "./mapview/" :base-url (cond
platform/android? "file:///android_asset/" platform/ios? "./mapview/"
:else nil)} platform/android? "file:///android_asset/"
:java-script-enabled true :else nil)}
:bounces false :java-script-enabled true
:over-scroll-mode "never" :bounces false
:local-storage-enabled true :over-scroll-mode "never"
:render-error web-view-error :local-storage-enabled true
:render-error web-view-error
;; load only local resources, for non-local resources open external browser ;; load only local resources, for non-local resources open external browser
:on-should-start-load-with-request #(let [url (.-url %)] :on-should-start-load-with-request #(let [url (.-url %)]

View File

@ -31,8 +31,8 @@
(def default-les-fleets (slurp "resources/config/fleets-les.json")) (def default-les-fleets (slurp "resources/config/fleets-les.json"))
(defn fleets [{:keys [custom-fleets]}] (defn fleets [{:keys [custom-fleets]}]
(as-> [default-fleets (as-> [(default-fleets)
default-les-fleets] $ (default-les-fleets)] $
(mapv #(:fleets (types/json->clj %)) $) (mapv #(:fleets (types/json->clj %)) $)
(conj $ custom-fleets) (conj $ custom-fleets)
(reduce merge $))) (reduce merge $)))

View File

@ -55,7 +55,13 @@
[list/item-icon {:icon :main-icons/next [list/item-icon {:icon :main-icons/next
:icon-opts {:color :gray}}]]]]) :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/defview new-public-chat []
(views/letsubs [topic [:public-group-topic] (views/letsubs [topic [:public-group-topic]
@ -71,7 +77,7 @@
[react/view styles/chat-name-container [react/view styles/chat-name-container
[react/text {:style styles/section-title} [react/text {:style styles/section-title}
(i18n/label :t/selected)]] (i18n/label :t/selected)]]
[list/flat-list {:data default-public-chats [list/flat-list {:data (default-public-chats)
:key-fn identity :key-fn identity
:render-fn render-topic :render-fn render-topic
:keyboard-should-persist-taps :always :keyboard-should-persist-taps :always

View File

@ -24,8 +24,13 @@
[status-im.utils.slurp :refer [slurp]] [status-im.utils.slurp :refer [slurp]]
[status-im.utils.views :as views])) [status-im.utils.views :as views]))
(def browser-config-edn
(slurp "./src/status_im/utils/browser_config.edn"))
(def browser-config (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?] (defn toolbar-content [url url-original {:keys [secure?]} url-editing?]
(let [url-text (atom url)] (let [url-text (atom url)]
@ -71,7 +76,7 @@
(defn get-inject-js [url] (defn get-inject-js [url]
(when url (when url
(let [domain-name (nth (re-find #"^\w+://(www\.)?([^/:]+)" url) 2)] (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?] (defn navigation [browser-id url webview can-go-back? can-go-forward?]
[react/view styles/navbar [react/view styles/navbar
@ -129,14 +134,14 @@
:on-bridge-message #(re-frame/dispatch [:browser/bridge-message-received %]) :on-bridge-message #(re-frame/dispatch [:browser/bridge-message-received %])
:on-load #(re-frame/dispatch [:browser/loading-started]) :on-load #(re-frame/dispatch [:browser/loading-started])
:on-error #(re-frame/dispatch [:browser/error-occured]) :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? (if opt-in?
(js-res/web3-opt-in-init (str network-id)) (js-res/web3-opt-in-init (str network-id))
(js-res/web3-init (js-res/web3-init
(ethereum/normalized-address address) (ethereum/normalized-address address)
(str network-id))) (str network-id)))
(get-inject-js url)) (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?] [navigation browser-id url-original webview can-go-back? can-go-forward?]
[permissions.views/permissions-panel [(:dapp? browser) (:dapp browser)] show-permission] [permissions.views/permissions-panel [(:dapp? browser) (:dapp browser)] show-permission]
(when show-tooltip (when show-tooltip

View File

@ -1,5 +1,5 @@
(ns status-im.utils.js-resources (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]] (:require [status-im.utils.types :refer [json->clj]]
[clojure.string :as s])) [clojure.string :as s]))
@ -9,17 +9,24 @@
(and (string? url) (s/starts-with? url local-protocol))) (and (string? url) (s/starts-with? url local-protocol)))
(def webview-js (slurp "resources/js/webview.js")) (def webview-js (slurp "resources/js/webview.js"))
(def web3 (str "; if (typeof Web3 == 'undefined') {" (def web3-file (slurp "node_modules/web3/dist/web3.min.js"))
(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] (defn web3-init [current-account-address network-id]
(str "var currentAccountAddress = \"" current-account-address "\";" (str "var currentAccountAddress = \"" current-account-address "\";"
"var networkId = \"" network-id "\";" "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] (defn web3-opt-in-init [network-id]
(str "var networkId = \"" network-id "\";" (str "var networkId = \"" network-id "\";"
(slurp "resources/js/web3_opt_in.js"))) (web3-opt-in-init-file)))
(defn local-storage-data [data] (defn local-storage-data [data]
(str "var localStorageData = " (or data "{}") ";")) (str "var localStorageData = " (or data "{}") ";"))

View File

@ -1,15 +1,31 @@
(ns status-im.utils.slurp (ns status-im.utils.slurp
(:refer-clojure :exclude [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] (defmacro slurp [file]
(clojure.core/slurp file)) (if prod?
(let [name (str/replace file #"[\/\.]" "_")
(defmacro slurp-bot [bot-name & files] file-name (str resources-dir name)]
(->> (concat files ["translations.js" "bot.js"]) (check-resources-dir)
(map (fn [file-name] (copy-file file (str file-name "-raw.js"))
(try (let [res (gensym "res")]
(clojure.core/slurp `(let [~res (atom nil)]
(string/join "/" ["resources/js/bots" (name bot-name) file-name])) (fn []
(catch Exception _ "")))) (or @~res
(apply str))) (reset! ~res (js/require ~(str file-name ".js"))))))))
`(fn []
~(clojure.core/slurp file))))