如果我使用ES6模块,是否还需要模块加载器? [英] Do I still need a module loader if I'm using ES6 modules?

查看:75
本文介绍了如果我使用ES6模块,是否还需要模块加载器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不幸的是,我对 JavaScript 模块加载器的了解仍在增长,我正在尝试了解它们与新的 ES6模块的关系。据我所知,使用 CommonJS RequireJS 等模块加载器使用 ES5 compliance JavaScript 确实需要使用异步模块加载器来提高性能,并仅在需要时使用相应的模块加载器语法加载。

Unfortunately my knowledge of JavaScript module loaders is still growing and I'm trying to understand their relationship to the new ES6 Modules. As far as I can tell using a module loader like CommonJS or RequireJS using ES5 compliant JavaScript really needed the use of an asynchronous module loader to increase performance and load only as needed using the respective module loader's syntax.

然而,查看 ES6 模块文档并阅读其他信息,在我看来,通过 import 和<$ c $本机支持模块加载c> export 关键字。如果是这种情况我是否更正 ES6 JS模块本身支持异步模块加载,因此我不需要使用其他工具,如 CommonJS RequireJS

However looking at the ES6 module documentation and reading other information, it appears to me that module loading is natively supported via the import and export keywords. If this is the case am I correct that ES6 JS modules natively support asynchronous module loading and therefore I do not need to use an additional tool like CommonJS or RequireJS?

推荐答案


在我看来,通过导入和导出关键字本身支持模块加载。

it appears to me that module loading is natively supported via the import and export keywords.

不完全。 import export 声明仅定义每个模块的依赖关系和接口。它们允许静态提取命名所需模块的字符串,没有别的。

Not exactly. The import and export declarations only define the dependencies and the interface of each module. They allow to statically extract strings that name the required modules, nothing else.


如果是这种情况,我是否需要使用CommonJS或RequireJS等附加工具?

If this is the case, do I not need to use an additional tool like CommonJS or RequireJS?

否。您仍然需要为ES6模块使用加载程序,它将名称或路径或任何内容从 import 解析为实际模块文件,并使用依赖于实现的方法加载它们。

No. You still need to use a loader for ES6 modules, which resolves the names or paths or whatever from the imports to actual module files and loads them with an implementation-dependent method.

有很多可用的工具或工具链,不同解决方案的示例是

There are many tools or toolchains available, examples for the different solutions are


  • webpack:将所有内容捆绑成一个大脚本

  • System.js:动态和异步加载单个模块(类似于require.js所做的)

  • native:node.js和web浏览器仍在计算如何在没有附加库的情况下支持模块加载

  • babel transpilation:你可以将ES6模块转换为AMD或CommonJS格式,并使用像这些

  • webpack: bundles everything into one large script
  • System.js: loads single modules dynamically and asynchronously (similar to what require.js does)
  • native: node.js and web browsers are still figuring out how to support module loading without additional libraries
  • babel transpilation: you can convert ES6 modules to AMD or CommonJS format and use the known tools like require.js for these

这篇关于如果我使用ES6模块,是否还需要模块加载器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆