Nodejs:将index.js用于EXPORTS的好习惯?

Mar*_*tin 9 javascript node.js iojs

我看到了一些我继承的代码的模式.每个目录都有自己的JS文件,但也有一个index.js实际上从其他JS文件导出项目.

我认为这样做是为了让你可以准确地看到你要导出的内容,因为主要的导出是在index.js中,而主要代码是在其他js文件或文件中.

它是否正确?这种模式叫什么?

我应该继续使用这种模式吗?

Qwe*_*rty 13

ES6 CommonJS 模块语法

鉴于这两种常见的结构类型......

MyApp
? // files divided per type (controllers, components, actions, ...)
??? actions
? ??? index.js
? ??? signIn.js
? ??? signOut.js
??? components ...
??? reducers ...
??? pages ...
? 
? // files divided per component
??? components ...
? ??? index.js 
? ??? SimpleComponent.jsx
? ??? AnotherComponent.duck.jsx // redux "duck" pattern
? ??? ComplexComponent // large complex logic, own actions, stylesheet, etc.
? ...
??? pages ...
? ??? index.js 
? ??? App
? ? ??? index.js // not necessary here, matter of habit
? ? ??? App.jsx
? ? ??? actions.js
? ? ??? reducer.js
? ??? Dashboard
??? another.js
...
Run Code Online (Sandbox Code Playgroud)

您可以another.js像这样简单地导入文件

import {signIn, signOut} from './actions'
import {App} from './pages'
import {ComplexComponent} from './components'
Run Code Online (Sandbox Code Playgroud)

而不是这个(没有index.js文件)

import {signIn} from './actions/signIn'
import {signOut} from './actions/signOut'
import {App} from './pages/App/App' //notice the redundancy here
import {ComplexComponent} from './components/ComplexComponent/ComplexComponent'
Run Code Online (Sandbox Code Playgroud)

更多阅读

ECMAScript 6 模块
导入 - JavaScript | MDN
Babel 转译器 - 现在为您的浏览器带来新的导入

构建 React 项目
React Redux “Ducks 模式” - 组件的单一文件方法


Bra*_*rie 7

假设我有以下目录结构:

MyApp
??? app.js
??? test.js
??? package.json
??? controllers
? ??? index.js
? ??? signIn.js
? ??? signOut.js
??? views
  ??? index.js
  ??? signIn.js
  ??? signOut.js
Run Code Online (Sandbox Code Playgroud)

将以下代码放在index.js文件中...

// index.js
module.exports = {
  signIn: require('./signIn')
, signOut: require('./signOut')
};
Run Code Online (Sandbox Code Playgroud)

...允许你到require整个目录...

// test.js
describe('controllers', () => {
  // ~/controllers/index.js
  const controllers = require('./controllers');

  it('performs a sign-in', () => {
    ...
  });
  it('performs a sign-out', () => {
    ...
  });
});
Run Code Online (Sandbox Code Playgroud)

另一种方法是require单独使用每个文件.

有一个index.js目录中不是必需的.您可能require在目录中没有index.js完全相同的文件.

// app.js
const signOut = require('./controllers/signOut.js')
Run Code Online (Sandbox Code Playgroud)

但是,随着应用程序的增长,它会变得乏味.我使用一个包就像require-directory在一个目录中输入每个文件一样繁琐且容易出错.

// index.js
module.exports = require('require-directory')(module);

/*

This yields the same result as:

module.exports = {
  signIn: require('./signIn')
, signOut: require('./signOut')
, ...
};

*/
Run Code Online (Sandbox Code Playgroud)


mac*_*ost 7

其他答案提供了很多很好的信息,但要尝试具体回答您的问题“我是否应该继续使用这种模式”,我会说不,至少在大多数情况下。

问题是,这种模式需要额外的努力,因为您必须维护这些额外的index.js文件。根据我的经验,这种努力比简单地编写一个目录更长的import语句的努力更大。另外,index.js通过使用像require-dir.

话虽如此,如果您正在制作一个可供大量人使用的库,例如大型编程部门的关键模块,或公共 NPM 模块,那么 a 的努力index.js变得更加合理。只要你有足够多的人使用你的模块,你的用户将(累积地)从你添加模块中节省的时间比你维护它们所损失的时间多。