Welcome! Please see the About page for a little more info on how this works.

0 votes
in ClojureScript by
JavaScript code using module-processing (CJS/ES6) can require ClojureScript namespaces using e.g. {{goog.require("name.space")}} or {{import * from "goog:name.space";}}. But currently Closure optimization fails if this namespace is only used by JS modules and not other Cljs namespaces.

This happens when {{build}} {{source}} is single file, or when {{:optimization}} and {{:main}} are set, as in that case build entrypoint is the {{:main}} file.

Problem is that in build pipeline, no new Cljs sources are added to build after JS files from {{:libs}} are added (including files from module processing): https://github.com/clojure/clojurescript/blob/master/src/main/clojure/cljs/closure.clj#L2809-L2819

With {{:optimization :none}} this doesn't show error during build, but there would be error during runtime about missing module. With optimizations, Closure will throw error about missing module.


;; build.clj
(require 'cljs.build.api)

(cljs.build.api/build
  "src"
  {:main 'hello.core
   :output-to "target/main.js"
   :output-dir "target/main.out"
   :asset-path "main.out"
   :optimizations :simple
   :foreign-libs [{:file "src"
                   :module-type :es6}]})

;; or "src/hello/core.cljs" as source and :optimization :one

;; src/hello/core.cljs
(ns hello.core (:require [hello.es-module :refer [greet]]))

(greet "test")

;; src/hello/say.cljs
(ns hello.say)
(defn ^:export it [m]
  (str "Hello, " m))

;; src/hello/es_module.js
import it from "goog:hello.say";

export var greet = function(m) {
    document.write(hello.say.it(m));
};


Closure error:

Mar 27, 2018 7:02:07 PM com.google.javascript.jscomp.LoggerErrorManager printSummary
WARNING: 1 error(s), 0 warning(s)
ERROR: JSC_MISSING_PROVIDE_ERROR. required "hello.say" namespace never provided at /home/juho/tmp/cljs-in-js-in-cljs/target/main.out/src/hello/es_module.js line 2 : 0
Exception in thread "main" java.lang.Exception: Closure compilation failed, compiling:(/home/juho/tmp/cljs-in-js-in-cljs/build.clj:3:1)

2 Answers

0 votes
by

Comment made by: deraen

I think I know what needs to be done. Build pipeline ({{build}} and {{compile-inputs}} functions) need to be refactored, to call {{add-dependency-sources}}, part of {{handle-js-modules}} and {{add-js-sources}} until no new sources are found. JS Module handling needs to be split into two parts: one that finds information about required JS modules and their dependencies and another that later processes all the JS modules.

0 votes
by
Reference: https://clojure.atlassian.net/browse/CLJS-2709 (reported by deraen)
...