在我正在合作的一个项目中,我们有两个选择可以使用哪个模块系统:
使用require导入模块,使用using module导出模块。Exports和Exports .foo。 使用ES6 import导入模块,使用ES6 export导出模块
使用其中一种是否有性能上的好处?如果我们要使用ES6模块而不是Node模块,还有什么我们应该知道的吗?
在我正在合作的一个项目中,我们有两个选择可以使用哪个模块系统:
使用require导入模块,使用using module导出模块。Exports和Exports .foo。 使用ES6 import导入模块,使用ES6 export导出模块
使用其中一种是否有性能上的好处?如果我们要使用ES6模块而不是Node模块,还有什么我们应该知道的吗?
当前回答
我个人使用import,因为我们可以通过import导入所需的方法和成员。
import {foo, bar} from "dep";
文件名:dep.js
export foo function(){};
export const bar = 22
这要归功于单霁翔。更多信息。
其他回答
主要优势是句法上的:
更多的声明性/紧凑语法 ES6模块基本上会让UMD(通用模块定义)过时——基本上消除了CommonJS和AMD(服务器vs浏览器)之间的分裂。
使用ES6模块你不太可能看到任何性能上的好处。你仍然需要一个额外的库来捆绑模块,即使在浏览器中完全支持ES6特性。
到目前为止,ES6的导入和导出总是编译为CommonJS,所以使用其中一个没有好处。尽管推荐使用ES6,因为当浏览器发布本机支持时,它应该是有利的。原因是,你可以从一个文件中导入部分,而使用CommonJS,你必须需要整个文件。
ES6→导入,导出默认,导出
CommonJS→require, module。出口,exports.foo
下面是它们的常用用法。
ES6导出默认值
// hello.js
function hello() {
return 'hello'
}
export default hello
// app.js
import hello from './hello'
hello() // returns hello
ES6导出多个和导入多个
// hello.js
function hello1() {
return 'hello1'
}
function hello2() {
return 'hello2'
}
export { hello1, hello2 }
// app.js
import { hello1, hello2 } from './hello'
hello1() // returns hello1
hello2() // returns hello2
CommonJS module.exports
// hello.js
function hello() {
return 'hello'
}
module.exports = hello
// app.js
const hello = require('./hello')
hello() // returns hello
CommonJS模块。出口多
// hello.js
function hello1() {
return 'hello1'
}
function hello2() {
return 'hello2'
}
module.exports = {
hello1,
hello2
}
// app.js
const hello = require('./hello')
hello.hello1() // returns hello1
hello.hello2() // returns hello2
ES模块是静态的,这意味着导入是在每个模块的顶层描述的,并且在任何控制流语句之外。这是行不通的: If(条件){ 从'module1'导入module1 }
但是在commonjs中,它是允许的:
if (condition) {
module = require('module1')
}
ES modules run implicitly in strict mode. This means that we don't have to explicitly add the "use strict" statements at the beginning of every file. Strict mode cannot be disabled; therefore, we cannot use undeclared variables or the with statement or have other features that are only available in non-strict mode. strict mode is a safer execution mode. In ESM, some important CommonJS references are not defined. These include require , exports , module.exports , __filename, and __dirname. We can import CommonJS modules from ESM by using the standard import syntax. But only default exports work: import packageName from 'commonjs-package' // Works import { moduleName } from 'commonjs-package' // Errors
但是,从CommonJS模块中导入ES模块是不可能的。
ESM不能直接将JSON文件作为模块导入,这个特性在CommonJS中使用非常频繁。这就是为什么在reactjs中使用fetch api。 从'./data导入数据。json / /失败
您可能需要考虑以下几种用法/功能:
要求:
你可以动态加载加载的模块名不是 预定义的/静态的,或者你有条件地加载一个模块仅当 它是“真正需要的”(取决于特定的代码流)。 加载 同步的。这意味着如果你有多个需求,它们就是 一个接一个地装载和处理。
ES6进口:
你可以使用 命名导入可选择性地只加载您需要的部分。可以 节省内存。 导入可以是异步的(在当前的ES6 Module Loader中,它实际上是异步的),并且可以执行得更好一些。
同样,Require模块系统也不是基于标准的。现在ES6模块已经存在,它不太可能成为标准。在未来,在各种实现中会有对ES6模块的原生支持,这将在性能方面具有优势。
更新
自Node v12(2019年4月)以来,默认启用了对ES模块的支持,自Node v15(2020年10月)以来,它是稳定的(见这里)。包含节点模块的文件必须以.mjs或最近的包结尾。Json文件必须包含“type”:“module”。Node文档有更多的信息,还有关于CommonJS和ES模块之间的互操作。
在性能方面,新特性总是有可能没有现有特性优化得那么好。但是,由于模块文件只计算一次,所以性能方面可能会被忽略。最后,您必须运行基准测试以获得明确的答案。
ES模块可以通过import()函数动态加载。与require不同,this返回一个promise。
以前的回答
使用其中一种是否有性能上的好处?
请记住,目前还没有原生支持ES6模块的JavaScript引擎。你自己说过你在使用通天塔。无论如何,Babel默认将导入和导出声明转换为CommonJS (require/module.exports)。因此,即使你使用ES6模块语法,如果你在Node中运行代码,你将在底层使用CommonJS。
CommonJS和ES6模块之间有一些技术上的差异,例如CommonJS允许你动态加载模块。ES6不允许这样做,但有一个正在开发的API。
因为ES6模块是标准的一部分,所以我会使用它们。