ES6 (EcmaScript 2015) modules: import index.js

社会主义新天地 提交于 2019-12-04 00:03:52

Is the "index.js" module resolution (specifying the containing folder) supported by the ES6 (EcmaScript 2015) modules official standard?

No. ES2015 doesn't contain anything about the module loader or how to interpret module identifiers.


Or is it just "custom" NodeJS/CommonJS transpiling behaviour?

Yes. You can read about the module resolution algorithm in the documentation. The relevant part:

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

Will it be possible to omit the /index|/index.js part of the import in all browsers as well (when modules will be supported on all browsers)?

Hopefully browser implementation will aim for maximum compatibility with existing module loaders, but for now we don't know. Maybe it doesn't have anything to do with the browser either, but with how to server resolves module identifiers. I confess that I haven't followed the development lately, so any other insights are much appreciated :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!