使用ES6,我可以像这样从文件中导入几个导出:

import {ThingA, ThingB, ThingC} from 'lib/things';

但是,我喜欢每个文件有一个模块的组织方式。我最终得到了这样的导入:

import ThingA from 'lib/things/ThingA';
import ThingB from 'lib/things/ThingB';
import ThingC from 'lib/things/ThingC';

我希望能够做到这一点:

import {ThingA, ThingB, ThingC} from 'lib/things/*';

或者类似的东西,按照大家理解的约定,每个文件包含一个默认导出,每个模块与其文件同名。

这可能吗?


当前回答

我可以借鉴用户atilkan的方法并稍加修改:

对于Typescript用户;

require.context('@/folder/with/modules', false, /\.ts$/).keys().forEach((fileName => {
    import('@/folder/with/modules' + fileName).then((mod) => {
            (window as any)[fileName] = mod[fileName];
            const module = new (window as any)[fileName]();

            // use module
});

}));

其他回答

只是对答案中已经提供的主题的一个变化,但是这样如何:

在一件事中,

export default function ThingA () {}

在事情/ index.js,

export {default as ThingA} from './ThingA'
export {default as ThingB} from './ThingB'
export {default as ThingC} from './ThingC'

然后去其他地方消费,

import * as things from './things'
things.ThingA()

或者只消耗一些东西,

import {ThingA,ThingB} from './things'

类似于公认的答案,但它允许你在每次创建索引文件时不需要添加一个新模块的情况下进行扩展:

/模块/ moduleA.js

export const example = 'example';
export const anotherExample = 'anotherExample';

/模块/ index.js

// require all modules on the path and with the pattern defined
const req = require.context('./', true, /.js$/);

const modules = req.keys().map(req);

// export all modules
module.exports = modules;

。/ example.js

import { example, anotherExample } from './modules'

目前的答案提出了一个解决方案,但这是bug我为什么不存在,所以我创建了一个通天塔插件,它做到了这一点。

使用以下方法安装:

npm i --save-dev babel-plugin-wildcard

然后将它添加到你的.babelrc:

{
    "plugins": ["wildcard"]
}

有关详细的安装信息,请参阅回购


这允许你这样做:

import * as Things from './lib/things';

// Do whatever you want with these :D
Things.ThingA;
Things.ThingB;
Things.ThingC;

同样,repo包含了关于它到底做什么的进一步信息,但是这样做避免了创建index.js文件,并且也发生在编译时,以避免在运行时进行readdirs。

同样,在一个更新的版本中,你可以完全像你的例子一样:

 import { ThingsA, ThingsB, ThingsC } from './lib/things/*';

工作原理与上述相同。

你也可以使用require:

const moduleHolder = []

function loadModules(path) {
  let stat = fs.lstatSync(path)
  if (stat.isDirectory()) {
    // we have a directory: do a tree walk
    const files = fs.readdirSync(path)
    let f,
      l = files.length
    for (var i = 0; i < l; i++) {
      f = pathModule.join(path, files[i])
      loadModules(f)
    }
  } else {
    // we have a file: load it
    var controller = require(path)
    moduleHolder.push(controller)
  }
}

然后使用动态加载控制器的moduleHolder:

  loadModules(DIR) 
  for (const controller of moduleHolder) {
    controller(app, db)
  }

太棒了!这比需要的要难。

导出一个平面默认值

这是一个很好的机会来使用传播(…在{……很重要,…联系方式如下:

// imports/collections/Matters.js
export default {           // default export
  hello: 'World',
  something: 'important',
};
// imports/collections/Contacts.js
export default {           // default export
  hello: 'Moon',
  email: 'hello@example.com',
};
// imports/collections/index.js
import Matters from './Matters';      // import default export as var 'Matters'
import Contacts from './Contacts';

export default {  // default export
  ...Matters,     // spread Matters, overwriting previous properties
  ...Contacts,    // spread Contacts, overwriting previosu properties
};

// imports/test.js
import collections from './collections';  // import default export as 'collections'

console.log(collections);

然后,从命令行(从项目根/)运行babel编译的代码:

$ npm install --save-dev @babel/core @babel/cli @babel/preset-env @babel/node 
(trimmed)

$ npx babel-node --presets @babel/preset-env imports/test.js 
{ hello: 'Moon',
  something: 'important',
  email: 'hello@example.com' }

导出一个树形默认值

如果您不希望覆盖属性,请更改:

// imports/collections/index.js
import Matters from './Matters';     // import default as 'Matters'
import Contacts from './Contacts';

export default {   // export default
  Matters,
  Contacts,
};

输出将是:

$ npx babel-node --presets @babel/preset-env imports/test.js
{ Matters: { hello: 'World', something: 'important' },
  Contacts: { hello: 'Moon', email: 'hello@example.com' } }

导出多个命名的导出w/ no默认

如果你专注于DRY,导入的语法也会改变:

// imports/collections/index.js

// export default as named export 'Matters'
export { default as Matters } from './Matters';  
export { default as Contacts } from './Contacts'; 

这将创建2个带有/没有默认导出的命名导出。然后改变:

// imports/test.js
import { Matters, Contacts } from './collections';

console.log(Matters, Contacts);

输出:

$ npx babel-node --presets @babel/preset-env imports/test.js
{ hello: 'World', something: 'important' } { hello: 'Moon', email: 'hello@example.com' }

导入所有指定的导出项

// imports/collections/index.js

// export default as named export 'Matters'
export { default as Matters } from './Matters';
export { default as Contacts } from './Contacts';
// imports/test.js

// Import all named exports as 'collections'
import * as collections from './collections';

console.log(collections);  // interesting output
console.log(collections.Matters, collections.Contacts);

注意'./collections'中的解构导入{Matters, Contacts};在前面的例子中。

$ npx babel-node --presets @babel/preset-env imports/test.js
{ Matters: [Getter], Contacts: [Getter] }
{ hello: 'World', something: 'important' } { hello: 'Moon', email: 'hello@example.com' }

在实践中

给定这些源文件:

/myLib/thingA.js
/myLib/thingB.js
/myLib/thingC.js

创建一个/myLib/index.js来捆绑所有的文件,这违背了导入/导出的目的。首先让所有内容都全局化要比通过index.js“包装文件”的导入/导出来使所有内容都全局化容易得多。

如果你想要一个特定的文件,从'./myLib/thingA'导入thingA;在你们自己的项目中。

只有当你在为npm或一个多年的多团队项目打包时,为模块创建带有导出的“包装器文件”才有意义。

走了这么远?更多细节请参阅文档。

此外,Stackoverflow终于支持三个作为代码围栏标记。