squint-cljs / cherry

Experimental ClojureScript to ES6 module compiler
https://squint-cljs.github.io/cherry
553 stars 22 forks source link

cherry.embed/compile-string ignores (require ...) expression #96

Closed kloimhardt closed 5 months ago

kloimhardt commented 1 year ago

When cherry.embed/compile-string is called within HTML

<html>
   <body>
       <script type="module">
        import {compile_string} from "./out/main.js";
        console.log(compile_string('(require ["A"]) (equire ["A"])'))
       </script>
   </body>
</html>

main.js being a CLJS advanced Shadow compiled ES6 module, the output on the console is

equire.call(null, cljs.core.vector("A"));

The expectation is that the output includes a JS import

import "A";
equire.call(null, cljs.core.vector("A"));

To reproduce issue: cherry_require.html

borkdude commented 1 year ago

@kloimhardt In your example I see "equire", what's that?

For require to work you need to quote the argument. Not sure if top level require always works, but you can also use (as a workaround): (ns foo (:require ["A"])) or write manually:

(js/await (js/import "A"))
kloimhardt commented 1 year ago

@borkdude The answer "Not sure if top level require always works" clarifies the issue for me. Thank you.

As explanation: My subjective expectation was that compile_string transpiles any Clojure "(require ...)" to some JS "import" statement (ignorant of the result being executable JS-code or not).

The "equire": I meant it to clarify the above subjective expectation (instead it caused confusion). Indeed, the "equire" could be replaced by any arbitrary string. This "equire" demonstrates that the word "require" (with "r") is ignored completely by compile_string whereas "equire" (without "r") is correctly transpiled as a function name.

borkdude commented 5 months ago

Can this one be closed?

kloimhardt commented 5 months ago

yes, pls. close