|
1247 | 1247 | (let [module-root (get-js-module-root file)]
|
1248 | 1248 | (ES6ModuleLoader. closure-compiler module-root))))
|
1249 | 1249 |
|
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)) |
1253 | 1253 | (CompilerInput.))]
|
1254 | 1254 | (.getAstRoot input closure-compiler)))
|
1255 | 1255 |
|
1256 | 1256 | (defn get-source-files [module-type opts]
|
1257 | 1257 | (->> (:foreign-libs opts)
|
1258 | 1258 | (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))))))) |
1260 | 1262 |
|
1261 | 1263 | (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] |
1265 | 1268 | (if (and (= module-type :amd) can-convert-amd?)
|
1266 | 1269 | ;; AMD modules are converted via CommonJS modules
|
1267 | 1270 | :commonjs
|
|
1275 | 1278 | (set-options (CompilerOptions.))))
|
1276 | 1279 |
|
1277 | 1280 | (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 |
1280 | 1283 | ^List externs '()
|
1281 | 1284 | ^List source-files (get-source-files module-type opts)
|
1282 | 1285 | ^CompilerOptions options (make-convert-js-module-options opts)
|
|
1286 | 1289 | (make-es6-loader source-files)
|
1287 | 1290 | (make-es6-loader closure-compiler file))
|
1288 | 1291 | cjs (ProcessCommonJSModules. closure-compiler es6-loader)
|
1289 |
| - ^Node root (get-root-node file closure-compiler)] |
| 1292 | + ^Node root (get-root-node ijs closure-compiler)] |
1290 | 1293 | (util/compile-if can-convert-amd?
|
1291 |
| - (when (= (:module-type js) :amd) |
| 1294 | + (when (= module-type :amd) |
1292 | 1295 | (.process (TransformAMDToCJSModule. closure-compiler) nil root)))
|
1293 | 1296 | (.process cjs nil root)
|
1294 | 1297 | (report-failure (.getResult closure-compiler))
|
1295 |
| - (.toSource closure-compiler root)))) |
| 1298 | + (assoc ijs :source (.toSource closure-compiler root))))) |
1296 | 1299 |
|
1297 | 1300 | (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 |
1300 | 1303 | ^List externs '()
|
1301 | 1304 | ^List source-files (get-source-files module-type opts)
|
1302 | 1305 | ^CompilerOptions options (doto (make-convert-js-module-options opts)
|
|
1308 | 1311 | (make-es6-loader source-files)
|
1309 | 1312 | (make-es6-loader closure-compiler file))
|
1310 | 1313 | 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)] |
1312 | 1315 | (.processFile cjs root)
|
1313 | 1316 | (report-failure (.getResult closure-compiler))
|
1314 |
| - (.toSource closure-compiler root)))) |
| 1317 | + (assoc ijs :source (.toSource closure-compiler root))))) |
1315 | 1318 |
|
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) |
1319 | 1332 |
|
1320 | 1333 | (defn write-javascript
|
1321 | 1334 | "Write or copy a JavaScript file to output directory. Only write if the file
|
|
1332 | 1345 | :group (:group js)}]
|
1333 | 1346 | (when-not (.exists out-file)
|
1334 | 1347 | (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))) |
1338 | 1353 | (if (map? js)
|
1339 | 1354 | (merge js ijs)
|
1340 | 1355 | ijs)))
|
|
0 commit comments