|
55 | 55 | CommandLineRunner AnonymousFunctionNamingPolicy
|
56 | 56 | JSModule JSModuleGraph SourceMap ProcessCommonJSModules
|
57 | 57 | ES6ModuleLoader AbstractCompiler TransformAMDToCJSModule
|
58 |
| - ProcessEs6Modules] |
| 58 | + ProcessEs6Modules CompilerInput] |
59 | 59 | [com.google.javascript.rhino Node]
|
60 | 60 | [java.security MessageDigest]
|
61 | 61 | [javax.xml.bind DatatypeConverter]
|
|
72 | 72 | (defn random-string [length]
|
73 | 73 | (apply str (take length (repeatedly random-char))))
|
74 | 74 |
|
75 |
| -(defmacro ^:private compile-if |
76 |
| - "Evaluate `exp` and if it returns logical true and doesn't error, expand to |
77 |
| - `then`. Else expand to `else`." |
78 |
| - [exp then & else] |
79 |
| - (if (try (eval exp) |
80 |
| - (catch Throwable _ false)) |
81 |
| - `(do ~then) |
82 |
| - `(do ~@else))) |
83 |
| - |
84 |
| -(compile-if |
| 75 | +(util/compile-if |
| 76 | + (.getConstructor ES6ModuleLoader |
| 77 | + (into-array java.lang.Class |
| 78 | + [java.util.List java.lang.Iterable])) |
| 79 | + (do (def is-new-es6-loader? true) |
| 80 | + (def default-module-root ES6ModuleLoader/DEFAULT_FILENAME_PREFIX)) |
| 81 | + (def is-new-es6-loader? false)) |
| 82 | + |
| 83 | +(util/compile-if |
| 84 | + (.getConstructor ES6ModuleLoader |
| 85 | + (into-array java.lang.Class |
| 86 | + [AbstractCompiler java.lang.String])) |
| 87 | + (def is-old-es6-loader? true) |
| 88 | + (def is-old-es6-loader? false)) |
| 89 | + |
| 90 | +(util/compile-if |
85 | 91 | (and (.getConstructor ProcessCommonJSModules
|
86 | 92 | (into-array java.lang.Class
|
87 | 93 | [com.google.javascript.jscomp.Compiler ES6ModuleLoader]))
|
88 |
| - (.getConstructor ES6ModuleLoader |
89 |
| - (into-array java.lang.Class |
90 |
| - [AbstractCompiler java.lang.String]))) |
| 94 | + (or is-new-es6-loader? is-old-es6-loader?)) |
91 | 95 | (def can-convert-commonjs? true)
|
92 | 96 | (def can-convert-commonjs? false))
|
93 | 97 |
|
94 |
| -(compile-if |
| 98 | +(util/compile-if |
95 | 99 | (and can-convert-commonjs?
|
96 | 100 | (.getConstructor TransformAMDToCJSModule
|
97 | 101 | (into-array java.lang.Class
|
98 | 102 | [AbstractCompiler])))
|
99 | 103 | (def can-convert-amd? true)
|
100 | 104 | (def can-convert-amd? false))
|
101 | 105 |
|
102 |
| -(compile-if |
| 106 | +(util/compile-if |
103 | 107 | (and (.getConstructor ProcessEs6Modules
|
104 | 108 | (into-array java.lang.Class
|
105 | 109 | [com.google.javascript.jscomp.Compiler ES6ModuleLoader Boolean/TYPE]))
|
106 |
| - (.getConstructor ES6ModuleLoader |
107 |
| - (into-array java.lang.Class |
108 |
| - [AbstractCompiler java.lang.String]))) |
| 110 | + (or is-new-es6-loader? is-old-es6-loader?)) |
109 | 111 | (def can-convert-es6? true)
|
110 | 112 | (def can-convert-es6? false))
|
111 | 113 |
|
112 |
| - |
113 | 114 | ;; Closure API
|
114 | 115 | ;; ===========
|
115 | 116 |
|
|
1227 | 1228 | (not (.startsWith path (str "." File/separator))) (str "." File/separator)
|
1228 | 1229 | (not (.endsWith path File/separator)) (#(str % File/separator)))))
|
1229 | 1230 |
|
1230 |
| -(defn init-js-module-processing [js-file options] |
1231 |
| - (let [^List externs '() |
1232 |
| - ^SourceFile source-file (js-source-file js-file (slurp js-file)) |
1233 |
| - closure-compiler (doto (make-closure-compiler) |
1234 |
| - (.init externs [source-file] options)) |
1235 |
| - ^Node root (.parse closure-compiler source-file)] |
1236 |
| - {:closure-compiler closure-compiler |
1237 |
| - :root root})) |
| 1231 | +(util/compile-if is-new-es6-loader? |
| 1232 | + (defn make-es6-loader [source-files] |
| 1233 | + (let [^List module-roots (list default-module-root) |
| 1234 | + ^List compiler-inputs (map #(CompilerInput. %) source-files)] |
| 1235 | + (ES6ModuleLoader. module-roots compiler-inputs))) |
| 1236 | + (defn make-es6-loader [closure-compiler file] |
| 1237 | + (let [module-root (get-js-module-root file)] |
| 1238 | + (ES6ModuleLoader. closure-compiler module-root)))) |
| 1239 | + |
| 1240 | +(defn ^Node get-root-node [file closure-compiler] |
| 1241 | + (let [^CompilerInput input (->> (slurp file) |
| 1242 | + (js-source-file file) |
| 1243 | + (CompilerInput.))] |
| 1244 | + (.getAstRoot input closure-compiler))) |
| 1245 | + |
| 1246 | +(defn get-source-files [module-type opts] |
| 1247 | + (->> (:foreign-libs opts) |
| 1248 | + (filter #(= (:module-type %) module-type)) |
| 1249 | + (map #(js-source-file (:file %) (slurp (:file %)))))) |
1238 | 1250 |
|
1239 | 1251 | (defmulti convert-js-module
|
1240 | 1252 | "Takes a JavaScript module and rewrites it into a Google Closure-compatible
|
1241 | 1253 | form. Returns the source of the new module as a single string."
|
1242 |
| - (fn [{module-type :module-type :as js}] |
| 1254 | + (fn [{module-type :module-type :as js} opts] |
1243 | 1255 | (if (and (= module-type :amd) can-convert-amd?)
|
1244 | 1256 | ;; AMD modules are converted via CommonJS modules
|
1245 | 1257 | :commonjs
|
1246 | 1258 | module-type)))
|
1247 | 1259 |
|
1248 |
| -(compile-if can-convert-commonjs? |
1249 |
| - (defmethod convert-js-module :commonjs [js] |
1250 |
| - (let [js-file (:file js) |
1251 |
| - path (.getParent (io/file js-file)) |
1252 |
| - module-root (get-js-module-root js-file) |
| 1260 | +(util/compile-if can-convert-commonjs? |
| 1261 | + (defmethod convert-js-module :commonjs [js opts] |
| 1262 | + (let [{:keys [file module-type]} js |
| 1263 | + ^List externs '() |
| 1264 | + ^List source-files (get-source-files module-type opts) |
1253 | 1265 | ^CompilerOptions options (CompilerOptions.)
|
1254 |
| - {:keys [closure-compiler root]} (init-js-module-processing js-file options) |
1255 |
| - es6-loader (ES6ModuleLoader. closure-compiler module-root) |
1256 |
| - cjs (ProcessCommonJSModules. closure-compiler es6-loader)] |
1257 |
| - (compile-if can-convert-amd? |
| 1266 | + closure-compiler (doto (make-closure-compiler) |
| 1267 | + (.init externs source-files options)) |
| 1268 | + es6-loader (if is-new-es6-loader? |
| 1269 | + (make-es6-loader source-files) |
| 1270 | + (make-es6-loader closure-compiler file)) |
| 1271 | + cjs (ProcessCommonJSModules. closure-compiler es6-loader) |
| 1272 | + ^Node root (get-root-node file closure-compiler)] |
| 1273 | + (util/compile-if can-convert-amd? |
1258 | 1274 | (when (= (:module-type js) :amd)
|
1259 | 1275 | (.process (TransformAMDToCJSModule. closure-compiler) nil root)))
|
1260 | 1276 | (.process cjs nil root)
|
1261 | 1277 | (.toSource closure-compiler root))))
|
1262 | 1278 |
|
1263 |
| -(compile-if can-convert-es6? |
1264 |
| - (defmethod convert-js-module :es6 [js] |
1265 |
| - (let [js-file (:file js) |
1266 |
| - module-root (get-js-module-root js-file) |
| 1279 | +(util/compile-if can-convert-es6? |
| 1280 | + (defmethod convert-js-module :es6 [js opts] |
| 1281 | + (let [{:keys [file module-type]} js |
| 1282 | + ^List externs '() |
| 1283 | + ^List source-files (get-source-files module-type opts) |
1267 | 1284 | ^CompilerOptions options (doto (CompilerOptions.)
|
1268 | 1285 | (.setLanguageIn CompilerOptions$LanguageMode/ECMASCRIPT6)
|
1269 | 1286 | (.setLanguageOut CompilerOptions$LanguageMode/ECMASCRIPT5))
|
1270 |
| - {:keys [closure-compiler root]} (init-js-module-processing js-file options) |
1271 |
| - es6-loader (ES6ModuleLoader. closure-compiler module-root) |
1272 |
| - cjs (ProcessEs6Modules. closure-compiler es6-loader true)] |
| 1287 | + closure-compiler (doto (make-closure-compiler) |
| 1288 | + (.init externs source-files options)) |
| 1289 | + es6-loader (if is-new-es6-loader? |
| 1290 | + (make-es6-loader source-files) |
| 1291 | + (make-es6-loader closure-compiler file)) |
| 1292 | + cjs (ProcessEs6Modules. closure-compiler es6-loader true) |
| 1293 | + ^Node root (get-root-node file closure-compiler)] |
1273 | 1294 | (.processFile cjs root)
|
1274 | 1295 | (.toSource closure-compiler root))))
|
1275 | 1296 |
|
1276 |
| -(defmethod convert-js-module :default [js] |
| 1297 | +(defmethod convert-js-module :default [js opts] |
1277 | 1298 | (ana/warning :unsupported-js-module-type @env/*compiler* js)
|
1278 | 1299 | (deps/-source js))
|
1279 | 1300 |
|
|
1293 | 1314 | (when-not (.exists out-file)
|
1294 | 1315 | (util/mkdirs out-file)
|
1295 | 1316 | (if (:module-type js)
|
1296 |
| - (spit out-file (convert-js-module js)) |
| 1317 | + (spit out-file (convert-js-module js opts)) |
1297 | 1318 | (spit out-file (deps/-source js))))
|
1298 | 1319 | (if (map? js)
|
1299 | 1320 | (merge js ijs)
|
|
1547 | 1568 | options where new modules are passed with :libs option."
|
1548 | 1569 | [opts]
|
1549 | 1570 | (let [js-modules (filter :module-type (:foreign-libs opts))]
|
1550 |
| - (reduce (fn [opts {:keys [file module-type] :as lib}] |
| 1571 | + (reduce (fn [new-opts {:keys [file module-type] :as lib}] |
1551 | 1572 | (if (or (and (= module-type :commonjs) can-convert-commonjs?)
|
1552 | 1573 | (and (= module-type :amd) can-convert-amd?)
|
1553 | 1574 | (and (= module-type :es6) can-convert-es6?))
|
1554 |
| - (let [module-name (-> (ProcessCommonJSModules/toModuleName file) |
1555 |
| - (string/replace "_" "-")) |
1556 |
| - ijs (write-javascript opts (deps/load-foreign-library lib))] |
| 1575 | + (let [ijs (write-javascript opts (deps/load-foreign-library lib)) |
| 1576 | + module-name (-> (deps/load-library (:out-file ijs)) first :provides first)] |
1557 | 1577 | (doseq [provide (:provides ijs)]
|
1558 | 1578 | (swap! env/*compiler*
|
1559 | 1579 | #(update-in % [:js-module-index] assoc provide module-name)))
|
1560 |
| - (-> opts |
| 1580 | + (-> new-opts |
1561 | 1581 | (update-in [:libs] (comp vec conj) (:out-file ijs))
|
1562 | 1582 | (update-in [:foreign-libs]
|
1563 | 1583 | (comp vec (fn [libs] (remove #(= (:file %) file) libs))))))
|
1564 |
| - opts)) |
| 1584 | + new-opts)) |
1565 | 1585 | opts js-modules)))
|
1566 | 1586 |
|
1567 | 1587 | (defn build
|
|
0 commit comments