You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Dune将index.html复制到编译后JS文件所在的_build目录?

解决Dune将index.html复制到_build目录的问题

项目目录结构

.
├── bin
│   ├── counter.ml
│   ├── dune
│   └── index.html
├── dune-project
└── fmlib_tutorial.opam

当前bin/dune内容

(executable
  (name counter)
  (modules counter)
  (modes js)                    ; 启用JavaScript编译
  (libraries fmlib_browser)     ; 使用该库
)

(rule
    (targets counter.js)         ; 生成文件'counter.js'
    (deps    counter.bc.js)
    (mode   (promote (until-clean)))
    (action (copy counter.bc.js counter.js))
)

解决方案

在bin/dune文件中添加一条复制index.html的规则,修改后的完整内容如下:

(executable
  (name counter)
  (modules counter)
  (modes js)                    ; 启用JavaScript编译
  (libraries fmlib_browser)     ; 使用该库
)

(rule
    (targets counter.js)         ; 生成文件'counter.js'
    (deps    counter.bc.js)
    (mode   (promote (until-clean)))
    (action (copy counter.bc.js counter.js))
)

(rule
    (targets index.html)
    (deps    ./index.html)
    (action  (copy %{deps} %{targets}))
)

规则说明

  • (targets index.html):指定目标文件为index.html,Dune会将其生成在当前目录对应的_build路径下(即_build/default/bin/,与编译后的JS文件同目录)
  • (deps ./index.html):声明依赖为当前源码目录下的index.html文件
  • (action (copy %{deps} %{targets})):执行复制操作,将依赖的源码文件复制到目标位置

执行dune build后,即可在_build/default/bin/目录下看到index.html、counter.bc.js和counter.js三个文件。

内容的提问来源于stack exchange,提问作者ixb

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.12 14:24:56