|
50 | 50 | (string/starts-with? path "./") |
51 | 51 | (subs 2))) |
52 | 52 |
|
53 | | -(defn- ->export-pkg-json [path export] |
| 53 | +(defn- ->export-pkg-json [package-path export] |
54 | 54 | (io/file |
55 | | - (trim-package-json path) |
| 55 | + (trim-package-json package-path) |
56 | 56 | (trim-relative export) |
57 | 57 | "package.json")) |
58 | 58 |
|
| 59 | +(defn resolve-export |
| 60 | + "Given an export value, find the entry point based on the |
| 61 | + :package-json-resolution value, defaults to :nodejs. Returns nil |
| 62 | + if we couldn't resolve it." |
| 63 | + [export opts] |
| 64 | + (if (string? export) |
| 65 | + export |
| 66 | + ;; we check for require to attempt to filter out |
| 67 | + ;; strange cases, i.e. import but no require etc. |
| 68 | + (when (and (map? export) (contains? export "require")) |
| 69 | + (let [resolve (:package-json-resolution opts :nodejs) |
| 70 | + lookup (if (sequential? resolve) |
| 71 | + (or (some #{"import" "require"} resolve) "require") |
| 72 | + ({:webpack "import" :nodejs "require"} resolve)) |
| 73 | + entry (get export lookup)] |
| 74 | + (if (map? entry) |
| 75 | + (get entry "default") |
| 76 | + entry))))) |
| 77 | + |
59 | 78 | (defn- export-subpaths |
60 | | - "Examine the export subpaths to compute subpackages" |
61 | | - [pkg-jsons export-subpath export path pkg-name] |
| 79 | + "Examine the export subpaths to compute subpackages. Add them to pkg-json |
| 80 | + parameter (this is a reduce-kv helper)." |
| 81 | + [pkg-jsons export-subpath export package-path pkg-name opts] |
62 | 82 | ;; NOTE: ignore "." exports for now |
63 | 83 | (if (= "." export-subpath) |
64 | | - pkg-jsons |
| 84 | + (if-let [resolved (resolve-export export opts)] |
| 85 | + (assoc-in pkg-jsons [package-path "main"] resolved) |
| 86 | + pkg-jsons) |
65 | 87 | ;; technically the following logic is a bit brittle since `exports` is |
66 | 88 | ;; supposed to be used to hide the package structure. |
67 | 89 | ;; instead, here we assume the export subpath does match the library structure |
68 | 90 | ;; on disk, if we find a package.json we add it to pkg-jsons map |
69 | 91 | ;; and we synthesize "name" key based on subpath |
70 | | - (let [export-pkg-json (->export-pkg-json path export-subpath)] |
| 92 | + (let [export-pkg-json-file (->export-pkg-json package-path export-subpath)] |
71 | 93 | ;; note this will ignore export wildcards etc. |
72 | 94 | (cond-> pkg-jsons |
73 | | - (.exists export-pkg-json) |
| 95 | + (.exists export-pkg-json-file) |
74 | 96 | (-> (assoc |
75 | | - (.getAbsolutePath export-pkg-json) |
| 97 | + (.getAbsolutePath export-pkg-json-file) |
76 | 98 | (merge |
77 | | - (json/read-str (slurp export-pkg-json)) |
| 99 | + (json/read-str (slurp export-pkg-json-file)) |
78 | 100 | ;; add the name field so that path->main-name works later |
79 | 101 | (when (and (map? export) |
80 | 102 | (contains? export "require")) |
|
92 | 114 | detailed information." |
93 | 115 | [pkg-jsons opts] |
94 | 116 | (reduce-kv |
95 | | - (fn [pkg-jsons path {:strs [exports] :as pkg-json}] |
| 117 | + (fn [pkg-jsons package-path {:strs [exports] :as pkg-json}] |
96 | 118 | (if (string? exports) |
97 | 119 | pkg-jsons |
98 | 120 | ;; map case |
99 | 121 | (reduce-kv |
100 | 122 | (fn [pkg-jsons export-subpath export] |
101 | | - (export-subpaths pkg-jsons |
102 | | - export-subpath export path (get pkg-json "name"))) |
| 123 | + (export-subpaths pkg-jsons export-subpath |
| 124 | + export package-path (get pkg-json "name") opts)) |
103 | 125 | pkg-jsons exports))) |
104 | 126 | pkg-jsons pkg-jsons)) |
105 | 127 |
|
|
0 commit comments