Skip to content

Commit 245bdee

Browse files
anmonteiroswannodette
authored andcommitted
CLJS-2339: Significant code reload slowdown with :npm-deps
1 parent 7a8803e commit 245bdee

File tree

3 files changed

+57
-27
lines changed

3 files changed

+57
-27
lines changed

src/main/clojure/cljs/analyzer.cljc

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -739,17 +739,23 @@
739739
(or (some? (get (:requires ns) prefix))
740740
(some? (get (:imports ns) prefix))))))
741741

742-
(defn js-module-exists?
743-
[module]
742+
(defn- internal-js-module-exists?
743+
[js-module-index module]
744744
;; we need to check both keys and values of the JS module index, because
745745
;; macroexpansion will be looking for the provided name - António Monteiro
746746
(contains?
747747
(into #{}
748748
(mapcat (fn [[k v]]
749749
[k (:name v)]))
750-
(get-in @env/*compiler* [:js-module-index]))
750+
js-module-index)
751751
(str module)))
752752

753+
(def js-module-exists?* (memoize internal-js-module-exists?))
754+
755+
(defn js-module-exists?
756+
[module]
757+
(js-module-exists?* (get-in @env/*compiler* [:js-module-index]) module))
758+
753759
(defn node-module-dep?
754760
#?(:cljs {:tag boolean})
755761
[module]

src/main/clojure/cljs/closure.clj

Lines changed: 44 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2141,7 +2141,7 @@
21412141
(defn maybe-install-node-deps!
21422142
[{:keys [npm-deps verbose] :as opts}]
21432143
(let [npm-deps (merge npm-deps (compute-upstream-npm-deps opts))]
2144-
(if-not (empty? npm-deps)
2144+
(when-not (empty? npm-deps)
21452145
(let [pkg-json (io/file "package.json")]
21462146
(when (or ana/*verbose* verbose)
21472147
(util/debug-prn "Installing Node.js dependencies"))
@@ -2165,9 +2165,8 @@
21652165
(bound-fn [] (pipe proc es ew)))))
21662166
err (.waitFor proc)]
21672167
(when (and (not (zero? err)) (not (.isAlive proc)))
2168-
(println (str ew)))
2169-
opts))
2170-
opts)))
2168+
(println (str ew)))))
2169+
true)))
21712170

21722171
(defn node-module-deps
21732172
"EXPERIMENTAL: return the foreign libs entries as computed by running
@@ -2232,11 +2231,23 @@
22322231
(let [node-modules (io/file "node_modules")]
22332232
(if (and (not (empty? modules)) (.exists node-modules) (.isDirectory node-modules))
22342233
(let [modules (into #{} (map name) modules)
2235-
deps-file (io/file (util/output-directory opts) "cljs$node_modules.js")]
2234+
deps-file (io/file (util/output-directory opts) "cljs$node_modules.js")
2235+
old-contents (when (.exists deps-file)
2236+
(slurp deps-file))
2237+
new-contents (let [sb (StringBuffer.)]
2238+
(run! #(.append sb (str "require('" % "');\n")) modules)
2239+
(str sb))]
22362240
(util/mkdirs deps-file)
2237-
(with-open [w (io/writer deps-file)]
2238-
(run! #(.write w (str "require('" % "');\n")) modules))
2239-
(node-inputs [{:file (.getAbsolutePath deps-file)}] opts))
2241+
(if-not (= old-contents new-contents)
2242+
(do
2243+
(spit deps-file new-contents)
2244+
(let [transitive-js (node-inputs [{:file (.getAbsolutePath deps-file)}] opts)]
2245+
(when-not (nil? env/*compiler*)
2246+
(swap! env/*compiler* update-in [::transitive-dep-set]
2247+
assoc modules transitive-js))
2248+
transitive-js))
2249+
(when-not (nil? env/*compiler*)
2250+
(get-in @env/*compiler* [::transitive-dep-set modules]))))
22402251
[]))))
22412252

22422253
(defn- node-file-seq->libs-spec*
@@ -2454,8 +2465,8 @@
24542465
- process the JS modules (preprocess + convert to Closure JS)
24552466
- save js-dependency-index for compilation"
24562467
[{:keys [npm-deps target] :as opts} js-sources compiler-env]
2457-
(let [;; Find all the top-level Node packages and their files
2458-
top-level (reduce
2468+
;; Find all the top-level Node packages and their files
2469+
(let [top-level (reduce
24592470
(fn [acc m]
24602471
(reduce (fn [acc p] (assoc acc p m)) acc (:provides m)))
24612472
{} (index-node-modules-dir))
@@ -2464,18 +2475,24 @@
24642475
;; and create list of all their dependencies
24652476
node-required (set/intersection (set (keys top-level)) requires)
24662477
expanded-libs (expand-libs (:foreign-libs opts))
2467-
opts (-> opts
2468-
(update :foreign-libs
2469-
(fn [libs]
2470-
(into (if (= target :nodejs)
2471-
[]
2472-
(index-node-modules node-required))
2473-
(into expanded-libs
2474-
(node-inputs (filter (fn [{:keys [module-type]}]
2475-
(and (some? module-type)
2476-
(not= module-type :amd)))
2477-
expanded-libs))))))
2478-
process-js-modules)]
2478+
output-dir (util/output-directory opts)
2479+
opts (update opts :foreign-libs
2480+
(fn [libs]
2481+
(into (if (= target :nodejs)
2482+
[]
2483+
(index-node-modules node-required))
2484+
(into expanded-libs
2485+
(node-inputs (filter (fn [{:keys [module-type]}]
2486+
(and (some? module-type)
2487+
(not= module-type :amd)))
2488+
expanded-libs))))))
2489+
opts (if (some
2490+
(fn [ijs]
2491+
(let [dest (io/file output-dir (rel-output-path (assoc ijs :foreign true) opts))]
2492+
(util/changed? (deps/-url ijs opts) dest)))
2493+
(:foreign-libs opts))
2494+
(process-js-modules opts)
2495+
(:options @compiler-env))]
24792496
(swap! compiler-env (fn [cenv]
24802497
(-> cenv
24812498
;; we need to also track the whole top level - this is to support
@@ -2503,7 +2520,10 @@
25032520
;; we want to warn about NPM dep conflicts before installing the modules
25042521
(when (:install-deps opts)
25052522
(check-npm-deps opts)
2506-
(maybe-install-node-deps! opts))
2523+
(swap! compiler-env update-in [:npm-deps-installed?]
2524+
(fn [installed?]
2525+
(when-not installed?
2526+
(maybe-install-node-deps! opts)))))
25072527
(let [compiler-stats (:compiler-stats opts)
25082528
checked-arrays (or (:checked-arrays opts)
25092529
ana/*checked-arrays*)
@@ -2569,6 +2589,7 @@
25692589
(-> (-find-sources source all-opts)
25702590
(add-dependency-sources compile-opts)))
25712591
all-opts (handle-js-modules all-opts js-sources compiler-env)
2592+
_ (swap! env/*compiler* update-in [:options] merge all-opts)
25722593
js-sources (-> js-sources
25732594
deps/dependency-order
25742595
(compile-sources compiler-stats compile-opts)

src/main/clojure/cljs/repl.cljc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -881,7 +881,10 @@
881881
(print value))))))]
882882
(when (:install-deps opts)
883883
(cljsc/check-npm-deps opts)
884-
(cljsc/maybe-install-node-deps! opts))
884+
(swap! env/*compiler* update-in [:npm-deps-installed?]
885+
(fn [installed?]
886+
(when-not installed?
887+
(cljsc/maybe-install-node-deps! opts)))))
885888
(comp/with-core-cljs opts
886889
(fn []
887890
(binding [*repl-opts* opts]

0 commit comments

Comments
 (0)