简体   繁体   English

es6模块与节点和声标志下的打字稿的commonjs

[英]es6 modules to commonjs with typescript under node harmony flag

I'm using TypeScript (1.6) with node under the --harmony flag, so I'd like to transpile the es6 module syntax to commonjs. 我正在使用带有--harmony标志下的节点的TypeScript(1.6),所以我想将es6模块语法转换为commonjs。

From what I can tell, I can't do this with TypeScript 1.6. 据我所知,我不能用TypeScript 1.6做到这一点。 If I set my target to es6, and module to commonjs, I get a TypeScript error - 如果我将目标设置为es6,将模块设置为commonjs,则会出现TypeScript错误 -

Cannot compile modules into 'commonjs', 'amd', 'system' or 'umd' when targeting 'ES6' or higher. 在定位“ES6”或更高版本时,无法将模块编译为“commonjs”,“amd”,“system”或“umd”。

Why won't TypeScript compile to commonjs with an ES6 target? 为什么TypeScript不会编译为带有ES6目标的commonjs? I imagine a lot people want to do this since node doesn't support ES6 modules yet. 我想很多人都想这样做,因为节点还不支持ES6模块。

I'd thought the new moduleResolution compiler option might solve this issue, but it doesn't appear to do anything. 我认为新的moduleResolution编译器选项可能会解决这个问题,但它似乎没有做任何事情。

Currently, I'm having to use babel just to transpile the module syntax to commonjs, but I'd like to remove babel from my builds so I can take advantage of source maps. 目前,我不得不使用babel将模块语法转换为commonjs,但我想从我的构建中删除babel,以便我可以利用源映射。

Is there a way I can achieve this? 有没有办法实现这个目标? NOTE: I do not want to transpile to ES5. 注意:我不想转发到ES5。 I want my JS running as ES6 under the harmony flag. 我希望我的JS在和声标志下作为ES6运行。 Thanks! 谢谢!

The TypeScript team will add support for what you are looking for in the next release . TypeScript团队将在下一版本中添加对您要查找的内容的支持。 You can wait for a few weeks/months. 你可以等几个星期/几个月。 Alternatively, you can use a Polyfill for the ES6 module loader: 或者,您可以将Polyfill用于ES6模块加载器:

There are more libraries like the ones above available online just check which one does the job for you until official support for --module with --target es6 arrives. 有更多的库,比如上面提供的那些库只是检查哪一个为你做的工作,直到官方支持--module with- --target es6到来。

UPDATE UPDATE

tsconfig.json tsconfig.json

{
  "compilerOptions": {
    "target":"ES6",
    "moduleResolution": "classic",
  }
}
  • ES6 support with generators ES6支持发电机
  • No import stuff transpiling due to "moduleResolution": "classic" 没有因"moduleResolution": "classic" moduleResolution”而导致的import内容"moduleResolution": "classic"

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

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