clojurescript_为什么ClojureScript在NPM上如此出色

clojurescript

by Jacek Schae

由Jacek Schae

为什么ClojureScript在NPM上如此出色 (Why ClojureScript works so well with NPM)

Every language that complies/transpiles to JavaScript wants to connect to npm to use this huge ecosystem. The master of this is, of course, ECMAScript. The second one — in my humble opinion — is ClojureScript, due to shadow-cljs.

每种符合/编译为JavaScript的语言都希望连接到npm以使用这个庞大的生态系统。 当然,最主要的是ECMAScript。 我的第二个观点是我的拙见-由于有shadow-cljs ,它是ClojureScript

Disclaimer: I know there is a lot of work going on in different communities that tap into npm. By all means I’m not trying to diminish any of that by stating that CLJS (ClojureScript) is the best . I just want to give you a glance at how this works in ClojureScript.
免责声明:我知道在npm中,不同社区中正在进行很多工作。 无论如何,我并不想通过声明CLJS(ClojureScript)是最好的方法来减少任何这种情况。 我只想让您了解一下ClojureScript中的工作方式。

安装npm软件包 (Installing npm packages)

In ClojureScript, we install npm packages as we would in JavaScript. We use the standard package.json and shadow-cljs build tool and you’ll figure out the rest.

在ClojureScript中,我们将像在JavaScript中那样安装npm软件包。 我们使用标准的package.json和shadow-cljs构建工具,其余的您将了解。

After the installation, we have to require the package. The require statement is almost identical to import from JS. In CLJS we swap the order — first we say from where, and then what.

安装后,我们需要包装。 require语句与从JS import几乎相同。 在CLJS中,我们交换顺序-首先我们从哪里说,然后是什么。

Every ClojureScript file starts with a ns — namespace declaration. Following that we have require instead of import . And then we define a function with defn. This function will tap into our required firebase packages, and instead of using . to navigate to our method initializeApp, we use / . We make sure that when we invoke the JS method initializeApp we convert CLJS map (datastructure) to a JS Object with #js.

每个ClojureScript文件都以ns —名称空间声明开头。 接下来,我们需要而不是import。 然后我们用defn定义一个函数。 此功能将使用我们所需的firebase软件包,而不是使用。 导航到我们的方法initializeApp,我们使用/。 我们确信,当我们调用JS方法initializeApp我们把CLJS图(数据结构)的JS对象与#js

Let’s try some other npm packages to get a better feeling for the interop between npm and ClojureScript.

让我们尝试其他npm软件包,以更好地了解npm和ClojureScript之间的互操作。

React (React)

How about using React? ClojureScript has a couple of wrappers for React— the most popular one is Reagent. Here is a simple counter example with React hooks and Reagent.

如何使用React? ClojureScript有几个React的包装器-最受欢迎的是Reagent 。 这是一个带有React挂钩和Reagent的简单计数器示例。

In both examples, we first import/require React and Reagent. Then we define state in React using hooks (and in Reagent using atoms).

在这两个示例中,我们首先导入/需要React和Reagent。 然后,我们在React中使用钩子定义状态(在Reagent中使用原子定义)。

What follows is a JSX (JavaScript) and hiccup (ClojureScript) component.

接下来是一个JSX(JavaScript)和打ic(ClojureScript)组件。

This is cool, but how do we use React UI Libraries from Reagent?

这很酷,但是我们如何使用Reagent中的React UI库?

React UI库 (React UI Libraries)

One of the most popular UI libraries is material-ui. After the installation we require this library and then import our Button component as well as React. In Clojure Script we only require the Button. We don’t need to require Reagent since it’s in our ClojureScript deps. To interop with React we would use :> form and pass all of the properties that we want in a {} .

最受欢迎的UI库之一是material-ui 。 安装后,我们需要这个库,然后导入我们的Button组件以及React。 在Clojure脚本中,我们仅需要按钮。 因为它在我们的ClojureScript deps中,所以我们不需要Reagent。 要与React互操作,我们将使用:& gt; 形成并通过的所有属性,我们希望在a {}。

Redux (Redux)

How about Redux, you might ask? Well, there is a library that is build on top of Reagent, called re-frame. First designed in Dec 2014, it even pre-dates the official Elm Architecture.

您可能会问Redux呢? 好吧,有一个库是在Reagent之上构建的,称为re-frame 。 它于2014年12月首次设计,甚至早于官方Elm Architecture

By now you should have a pretty good picture as to why CLJS loves the npm ecosystem and how easy it is to interop from CLJS to JS. Maybe this is interesting to you, and you are wondering why? Why should you even try ClojureScript?

到目前为止,您应该对CLJS为什么喜欢npm生态系统以及从CLJS到JS互操作有多么容易有了一个很好的了解。 也许这对您来说很有趣,您想知道为什么吗? 为什么还要尝试ClojureScript?

为什么? (Why?)

一成不变的 (Immutable)

All ClojureScritp data structures are immutable and persistent. You don’t need to learn a new API if you want to leverage something like ImmutableJS.

所有ClojureScritp数据结构都是不可变的和持久的。 如果您想利用ImmutableJS之类的东西,则无需学习新的API。

功能性 (Functional)

ClojureScript embraces Functional Programming ideas at its core. You don’t need Lodash or Ramda.

ClojureScript以函数式编程思想为核心。 你不需要LodashRamda

简单 (Simple)

With shadow-cljs you don’t need to spend time configuring your builds. You require what you need and the build tool will do the job.

使用shadow-cljs,您无需花费时间来配置构建。 您需要所需的内容,构建工具将完成这项工作。

简洁 (Concise)

You liability is the LoC you write . ClojureScript is one of the concise programming languages out there. Check out the last section of this comparison.

您的责任是您编写的LoC。 ClojureScript是其中一种简洁的编程语言。 查看此比较的最后一部分。

强大 (Powerful)

ClojureScript uses Google Closure Tools for code minification and tree shaking. The same tools that Google is using to build Gmail, Google Calendar, Google Docs, and Google Maps.

ClojureScript使用Google Closure Tools进行代码最小化和摇树。 Google用于构建Gmail,Google日历,Google文档和Google地图的工具相同。

JavaScript (JavaScript)

It compiles/transpiles to JavaScript. Just as ES (EcmaScript) ReasonML, PureScript, and Elm.

它编译/编译为JavaScript。 就像ES(EcmaScript)ReasonML,PureScript和Elm。

友好 (Friendly)

The ClojureScript community is the most friendly and welcoming group of people that I have ever meet online. We mainly hang out on Slack and ClojureVerse.

ClojureScript社区是我网上见过的最友好,最热情的一群人。 我们主要在SlackClojureVerse闲逛

全栈 (Full-Stack)

ClojureScript’s older brother, Clojure, embraces all of these ideas with Java. If you want to write your server on one of the most performant and stable platforms there is — the Java Virtuel Machine — you can do that using the same language.

ClojureScript的哥哥Clojure用Java拥抱了所有这些想法。 如果要在性能最高且最稳定的平台之一上编写服务器-Java Virtuel Machine,则可以使用相同的语言来完成。

If you like this article you should follow me on Twitter. I only write/tweet about programming and technology — mainly about ClojureScript and Clojure.

如果您喜欢这篇文章,应该在Twitter上关注我。 我只写/推文有关编程和技术-主要是关于ClojureScript和Clojure。

翻译自: https://www.freecodecamp.org/news/why-clojurescript-works-so-well-with-npm-128221d302ba/

clojurescript

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值