webpack - 使用 ScalaJSBundlerPlugin (webpack bundler) 时无法从 Javascript 访问 JSExport
问题描述
我正在尝试在此处找到的以下 scalajs 导出的 hello world 示例:
@JSExportTopLevel("HelloWorld")
object HelloWorld {
@JSExport
def sayHello(): Unit = {
println("Hello world!")
}
}
我的 html 看起来像这样:
<!DOCTYPE html>
<html>
<head>
<title>Example Scala.js application</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body style="margin: 0px">
<script type="text/javascript" src="./../../../target/scala-2.13/scala-js-tutorial-fastopt.js"></script>
<script>
HelloWorld.sayHello();
</script>
</body>
</html>
效果很好。
现在,如果我尝试使用 ScalaJSBundlerPlugin(它在后台使用 webpack),运行fastOptJS::webpack
并将路径更改为“./../../../target/scala-2.13/scalajs-bundler/main/scala-js- tutorial-fastopt-bundle.js”,我得到“HelloWorld 未定义”。
捆绑后如何访问 HelloWorld?
解决方案
默认情况下,scalajs-bundler 使用“应用程序”捆绑模式,可以丢弃顶级导出。正如食谱中解释的那样,使用时@JSExportTopLevel
,您需要使用“LibraryAndApplication”捆绑模式,使用
webpackBundlingMode := BundlingMode.LibraryAndApplication()
推荐阅读
- arrays - 仅打印数组的不同或重复元素并忽略其余元素 - vb
- ssis - 在虚拟机上的 Azure 上运行 SSIS 包处理表格多维数据集的问题
- ios - 使用 expo 为应用程序 Ios 生成的证书
- ionic-framework - 如何修复 Ionic 地理定位插件中的“WEBPACK_IMPORTED_MODULE_1_[“Cordova”]”
- python - Python中的正则表达式问题?
- javascript - chokidar 忽略除 xml 文件之外的所有内容
- c# - 在使用寄存器的程序中重复代码
- python - Python,重新组织字典数组
- r - 如何使用具有变换比例的无限极限?
- python - 如何修复我的 python 脚本,以便接收矩阵格式的输出?