简体   繁体   English

通过import语句加载的ECMA-Script 2015(及更高版本)模块实际上在后台如何工作?

[英]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: 当前,浏览器处于不同的实现点:

  • IE/Edge: Under Consideration IE / Edge:正在考虑中
  • Firefox: In progress Firefox:进行中
  • Chrome: In progress Chrome:正在进行中
  • Webkit: Meta Bug Webkit:元错误

Please feel free to correct me or extend this answer. 请随时纠正我或扩展此答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何告诉eslint允许分阶段的ECMA-Script动态导入 - How to tell eslint to allow staged ECMA-Script dynamic imports 使用ECMA-Script / JavaScript更改MS-Office / Outlook日期 - Change MS-Office / Outlook dates with ECMA-Script / JavaScript 回报如何在JS幕后运作 - How return works in JS behind the scenes angularjs,$ scope参数在后台如何工作? - angularjs, how $scope parameter works behind the scenes? nodejs域实际上如何在后台处理多个请求? - How nodejs domains actually work behind the scenes for multiple requests? 在不支持ECMA脚本的Javascript中如何写这条语句? - How can I write this statement in Javascript where ECMA Script is not supported? 在JavaScript中,forEach回调如何在后台运行? 有收益声明吗? - How does the forEach callback work behind the scenes in javascript? Is there a yield statement? Visual Studio 2015编辑器使用ECMA Script 6语法显示错误 - Visual studio 2015 editor display errors with ECMA Script 6 syntax 对在JavaScript ECMA2015中导入或创建的模块做出反应 - Reacting to a module being imported or created in JavaScript ECMA2015 ECMA 2015:如何在回调例程中将异步函数与“ yield”一起使用? - ECMA 2015: how to use async functions with “yield” in a callback-routine?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM