如何使摘要模块重新导出ESM模块的所有子模块输出? [英] How to make summary module that re-exports all the exports of sub-modules for ESM modules?

查看:109
本文介绍了如何使摘要模块重新导出ESM模块的所有子模块输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从ESM模块中的多个文件重新导出导出,而又不分别列出每个导出?

How do you re-export the exports from multiple files in an ESM module without listing each individual export separately?

我有一个CommonJS模块目录,该目录由许多文件组成,我希望将这些文件转换为ESM导入/导出.目前,我有一个 index.js 文件,其中包含以下内容:

I have a CommonJS module directory that consists of a number of files that I would like to convert to ESM imports/exports. Currently, I have an index.js file that contains this:

// this just re-exports everything that the sub-modules export
module.exports = [
    './mapConcurrent.js',
    './deferred.js',
    './utils.js',
    './rateMap.js',
    './concurrency.js',
    './retry.js',
].reduce((obj, file) => {
    const m = require(file);
    Object.assign(obj, m);
    return obj;
}, {});

这将重新导出模块目录中所有文件的所有导出,因此该模块的客户端只需导入一个文件并获取所有文件的所有入口点,而不必知道哪个入口在哪个位置文件等.这对于CommonJS很好.

This re-exports all the exports of all the files in the module directory so that a client of this module can just import one file and get all the entry points for all the files without having to know which entry point is in which file and so on. This works fine for CommonJS.

您如何在ESM模块世界中完成类似的任务,而不必显式命名所有子文件中的每个导出?

How do you accomplish something similar in the ESM module world without having to explicitly name each export from all the sub-files?

推荐答案

您可以使用星级 export :

You can use a star export for each of them:

export * from './mapConcurrent.js';
export * from './deferred.js';
export * from './utils.js';
export * from './rateMap.js';
export * from './concurrency.js';
export * from './retry.js';

它将重新导出各个模块中的所有命名的导出,但不导出默认的导出(那些您需要重命名否则它们会发生冲突).

It will re-export all the named exports from the respective module, but not the default export (those you'd need to rename or they would collide).

因此,不必显式命名每个导出,但是必须显式声明所有子文件.

So no, you don't have to explicitly name each export, but you must explicitly declare all of the sub-files.

这篇关于如何使摘要模块重新导出ESM模块的所有子模块输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆