简体   繁体   English

如何使用 node.js 创建要在 angular 应用程序和服务器上使用的 javascript 库

[英]How to create a javascript library to be used on angular app and on the server with node.js

I'd like to create a js library that can be used in both the browser using angular and on the server using node.js.我想创建一个 js 库,它既可以在使用 angular 的浏览器中使用,也可以在使用 node.js 的服务器上使用。

I'm a bit confused on how to set this up.我对如何设置这个有点困惑。 I'd like to use typescript as well.我也想使用打字稿。 But I'm so used to something like angular-cli which takes care of all the setup (webpack, ts, babel, etc) that I'm a bit lost on how to start a javascript project using those tool and I assume I'll have to setup all the configuration by hand.但是我已经习惯了像 angular-cli 这样的东西,它负责所有的设置(webpack、ts、babel 等),我对如何使用这些工具启动 javascript 项目有点迷茫,我想我'必须手动设置所有配置。

Is there anything I'd have to look out for when trying to support the library for both browser and server?在尝试支持浏览器和服务器的库时,有什么我需要注意的吗? I guess I should be targeting es5.我想我应该针对 es5。 I know in the tsconfig I can specify the target as es5, what about the module and module resolution in the tsconfig?我知道在 tsconfig 中我可以指定目标为 es5,那么 tsconfig 中的模块和模块分辨率呢? Anything else to do in the babel config and webpack?在 babel 配置和 webpack 中还有什么可做的吗?

Any one has good tutorials?有人有好的教程吗? links?链接? starter?起动机?

there is actually a built in command in Angular CLI to create a library: "ng generate library yourLibrary --prefix demoPrefix "实际上在 Angular CLI 中有一个内置命令来创建一个库:“ng generate library yourLibrary --prefix demoPrefix

now a new projects directory is created with your folder inside + the relevant config json.现在创建了一个新的项目目录,其中包含您的文件夹 + 相关的配置 json。

you'll need to create a public API next to allow access to this library -> edit the public api.ts file of your newly created lib to do this and provide -> export the relevant services, modules and models.接下来您需要创建一个公共 API 以允许访问此库 -> 编辑新创建的库的公共 api.ts 文件以执行此操作并提供 -> 导出相关服务、模块和模型。

you can also add components to your library using CLI like so:您还可以使用 CLI 将组件添加到您的库中,如下所示:

ng generate component poster --project= demoLibrary ng 生成组件海报 --project= demoLibrary

here's an article that might help https://blog.angularindepth.com/creating-a-library-in-angular-6-87799552e7e5这是一篇可能有帮助的文章https://blog.angularindepth.com/creating-a-library-in-angular-6-87799552e7e5

使用无依赖项的纯 javascript(或打字稿)编写库

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

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