Skip to content

Commit d82b10a

Browse files
kommenswannodette
authored andcommitted
CLJS-3293: Some npm packages fail to require
1 parent 4a73bc8 commit d82b10a

File tree

2 files changed

+36
-20
lines changed

2 files changed

+36
-20
lines changed

src/main/clojure/cljs/closure.clj

+22-20
Original file line numberDiff line numberDiff line change
@@ -2733,7 +2733,7 @@
27332733
[]))))
27342734

27352735
(defn- node-file-seq->libs-spec*
2736-
[module-fseq]
2736+
[module-fseq opts]
27372737
(letfn [(package-json? [path]
27382738
(boolean (re-find #"node_modules[/\\](@[^/\\]+?[/\\])?[^/\\]+?[/\\]package\.json$" path)))]
27392739
(let [pkg-jsons (into {}
@@ -2756,24 +2756,26 @@
27562756
:module-type :es6}
27572757
(when-not (package-json? path)
27582758
(let [pkg-json-main (some
2759-
(fn [[pkg-json-path {:strs [main name]}]]
2760-
(when-not (nil? main)
2761-
;; should be the only edge case in
2762-
;; the package.json main field - Antonio
2763-
(let [main (cond-> main
2764-
(string/starts-with? main "./")
2765-
(subs 2))
2766-
main-path (-> pkg-json-path
2767-
(string/replace \\ \/)
2768-
trim-package-json
2769-
(str main))]
2770-
(some (fn [candidate]
2771-
(when (= candidate (string/replace path \\ \/))
2772-
name))
2773-
(cond-> [main-path]
2774-
(not (or (string/ends-with? main-path ".js")
2775-
(string/ends-with? main-path ".json")))
2776-
(into [(str main-path ".js") (str main-path "/index.js") (str main-path ".json")]))))))
2759+
(fn [[pkg-json-path {:as pkg-json :strs [name]}]]
2760+
(let [entries (package-json-entries opts)
2761+
entry (first (keep (partial get pkg-json) entries))]
2762+
(when-not (nil? entry)
2763+
;; should be the only edge case in
2764+
;; the package.json main field - Antonio
2765+
(let [entry (cond-> entry
2766+
(string/starts-with? entry "./")
2767+
(subs 2))
2768+
entry-path (-> pkg-json-path
2769+
(string/replace \\ \/)
2770+
trim-package-json
2771+
(str entry))]
2772+
(some (fn [candidate]
2773+
(when (= candidate (string/replace path \\ \/))
2774+
name))
2775+
(cond-> [entry-path]
2776+
(not (or (string/ends-with? entry-path ".js")
2777+
(string/ends-with? entry-path ".json")))
2778+
(into [(str entry-path ".js") (str entry-path "/index.js") (str entry-path ".json")])))))))
27772779
pkg-jsons)]
27782780
{:provides (let [module-rel-name (-> (subs path (.lastIndexOf path "node_modules"))
27792781
(string/replace \\ \/)
@@ -2797,7 +2799,7 @@
27972799
(:options @env/*compiler*))))
27982800
([opts]
27992801
(let [module-fseq (util/module-file-seq)]
2800-
(node-file-seq->libs-spec module-fseq))))
2802+
(node-file-seq->libs-spec module-fseq opts))))
28012803

28022804
(defn preprocess-js
28032805
"Given js-module map, apply preprocessing defined by :preprocess value in the map."

src/test/clojure/cljs/closure_tests.clj

+14
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,20 @@
159159
"jss-extend/lib"]}))
160160
modules))))
161161
(.delete (io/file "package.json"))
162+
(test/delete-node-modules)
163+
(spit (io/file "package.json") "{}")
164+
(closure/maybe-install-node-deps! {:npm-deps {"@codemirror/state" "0.17.1"}})
165+
(let [modules (closure/index-node-modules-dir)]
166+
(is (true? (some (fn [module]
167+
(= module
168+
{:file (.getAbsolutePath (io/file "node_modules/@codemirror/state/dist/index.js"))
169+
:module-type :es6
170+
:provides ["@codemirror/state/dist/index.js"
171+
"@codemirror/state/dist/index"
172+
"@codemirror/state"
173+
"@codemirror/state/dist"]}))
174+
modules))))
175+
(.delete (io/file "package.json"))
162176
(test/delete-node-modules))
163177

164178
(deftest test-index-node-modules-module-deps-js

0 commit comments

Comments
 (0)