Skip to content

Commit

Permalink
Parse require calls from JS files using Closure
Browse files Browse the repository at this point in the history
  • Loading branch information
Deraen committed Oct 26, 2016
1 parent c8c8561 commit 17be1dc
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
3 changes: 2 additions & 1 deletion build.boot
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
(set-env!
:dependencies '[[cheshire "5.6.3"]]
:dependencies '[[cheshire "5.6.3"]
[com.google.javascript/closure-compiler-unshaded "v20160911"]]
:source-paths #{"src"})

(require '[cljsjs.npm.build :refer [package]])
40 changes: 40 additions & 0 deletions src/cljsjs/closure.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
(ns cljsjs.closure
(:require [clojure.java.io :as io])
(:import [com.google.javascript.jscomp CompilerPass NodeTraversal
CompilerOptions SourceFile NodeTraversal$Callback JSModule]
[com.google.javascript.rhino Node]))

(defn is-require? [n]
(and (= 2 (.getChildCount n))
(.. n getFirstChild (matchesQualifiedName "require"))
(.. n getSecondChild isString)))

(defn is-npm-require? [module-name]
false)

(defn finder [requires]
(reify NodeTraversal$Callback
(shouldTraverse ^boolean [this t n parent]
true)
(visit ^void [this t n parent]
(if (is-require? n)
(swap! requires conj (.. n getSecondChild getString)))
nil)))

(defn process-pass [compiler requires]
(reify CompilerPass
(process [this _ root]
(NodeTraversal/traverseEs6 compiler root (finder requires)))))

(defn find-requires [f]
(let [requires (atom [])
module (doto (JSModule. "$singleton$")
(.add (SourceFile/fromFile f)))
closure-compiler (com.google.javascript.jscomp.Compiler.)
pass (process-pass closure-compiler requires)]
(doseq [input (.getInputs module)]
(.process pass nil (.getAstRoot input closure-compiler)))
@requires))

(comment
(find-requires (io/file "node_modules/react/lib/React.js")))
1 change: 1 addition & 0 deletions src/cljsjs/npm/build.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
(:require [cheshire.core :as json]
[clojure.java.io :as io]
[clojure.string :as string]
[cljsjs.closure :as closure]
[boot.util :as util]
[boot.core :as boot]
[boot.task.built-in :as built-in]))
Expand Down

0 comments on commit 17be1dc

Please sign in to comment.