javascript - Apache Cordova 混合应用程序中的 ES6 模块/导入抛出 MIME 类型错误

标签 javascript cordova google-chrome hybrid-mobile-app es6-modules

我正在尝试在使用 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/

相关文章:

Javascript 递归

javascript - new Function(...) 在 Debug模式下在 Chrome 58 中返回 undefined

google-chrome - Chrome 58 不安全 URL 中的数据

javascript - 为图片生成框架

javascript - jQuery 每个函数用于多个轮播下一个、上一个标识符问题

javascript - 获取表格内的输入标签

ios - 在 Cordova 中将照片保存到 iOs 相册

javascript - 页面重定向在 Angular JS 中不起作用

cordova - 在 Cordova config.xml 文件中创建注释

javascript - 即使只有一个固定元素,chrome 应用程序滚动也不起作用