(env
 (dev     (flags :standard -rectypes))
 (release (flags :standard -rectypes -w +27+32)))

(rule
 (targets (dir coq-pkgs))
 (mode (promote (until-clean)))
 (deps
  (package coq-core)
  (package coq-stdlib)
  Makefile)
 (action
  (progn
   (run
     node
     %{dep:dist-cli}/cli.cjs
     %{dep:etc/pkg-metadata/coq-pkgs.json}
     --rootdir %{env:COQBUILDDIR_REL=????}
     --nostdlib)
   ; building symbols using Make because Dune has no pattern rules T.T
   (run make libs-symb))))

(rule
 (targets (dir node_modules))
 (mode (promote (until-clean)))
 (deps
   package.json
   package-lock.json)
 (action
  (run npm install --no-progress --no-save)))

(alias
 (name jscoq)
 (deps
  (alias shared)
  backend/jsoo/jscoq_worker.bc.js))

(alias
 (name wacoq)
 (deps
  (alias shared)
  backend/wasm/wacoq_worker.bc
  (glob_files backend/wasm/*.wasm)))

(alias
 (name shared)
 (deps
  (source_tree examples)
  (source_tree docs)  ; for `quick-help.html`
  dist
  jscoq.js
  index.html
  coq-pkgs))

; JavaScript build (esbuild)
(rule
 (targets (dir dist))
 (mode (promote (until-clean)))
 (deps
  (source_tree backend)
  (source_tree frontend)
  tsconfig.json
  esbuild.mjs
  node_modules)
 (action
  ; Set JSCOQ_BUNDLE_TARGET=release to generate minimized dist files
  (run npm run %{env:JSCOQ_BUNDLE_TARGET=esbuild})))

(rule
 (targets (dir dist-cli))
 (mode (promote (until-clean)))
 (deps
  backend/jsoo/jscoq_worker.bc.js
  etc/pkg-metadata/coq-pkgs.json
  (source_tree backend)
  (source_tree frontend)
  tsconfig.json
  esbuild-cli.mjs
  node_modules)
 (action
  (run npm run esbuild-cli)))

; Webpack build (legacy)
(rule
 (targets (dir dist-webpack))
 (mode (promote (until-clean)))
 (deps
  (source_tree backend)
  (source_tree frontend)
  tsconfig.json
  webpack.config.js
  node_modules)
 (action
  (run npm run webpack)))

; Just the worker
(alias
  (name jscoq_worker)
  (deps
    backend/jsoo/jscoq_worker.bc.js))

(alias
  (name wacoq_worker)
  (deps
    backend/wasm/wacoq_worker.bc))

(dirs (:standard _vendor+*))
(vendored_dirs vendor)