This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2021-08-05
Channels
- # announcements (1)
- # babashka (5)
- # beginners (151)
- # calva (43)
- # clj-kondo (23)
- # cljdoc (1)
- # cljs-dev (6)
- # cljsrn (10)
- # clojure (60)
- # clojure-australia (1)
- # clojure-europe (26)
- # clojure-gamedev (14)
- # clojure-nl (1)
- # clojure-spec (10)
- # clojure-uk (80)
- # clojurescript (66)
- # clojureverse-ops (4)
- # community-development (7)
- # conjure (8)
- # datomic (15)
- # deps-new (1)
- # docker (27)
- # emacs (2)
- # fulcro (13)
- # honeysql (13)
- # java (5)
- # jobs-discuss (43)
- # lsp (121)
- # luminus (13)
- # malli (1)
- # off-topic (73)
- # pathom (12)
- # polylith (29)
- # practicalli (4)
- # re-frame (35)
- # reagent (44)
- # remote-jobs (5)
- # rewrite-clj (2)
- # sci (7)
- # shadow-cljs (125)
- # sql (4)
- # tools-deps (9)
- # xtdb (5)
@borkdude if you want react
to be optional you'd need to use the same createRequire
path you used for the intepreter
I tried this, but the builtin reagent doesn’t seem to be satisfied if I require react myself this way before loading reagent itself
I mean, it seems it still wants to load react relative to nbb itself instead of relative to the script
yeah thats what I mean. if you require it from the ns it'll be relative to the source itself
so the global install. if you instead require it dynamically via createRequire it should be fine
IIRC there is also an enviroment variable NODE_PATH
or NODE_INLUDE_PATH
or something like that
maybe you can find a way with that. needs to be set before starting node though so might be tricky
If you require it from the ns… doesn’t Reagent itself require react from the ns? How can I make that use createRequire?
did you experiment with a simple node script that creates a secondary process actually running nbb? that way you can control the NODE_PATH env or whatever. assuming thats actually still a thing
I didn’t do that yet, but this may be the most flexible solution, if it works. I guess it could also just be a bash script
So I guess I'll have to rename my bin to nbb-runner
and expose an additional nbb
script which is bash on linux/macOS and .cmd or so on Windows with:
#!/usr/bin/env bash
NODE_PATH=$NODE_PATH:$PWD/node_modules nbb-runner $@
I've made some notes here: https://github.com/borkdude/nbb/issues/25
It seems you can "hack" using module.constructor._initPaths
which, if this isn't _too_ hacky, is my preferred solution as this makes deploying stuff much easier
yeah, might work. I tried to incorporate this into nbb.main
but it complained about module
not being available in ESM module scope
I didn't require "module" explicitly (but I think I have done this in the past and that failed as well)
$ node out/nbb_main.js script.cljs
file:///Users/borkdude/git/nbb/out/cljs-runtime/nbb.core.js:3
import$module._initPaths();
^
ReferenceError: import$module is not defined
I've seen errors like "module is not defined in import / ESM scope" before somewhere, can't recall exactly when
FWIW, pushed it here: https://github.com/borkdude/nbb/tree/init-paths
it is not pointless since this makes (js/require "foo")
work in the interpreted code
I thought thats what https://github.com/borkdude/nbb/blob/init-paths/src/nbb/main.cljs#L20 is for
ah gotcha. when using direct JS interop:
$ node out/nbb_main.js script.cljs
file:///Users/borkdude/git/nbb/out/cljs-runtime/nbb.core.js:3
module["constructor"]._initPaths();
^
ReferenceError: module is not defined in ES module scope
@U05224H0W that :require require
is passed to the namespace evaluation logic, but users can also do interop on the global object directly
$ node out/nbb_main.js script.cljs
file:///Users/borkdude/git/nbb/out/cljs-runtime/nbb.core.js:3
module._initPaths();
^
ReferenceError: module is not defined in ES module scope
the implicit module
you get in commonjs code is different from the one you get via the "module"
module
there have been a bunch of changes in the closure compiler regarding this as well so maybe the whole shadow.esm/dynamic-import stuff isn't necessary anymore
This is the entire output btw:
$ node out/nbb_main.js script.cljs
file:///Users/borkdude/git/nbb/out/cljs-runtime/nbb.core.js:3
cljs.core.prn.cljs$core$IFn$_invoke$arity$variadic(cljs.core.prim_seq.cljs$core$IFn$_invoke$arity$2([module], 0));
^
ReferenceError: module is not defined in ES module scope
This file is being treated as an ES module because it has a '.js' file extension and '/Users/borkdude/git/nbb/package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.
at file:///Users/borkdude/git/nbb/out/cljs-runtime/nbb.core.js:3:102
at ModuleJob.run (node:internal/modules/esm/module_job:183:25)
at async Loader.import (node:internal/modules/esm/loader:178:24)
at async Object.loadESM (node:internal/process/esm_loader:68:5)
at async handleMainPromise (node:internal/modules/run_main:63:12)
(I just realized I left out the interesting bit perhaps)crap, I think I was too early. it probably picked up a "react" from an earlier install in node_modules. it seems npm remove -g nbb
doesn't clear that
I thought for the interpreted code you are setting up the require via createRequire?
ok, to tease things apart a bit: my nbb.core namespace is compiled as an ESM module my nbb.reagent namespace is compiled as an ESM module why? because code splitting only works with ESM modules currently nbb.core loads nbb.reagent on demand. the only way this works (I think) is via dynamic import (ESM modules) Then nbb.reagent loads a pre-compiled version of reagent. This doesn't run through the interpreter, it is pre-compiled. So that's just normal CLJS requires happening in reagent.
lol:
$ npx shadow-cljs --force-spawn test modules
shadow-cljs - config: /Users/borkdude/git/nbb/shadow-cljs.edn
shadow-cljs - starting via "clojure"
TBD
I was wondering how to test my ;target :esm
build.I could just compile it and then use another test runner (I've used cljs-test-runner before and that worked fine)
will this work together with :target :esm
somehow? I don't know really where to start
$ npx shadow-cljs compile test
shadow-cljs - config: /Users/borkdude/git/nbb/shadow-cljs.edn
shadow-cljs - starting via "clojure"
[:test] Compiling ...
========= Running Tests =======================
file:///Users/borkdude/git/nbb/target/test/test.js:5
var SHADOW_IMPORT_PATH = __dirname + '/../../.shadow-cljs/builds/test/dev/out/cljs-runtime';
^
ReferenceError: __dirname is not defined in ES module scope
This file is being treated as an ES module because it has a '.js' file extension and '/Users/borkdude/git/nbb/package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.
at file:///Users/borkdude/git/nbb/target/test/test.js:5:26
at file:///Users/borkdude/git/nbb/target/test/test.js:1571:3
at ModuleJob.run (node:internal/modules/esm/module_job:183:25)
at async Loader.import (node:internal/modules/esm/loader:178:24)
at async Object.loadESM (node:internal/process/esm_loader:68:5)
at async handleMainPromise (node:internal/modules/run_main:63:12)
that way you also never have to worry about including files in the published package that shouldn't be there (eg. cljs sources)
something like :aliases [:test]
on the level of the build for example which would pull in :extra-paths ["test"]