gpt4 book ai didi

clojurescript - 如何在 ClojureScript 中编写不可知的 JavaScript 库?

转载 作者:行者123 更新时间:2023-12-04 06:19:09 26 4
gpt4 key购买 nike

假设我有一个 cljs包含以下内容的文件:

(ns foo)
(defn add [x y]
(+ x y))

并希望将其作为 JavaScript 库提供给非 ClojureScript 开发人员(主要关注 node.js)。我可以做这个:
clj -m cljs.main -c foo

但问题是输出是针对谷歌闭包的模块系统(例如 goog.require )。我可以将目标设置为 none-t标志(与浏览器或节点相反),并且......不能解决这个问题。将其设置为 node也不能解决问题:否 index.js (它在 Java 中称为 main),没有 module.exports = blah blah .似乎它面向独立的全节点应用程序,而不是库。

我知道 ClojureScript 对它自己的子模块使用 google 闭包,我不一定希望摆脱所有这些(我不确定你是否可以)。我知道 es2015 原生 JavaScript 模块因为它们的静态特性而被淘汰了。

我可以手动或通过脚本按摩输出以与 npm 生态系统配合使用,但令我惊讶的是,没有编译器选项可以实际输出对 npm 友好的模块。或者有吗?我只是在阅读 --help错误的?

最佳答案

这假设您已经安装了 ClojureScript 和 Node.js

鉴于:

math101
|-- package.json
|-- src
| `-- com
| `-- example
| `-- math.cljs

包.json
{
"name": "math101",
"version": "1.0.0",
"main": "dist/index.js",
"license": "MIT",
"devDependencies": {
"shadow-cljs": "^2.8.52",
"source-map-support": "^0.5.13"
}
}

笔记:
  • dist/index.js - 这将包含我们转换为 JavaScript
  • 的 ClojureScript 代码
  • shadow-cljs - 我们需要的构建(和依赖管理)工具
  • source-map-support - Required to run ClojureScript on Node.js

  • 📣在继续之前,请确保您已经安装了这两个 NPM 依赖项。

    数学.cljs

    (ns com.example.math)

    (defn add [x y]
    (+ x y))

    1.设置构建工具

    math101 的根目录下运行 yarn shadow-cljs init .
    这将创建一个名为 shadow-cljs.edn 的文件。使用一些默认设置:

    ;; shadow-cljs configuration
    {:source-paths
    ["src/dev"
    "src/main"
    "src/test"]

    :dependencies
    []

    :builds
    {}}

    让我们做一些改变。

    首先,您不需要那么多源路径:

    {:source-paths
    ["src"]

    :dependencies
    []

    :builds
    {}}

    然后让我们添加一个构建配置:

    ;; shadow-cljs configuration
    {:source-paths
    ["src"]

    :dependencies
    []

    :builds
    {:math101 {:target :node-library
    :output-to "dist/index.js"
    :exports-var com.example.math/add}}}

    笔记:
  • :math101 - 这是我们稍后将使用的构建 ID
  • :target :node-library - 这告诉 shadow-cljs你打算创作一个图书馆
  • :output-to "dist/index.js" - 您打算发布的代码
  • :exports-var com.example.math/add - 您打算发布的函数的“完全限定名称”。这将产生一个默认导出。

  • 补充笔记:

    The :target :node-library emits code that can be used (via require) as a standard node library, and is useful for publishing your code for re-use as a compiled Javascript artifact.



    Source

    有一个 :npm-module目标可用但到目前为止 :node-library已为我检查了所有框。

    2. 让我们 build 它!

    运行 yarn shadow-cljs compile math101 .
    第一次运行时, shadow-cljs会下载一堆东西。最终它会完成,当它完成时......
    $ node
    > var add = require('./dist')
    > add(40, 2)
    42

    ✨✨✨

    需要导出多个函数?没问题。

    让我们添加 subtract :

    (ns com.example.math)

    (defn add [x y]
    (+ x y))

    (defn subtract [x y]
    (- x y))

    现在让我们更新我们的构建配置:

    ;; shadow-cljs configuration
    {:source-paths
    ["src"]

    :dependencies
    []

    :builds
    {:math101 {:target :node-library
    :output-to "dist/index.js"
    :exports {:add com.example.math/add
    :subtract com.example.math/subtract}}}}

    运行 yarn shadow-cljs compile math101再次,当它完成时:
    $ node
    > var math101 = require('./dist')
    > math101.add(40, 2)
    42
    > math101.subtract(44, 2)
    42

    ✨✨✨✨✨✨

    附录

    这只是为了让您入门。 shadow-cljs compile生成非生产代码(即它没有被缩小,死代码没有被删除 AFAIK 并且 Google Closure 还没有运行任何优化)。

    生成生产就绪代码的命令是 shadow-cljs release但你可能想在之前调整你的构建配置。

    我强烈建议您花时间阅读 shadow-cljs documentation .这是一个了不起的工具。

    关于clojurescript - 如何在 ClojureScript 中编写不可知的 JavaScript 库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52894430/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com