Skip to content

Commit b7309fd

Browse files
Maria Neisednolen
authored andcommitted
CLJS-1177: A compiler support for non-Closure transforms (JSX, etc)
Adding support for preprocessing foreign libs. Users can implement the js-transforms function and then add a :preprocess option to their foreign lib. Works in conjunction with JS module processing or without. Both functions, js-transforms and convert-js-modules, now expect and return an IJavaScript.
1 parent fb12e78 commit b7309fd

File tree

2 files changed

+44
-23
lines changed

2 files changed

+44
-23
lines changed

src/main/clojure/cljs/analyzer.cljc

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@
8787
:munged-namespace true
8888
:ns-var-clash true
8989
:extend-type-invalid-method-shape true
90-
:unsupported-js-module-type true})
90+
:unsupported-js-module-type true
91+
:unsupported-preprocess-value true})
9192

9293
(def js-reserved
9394
#{"arguments" "abstract" "boolean" "break" "byte" "case"
@@ -339,6 +340,11 @@
339340
(str "Unsupported JavaScript module type " module-type " for foreign library "
340341
file "."))
341342

343+
(defmethod error-message :unsupported-preprocess-value
344+
[warning-type {:keys [preprocess file]}]
345+
(str "Unsupported preprocess value " preprocess " for foreign library "
346+
file "."))
347+
342348
(defn default-warning-handler [warning-type env extra]
343349
(when (warning-type *cljs-warnings*)
344350
(when-let [s (error-message warning-type extra)]

src/main/clojure/cljs/closure.clj

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1247,21 +1247,24 @@
12471247
(let [module-root (get-js-module-root file)]
12481248
(ES6ModuleLoader. closure-compiler module-root))))
12491249

1250-
(defn ^Node get-root-node [file closure-compiler]
1251-
(let [^CompilerInput input (->> (slurp file)
1252-
(js-source-file file)
1250+
(defn ^Node get-root-node [ijs closure-compiler]
1251+
(let [^CompilerInput input (->> (deps/-source ijs)
1252+
(js-source-file (:file ijs))
12531253
(CompilerInput.))]
12541254
(.getAstRoot input closure-compiler)))
12551255

12561256
(defn get-source-files [module-type opts]
12571257
(->> (:foreign-libs opts)
12581258
(filter #(= (:module-type %) module-type))
1259-
(map #(js-source-file (:file %) (slurp (:file %))))))
1259+
(map (fn [lib]
1260+
(let [lib (deps/load-foreign-library lib)]
1261+
(js-source-file (:file lib) (deps/-source lib)))))))
12601262

12611263
(defmulti convert-js-module
1262-
"Takes a JavaScript module and rewrites it into a Google Closure-compatible
1263-
form. Returns the source of the new module as a single string."
1264-
(fn [{module-type :module-type :as js} opts]
1264+
"Takes a JavaScript module as an IJavaScript and rewrites it into a Google
1265+
Closure-compatible form. Returns an IJavaScript with the converted module
1266+
code set as source."
1267+
(fn [{module-type :module-type :as ijs} opts]
12651268
(if (and (= module-type :amd) can-convert-amd?)
12661269
;; AMD modules are converted via CommonJS modules
12671270
:commonjs
@@ -1275,8 +1278,8 @@
12751278
(set-options (CompilerOptions.))))
12761279

12771280
(util/compile-if can-convert-commonjs?
1278-
(defmethod convert-js-module :commonjs [js opts]
1279-
(let [{:keys [file module-type]} js
1281+
(defmethod convert-js-module :commonjs [ijs opts]
1282+
(let [{:keys [file module-type]} ijs
12801283
^List externs '()
12811284
^List source-files (get-source-files module-type opts)
12821285
^CompilerOptions options (make-convert-js-module-options opts)
@@ -1286,17 +1289,17 @@
12861289
(make-es6-loader source-files)
12871290
(make-es6-loader closure-compiler file))
12881291
cjs (ProcessCommonJSModules. closure-compiler es6-loader)
1289-
^Node root (get-root-node file closure-compiler)]
1292+
^Node root (get-root-node ijs closure-compiler)]
12901293
(util/compile-if can-convert-amd?
1291-
(when (= (:module-type js) :amd)
1294+
(when (= module-type :amd)
12921295
(.process (TransformAMDToCJSModule. closure-compiler) nil root)))
12931296
(.process cjs nil root)
12941297
(report-failure (.getResult closure-compiler))
1295-
(.toSource closure-compiler root))))
1298+
(assoc ijs :source (.toSource closure-compiler root)))))
12961299

12971300
(util/compile-if can-convert-es6?
1298-
(defmethod convert-js-module :es6 [js opts]
1299-
(let [{:keys [file module-type]} js
1301+
(defmethod convert-js-module :es6 [ijs opts]
1302+
(let [{:keys [file module-type]} ijs
13001303
^List externs '()
13011304
^List source-files (get-source-files module-type opts)
13021305
^CompilerOptions options (doto (make-convert-js-module-options opts)
@@ -1308,14 +1311,24 @@
13081311
(make-es6-loader source-files)
13091312
(make-es6-loader closure-compiler file))
13101313
cjs (ProcessEs6Modules. closure-compiler es6-loader true)
1311-
^Node root (get-root-node file closure-compiler)]
1314+
^Node root (get-root-node ijs closure-compiler)]
13121315
(.processFile cjs root)
13131316
(report-failure (.getResult closure-compiler))
1314-
(.toSource closure-compiler root))))
1317+
(assoc ijs :source (.toSource closure-compiler root)))))
13151318

1316-
(defmethod convert-js-module :default [js opts]
1317-
(ana/warning :unsupported-js-module-type @env/*compiler* js)
1318-
(deps/-source js))
1319+
(defmethod convert-js-module :default [ijs opts]
1320+
(ana/warning :unsupported-js-module-type @env/*compiler* ijs)
1321+
ijs)
1322+
1323+
(defmulti js-transforms
1324+
"Takes an IJavaScript with the source code set as source, transforms the
1325+
source code and returns an IJavascript with the new code set as source."
1326+
(fn [ijs opts]
1327+
(:preprocess ijs)))
1328+
1329+
(defmethod js-transforms :default [ijs opts]
1330+
(ana/warning :unsupported-preprocess-value @env/*compiler* ijs)
1331+
ijs)
13191332

13201333
(defn write-javascript
13211334
"Write or copy a JavaScript file to output directory. Only write if the file
@@ -1332,9 +1345,11 @@
13321345
:group (:group js)}]
13331346
(when-not (.exists out-file)
13341347
(util/mkdirs out-file)
1335-
(if (:module-type js)
1336-
(spit out-file (convert-js-module js opts))
1337-
(spit out-file (deps/-source js))))
1348+
(spit out-file
1349+
(cond-> (assoc js :source (deps/-source js))
1350+
(:preprocess js) (js-transforms opts)
1351+
(:module-type js) (convert-js-module opts)
1352+
true deps/-source)))
13381353
(if (map? js)
13391354
(merge js ijs)
13401355
ijs)))

0 commit comments

Comments
 (0)