-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Parse require calls from JS files using Closure
- Loading branch information
Showing
3 changed files
with
43 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters