Skip to content

Commit 9717d49

Browse files
author
dnolen
committed
wip cljs.build.api for computing node_modules deps
1 parent eb7396b commit 9717d49

File tree

2 files changed

+60
-4
lines changed

2 files changed

+60
-4
lines changed

src/main/cljs/cljs/module_deps.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main/clojure/cljs/build/api.clj

Lines changed: 59 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,17 @@
1212
For example: a build script may need to how to invalidate compiled
1313
files so that they will be recompiled."
1414
(:refer-clojure :exclude [compile])
15-
(:require [cljs.util :as util]
15+
(:require [clojure.java.io :as io]
16+
[clojure.string :as string]
17+
[clojure.data.json :as json]
18+
[cljs.util :as util]
1619
[cljs.env :as env]
1720
[cljs.analyzer :as ana]
1821
[cljs.compiler :as comp]
1922
[cljs.closure :as closure]
20-
[cljs.js-deps :as js-deps]
21-
[clojure.java.io :as io])
22-
(:import java.io.File))
23+
[cljs.js-deps :as js-deps])
24+
(:import [java.io File]
25+
[java.lang ProcessBuilder Process]))
2326

2427
;; =============================================================================
2528
;; Useful Utilities
@@ -213,8 +216,60 @@
213216
(binding [ana/*cljs-warning-handlers* (:warning-handlers opts ana/*cljs-warning-handlers*)]
214217
(closure/watch source opts compiler-env stop))))
215218

219+
(defn add-package-jsons
220+
"EXPERIMENTAL: see node-module-deps"
221+
[deps]
222+
(let [checked (atom #{})]
223+
(reduce
224+
(fn [ret {:keys [file] :as dep}]
225+
(let [f (.getParentFile (io/file file))
226+
path (.getAbsolutePath f)]
227+
(if-not (contains? @checked path)
228+
(let [f' (io/file f "package.json")]
229+
(swap! checked conj path)
230+
(if (.exists f')
231+
(conj ret dep
232+
{:file (.getAbsolutePath f')
233+
:module-type :commonjs})
234+
(conj ret dep)))
235+
ret)))
236+
[] deps)))
237+
238+
(defn node-module-deps
239+
"EXPERIMENTAL: return the foreign libs entries as computed by running
240+
the module-deps package on the supplied JavaScript entry point. Assumes
241+
that the module-deps & JSONStream NPM packages are either locally or
242+
globally installed."
243+
[entry]
244+
(let [code (string/replace
245+
(slurp (io/resource "cljs/module_deps.js"))
246+
"JS_FILE" entry)
247+
proc (-> (ProcessBuilder.
248+
(into-array
249+
["node" "--eval" (str code)]))
250+
.start)
251+
err (.waitFor proc)]
252+
(if (zero? err)
253+
(let [is (.getInputStream proc)]
254+
(into []
255+
(map (fn [{:strs [file]}] file
256+
{:file file :module-type :commonjs}))
257+
(butlast (json/read-str (slurp is)))))
258+
[])))
259+
216260
(comment
261+
(add-package-jsons (node-module-deps "src/test/node/test.js"))
262+
)
217263

264+
(defn node-inputs
265+
"EXPERIMENTAL: return the foreign libs entries as computed by running
266+
the module-deps package on the supplied JavaScript entry points. Assumes
267+
that the module-deps & JSONStream NPM packages are either locally or
268+
globally installed."
269+
[entries]
270+
)
271+
272+
(comment
218273
(def test-cenv (atom {}))
219274
(def test-env (assoc-in (ana/empty-env) [:ns :name] 'cljs.user))
220275

0 commit comments

Comments
 (0)