“index.js”模块解析(指定包含文件夹)是否得到ES6(ECMAScript 2015)模块官方标准支持?
不是。ES2015没有涉及模块加载器或如何解释模块标识符的任何内容。
还是这只是“自定义”Node.js/CommonJS转换行为?
是的。您可以在文档中了解有关模块解析算法的相关部分:
require(X) from module at path Y
1. If X is a core module,
a. return the core module
b. STOP
2. If X begins with './' or '/' or '../'
a. LOAD_AS_FILE(Y + X)
b. LOAD_AS_DIRECTORY(Y + X)
3. LOAD_NODE_MODULES(X, dirname(Y))
4. THROW "not found"
[...]
LOAD_AS_DIRECTORY(X)
1. If X/package.json is a file,
a. Parse X/package.json, and look for "main" field.
b. let M = X + (json main field)
c. LOAD_AS_FILE(M)
2. If X/index.js is a file, load X/index.js as JavaScript text. STOP
3. If X/index.json is a file, parse X/index.json to a JavaScript object. STOP
4. If X/index.node is a file, load X/index.node as binary addon. STOP
当所有浏览器支持模块时,是否可以在导入中省略 /index | /index.js 部分?
希望浏览器实现能够最大程度地兼容现有的模块加载器,但目前我们还不知道。也许这与浏览器无关,而是与服务器如何解析模块标识符有关。我承认我最近没有跟进过这方面的发展,如果有其他见解,非常感谢:)