Skip to content

Instantly share code, notes, and snippets.

@loganpowell
Created September 21, 2022 17:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save loganpowell/579edc529f2e6b3cdcb1f796bdab7dd1 to your computer and use it in GitHub Desktop.
Save loganpowell/579edc529f2e6b3cdcb1f796bdab7dd1 to your computer and use it in GitHub Desktop.
shadow-cljs `:esm` target with ESM dependency for node

Using the REPL build-specific

I'm using Calva with VSCode. My steps

  1. watch the build
D:\projects\census\census-geojson (master -> origin) (census-geojson@2.0.0)
λ shadow-cljs watch lib
  1. fire runtime in separate terminal
D:\projects\census\census-geojson (master -> origin) (census-geojson@2.0.0)
λ shadow-cljs cljs-repl lib
  1. "Jack in" to Calva on VSCode
clj꞉shadow.user꞉> 
; Creating cljs repl session...
; Connecting cljs repl: shadow-cljs...
;   The Calva Connection Log might have more connection progress information.
; Connected session: cljs, repl: node-repl
; Evaluating file: tests.cljs
; SHADOW import error D:\projects\census\census-geojson\.shadow-cljs\builds\node-repl\dev\out\cljs-runtime\shadow.js.shim.module$node_fetch.js
; 
; Execution error (Error) at (<cljs repl>:1).
+ require() of ES Module D:\projects\census\census-geojson\node_modules\node-fetch\src\index.js from D:\projects\census\census-geojson\[stdin] not supported.
+ Instead change the require of index.js in D:\projects\census\census-geojson\[stdin] to a dynamic import() which is available in all CommonJS modules.
:repl/exception!
cljs꞉test.utils.tests꞉> 
; nREPL Connection was closed
{
"name": "census-geojson",
"version": "2.0.0",
"description": "Convenience functions for working with Census APIs: Statistics, Cartographic GeoJSON, lat/lng -> FIPS, and other niceties.",
"scripts": {
"build": "shadow-cljs release lib"
},
"type": "module",
"repository": {
"type": "git",
"url": "git+https://github.com/loganpowell/census-geojson.git"
},
"keywords": [
"geojson",
"census",
"shapefiles",
"boundaries",
"government",
"usa",
"cartography"
],
"author": "Logan Powell",
"license": "MIT",
"bugs": {
"url": "https://github.com/loganpowell/census-geojson/issues"
},
"homepage": "https://github.com/loganpowell/census-geojson#readme",
"dependencies": {
"node-fetch": "^3.2.10",
"xmlhttprequest": "^1.8.0",
"xregexp": "^5.1.1"
}
}
{:source-paths ["src"]
:dependencies [[cljs-ajax "0.8.4"]
[funcool/cuerdas "2022.06.16-403"]
[clojusc/defun "0.4.0"]
[lambdaisland/fetch "1.1.60"]
[frankiesardo/linked "1.3.0"]
[net.cgrand/xforms "0.19.2"]]
:nrepl {:port 3333}
:builds {:lib {:target :esm
:output-dir "public/census"
:modules {:census {:exports {default census.core/census}}}
:js-options {:keep-as-import #{"node-fetch"}}
:compiler-options {:optimizations :simple}}}}
(ns census.utils.core
(:require
[ajax.core :refer [GET]]
[lambdaisland.fetch :as fetch]
["node-fetch" :as node-fetch]
[clojure.walk :refer [keywordize-keys]]))
;; trying to patch fetch when user in Node environment
(def isNode #? (:cljs (and (exists? js/process)
(exists? js/process.versions)
(exists? js/process.versions.node))
:clj false))
(when isNode (set! js/fetch node-fetch))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment