如何运行优化的ClojureScript输出?

问题描述 投票:2回答:1

我有一个ClojureScript项目,我在“dev”模式下编译,没有编译器优化。我会像在quick start docs中描述的HTML那样“运行”它。

<script src="/js/out/goog/base.js"></script>
<script src="/js/out/main.js"></script>
<script> goog.require("myapp.mymodule"); </script>

main.js文件是由ClojureScript编译器生成的文件,来自.cljs源文件。

现在我在Leiningen项目中有第二个配置文件,可以实现高级编译器优化。正如所料,main.js更改为包含大量混淆的Javascript。但是现在上面的require线失败了:

Error: goog.require could not find: myapp.mymodule

我还尝试用:export标记我的一个函数:

(defn ^:export foo [] ...)

但是docs没有提到那个符号的位置,我在window或其他任何地方都没有看到它。

那么,HTML如何调用已编译的ClojureScript来启动程序?

以下是我的project.clj的相关部分

:cljsbuild {
  :builds {
    :app {
      :source-paths ["src/main/clj"]
      :compiler {
        :output-to "target/resources/public/js/out/main.js"
        :output-dir "target/resources/public/js/out"
        :asset-path "/js/out"
      }
    }
  }
}
...
:profiles {
  :uberjar {
    :hooks [leiningen.cljsbuild]       
    :omit-source true        
    :cljsbuild {
      :builds {
        :app {             
         :compiler {:optimizations :advanced
                    :pretty-print false}
        }
      }
    }
  }      

并使用我运行的优化进行编译:

lein with-profile uberjar cljsbuild once
clojurescript
1个回答
2
投票

您需要添加一些编译器选项,如:output-to:source-path。您可以在https://clojurescript.org/reference/compiler-options找到选项列表。

如果您想查看最小生产构建的外观,请使用lein new cljsbuild-template <project-name>创建一个新的空项目

© www.soinside.com 2019 - 2024. All rights reserved.