简体   繁体   English

TypeScript NPM软件包的目标体系结构

[英]Target architecture for TypeScript NPM packages

I'm working on a number of NPM packages in TypeScript and I'm wondering about the best way to support different target architectures. 我正在使用TypeScript处理大量NPM软件包,并且想知道支持不同目标体系结构的最佳方法。

If you compile to ES3, you have the widest support, but also additional boilerplate for compatibility (size, partially impossible to parse). 如果您使用ES3进行编译,则将获得最广泛的支持,同时还具有兼容性的额外样板(大小,部分无法解析)。 If you target esnext, you basically have no boilerplate (clean output code!), but very limited support. 如果目标是esnext,则基本上没有样板(干净的输出代码!),但是支持非常有限。

The problem is that the TypeScript compiler does not transpile JS code or any code from node_modules in general. 问题在于,TypeScript编译器通常不会转换JS代码或来自node_modules任何代码。 Hence, if you eg compile a library to ES6 and someone wants to use it in their browser app (targeting ES3 or ES5), then they wouldn't be able to just use that library. 因此,如果您将一个库编译为ES6,并且有人想要在其浏览器应用程序中使用它(针对ES3或ES5),那么他们将无法仅使用该库。

What is the best practice here? 最佳做法是什么? How should I configure my package.json and tsconfig.json ? 我应如何配置package.jsontsconfig.json And if the library user will need to do something, what's the preferred way here? 而且,如果库用户需要执行某些操作,那么这里首选的方法是什么?

Thanks a lot! 非常感谢!

I think there can be compromise. 我认为可能会有妥协。 For me the main reason to use es6 in the library - to support rollup's tree-shaking. 对我来说,在库中使用es6的主要原因-支持汇总的树状摇动。 For this the package.json has option module : 为此,package.json具有选项模块

"main": "dist/alina-core.js",
"module": "dist/alina-core-es.js",

When user uses rollup, it will be required to transpile to ES5 after tree-shaking anyway, so, in module specified ES6 version. 当用户使用汇总时,无论如何都要摇晃后才能转换到ES5,因此,在module指定的ES6版本中。 But if library user uses old build pipeline, he will get main transpiled ES5 version. 但是,如果库用户使用旧的构建管道,那么他将获得main编译ES5版本。

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

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