Re-exporting ES6 modules in TS 1.7?

北城以北 提交于 2019-11-29 03:47:55

You shouldn’t be using module.exports when you are working with ES modules; module.exports is a part of CommonJS modules, not a part of EcmaScript modules.

Rollup, exporting directly

Your correct rollup module will simply be:

export * from './test1';
export * from './test2';

Then to use the rollup:

import * as rollup from './combined';
// or `import { test1, test2 } from './combined'`;
// or any other valid import

rollup.test1();
rollup.test2();

Rollup, adding namespace objects

If you want to export test1 and test2 with extra namespacing then use export {} syntax:

import * as test1 from './test1';
import * as test2 from './test2';
export { test1, test2 };

Then usage becomes:

import * as rollup from './combined';
rollup.test1.test1();
rollup.test2.test2();

Rollup, using different export names

You can also redirect names using as if you have some name conflict, just like with import:

export { test1 as t1 } from './test1';
export { test2 as t2 } from './test2';

Then usage becomes:

import * as rollup from './combined';
rollup.t1();
rollup.t2();

It looks like you can not export everything in a module using *, not even if you use * as localModuleName.

Instead you have to name what your the combined module exports from the other modules.

// combined.ts
export {test1, test3} from './test1'; 
export {test2} from './test2';
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!