Skip to content

Commit 067eaef

Browse files
authored
* refactor some names for readability (#215)
* add resolve-export which tries to find the entrypoint based on :package-json-resolution * make sure compiler opts are threaded through * add unit test for reported @mantine/core issue
1 parent a5b2c8a commit 067eaef

File tree

2 files changed

+79
-33
lines changed

2 files changed

+79
-33
lines changed

src/main/clojure/cljs/foreign/node.clj

+34-12
Original file line numberDiff line numberDiff line change
@@ -50,31 +50,53 @@
5050
(string/starts-with? path "./")
5151
(subs 2)))
5252

53-
(defn- ->export-pkg-json [path export]
53+
(defn- ->export-pkg-json [package-path export]
5454
(io/file
55-
(trim-package-json path)
55+
(trim-package-json package-path)
5656
(trim-relative export)
5757
"package.json"))
5858

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+
5978
(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]
6282
;; NOTE: ignore "." exports for now
6383
(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)
6587
;; technically the following logic is a bit brittle since `exports` is
6688
;; supposed to be used to hide the package structure.
6789
;; instead, here we assume the export subpath does match the library structure
6890
;; on disk, if we find a package.json we add it to pkg-jsons map
6991
;; 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)]
7193
;; note this will ignore export wildcards etc.
7294
(cond-> pkg-jsons
73-
(.exists export-pkg-json)
95+
(.exists export-pkg-json-file)
7496
(-> (assoc
75-
(.getAbsolutePath export-pkg-json)
97+
(.getAbsolutePath export-pkg-json-file)
7698
(merge
77-
(json/read-str (slurp export-pkg-json))
99+
(json/read-str (slurp export-pkg-json-file))
78100
;; add the name field so that path->main-name works later
79101
(when (and (map? export)
80102
(contains? export "require"))
@@ -92,14 +114,14 @@
92114
detailed information."
93115
[pkg-jsons opts]
94116
(reduce-kv
95-
(fn [pkg-jsons path {:strs [exports] :as pkg-json}]
117+
(fn [pkg-jsons package-path {:strs [exports] :as pkg-json}]
96118
(if (string? exports)
97119
pkg-jsons
98120
;; map case
99121
(reduce-kv
100122
(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))
103125
pkg-jsons exports)))
104126
pkg-jsons pkg-jsons))
105127

src/test/clojure/cljs/foreign/node_test.clj

+45-21
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,20 @@
2929
;; =============================================================================
3030
;; Tests
3131

32-
(defn pkg-jsons []
33-
(-> (util/module-file-seq {})
34-
node/get-pkg-jsons))
32+
(defn pkg-jsons
33+
([]
34+
(pkg-jsons {}))
35+
([opts]
36+
(-> (util/module-file-seq opts)
37+
(node/get-pkg-jsons opts))))
3538

36-
(defn indexed-lib-specs []
37-
(as-> (-> (util/module-file-seq {})
38-
(node/node-file-seq->libs-spec* {}))
39-
xs (zipmap (map :file xs) xs)))
39+
(defn indexed-lib-specs
40+
([]
41+
(indexed-lib-specs {}))
42+
([opts]
43+
(as-> (-> (util/module-file-seq opts)
44+
(node/node-file-seq->libs-spec* opts))
45+
xs (zipmap (map :file xs) xs))))
4046

4147
(defn relpath->data
4248
([index path]
@@ -60,27 +66,45 @@
6066
(deftest test-path->main-name
6167
(install :yarn "react-select" "5.7.2")
6268
(testing "Verify that path->main works as expected"
63-
(is (= "react-select"
64-
(node/path->main-name
69+
(let [node-opts {:package-json-resolution :nodejs}
70+
webpack-opts {:package-json-resolution :webpack}]
71+
(is (= "react-select"
72+
(node/path->main-name
6573
(.getAbsolutePath (io/file "node_modules/react-select/dist/react-select.cjs.js"))
66-
(relpath->data (pkg-jsons)
74+
(relpath->data (pkg-jsons node-opts)
6775
"node_modules/react-select/package.json" :find)
68-
{:package-json-resolution :nodejs})))
69-
(is (= "react-select/creatable"
76+
node-opts)))
77+
(is (= "react-select/creatable"
7078
(node/path->main-name
7179
(.getAbsolutePath (io/file "node_modules/react-select/creatable/dist/react-select-creatable.cjs.js"))
72-
(relpath->data (pkg-jsons)
80+
(relpath->data (pkg-jsons node-opts)
7381
"node_modules/react-select/creatable/package.json" :find)
74-
{:package-json-resolution :nodejs})))
75-
(is (nil? (node/path->main-name
76-
(.getAbsolutePath (io/file "node_modules/react-select/dist/react-select.cjs.js"))
77-
(relpath->data (pkg-jsons)
78-
"node_modules/react-select/package.json" :find)
79-
{:package-json-resolution :webpack})))))
82+
node-opts)))
83+
(is (nil? (node/path->main-name
84+
(.getAbsolutePath (io/file "node_modules/react-select/dist/react-select.cjs.js"))
85+
(relpath->data (pkg-jsons webpack-opts)
86+
"node_modules/react-select/package.json" :find)
87+
webpack-opts))))))
8088

81-
(comment
89+
(deftest test-exports-with-choices
90+
(install :yarn "@mantine/core" "7.0.2")
91+
(testing "Verify that complex exports are handled"
92+
(let [node-opts {:package-json-resolution :nodejs}
93+
webpack-opts {:package-json-resolution :webpack}]
94+
(is (= "@mantine/core"
95+
(node/path->main-name
96+
(.getAbsolutePath (io/file "node_modules/@mantine/core/cjs/index.js"))
97+
(relpath->data (pkg-jsons node-opts)
98+
"node_modules/@mantine/core/package.json" :find)
99+
node-opts)))
100+
(is (= "@mantine/core"
101+
(node/path->main-name
102+
(.getAbsolutePath (io/file "node_modules/@mantine/core/esm/index.mjs"))
103+
(relpath->data (pkg-jsons webpack-opts)
104+
"node_modules/@mantine/core/package.json" :find)
105+
webpack-opts))))))
82106

107+
(comment
83108
(test/run-tests)
84109
(cleanup)
85-
86110
)

0 commit comments

Comments
 (0)