Fork me on GitHub
#clojurescript
<
2022-06-26
>
winsome01:06:12

@zane, I tangled with this for a while, this is the compiler config that worked:

{:target        :nodejs
 :output-to     "out/nodejs/package/my-package.js"
 :output-dir    "out/nodejs/build"
 :main          main
 :optimizations :simple
 :pretty-print  false
 :install-deps  true
 :npm-deps      {}
 :hashbang      false
 :output-wrapper "(function (root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define([], factory);
} else if (typeof module === 'object' && module.exports) {
// Node. Does not work with strict CommonJS, but
// only CommonJS-like environments that support module.exports,
// like Node.
module.exports = factory();
} else {
// Browser globals (root is window)
root.returnExports = factory();
}
}(this, function () {

// CLJS-COMPILED-OUTPUT-HERE
%s

return main;
}));"}

winsome01:06:47

That gives you a UMD module that works in Node

winsome01:06:46

I spent a decent amount of time trying to get :target :bundle to work, but I gave up and went with this in the end.

zane06:06:33

Thanks so much! I wound up going the same route in the end, but there are some subtleties to your implementation that I'm going to borrow.

zane06:06:07

This has forced me to understand AMD modules and the Google Closure Compiler a lot better, and my current read is that it's actually not possible for :target :bundle to build AMD modules in conjunction with Webpack without a huge amount of work on the Webpack internals.

winsome01:06:28

When building for the browser we use a separate config that does use :target :bundle

{:output-to          "out/browser/index.js"
 :output-dir         "out/browser"
 :target             :bundle
 :main               main
 :optimizations      :advanced
 :optimize-constants false
 :pretty-print       false
 :pseudo-names       false
 :infer-externs      true
 :install-deps       true
 :npm-deps           {}
 :bundle-cmd         {:none    ["npx" "webpack" "--config" "./webpack.browser.config.js" "--mode=development"]
                      :default ["npx" "webpack" "--config" "./webpack.browser.config.js"]}
 :closure-defines    {cljs.core/*global* "window"}}

winsome01:06:11

This approach may be overkill, we build a separate node, browser, and clojure package from the same source code.

pinkfrog07:06:37

Hi. It seems there is no resolving-require in cljs yet?

thheller07:06:40

there never will be. well, rather unlikely given that regular builds don't have the compiler available and all IO is async.