GithubHelp home page GithubHelp logo

Comments (1)

darwin avatar darwin commented on June 8, 2024 1

Thanks for the heads up. Unfortunately that code is quite hairy and I had to copy&paste bunch of stuff[1] from cljs.repl namespace because I needed to generate source maps inline.

[1]

; unfortunately I had to copy&paste bunch of code from cljs.repl
; ------ evaluate-form --------> cut here
(defn load-dependencies [repl-env requires opts]
(doseq [ns (distinct requires)]
(cljs.repl/load-namespace repl-env ns opts)))
(defn gen-source-map [filename js-filename form]
(sm/encode*
{filename (:source-map @compiler/*source-map-data*)}
{:lines (+ (:gen-line @compiler/*source-map-data*) 3)
:file js-filename
:sources-content [(or (:source (meta form))
;; handle strings / primitives without metadata
(with-out-str (pr form)))]}))
(defmacro bind-compiler-source-map-data-gen-col-optionally [& body]
(if (some? (ns-resolve 'cljs.compiler '*source-map-data-gen-col*))
`(binding [cljs.compiler/*source-map-data-gen-col* (AtomicLong.)]
~@body)
body))
(defmacro bind-compiler-source-map-data [& body]
`(binding [cljs.compiler/*source-map-data* (atom {:source-map (sorted-map)
:gen-col 0
:gen-line 0})]
~@body))
(defn generate-js-with-source-maps! [ast filename form]
(bind-compiler-source-map-data
(bind-compiler-source-map-data-gen-col-optionally ; see https://github.com/binaryage/dirac/issues/81
(let [js-filename (string/replace filename #"\.cljs$" ".js")
generated-js (compiler/emit-str ast)
source-map-json (json/write-str (gen-source-map filename js-filename form))]
(str generated-js
"\n//# sourceURL=" js-filename
"\n//# sourceMappingURL=data:application/json;base64,"
(DatatypeConverter/printBase64Binary (.getBytes source-map-json "UTF-8")))))))
(defn load-dependencies-if-needed! [ast form env repl-env opts]
(when (#{:ns :ns*} (:op ast))
(let [ast (analyzer/no-warn (analyzer/analyze env form nil opts))
requires (into (vals (:requires ast)) (distinct (vals (:uses ast))))]
(load-dependencies repl-env requires opts))))
(defn prepare-error-data [type error repl-env form generated-js]
{:type type
:error error
:repl-env repl-env
:form form
:js generated-js})
(defn evaluate-form [repl-env env filename form wrap opts]
(binding [analyzer/*cljs-file* filename]
(let [wrapped-form (wrap form)
_ (log/trace "wrapped-form:\n" (utils/pp wrapped-form 100))
env-with-source-info (assoc env :root-source-info {:source-type :fragment
:source-form form})
env-with-source-info-and-repl-env (assoc env-with-source-info
:repl-env repl-env
:def-emits-var (:def-emits-var opts))
generated-ast (analyzer/analyze env-with-source-info-and-repl-env wrapped-form nil opts)
generated-js (generate-js-with-source-maps! generated-ast filename form)]
(log/trace "generated-js:\n" generated-js)
;; NOTE: means macros which expand to ns aren't supported for now
;; when eval'ing individual forms at the REPL - David
(load-dependencies-if-needed! generated-ast form env-with-source-info repl-env opts)
(let [result (cljs.repl/-evaluate repl-env filename (:line (meta form)) generated-js)]
(case (:status result)
:error (throw (ex-info (:value result) (prepare-error-data :js-eval-error result repl-env form generated-js)))
:exception (throw (ex-info (:value result) (prepare-error-data :js-eval-exception result repl-env form generated-js)))
:success (:value result))))))
; <----- evaluate-form -------- cut-here

from dirac.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.