|
396 | 396 |
|
397 | 397 | (defn compile-string*
|
398 | 398 | ([s] (compile-string* s nil))
|
| 399 | + ([s opts] (compile-string* s opts nil)) |
399 | 400 | ([s {:keys [elide-exports
|
400 | 401 | elide-imports
|
401 | 402 | core-alias
|
402 | 403 | aliases]
|
403 | 404 | :or {core-alias "squint_core"}
|
404 |
| - :as opts}] |
405 |
| - (binding [cc/*core-package* "squint-cljs/core.js" |
406 |
| - cc/*target* :squint |
407 |
| - *jsx* false |
408 |
| - cc/*repl* (:repl opts cc/*repl*)] |
409 |
| - (let [opts (merge {:ns-state (atom {})} opts) |
410 |
| - imported-vars (atom {}) |
411 |
| - public-vars (atom #{}) |
412 |
| - aliases (atom (merge aliases {core-alias cc/*core-package*})) |
413 |
| - imports (atom (if cc/*repl* |
414 |
| - (format "var %s = await import('%s');\n" |
415 |
| - core-alias cc/*core-package*) |
416 |
| - (format "import * as %s from '%s';\n" |
417 |
| - core-alias cc/*core-package*)))] |
418 |
| - (binding [*imported-vars* imported-vars |
419 |
| - *public-vars* public-vars |
420 |
| - *aliases* aliases |
421 |
| - *jsx* false |
422 |
| - *excluded-core-vars* (atom #{}) |
423 |
| - *cljs-ns* *cljs-ns* |
424 |
| - cc/*target* :squint |
425 |
| - cc/*async* (:async opts)] |
426 |
| - (let [transpiled (transpile-string* s (assoc opts |
427 |
| - :core-alias core-alias |
428 |
| - :imports imports)) |
429 |
| - imports (when-not elide-imports @imports) |
430 |
| - exports (when-not elide-exports |
431 |
| - (str |
432 |
| - (when-let [vars (disj @public-vars "default$")] |
433 |
| - (when (seq vars) |
434 |
| - (str (format "\nexport { %s }\n" |
435 |
| - (str/join ", " vars))))) |
436 |
| - (when (contains? @public-vars "default$") |
437 |
| - "export default default$\n")))] |
438 |
| - {:imports imports |
439 |
| - :exports exports |
440 |
| - :body transpiled |
441 |
| - :javascript (str imports transpiled exports) |
442 |
| - :jsx *jsx* |
443 |
| - :ns *cljs-ns*})))))) |
| 405 | + :as opts} state] |
| 406 | + (let [opts (merge state opts)] |
| 407 | + (binding [cc/*core-package* "squint-cljs/core.js" |
| 408 | + cc/*target* :squint |
| 409 | + *jsx* false |
| 410 | + cc/*repl* (:repl opts cc/*repl*)] |
| 411 | + (let [opts (merge {:ns-state (atom {})} opts) |
| 412 | + imported-vars (atom {}) |
| 413 | + public-vars (atom #{}) |
| 414 | + aliases (atom (merge aliases {core-alias cc/*core-package*})) |
| 415 | + imports (atom (if cc/*repl* |
| 416 | + (format "var %s = await import('%s');\n" |
| 417 | + core-alias cc/*core-package*) |
| 418 | + (format "import * as %s from '%s';\n" |
| 419 | + core-alias cc/*core-package*)))] |
| 420 | + (binding [*imported-vars* imported-vars |
| 421 | + *public-vars* public-vars |
| 422 | + *aliases* aliases |
| 423 | + *jsx* false |
| 424 | + *excluded-core-vars* (atom #{}) |
| 425 | + *cljs-ns* (:ns opts *cljs-ns*) |
| 426 | + cc/*target* :squint |
| 427 | + cc/*async* (:async opts)] |
| 428 | + (let [transpiled (transpile-string* s (assoc opts |
| 429 | + :core-alias core-alias |
| 430 | + :imports imports)) |
| 431 | + imports (when-not elide-imports @imports) |
| 432 | + exports (when-not elide-exports |
| 433 | + (str |
| 434 | + (when-let [vars (disj @public-vars "default$")] |
| 435 | + (when (seq vars) |
| 436 | + (str (format "\nexport { %s }\n" |
| 437 | + (str/join ", " vars))))) |
| 438 | + (when (contains? @public-vars "default$") |
| 439 | + "export default default$\n")))] |
| 440 | + (assoc opts |
| 441 | + :imports imports |
| 442 | + :exports exports |
| 443 | + :body transpiled |
| 444 | + :javascript (str imports transpiled exports) |
| 445 | + :jsx *jsx* |
| 446 | + :ns *cljs-ns* |
| 447 | + :ns-state (:ns-state opts))))))))) |
| 448 | + |
| 449 | +#?(:cljs |
| 450 | + (defn clj-ize-opts [opts] |
| 451 | + (cond-> opts |
| 452 | + (:context opts) (update :context keyword) |
| 453 | + (:ns opts) (update :ns symbol) |
| 454 | + (:elide_imports opts) (assoc :elide-imports (:elide_imports opts)) |
| 455 | + (:elide_exports opts) (assoc :elide-exports (:elide_exports opts))))) |
| 456 | + |
| 457 | +#?(:cljs |
| 458 | + (defn compileStringEx [s opts state] |
| 459 | + (let [opts (js->clj opts :keywordize-keys true) |
| 460 | + state (js->clj state :keywordize-keys true)] |
| 461 | + (clj->js (compile-string* s (clj-ize-opts opts) (clj-ize-opts state)))))) |
444 | 462 |
|
445 | 463 | (defn compile-string
|
446 | 464 | ([s] (compile-string s nil))
|
447 | 465 | ([s opts]
|
448 | 466 | (let [opts #?(:cljs (if (object? opts)
|
449 |
| - (cond-> (js->clj opts :keywordize-keys true) |
450 |
| - :context (update :context keyword)) |
| 467 | + (clj-ize-opts opts) |
451 | 468 | opts)
|
452 | 469 | :default opts)
|
453 | 470 | {:keys [javascript]}
|
454 | 471 | (compile-string* s opts)]
|
455 | 472 | javascript)))
|
456 |
| - |
457 |
| -#_(defn compile! [s] |
458 |
| - (prn :s s) |
459 |
| - (let [expr (e/parse-string s {:row-key :line |
460 |
| - :col-key :column |
461 |
| - :end-location false}) |
462 |
| - compiler-env (ana-api/empty-state)] |
463 |
| - (prn :expr expr (meta expr)) |
464 |
| - (binding [cljs.env/*compiler* compiler-env |
465 |
| - ana/*cljs-ns* 'cljs.user] |
466 |
| - (let [analyzed (ana/analyze (ana/empty-env) expr)] |
467 |
| - (prn (keys analyzed)) |
468 |
| - (prn (compiler/emit-str analyzed)))))) |
|
0 commit comments