使用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/*';

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

这可能吗?


当前回答

目前的答案提出了一个解决方案,但这是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/*';

工作原理与上述相同。

其他回答

这并不完全是你所要求的,但是,有了这个方法,我可以在我的其他文件中迭代componentsList,并使用componentsList.map(…)等函数,我发现非常有用!

import StepOne from './StepOne';
import StepTwo from './StepTwo';
import StepThree from './StepThree';
import StepFour from './StepFour';
import StepFive from './StepFive';
import StepSix from './StepSix';
import StepSeven from './StepSeven';
import StepEight from './StepEight';

const componentsList= () => [
  { component: StepOne(), key: 'step1' },
  { component: StepTwo(), key: 'step2' },
  { component: StepThree(), key: 'step3' },
  { component: StepFour(), key: 'step4' },
  { component: StepFive(), key: 'step5' },
  { component: StepSix(), key: 'step6' },
  { component: StepSeven(), key: 'step7' },
  { component: StepEight(), key: 'step8' }
];

export default componentsList;

这只是@Bergi回答的另一种方法

// lib/things/index.js
import ThingA from './ThingA';
import ThingB from './ThingB';
import ThingC from './ThingC';

export default {
 ThingA,
 ThingB,
 ThingC
}

Uses

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

你现在可以使用async import():

import fs = require('fs');

然后:

fs.readdir('./someDir', (err, files) => {
 files.forEach(file => {
  const module = import('./' + file).then(m =>
    m.callSomeMethod();
  );
  // or const module = await import('file')
  });
});

目前的答案提出了一个解决方案,但这是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/*';

工作原理与上述相同。

我用过它们几次(特别是在构建大型对象时,将数据拆分到多个文件中(例如AST节点)),为了构建它们,我做了一个小脚本(我刚刚添加到npm中,以便其他人都可以使用它)。

使用方法(目前你需要使用babel来使用导出文件):

$ npm install -g folder-module
$ folder-module my-cool-module/

生成包含以下内容的文件:

export {default as foo} from "./module/foo.js"
export {default as default} from "./module/default.js"
export {default as bar} from "./module/bar.js"
...etc

然后你就可以使用这个文件了:

import * as myCoolModule from "my-cool-module.js"
myCoolModule.foo()