Suppose I have two imports containing numerous named exports, too many to list individually, and I need to reexport them while excluding a few due to naming conflicts.
For example, consider the following two files:
foo.js
:
export const a = 1;
export const b = 2;
bar.js
:
export const b = 1;
export const c = 3;
If I want to combine and reexport all of them using CommonJS, I can achieve this with the following approach:
/* use rest destructuring to store everything except "b" in variable "foo" */
const { b, ...foo } = require("./foo");
const bar = require("./bar");
module.exports = {
...foo,
...bar
};
Although in my TypeScript project, the closest solution I found was using export = { ... }
in a similar manner:
import * as foo from "./foo";
import * as bar from "./bar";
const { b, ...foo2 } = foo;
export = {
...foo2,
...bar,
};
However, I believe this would output a file with CommonJS module.exports
instead of a proper ESM module (likely requiring esModuleInterop
). Furthermore, it's specific to TypeScript and not applicable when working with pure JavaScript.
Is there a way to achieve this using ESM import
/ export
syntax instead?