|
2766 | 2766 | []))))
|
2767 | 2767 |
|
2768 | 2768 | (defn- node-file-seq->libs-spec*
|
| 2769 | + "Given a sequence of non-nested node_module paths where the extension ends in |
| 2770 | + `.js/.json`, return lib-spec maps for each path containing at least :file, |
| 2771 | + :module-type, and :provides." |
2769 | 2772 | [module-fseq opts]
|
2770 | 2773 | (letfn [(package-json? [path]
|
2771 |
| - (boolean (re-find #"node_modules[/\\](@[^/\\]+?[/\\])?[^/\\]+?[/\\]package\.json$" path)))] |
2772 |
| - (let [pkg-jsons (into {} |
2773 |
| - (comp |
2774 |
| - (map #(.getAbsolutePath %)) |
2775 |
| - (filter package-json?) |
2776 |
| - (map (fn [path] |
2777 |
| - [path (json/read-str (slurp path))]))) |
2778 |
| - module-fseq) |
2779 |
| - trim-package-json (fn [s] |
2780 |
| - (if (string/ends-with? s "package.json") |
2781 |
| - (subs s 0 (- (count s) 12)) |
2782 |
| - s))] |
| 2774 | + (= "package.json" (.getName (io/file path)))) |
| 2775 | + |
| 2776 | + (top-level-package-json? [path] |
| 2777 | + (boolean (re-find #"node_modules[/\\](@[^/\\]+?[/\\])?[^/\\]+?[/\\]package\.json$" path))) |
| 2778 | + |
| 2779 | + ;; the path sans the package.json part |
| 2780 | + ;; i.e. some_lib/package.json -> some_lib |
| 2781 | + (trim-package-json [s] |
| 2782 | + (if (string/ends-with? s "package.json") |
| 2783 | + (subs s 0 (- (count s) 12)) |
| 2784 | + s)) |
| 2785 | + |
| 2786 | + (trim-relative [path] |
| 2787 | + (cond-> path |
| 2788 | + (string/starts-with? path "./") |
| 2789 | + (subs 2))) |
| 2790 | + |
| 2791 | + (add-exports [pkg-jsons] |
| 2792 | + (reduce-kv |
| 2793 | + (fn [pkg-jsons path {:strs [exports] :as pkg-json}] |
| 2794 | + (reduce-kv |
| 2795 | + (fn [pkg-jsons export _] |
| 2796 | + ;; NOTE: ignore "." exports for now |
| 2797 | + (if (= "." export) |
| 2798 | + pkg-jsons |
| 2799 | + (let [export-pkg-json |
| 2800 | + (io/file |
| 2801 | + (.getCanonicalPath (io/file (trim-package-json path) export)) |
| 2802 | + "package.json")] |
| 2803 | + (cond-> pkg-jsons |
| 2804 | + (.exists export-pkg-json) |
| 2805 | + (assoc |
| 2806 | + (.getAbsolutePath export-pkg-json) |
| 2807 | + (json/read-str (slurp export-pkg-json))))))) |
| 2808 | + pkg-jsons exports)) |
| 2809 | + pkg-jsons pkg-jsons))] |
| 2810 | + (let [ |
| 2811 | + ;; a map of all the *top-level* package.json paths and their exports |
| 2812 | + ;; to the package.json contents as EDN |
| 2813 | + pkg-jsons (add-exports |
| 2814 | + (into {} |
| 2815 | + (comp |
| 2816 | + (map #(.getAbsolutePath %)) |
| 2817 | + (filter top-level-package-json?) |
| 2818 | + (map (fn [path] |
| 2819 | + [path (json/read-str (slurp path))]))) |
| 2820 | + module-fseq))] |
2783 | 2821 | (into []
|
2784 | 2822 | (comp
|
2785 | 2823 | (map #(.getAbsolutePath %))
|
2786 | 2824 | (map (fn [path]
|
2787 | 2825 | (merge
|
2788 | 2826 | {:file path
|
2789 | 2827 | :module-type :es6}
|
| 2828 | + ;; if the file is *not* a package.json, then compute what |
| 2829 | + ;; namespaces it :provides to ClojureScript |
2790 | 2830 | (when-not (package-json? path)
|
2791 | 2831 | (let [pkg-json-main (some
|
2792 | 2832 | (fn [[pkg-json-path {:as pkg-json :strs [name]}]]
|
|
2795 | 2835 | (when-not (nil? entry)
|
2796 | 2836 | ;; should be the only edge case in
|
2797 | 2837 | ;; the package.json main field - Antonio
|
2798 |
| - (let [entry (cond-> entry |
2799 |
| - (string/starts-with? entry "./") |
2800 |
| - (subs 2)) |
| 2838 | + (let [entry (trim-relative entry) |
2801 | 2839 | entry-path (-> pkg-json-path
|
2802 | 2840 | (string/replace \\ \/)
|
2803 | 2841 | trim-package-json
|
2804 | 2842 | (str entry))]
|
| 2843 | + ;; find a package.json entry point that matches |
| 2844 | + ;; the `path` |
2805 | 2845 | (some (fn [candidate]
|
2806 | 2846 | (when (= candidate (string/replace path \\ \/))
|
2807 | 2847 | name))
|
|
0 commit comments