在我正在合作的一个项目中,我们有两个选择可以使用哪个模块系统:

使用require导入模块,使用using module导出模块。Exports和Exports .foo。 使用ES6 import导入模块,使用ES6 export导出模块

使用其中一种是否有性能上的好处?如果我们要使用ES6模块而不是Node模块,还有什么我们应该知道的吗?


当前回答

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模块基本上会让UMD(通用模块定义)过时——基本上消除了CommonJS和AMD(服务器vs浏览器)之间的分裂。

使用ES6模块你不太可能看到任何性能上的好处。你仍然需要一个额外的库来捆绑模块,即使在浏览器中完全支持ES6特性。

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模块的原生支持,这将在性能方面具有优势。

使用ES6模块可以用于“摇树”;例如,启用Webpack 2、Rollup(或其他捆绑程序)来识别未使用/导入的代码路径,因此不会将其放入最终的捆绑程序中。这可以通过消除你永远不需要的代码来显著减少它的文件大小,但是默认情况下与CommonJS绑定,因为Webpack等人无法知道是否需要它。

这是使用代码路径的静态分析来完成的。

例如,使用:

import { somePart } 'of/a/package';

... 给捆扎者一个包装的暗示。anotherPart不是必需的(如果它没有被导入,它就不能被使用——对吧?),所以它不会费心捆绑它。

为了在Webpack 2中启用这个功能,你需要确保你的编译器不会吐出CommonJS模块。如果你正在使用带有babel的es2015插件,你可以像这样在.babelrc中禁用它:

{
  "presets": [
    ["es2015", { modules: false }],
  ]
}

Rollup和其他工具的工作方式可能不同——如果您感兴趣,请查看文档。

不确定为什么(可能是优化-延迟加载?)它是这样工作的,但我注意到,如果导入的模块不使用,导入可能无法解析代码。 在某些情况下,这可能不是预期的行为。

以讨厌的Foo类为例。

foo.ts

export default class Foo {}
console.log('Foo loaded');

例如:

index.ts

import Foo from './foo'
// prints nothing

index.ts

const Foo = require('./foo').default;
// prints "Foo loaded"

index.ts

(async () => {
    const FooPack = await import('./foo');
    // prints "Foo loaded"
})();

另一方面:

index.ts

import Foo from './foo'
typeof Foo; // any use case
// prints "Foo loaded"