Skip to content

Commit

Permalink
Don't load modules more than once (babashka#159)
Browse files Browse the repository at this point in the history
* Also rename out to lib
  • Loading branch information
borkdude authored Apr 7, 2022
1 parent e5710db commit 6fdacee
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.shadow-cljs
node_modules
out
lib
.nrepl-port
.cpcache
.lsp
Expand Down
18 changes: 9 additions & 9 deletions bb.edn
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,32 @@
" --config-merge shadow-tests.edn")
cmd)]
cmd)))
clean (fs/delete-tree "out")
clean (fs/delete-tree "lib")

compile (clojure (wrap-cmd "-M -m shadow.cljs.devtools.cli --force-spawn compile modules"))

dev {:doc "Run shadow in watch mode with tests enabled."
:task
(binding [*test* true]
(println "Starting shadow-cljs in watch mode.")
(println "Run node out/nbb_main.js to test nbb")
(println "Run node lib/nbb_main.js to test nbb")
(println "Run bb run-tests to run the tests")
(apply clojure (wrap-cmd "-M -m shadow.cljs.devtools.cli --force-spawn watch modules")
*command-line-args*))}

run-tests (shell "node out/nbb_tests.js")
run-tests (shell "node lib/nbb_tests.js")

release {:depends [clean]
:doc "Compiles release build."
:task
(do (apply clojure (wrap-cmd "-M -m shadow.cljs.devtools.cli --force-spawn release modules")
*command-line-args*)
(spit "out/nbb_core.js"
(clojure.string/replace (slurp "out/nbb_core.js") (re-pattern "self") "globalThis"))
(spit "out/nbb_main.js"
(str "#!/usr/bin/env node\n\n" (slurp "out/nbb_main.js")))
(shell "chmod +x out/nbb_main.js")
(run! fs/delete (fs/glob "out" "**.map")))}
(spit "lib/nbb_core.js"
(clojure.string/replace (slurp "lib/nbb_core.js") (re-pattern "self") "globalThis"))
(spit "lib/nbb_main.js"
(str "#!/usr/bin/env node\n\n" (slurp "lib/nbb_main.js")))
(shell "chmod +x lib/nbb_main.js")
(run! fs/delete (fs/glob "lib" "**.map")))}

run-integration-tests nbb-tests/main

Expand Down
2 changes: 1 addition & 1 deletion index.mjs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './out/nbb_api.js';
export * from './lib/nbb_api.js';
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"type": "module",
"main": "index.mjs",
"bin": {
"nbb": "./out/nbb_main.js"
"nbb": "./lib/nbb_main.js"
},
"description": "Babashka-like tool for Node.js.",
"devDependencies": {
Expand All @@ -16,13 +16,13 @@
},
"repository": {
"type": "git",
"url": "https://github.com/borkdude/nbb"
"url": "https://github.com/babashka/nbb"
},
"license": "EPL-1.0",
"files": [
"README.md",
"img",
"out"
"lib"
],
"dependencies": {}
}
2 changes: 1 addition & 1 deletion script/nbb_nrepl_tests.clj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
(def port (atom 13337))

(defn nrepl-server []
(process ["node" "out/nbb_main.js" "nrepl-server" ":port" @port]
(process ["node" "lib/nbb_main.js" "nrepl-server" ":port" @port]
(merge {:out :inherit
:err :inherit})))

Expand Down
4 changes: 2 additions & 2 deletions script/nbb_repl_tests.clj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

(defn repl-process
[input dir opts]
(process (into ["node" (str (fs/absolutize "out/nbb_main.js"))] (:cmd opts))
(process (into ["node" (str (fs/absolutize "lib/nbb_main.js"))] (:cmd opts))
(merge {:dir (or dir ".")
:out :string
:in input
Expand Down Expand Up @@ -77,7 +77,7 @@
([input match] (socket-repl input match nil))
([input match dir] (socket-repl input match dir nil))
([input match dir opts]
(let [p (process (into ["node" "out/nbb_main.js"]
(let [p (process (into ["node" "lib/nbb_main.js"]
(or (:cmd opts)
["socket-repl" ":port" "1337"]))
(merge {:inherit true
Expand Down
4 changes: 2 additions & 2 deletions script/nbb_tests.clj
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
(let [[opts args] (if (map? x)
[x xs]
[nil (cons x xs)])]
(-> (process (into ["node" "out/nbb_main.js"] args)
(-> (process (into ["node" "lib/nbb_main.js"] args)
(merge {:out :string
:err :inherit}
opts)))))
Expand Down Expand Up @@ -162,7 +162,7 @@
(normalize-interop-output (nbb* "examples/cljs-bean/example.cljs")))))

(deftest error-test
(let [err (-> (process ["node" "out/nbb_main.js" "test-scripts/error.cljs"]
(let [err (-> (process ["node" "lib/nbb_main.js" "test-scripts/error.cljs"]
{:out :string
:err :string})
deref
Expand Down
2 changes: 1 addition & 1 deletion shadow-cljs.edn
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
:compiler-options {:infer-externs :auto}
:target :esm
:runtime :node
:output-dir "out"
:output-dir "lib"
:modules
{:nbb_core {:init-fn nbb.core/init}
:nbb_goog_string {:init-fn nbb.impl.gstring/init
Expand Down
6 changes: 5 additions & 1 deletion src/nbb/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,12 @@
{'clojure.pprint 'cljs.pprint
'clojure.test 'cljs.test})

(def sci-find-ns (delay (sci/eval-form @sci-ctx 'find-ns)))

(defn load-module [m libname as refer rename libspecs]
(-> (esm/dynamic-import m)
(-> (if (some? (@sci-find-ns libname))
(js/Promise.resolve nil)
(esm/dynamic-import m))
(.then (fn [_module]
(let [nlib (normalize-libname libname)]
(when-not (= nlib libname)
Expand Down

0 comments on commit 6fdacee

Please sign in to comment.