Hi. Just for fun I'm trying to develop a JS plugin for an application I'm using, using clojurescript (as far as I understand about clojurescript is possible). Is not working and trying to understand why, I noticed I have a simple function
(defn decir-hola-run [_ctx _context]
(println "¡Hola desde la acción decir_hola!")
{:message "¡Hola! Acción decir_hola ejecutada correctamente."})
But in the generated JS file, there is nothing about my "Hola" strings.
At the end, what I'm trying to do the same as this JS when loaded, but I must be doing all wrong because I don't see any trace about my code in the generated JS file):
module.exports = {
decir_hola: {
description: "Muestra un mensaje simple",
run: async (args, context) => {
console.log("¡Hola desde decir_hola!");
// Podrías usar aquí mecanismos Saltcorn para mostrar toast si la API lo permite
return { success: true, message: "¡Hola desde la acción decir_hola!" };
}
}
};You have to explicitly ^:export your defn
And specify build target based on your JS env, browser or node
Or just do manual module.exports in cljs same way as you do it in js
Sorry, that was not the complete code.
(ns plugin.correos-api)
(defn decir-hola-run [_ctx _context]
(println "¡Hola desde la acción decir_hola!")
{:message "¡Hola! Acción decir_hola ejecutada correctamente."})
(def decirhola
{:decir_hola
{:description "Mostrar un saludo simple"
:run decir-hola-run}})
And this is the shadow-cljs.edn
{:source-paths ["src"]
:builds {:app {:target :node-library
:output-to "out/correos_api.js"
:exports {:correos_api plugin.correos-api/decirhola}
:closure-defines {goog.DEBUG true}
:compiler-options {:optimizations :none}}}}BTW, because the idea was to get something fast just to test if it's possible to do it and work, is AI code
Was the file that you were checking built with a release build?
Sorry @p-himik but I'm not sure I understand you. New rto clojure and more new to clojurescript
With shadow-cljs and other similar tools you can make two kinds of builds: for development and for production.
The former lets the code be reloadable, doesn't optimize things, etc.
The latter does a lot to your code.
If you were checking the dev code, there are multiple files and the top-level file only loads other files. The top-level file itself won't have any useful code, it won't have your strings. But some other files will.
In a production file (with shadow-cljs, it's created with its release command), every module has just a single file. Since you don't define any modules in shadow-cljs.edn, it will be just a single module, with just one file - all the strings should be there.
Ok, that makes sense. Now I know why maybe I don't see my code, my issue is how to detect if the generated JS makes the same at the end because the third app is not showing me the action. Need to do more testing
thanks
I'd try first with a production build to confirm that it works.
Yes, . I will try to get it working with a pure javascript module, then will trasnlate to clojurescript because I noticed several issues here
not related with clojurescruipt
Finnally got it working!!! yay!!!!!
"clj->js", and ":exports-var" in shadow-cljs.edn were the key
I have forgotten why it is in cljs you do
(extend-protocol Foo object ... )
or similarly, string. are there any docs on this? tried to find but failed. only this https://www.exchangetuts.com/extend-protocol-in-clojurescript-to-get-value-from-native-js-object-1641206764327839 which doesnt explain
also, what if you want to extend to some other 'built-in' type such as js/Foo.Bar ?There is a list here: https://cljs.github.io/api/cljs.core/extend-type