繁体   English   中英

是否可以使用通配符从目录中的所有文件导入模块?

[英]Is it possible to import modules from all files in a directory, using a wildcard?

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

或类似的东西,根据理解的约定,每个文件包含一个默认导出,并且每个模块的名称与其文件相同。

这可能吗?

我不认为这是可能的,但是 afaik 模块名称的解析取决于模块加载器,因此可能有一个加载器实现支持这一点。

在那之前,你可以在lib/things/index.js中使用一个中间的“模块文件”, lib/things/index.js包含

export * from 'ThingA';
export * from 'ThingB';
export * from 'ThingC';

它可以让你做

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

只是答案中已经提供的主题的一个变体,但是这个怎么样:

在一Thing

export default function ThingA () {}

things/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'

当前的答案提出了一种解决方法,但它让我烦恼为什么它不存在,所以我创建了一个babel插件来做到这一点。

使用以下方法安装它:

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

然后将其添加到您的.babelrc中:

{
    "plugins": ["wildcard"]
}

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


这允许您执行以下操作:

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

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

同样, repo包含有关它究竟做什么的更多信息,但这样做可以避免创建index.js文件,并且也会在编译时发生,以避免在运行时执行readdir

同样使用较新的版本,您可以完全按照您的示例进行操作:

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

与上述相同。

伟大的咕噜咕噜! 这比它需要的更难。

导出一个平面默认值

这是使用传播... in { ...Matters, ...Contacts }下面的绝佳机会:

// 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' } }

导出多个命名导出,无默认值

如果您专注于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);

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

$ 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“包装文件”导入/导出使所有内容全局化要容易得多。

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

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

走到这一步了吗? 有关更多详细信息,请参阅文档

另外,是的,Stackoverflow 终于支持三个`s 作为代码围栏标记。

您现在可以使用异步导入():

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')
  });
});

类似于已接受的问题,但它允许您在每次创建一个新模块时无需向索引文件添加新模块的情况下进行扩展:

./modules/moduleA.js

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

./modules/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'

我已经使用过它们几次(特别是用于构建将数据拆分到多个文件(例如 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()

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

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

export default {
 ThingA,
 ThingB,
 ThingC
}

用途

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

如果你正在使用 webpack。 这会自动导入文件并导出为api命名空间。

因此无需在每次添加文件时更新。

import camelCase from "lodash-es";
const requireModule = require.context("./", false, /\.js$/); // 
const api = {};

requireModule.keys().forEach(fileName => {
  if (fileName === "./index.js") return;
  const moduleName = camelCase(fileName.replace(/(\.\/|\.js)/g, ""));
  api[moduleName] = {
    ...requireModule(fileName).default
  };
});

export default api;

对于打字稿用户;

import { camelCase } from "lodash-es"
const requireModule = require.context("./folderName", false, /\.ts$/)

interface LooseObject {
  [key: string]: any
}

const api: LooseObject = {}

requireModule.keys().forEach(fileName => {
  if (fileName === "./index.ts") return
  const moduleName = camelCase(fileName.replace(/(\.\/|\.ts)/g, ""))
  api[moduleName] = {
    ...requireModule(fileName).default,
  }
})

export default api

您也可以使用 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)
  }

我能够从用户 atilkan 的方法中获取并对其进行一些修改:

对于打字稿用户;

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
});

}));

节点? 这样做:

使用 index.js 创建一个文件夹,在 index 文件中,添加以下内容:

var GET = require('./GET');
var IS = require('./IS');
var PARSE = require('./PARSE');
module.exports = { ...GET, ...IS, ...PARSE};

并且,在文件 GET.js 或 IS.js 中正常导出:

module.exports = { /* something as you like */}

并且现在,您只需要包含 index.js ,例如:

const Helper = require('./YourFolder');

Helper将包含 YourFolder 中的所有功能。

再会!

这不完全是您所要求的,但是,通过这种方法,我可以在其他文件中遍历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;

如果您不在 A、B、C 中导出默认值而只是导出 {},则可以这样做

// things/A.js
export function A() {}

// things/B.js
export function B() {}

// things/C.js
export function C() {}

// foo.js
import * as Foo from ./thing
Foo.A()
Foo.B()
Foo.C()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM