1

Something confuse me when trying use clj command line tools to compile and bundle clojurescript with npm modules.

My clj command :

clj -M --main cljs.main \
    --repl-opts "{:launch-browser false}" \
    --compile hello.core --repl

And my package.json

{
  "dependencies": {
    "@cljs-oss/module-deps": "^1.1.1",
    "react": "^18.2.0",
    "solid-js": "^1.7.5"
  }
}

My demo code

(ns hello.core
    (:require 
        [react :as r] ;; this worked 
        ["solid-js" :as s] ;; this will not work
))


(js/console.log r s) 

When I try to require solid-js, got error : `No such namespace: solid-js, could not locate solid_js.cljs, solid_js.cljc, or JavaScript source providing "solid-js" `.

I notice that, unlike node_modules/react , node_modules/solid-js do not have a index.js in package folder. Is that the reason ? How can I import solid-js correctly ?

I have tried shadow-cljs , when using shadow-cljs the require "solid-js" works well. But I don't understand this. I want use webpack to bundle files ( clojurescript webpack guide ) . Do I MUST use shadow-cljs to compile and bundle apps ?

kmrk
  • 51
  • 4
  • You can also use webpack with shadow-cljs via [:js-provider :external](https://shadow-cljs.github.io/docs/UsersGuide.html#js-provider-external). Did you `npm install solid-js`? Or just edit the `package.json`? If you just edited it you can run `npm install` to properly install it. – Thomas Heller May 16 '23 at 06:49
  • yes. I do npm install react and solid-js before run clj compile.the package.json dependency part is generated by npm install --save. – kmrk May 16 '23 at 06:55
  • I'm sorry that perhaps I didn't catch you well. I think that the :js-provider :external is a way to "import webpack-bundled js lib to shadow-cljs" but not a way to "bundle clojurescript to main.js using webpack" – kmrk May 16 '23 at 07:04
  • Correct, it is a shadow-cljs option. You made it sound like you specifically wanted to use webpack, so I just wanted to mention that this option also exists for shadow-cljs. I cannot help you with your issue, other than say that you need to follow the full instructions. You specifically need the `build.edn` and `:bundle-cmd`. It is somewhat limited in a REPL setting, which you are trying above. – Thomas Heller May 16 '23 at 07:51
  • Before this try, I tried the 'clojurescript webpack guide' first , step by step , got the same error. After all I'm just looking to compare if webpack can produce a smaller bundle size. thank you for your suggestion. – kmrk May 16 '23 at 08:05
  • In the above `cljs.main` invocation you are create a development build. It will be gigantic and not a good measure of overall build size. The [production builds](https://clojurescript.org/guides/quick-start#production-builds) will be substantially smaller. Using webpack doesn't change that much. – Thomas Heller May 16 '23 at 08:11
  • Yes I know, what I mean is I was trying to follow the manual packing process and encountered this issue `solid-js not being able to find `. It may be that I missed some `key clojure script steps` or something. However I was able to pack successfully with cljcli + :bundle-cmd + webpack + react, indeed the size difference is not big. – kmrk May 16 '23 at 08:53

0 Answers0