|
1605 | 1605 | (js-source-file file (deps/-source lib))))
|
1606 | 1606 | js-modules))
|
1607 | 1607 |
|
1608 |
| -(defmulti convert-js-modules |
1609 |
| - "Takes a list JavaScript modules as an IJavaScript and rewrites them into a Google |
1610 |
| - Closure-compatible form. Returns list IJavaScript with the converted module |
1611 |
| - code set as source." |
1612 |
| - (fn [module-type js-modules opts] |
1613 |
| - (if (= module-type :amd) |
1614 |
| - ;; AMD modules are converted via CommonJS modules |
1615 |
| - :commonjs |
1616 |
| - module-type))) |
1617 |
| - |
1618 | 1608 | (defn make-convert-js-module-options [opts]
|
1619 | 1609 | (-> opts
|
1620 | 1610 | (select-keys
|
|
1642 | 1632 | (assoc ijs :source
|
1643 | 1633 | (.toSource closure-compiler ^Node (get result-nodes processed-file)))))
|
1644 | 1634 |
|
1645 |
| -(defmethod convert-js-modules :commonjs [module-type js-modules opts] |
1646 |
| - (let [^List externs '() |
1647 |
| - ^List source-files (get-source-files js-modules opts) |
1648 |
| - ^CompilerOptions options (doto (make-convert-js-module-options opts) |
1649 |
| - (.setProcessCommonJSModules true) |
1650 |
| - (.setTransformAMDToCJSModules (= module-type :amd))) |
1651 |
| - closure-compiler (doto (make-closure-compiler) |
1652 |
| - (.init externs source-files options))] |
1653 |
| - (.parse closure-compiler) |
1654 |
| - (report-failure (.getResult closure-compiler)) |
1655 |
| - (map (partial add-converted-source |
1656 |
| - closure-compiler (get-closure-sources closure-compiler) opts) |
1657 |
| - js-modules))) |
1658 |
| - |
1659 |
| -(defmethod convert-js-modules :es6 [module-type js-modules opts] |
| 1635 | +(defn convert-js-modules |
| 1636 | + "Takes a list JavaScript modules as an IJavaScript and rewrites them into a Google |
| 1637 | + Closure-compatible form. Returns list IJavaScript with the converted module |
| 1638 | + code set as source." |
| 1639 | + [js-modules opts] |
1660 | 1640 | (let [^List externs '()
|
1661 | 1641 | ^List source-files (get-source-files js-modules opts)
|
1662 | 1642 | ^CompilerOptions options (doto (make-convert-js-module-options opts)
|
1663 | 1643 | (.setProcessCommonJSModules true)
|
| 1644 | + (.setTransformAMDToCJSModules |
| 1645 | + (boolean (some (fn [{:keys [module-type]}] |
| 1646 | + (= module-type :amd)) js-modules))) |
1664 | 1647 | (.setLanguageIn (lang-key->lang-mode :ecmascript6))
|
1665 | 1648 | (.setLanguageOut (lang-key->lang-mode (:language-out opts :ecmascript3))))
|
1666 | 1649 | closure-compiler (doto (make-closure-compiler)
|
|
1674 | 1657 | closure-compiler (get-closure-sources closure-compiler) opts)
|
1675 | 1658 | js-modules)))
|
1676 | 1659 |
|
1677 |
| -(defmethod convert-js-modules :default [module-type js-modules opts] |
1678 |
| - (ana/warning :unsupported-js-module-type @env/*compiler* (first js-modules)) |
1679 |
| - js-modules) |
1680 |
| - |
1681 | 1660 | (defmulti js-transforms
|
1682 | 1661 | "Takes an IJavaScript with the source code set as source, transforms the
|
1683 | 1662 | source code and returns an IJavascript with the new code set as source."
|
|
2327 | 2306 | (if (seq js-modules)
|
2328 | 2307 | (util/measure (:compiler-stats opts)
|
2329 | 2308 | "Process JS modules"
|
2330 |
| - (let [;; Load all modules - add :source so preprocessing and conversion can access it |
| 2309 | + (let [_ (when-let [unsupported (first (filter (complement #{:es6 :commonjs :amd}) |
| 2310 | + (map :module-type js-modules)))] |
| 2311 | + (ana/warning :unsupported-js-module-type @env/*compiler* unsupported)) |
| 2312 | + ;; Load all modules - add :source so preprocessing and conversion can access it |
2331 | 2313 | js-modules (map (fn [lib]
|
2332 | 2314 | (let [js (deps/load-foreign-library lib)]
|
2333 | 2315 | (assoc js :source (deps/-source js opts))))
|
|
2337 | 2319 | (preprocess-js js opts)
|
2338 | 2320 | js))
|
2339 | 2321 | js-modules)
|
2340 |
| - ;; Conversion is done per module-type, because Compiler needs to process e.g. all CommonJS |
2341 |
| - ;; modules on one go, so it can handle the dependencies between modules. |
2342 |
| - ;; Amdjs modules are converted separate from CommonJS modules so they can't |
2343 |
| - ;; depend on each other. |
2344 |
| - modules-per-type (group-by :module-type js-modules) |
2345 |
| - js-modules (mapcat (fn [[module-type js-modules]] |
2346 |
| - (convert-js-modules module-type js-modules opts)) |
2347 |
| - modules-per-type)] |
2348 |
| - |
| 2322 | + js-modules (convert-js-modules js-modules opts)] |
2349 | 2323 | ;; Write modules to disk, update compiler state and build new options
|
2350 | 2324 | (reduce (fn [new-opts {:keys [file] :as ijs}]
|
2351 | 2325 | (let [ijs (write-javascript opts ijs)
|
|
0 commit comments