|
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 | + (trim-package-json path) |
| 2802 | + (trim-relative export) |
| 2803 | + "package.json")] |
| 2804 | + (cond-> pkg-jsons |
| 2805 | + (.exists export-pkg-json) |
| 2806 | + (assoc |
| 2807 | + (.getAbsolutePath export-pkg-json) |
| 2808 | + (json/read-str (slurp export-pkg-json))))))) |
| 2809 | + pkg-jsons exports)) |
| 2810 | + pkg-jsons pkg-jsons))] |
| 2811 | + (let [ |
| 2812 | + ;; a map of all the *top-level* package.json paths and their exports |
| 2813 | + ;; to the package.json contents as EDN |
| 2814 | + pkg-jsons (add-exports |
| 2815 | + (into {} |
| 2816 | + (comp |
| 2817 | + (map #(.getAbsolutePath %)) |
| 2818 | + (filter top-level-package-json?) |
| 2819 | + (map (fn [path] |
| 2820 | + [path (json/read-str (slurp path))]))) |
| 2821 | + module-fseq))] |
2783 | 2822 | (into [] |
2784 | 2823 | (comp |
2785 | 2824 | (map #(.getAbsolutePath %)) |
2786 | 2825 | (map (fn [path] |
2787 | 2826 | (merge |
2788 | 2827 | {:file path |
2789 | 2828 | :module-type :es6} |
| 2829 | + ;; if the file is *not* a package.json, then compute what |
| 2830 | + ;; namespaces it :provides to ClojureScript |
2790 | 2831 | (when-not (package-json? path) |
2791 | 2832 | (let [pkg-json-main (some |
2792 | 2833 | (fn [[pkg-json-path {:as pkg-json :strs [name]}]] |
|
2795 | 2836 | (when-not (nil? entry) |
2796 | 2837 | ;; should be the only edge case in |
2797 | 2838 | ;; the package.json main field - Antonio |
2798 | | - (let [entry (cond-> entry |
2799 | | - (string/starts-with? entry "./") |
2800 | | - (subs 2)) |
| 2839 | + (let [entry (trim-relative entry) |
2801 | 2840 | entry-path (-> pkg-json-path |
2802 | 2841 | (string/replace \\ \/) |
2803 | 2842 | trim-package-json |
2804 | 2843 | (str entry))] |
| 2844 | + ;; find a package.json entry point that matches |
| 2845 | + ;; the `path` |
2805 | 2846 | (some (fn [candidate] |
2806 | 2847 | (when (= candidate (string/replace path \\ \/)) |
2807 | 2848 | name)) |
|
0 commit comments