Fork me on GitHub
#shadow-cljs
<
2023-11-20
>
Chris McCormick09:11:49

I'm writing some tests in cljs code and encountering the following error when importing an esm module. I tried several different import styles. Is there a way around this?

Chris McCormick09:11:10

[:test] Compiling ...
========= Running Tests =======================
SHADOW import error /home/chrism/dev/cljs-dopeloop/.shadow-cljs/builds/test/dev/out/cljs-runtime/shadow.js.shim.module$audio_buffer.js

/home/chrism/dev/cljs-dopeloop/tests.js:64
  /* ignore this, look at stacktrace */ fn.call(global, require, module, __filename, __dirname);
                                           ^
Error [ERR_REQUIRE_ESM]: require() of ES Module /home/chrism/dev/cljs-dopeloop/node_modules/.pnpm/[email protected]/node_modules/audio-buffer/index.js from /home/chrism/dev/cljs-dopeloop/tests.js not supported.
Instead change the require of index.js in /home/chrism/dev/cljs-dopeloop/tests.js to a dynamic import() which is available in all CommonJS modules.
    at /home/chrism/dev/cljs-dopeloop/.shadow-cljs/builds/test/dev/out/cljs-runtime/shadow.js.shim.module$audio_buffer.js:3:81
    at global.SHADOW_IMPORT (/home/chrism/dev/cljs-dopeloop/tests.js:64:44)
    at /home/chrism/dev/cljs-dopeloop/tests.js:1416:1
    at Object.<anonymous> (/home/chrism/dev/cljs-dopeloop/tests.js:1428:3)

Chris McCormick09:11:23

shadow-cljs.edn

{:source-paths ["src"]
 :dependencies []
 :builds {:test {:target :node-test
                 :output-to "tests.js"
                 :ns-regexp "dopeloop.*$"
                 :autorun true
                 :compiler-options {:output-feature-set :es8}
                 }}}

Chris McCormick09:11:36

require: ["audio-buffer$default" :as AudioBuffer]

thheller09:11:46

that is node telling you that it cannot require esm modules. there is no way arround that, as it is a node limitation

👍 1
thheller09:11:02

well the way arround that is make the CLJS output ESM via :target :esm

Chris McCormick09:11:30

Ah ok, but I guess there's no way to get :node-test to also be esm?

thheller09:11:19

not currently no, I have some plans that might enable this but thats gonna take a while

Chris McCormick09:11:40

Ok no worries. I'll try to figure something else out for now, thank you.

Aviv13:11:14

Hey, I’m trying to write some script that needs to run in browser, is there any target I can use? (headless chrome or something?)

1
thheller14:11:37

what is a script thats runs in the browser?

thheller14:11:12

do you mean a script to run a browser? like puppeteer? https://pptr.dev/

Aviv14:11:30

I’m running a script that some of its deps require a browser (the error i’m getting in node is google not found)

thheller14:11:12

what does "google not found" mean? I mean what does that mean even in a browser context? 😛

Aviv14:11:21

ReferenceError: google is not defined i guess the lib using some of the browser api

thheller14:11:31

I mean if you want something to control a browser you can use the above puppeteer with :node-script for example, but have all your other stuff in a regular :target :browser build

thheller14:11:45

well you should identify what lib that is

Aviv14:11:03

google maps lib

Aviv14:11:18

i need a browser environment

thheller14:11:24

fine, but what do you want to do in a script that would use google maps?

Aviv14:11:27

(not a puppeteer)

thheller14:11:52

its really hard to give you any answers without knowing what the actual problem is that you are trying to solve

Aviv14:11:27

i’ll try to explain the specific issue: • I have a build for browser • I’m writing a script that uses this build • The script read some var from the browser build, which uses google maps lib for some def This means the script need a browser environment

thheller14:11:55

> I’m writing a script that uses this build

thheller14:11:57

explain that

thheller14:11:15

how would is a script use that build, what does script mean there?

Aviv14:11:20

it’s an internal lib (cljs, for browser), the lib exporting a map that the script requires

thheller14:11:39

please define "script" in this context

thheller14:11:48

you mean a node utility that you run in the command line?

Aviv14:11:52

a cljs file i want to run in a ci

thheller14:11:12

ok, so then you need a headless browser I guess. so puppeteer works

Aviv14:11:19

is there something built in for shadow (like karma?) if not, what should i do? build for a browser and run using puppeteer?

thheller14:11:51

I'm still not quite sure what problem you are trying to solve, so my guesses might not fit

thheller14:11:24

but yes, two builds one for the browser, one :node-script using something like puppeteer to open a headless browser and interact with it

Aviv14:11:56

ok it sounds it might fit, i’ll give it a shot, thanks!

thheller14:11:42

karma is only useful for testing, so doesn't sound like what you want

Aviv15:11:01

i’m not sure about something, let’s say i’m running puppeteer from the node-script, how do i run the build for the browser in it?

thheller15:11:47

you don't run the build per se

thheller15:11:54

you open a webpage that runs it

thheller15:11:12

so basically you are just scripting how you'd open it in the browser yourself

Aviv15:11:54

i see, ok i’ll try, thanks a lot 🙏

Bingen Galartza Iparragirre14:11:10

Hello. Anyone using https://github.com/henryw374/cljc.java-time with shadow-cljs? We have been using it with cljsjs with no problem, but with shadow we are having some trouble. Using most of the functions works, but when dealing with timezone related one we get "zone.rules is not a function]" error. I suspect it's because for some reason js-joda/timezone is not being loaded. Any idea what could be? In package.json we have the following:

"@js-joda/core": "5.6.1",
"@js-joda/timezone": "2.18.2",
And in the code we require `js-joda/timezone like this: ["@js-joda/timezone"]

thheller14:11:27

no idea. I have never used either libraries

thheller14:11:00

could be that you just need to assign it to something yourself?

thheller14:11:11

or that you are using a newer version that has a different API

wevrem14:11:13

I encountered something similar when experimenting with updating my dependencies. This was with tick. From reading other threads by Henry here on Clojurians slack, it appears we need to be using using the same versions of js-joda as you find in the deps used for tick. See https://github.com/juxt/tick/blob/master/src/deps.cljs and https://github.com/henryw374/cljs.java-time/blob/master/src/deps.cljs

{:npm-deps {"@js-joda/timezone" "2.5.0"
            "@js-joda/locale_en-us" "3.1.1"}}
{:npm-deps {"@js-joda/core" "3.2.0"}
 :externs ["cljsjs/js-joda/common/js-joda-dup.ext.js"]}

dvingo20:11:05

I have been using newer versions of js-joda, but just noticed I'm still using locale ~3, when trying 4.x shadow-cljs fails to load the file. I made a small reproduction with a comparison of a working vite version: https://github.com/dvingo/shadow-cljs-js-joda-locale-bug

thheller08:11:19

sorry not a clue. can't say what is going on. everything looks ok to me on the compiler side

thheller08:11:30

might be the closure compiler rewriting the code in some way that doesn't work, but its gonna be hard to reproduce this with just the closure compiler

thheller08:11:24

so no clue now to reduce this further

Bingen Galartza Iparragirre09:11:10

Thank you all for your responses. I tested with the versions pointed by wevrem, and I was getting the same errors. But then I realized I was just using the library wrong. I did a mistake when copy/pasting from another project where it works, so I didn't even consider the code might be wrong 😅 After fixing the code the library works even with the latest available versions. But I'm not using js-joda/locale-en-us , just core and timezone. Nevertheless I can confirm that using the latest locale-en-us version throws the error pointed by dvingo.

dvingo10:11:58

thanks for taking a look @U05224H0W in a case like this would the only option be to add it as global and use js-options to refer to it?

thheller10:11:55

well you can always use any other bundler for the JS parts https://shadow-cljs.github.io/docs/UsersGuide.html#js-provider-external

dvingo10:11:25

oh man, totally missed that - I was trying to bundle it using webpack and then require it (https://shadow-cljs.github.io/docs/UsersGuide.html#_requiring_js) but that still had the error - the external provider should work for me, thank you!

dvingo22:11:15

if anyone is interested in using the latest js-joda locale package I updated that repo with an example of bundling it using rollup https://github.com/dvingo/shadow-cljs-js-joda-locale-bug (check the last commit to see the changes) I still wanted shadow-cljs to handle the rest of my npm deps, so this is a nice workaround

thheller06:11:38

this is absolutely NOT recommended. the issue is that any common dependency that the "externalized" dependencies might have, will now be duplicated and maybe incompatible with each other

thheller06:11:13

don't know what the dependency situation in this case looks like, but say js-joda dependent on something you also still leave shadow-cljs to bundle. then you'd 2 have versions of the "same" thing

thheller06:11:38

the only real solution is to have one bundler bundle all the JS code

thheller06:11:10

it might be fine in this case, but this is absolutely not recommended.

dvingo13:11:55

ah good to know, so the external bundle would be the way to go then - I was wondering if you have multiple shadow-cljs modules if you can get an external file for each bundle with the js deps used for that bundle? this way you can package the js deps for each cljs bundle to split things up at a more granular level

dvingo15:11:53

So you would have one common externals.js file, then one per bundle that has the unique js dependencies per each bundle

dvingo22:11:26

I am trying to load specific files from a large JS library (https://mantine.dev/getting-started/) using the techniques described here: https://shadow-cljs.github.io/docs/UsersGuide.html#_using_npm_packages but shadow is failing to resolve the file:

package @mantine/core had exports, but could not resolve ./cjs/core/MantineProvider/MantineProvider
{:package #object[java.io.File 0x89fce71 "/home/user/shadow-cljs-mantine/node_modules/@mantine/core"], :require-from nil, :rel-require "./cjs/core/MantineProvider/MantineProvider"}
ExceptionInfo: package @mantine/core had exports, but could not resolve ./cjs/core/MantineProvider/MantineProvider
the package includes both esm and commonjs and I get the same error when trying either. I put together a repo here https://github.com/dvingo/mantine-shadow-cljs-import-one demonstrating this Any ideas on how to resolve this?

thheller06:11:29

define which files you are allowed to access directly. so in this case you are not supposed to access this file directly

thheller06:11:15

you can set :js-options {:ignore-exports true} to ignore this, but the authors of the package didn't want you to 😛

dvingo10:11:03

it works! thanks so much for the help