[英]javascript import from '/folder' with index.js
I've noticed a few cases where I've seen something like the following:我注意到一些案例,其中我看到了以下内容:
// /reducers/reducer1.js
export default function reducer1(state = {}, action){
// etc...
}
// /reducers/reducer2.js
export default function reducer2(state = {}, action){
// etc...
}
// /reducers/index.js
import { combineReducers } from 'redux';
import reducer1 from './reducer1';
import reducer2 from './reducer2';
export default combineReducers({
reducer1,
reducer2
})
// /store.js
import masterReducer from './reducers';
export default function makeStore(){
// etc...
}
Notice the last "file" where we call import masterReducer from './reducers'
- A few people seem to believe this should import the default export
from the index.js file.注意最后一个“文件”,我们在其中调用
import masterReducer from './reducers'
- 有些人似乎认为这应该从 index.js 文件导入default export
。
Is this actually part of the specification?这实际上是规范的一部分吗? - my interpretation/question is that this is the result of many folks using WebPack v1 which translates
import
statements into CommonJS-style requires
statements? - 我的解释/问题是,这是许多人使用 WebPack v1 将
import
语句转换为 CommonJS 样式的requires
语句的结果? Or will this break in WebPack v2 with "official" import
/ export
support?或者这会在具有“官方”
import
/ export
支持的 WebPack v2 中中断吗?
Is this actually part of the specification?
这实际上是规范的一部分吗?
No. How module identifiers ( './reducers'
in your case) are resolved to the actual modules is left to the implementation of the module loader/bundler, it's not specificed by ES6.不。模块标识符(在您的情况下为
'./reducers'
)如何解析为实际模块由模块加载器/捆绑器的实现决定,ES6 没有具体说明。 And it doesn't seem to be specified in CommonJs either.而且它似乎也没有在CommonJs中指定。
This is just how node does it - when requiring a directory, it's index.js
file will be used.这就是 node 的工作方式 - 当需要一个目录时,将使用它的
index.js
文件。 Bundlers like browserify or webpack followed this convention (for compat reasons).像捆扎机browserify或的WebPack遵循了这一约定(compat的原因)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.