Skip to content

Commit 78753d3

Browse files
mfikesdnolen
authored andcommitted
CLJS-2724: Native Node modules Node (like "fs") cannot be required
1 parent b31a9a9 commit 78753d3

File tree

2 files changed

+90
-81
lines changed

2 files changed

+90
-81
lines changed

src/main/clojure/cljs/cli.clj

Lines changed: 81 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -299,90 +299,90 @@ present"
299299

300300
(defn default-main
301301
[repl-env {:keys [main script args repl-env-options options inits] :as cfg}]
302-
(env/ensure
303-
(let [opts (cond-> options
304-
(not (:output-dir options))
305-
(assoc :output-dir (temp-out-dir) :temp-output-dir? true)
306-
(not (contains? options :aot-cache))
307-
(assoc :aot-cache true))
308-
reopts (merge repl-env-options
309-
(select-keys opts [:output-to :output-dir]))
310-
_ (when (or ana/*verbose* (:verbose opts))
311-
(util/debug-prn "REPL env options:" (pr-str reopts)))
312-
renv (apply repl-env (mapcat identity reopts))
313-
coptsf (when-let [od (:output-dir opts)]
314-
(io/file od "cljsc_opts.edn"))
315-
copts (when (and coptsf (.exists coptsf))
316-
(-> (edn/read-string (slurp coptsf))
317-
(dissoc-entry-point-opts)))
318-
opts (merge copts
319-
(build/add-implicit-options
320-
(merge (repl/repl-options renv) opts)))]
321-
(binding [ana/*cljs-ns* 'cljs.user
322-
repl/*repl-opts* opts
323-
ana/*verbose* (:verbose opts)
324-
repl/*repl-env* renv]
325-
(when ana/*verbose*
326-
(util/debug-prn "Compiler options:" (pr-str repl/*repl-opts*)))
327-
(comp/with-core-cljs repl/*repl-opts*
328-
(fn []
329-
(try
330-
(repl/setup renv repl/*repl-opts*)
331-
;; REPLs don't normally load cljs_deps.js
332-
(when (and coptsf (.exists coptsf))
333-
(let [depsf (io/file (:output-dir opts) "cljs_deps.js")]
334-
(when (.exists depsf)
335-
(repl/evaluate renv "cljs_deps.js" 1 (slurp depsf)))))
336-
(repl/evaluate-form renv (ana-api/empty-env) "<cljs repl>"
337-
(when-not (empty? args)
338-
`(set! *command-line-args* (list ~@args))))
339-
(repl/evaluate-form renv (ana-api/empty-env) "<cljs repl>"
340-
`(~'ns ~'cljs.user))
341-
(repl/run-inits renv inits)
342-
(when script
343-
(cond
344-
(= "-" script)
345-
(repl/load-stream renv "<cljs repl>" *in*)
346-
347-
(.exists (io/file script))
348-
(with-open [stream (io/reader script)]
349-
(repl/load-stream renv script stream))
350-
351-
(string/starts-with? script "@/")
352-
(if-let [rsrc (io/resource (subs script 2))]
353-
(repl/load-stream renv (util/get-name rsrc) rsrc)
354-
(missing-resource script))
355-
356-
(string/starts-with? script "@")
357-
(if-let [rsrc (io/resource (subs script 1))]
358-
(repl/load-stream renv (util/get-name rsrc) rsrc)
359-
(missing-resource script))
360-
361-
(string/starts-with? script "-")
362-
(throw
363-
(ex-info
364-
(str "Expected script or -, got flag " script " instead")
365-
{:cljs.main/error :invalid-arg}))
302+
(let [opts (cond-> options
303+
(not (:output-dir options))
304+
(assoc :output-dir (temp-out-dir) :temp-output-dir? true)
305+
(not (contains? options :aot-cache))
306+
(assoc :aot-cache true))
307+
reopts (merge repl-env-options
308+
(select-keys opts [:output-to :output-dir]))
309+
_ (when (or ana/*verbose* (:verbose opts))
310+
(util/debug-prn "REPL env options:" (pr-str reopts)))
311+
renv (apply repl-env (mapcat identity reopts))
312+
coptsf (when-let [od (:output-dir opts)]
313+
(io/file od "cljsc_opts.edn"))
314+
copts (when (and coptsf (.exists coptsf))
315+
(-> (edn/read-string (slurp coptsf))
316+
(dissoc-entry-point-opts)))
317+
opts (merge copts
318+
(build/add-implicit-options
319+
(merge (repl/repl-options renv) opts)))]
320+
(binding [env/*compiler* (env/default-compiler-env opts)
321+
ana/*cljs-ns* 'cljs.user
322+
repl/*repl-opts* opts
323+
ana/*verbose* (:verbose opts)
324+
repl/*repl-env* renv]
325+
(when ana/*verbose*
326+
(util/debug-prn "Compiler options:" (pr-str repl/*repl-opts*)))
327+
(comp/with-core-cljs repl/*repl-opts*
328+
(fn []
329+
(try
330+
(repl/setup renv repl/*repl-opts*)
331+
;; REPLs don't normally load cljs_deps.js
332+
(when (and coptsf (.exists coptsf))
333+
(let [depsf (io/file (:output-dir opts) "cljs_deps.js")]
334+
(when (.exists depsf)
335+
(repl/evaluate renv "cljs_deps.js" 1 (slurp depsf)))))
336+
(repl/evaluate-form renv (ana-api/empty-env) "<cljs repl>"
337+
(when-not (empty? args)
338+
`(set! *command-line-args* (list ~@args))))
339+
(repl/evaluate-form renv (ana-api/empty-env) "<cljs repl>"
340+
`(~'ns ~'cljs.user))
341+
(repl/run-inits renv inits)
342+
(when script
343+
(cond
344+
(= "-" script)
345+
(repl/load-stream renv "<cljs repl>" *in*)
346+
347+
(.exists (io/file script))
348+
(with-open [stream (io/reader script)]
349+
(repl/load-stream renv script stream))
350+
351+
(string/starts-with? script "@/")
352+
(if-let [rsrc (io/resource (subs script 2))]
353+
(repl/load-stream renv (util/get-name rsrc) rsrc)
354+
(missing-resource script))
355+
356+
(string/starts-with? script "@")
357+
(if-let [rsrc (io/resource (subs script 1))]
358+
(repl/load-stream renv (util/get-name rsrc) rsrc)
359+
(missing-resource script))
360+
361+
(string/starts-with? script "-")
362+
(throw
363+
(ex-info
364+
(str "Expected script or -, got flag " script " instead")
365+
{:cljs.main/error :invalid-arg}))
366366

367-
:else
367+
:else
368+
(throw
369+
(ex-info
370+
(str "Script " script " does not exist")
371+
{:cljs.main/error :invalid-arg}))))
372+
(when main
373+
(let [src (build/ns->source main)]
374+
(when-not src
368375
(throw
369376
(ex-info
370-
(str "Script " script " does not exist")
371-
{:cljs.main/error :invalid-arg}))))
372-
(when main
373-
(let [src (build/ns->source main)]
374-
(when-not src
375-
(throw
376-
(ex-info
377-
(str "Namespace " main " does not exist."
378-
(when (string/includes? main "-")
379-
" Please check that namespaces with dashes use underscores in the ClojureScript file name."))
380-
{:cljs.main/error :invalid-arg})))
381-
(repl/load-stream renv (util/get-name src) src)
382-
(repl/evaluate-form renv (ana-api/empty-env) "<cljs repl>"
383-
`(~(symbol (name main) "-main") ~@args))))
384-
(finally
385-
(repl/tear-down renv)))))))))
377+
(str "Namespace " main " does not exist."
378+
(when (string/includes? main "-")
379+
" Please check that namespaces with dashes use underscores in the ClojureScript file name."))
380+
{:cljs.main/error :invalid-arg})))
381+
(repl/load-stream renv (util/get-name src) src)
382+
(repl/evaluate-form renv (ana-api/empty-env) "<cljs repl>"
383+
`(~(symbol (name main) "-main") ~@args))))
384+
(finally
385+
(repl/tear-down renv))))))))
386386

387387
(defn- main-opt
388388
"Call the -main function from a namespace with string arguments from

src/test/cljs_cli/cljs_cli/test.clj

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,12 @@
9797
(output-is
9898
nil
9999
"{:ns cljs.user, :value 3}"))))
100+
101+
(deftest test-cljs-2724
102+
(with-repl-env-filter #{"node"}
103+
(-> (cljs-main
104+
"-e" "(require 'fs)"
105+
"-e" "fs/R_OK")
106+
(output-is
107+
nil
108+
4))))

0 commit comments

Comments
 (0)