Skip to content

Commit

Permalink
[perf] prod version of slurp
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
rasom committed Jun 10, 2019
1 parent 9f8ab37 commit 198b6ec
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 53 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,4 @@ conan.cmake
# modules
status-modules/translations
status-modules/cljs
status-modules/resources
39 changes: 22 additions & 17 deletions prepare-modules.js
Original file line number Diff line number Diff line change
@@ -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);
}
});
});
}
});
}
});

});
23 changes: 12 additions & 11 deletions src/status_im/extensions/capacities/map.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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 %)]
Expand Down
4 changes: 2 additions & 2 deletions src/status_im/fleet/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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 $)))
Expand Down
10 changes: 8 additions & 2 deletions src/status_im/ui/screens/add_new/new_public_chat/view.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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
Expand Down
13 changes: 9 additions & 4 deletions src/status_im/ui/screens/browser/views.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
19 changes: 13 additions & 6 deletions src/status_im/utils/js_resources.cljs
Original file line number Diff line number Diff line change
@@ -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]))

Expand All @@ -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 "{}") ";"))
Expand Down
38 changes: 27 additions & 11 deletions src/status_im/utils/slurp.clj
Original file line number Diff line number Diff line change
@@ -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)))
(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))))

0 comments on commit 198b6ec

Please sign in to comment.