我正在尝试在使用 Apache Cordova 构建的混合移动应用程序中使用 ES6 模块。不幸的是,Cordova 似乎在提供没有 MIME 类型的模块,这会在 WebView 中引发错误(在 Chrome 63 和 64 beta 中)。
具体来说,部署的应用程序(使用 chrome remote debugger )抛出以下错误:
Failed to load module script: The server responded with a non-JavaScript MIME type of "". Strict MIME type checking is enforced for module scripts per HTML spec.
我使用的是标准 ES6 导入,目标版本的 chrome 现在支持它。
<!--index.html-->
<script type="module">
import App from "./app.js"
import config from "./config.js"
window.app = new App(config)
</script>
当预构建 Assets 通过网络服务器提供服务而不是通过 Cordova 部署时,一切正常。通过 Babel 进行转译可能是一种选择,但这似乎让人非常头疼,在我知道没有更好的选择之前,我宁愿不走那条路。
有什么建议吗?
最佳答案
我最终使用 webpack 解决了这个问题将我所有的代码和 Assets 编译到一个文件中。这允许使用导入语法,而不会在 Apache Cordova 中出现所有相关问题。它不如 native 支持,但对我来说是最好的解决方案。
关于javascript - Apache Cordova 混合应用程序中的 ES6 模块/导入抛出 MIME 类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48113359/