在TS 1.7中重新导出ES6模块? [英] Re-exporting ES6 modules in TS 1.7?

查看:264
本文介绍了在TS 1.7中重新导出ES6模块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在TS转口中感到有些失落。假设我创建了一对测试模块;



test1.ts;

  export function test1(){
return'test';
}

test2.ts;



导出函数test2(){pre> 返回'test';
}

我相信我应该可以这样做了;



combined.ts;

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

module.exports = {
test1:test1,
test2:test2
};

但是,没有这样的运气。似乎有很多GitHub问题讨论了各种方法,包括使用<。c $ c>导出导出*从'./test1'的老黑客,但他们似乎都在争论ES6规范真的意味着,没有一个实际工作。



这样做一个汇总是什么正确的方法?我只是沿着错误的路径将文件拆分成一个模块?命名空间更合适吗?

解决方案

您不应该使用 module.exports 当您使用ES模块时; module.exports 是CommonJS模块的一部分,不是EcmaScript模块的一部分。



汇总,直接导出



您正确的汇总模块将只是:

 export * from'./test2'; 

然后使用汇总:

  import *作为汇总从'./combined'; 
//或`import {test1,test2} from'。/ combined'`;
//或任何其他有效导入

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



汇总,添加命名空间对象



如果您要导出test1和test2,并使用额外的命名空间,然后使用 export {} 语法:

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

然后使用成为:

  import *作为汇总从'./combined'; 
rollup.test1.test1();
rollup.test2.test2();



汇总,使用不同的导出名称



您也可以使用将名称重定向为,如果您有某些名称冲突,就像使用 import

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

然后使用成为:

  import *作为汇总从'./combined'; 
rollup.t1();
rollup.t2();


I'm getting a bit lost in TS re-exports. Say I create a pair of test modules;

test1.ts;

export function test1() {
    return 'test';
}

test2.ts;

export function test2() {
    return 'test';
}

I believe I should be able to then do something like this;

combined.ts;

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

module.exports = {
    test1: test1,
    test2: test2
};

But, no such luck. There seem to be lots of GitHub issues discussing various approaches to this, including an old hack using export import * from './test1' but they all seem to argue what the ES6 spec really means, and none actually work..

What's the right way to do a rollup like this? Am I just going down the wrong path to split a module up across files? Are namespaces more appropriate here?

解决方案

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

这篇关于在TS 1.7中重新导出ES6模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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