[英]How actually ECMA-Script 2015 (and above) module loading through import statement works behind the scenes?
Since mostly we're using modules and import
statement transpiling them with tools like Babel , I'm intrigued about how native Web browser's implementations will load external files with the so-called import
statement. 由于大多数情况下,我们使用模块并通过Babel之类的工具
import
转译为import
语句,所以我对本机Web浏览器的实现将如何使用所谓的import
语句加载外部文件很感兴趣。
Will modules be loaded using XmlHttpRequest
/ XmlHttpRequest2
under the hoods? 可以在
XmlHttpRequest
使用XmlHttpRequest
/ XmlHttpRequest2
加载模块吗?
AFAIK, the whole standard defines a programmatic API as System
global variable where there're methods like System.define
to evaluate a JavaScript source code and register it as a named module. 整个标准AFAIK将一个程序化API定义为
System
全局变量,其中存在诸如System.define
方法来评估JavaScript源代码并将其注册为命名模块。
Does this mean that actual module system implementation won't cover external file module loading (meaning that a library/framework or your own vanilla JavaScript code should get the source code using an XmlHttpRequest
?) 这是否意味着实际的模块系统实现将无法涵盖外部文件模块的加载(意味着库/框架或您自己的原始JavaScript代码应使用
XmlHttpRequest
获取源代码?)
There is a loader standard being actively developed by the WHATWG that will handle the loading in the browser: http://whatwg.github.io/loader/ WHATWG正在积极开发一种加载程序标准 ,该标准将处理浏览器中的加载: http : //whatwg.github.io/loader/
As this is still a work in progress, things might still change. 由于这项工作仍在进行中,因此情况可能仍会改变。 As far as I can see, the exact way that the browser loads the files is not specified, but it is probable that it will use the
Fetch
API (the Promise-based replacement for XmlHttpRequest2). 据我所知,没有指定浏览器加载文件的确切方式,但是很可能它将使用
Fetch
API (XmlHttpRequest2的基于Promise的替代品)。
In the end, you should be able to use the module syntax with script tags and the browser (or whatever your JS environment is) will handle the loading: 最后,您应该能够将模块语法与脚本标签一起使用,并且浏览器(或您的JS环境是什么)将处理加载:
<script type="module">
import x from 'y';
import a from 'b';
...
</script>
or 要么
<script type="module" src="y.js"></script>
Currently, browsers are at different points of implementation: 当前,浏览器处于不同的实现点:
Please feel free to correct me or extend this answer. 请随时纠正我或扩展此答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.