This repository has been archived by the owner on Jun 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 85
Invoke node
Andrea Richiardi edited this page Mar 28, 2018
·
1 revision
It is unlikely that everything you want to can be done entirely in ClojureScript; you will almost certainly need to do some work using Node.js libraries, so that means some interop.
In ClojureScript, interop with JavaScript occurs using the js/
prefix.
For example, to invoke the JavaScript process.exit
function,
simply add a js/
prefix:
(defn exit [code]
(js/process.exit code))
You can also use the :require
option to add an entire JavaScript module as if
it were a ClojureScript namespace.
(ns config.gen
(:require fs
[cljstache.core :refer [render]]))
(defn render-template
[source-path output-path render-data]
(let [source-text (fs/readFileSync source-path)
rendered (render source-text render-data)]
(fs/writeFileSync output-path rendered)))
This example requires the Node fs (File System) module as fs
, then
invokes functions exported by the module.
For a complete walkthrough of ClojureScript syntax and interop, see:
- ClojureScript-Syntax-in-15-minutes - Shaun Lebron
- ClojureScript: JavaScript Interop - Rafal Spacjer
- ClojureScript Interop - Lambda Island