编写Angular库,可以安全地假设用户将使用commonjs吗? [英] Writing an Angular library, is it safe to assume users will use commonjs?

查看:44
本文介绍了编写Angular库,可以安全地假设用户将使用commonjs吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为Angular2 +用户(在构建自己的发行版之前将其导入)编写一个库,并且要从发行版中排除Angular本身,我需要做一些配置(在我的情况下包括

I'm writing a library for Angular2+ users (who would import it before building their own distributions) and to exclude Angular itself from the distribution, I need to do some configuration (in my case include externals in the Webpack config).

要执行此操作,请根据Webpack维护者 ,我需要假设我图书馆的用户使用commonjs.

To make this work, according to the Webpack maintainers, I need to assume my library's users use commonjs.

假设Angular用户使用nodejs是否合理?还是可以说他们正在使用的任何捆绑程序/构建工具都将支持nodejs?我不想忽略Angular用户组.仅仅发布源代码呢?我认为假设TypeScript对Angular2用户来说比假设commonjs更安全.

Is it reasonable to assume Angular users use nodejs? Or can we say that whatever bundler/build tool they are using will support nodejs? I don't want to ignore groups of Angular users. What about just releasing the source code? I think assuming TypeScript is safer for Angular2 users than assuming commonjs.

推荐答案

可以肯定地说,假设关于您的库用户环境的任何问题都是不好的.但是,几乎所有捆绑器都支持commonjs.UMD受到更好的支持(因为它与commonjs和AMD兼容),所以这是一个更安全的选择.

To some extend yes, assuming anything about your library user's environment is bad. However, commonjs is supported by virtually all bundlers. UMD is even better supported (since it is compatible with commonjs and AMD), so that would be a safer bet.

但是,通过正确的配置,您可以支持commonjs,esmodules和UMD,而无需进行过多的配置.区别在于前两个是使用TypeScript编译器(重复使用相同的tsconfig.json)完成的,而UMD发行版是使用Webpack进行构建的.

However, with the right config you can support commonjs, esmodules and UMD without much extra config. The difference is in building the first two is done with the TypeScript compiler (reusing the same tsconfig.json), while the UMD distribution is build with Webpack.

使用Webpack编译和捆绑TypeScript库解释详细设置,其中包括我演示回购://github.com/bbottema/typescript-lib-example"rel =" nofollow noreferrer>分叉并完全升级,请求

Compiling and bundling TypeScript libraries with Webpack explains the setup in details, which includes a demo repo that I forked and completely upgraded, pull request pending.

除此之外,显然可以提供AOT构建,但是我对此并不了解.我所知道的唯一的AOT是Angular的用于预编译@Components的编译器,因此浏览器不必这样做(性能和dist大小优化).

Aside from this, apparently it is possible to provide AOT build, but I don't know much about that. The only AOT I know is Angular's compiler for precompiling @Components so the browser doesn't have to do it (performance and dist size optimisation).

这篇关于编写Angular库,可以安全地假设用户将使用commonjs吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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