This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-07-02
Channels
- # babashka (68)
- # beginners (22)
- # calva (8)
- # cider (10)
- # cljs-dev (31)
- # clojure (35)
- # clojure-europe (6)
- # clojure-norway (17)
- # clojurescript (5)
- # conjure (10)
- # data-science (8)
- # datascript (10)
- # emacs (3)
- # fulcro (20)
- # humbleui (3)
- # london-clojurians (1)
- # membrane (9)
- # nbb (34)
- # off-topic (16)
- # pathom (15)
- # releases (1)
- # shadow-cljs (15)
- # sql (15)
I'm exploring making a library that allows you to programmatically spawn webworkers in CLJS. To do this, I need to craft a JS script to inject into the worker that will correctly import/require things in a way that works within webworkers. I'm focusing on non-advanced compile mode right now. I've got a thing that works with cljs.main cli and figwheel, but I haven't got it to work yet in shadow:
(defn create-worker-body [require-ns afn value tid]
(println :require-ns require-ns)
(let [pre-loads
(str "var CLOSURE_UNCOMPILED_DEFINES = {\"cljs.core._STAR_target_STAR_\":\"webworker\"};"
"var quarkpreload = {};\n"
"quarkpreload.fn = `" afn "`;\n"
"quarkpreload.args = `" value "`;\n"
"quarkpreload.tid = `" tid "`;\n")]
(if (empty? goog/basePath)
(str pre-loads
"importScripts('" (last (scripts-src)) "');\n")
(str pre-loads
"CLOSURE_BASE_PATH = '" goog/basePath "';\n"
"this.CLOSURE_IMPORT_SCRIPT = (function(global) {\n"
" return function(src) {\n"
" global['importScripts'](src);\n"
" return true;\n"
" };\n"
"})(this);\n"
"if(typeof goog == 'undefined') importScripts(CLOSURE_BASE_PATH + 'base.js');\n"
"importScripts(CLOSURE_BASE_PATH + 'deps.js');\n"
"importScripts(CLOSURE_BASE_PATH + '../cljs_deps.js');\n"
"goog.require('process.env');\n"
"goog.require('" require-ns "');\n"))))
In shadow, I get Error: browser bootstrap used in incorrect target
because goog.global.document
isn't available:
var doc = goog.global.document;
if (!doc) {
throw new Error("browser bootstrap used in incorrect target");
}
Any pointers here?I'm trying to make a minimal script that will work across shadow, figwheel and cljs.main
If I need a special version for shadow, I could potentially make a thing to detect which build environment I'm in, and spit out the correct one
In the long run, maybe it'd be good for cljs.main/figwheel/shadow to all expose a out/webworker.js
file that CLJS users can know they can always pass to new Worker(...
and know that the given build system will handle setup the way it wants to.
Is possible to define ^:export
'ed functions at shadow-cljs.edn
?
I have two builds and I want to export a different set of functions in the same ns
@john the web-worker output is supposed to be loaded as a regular file (ie. new Worker("/js/worker.js")
) not as a blob. then source maps will also work
@souenzzo no, but you can skip export completely and instead create 2 different entry namespace