简体   繁体   English

如何创建 JavaScript 库

[英]How to create a JavaScript Library

I've created some functions in JavaScript.我在 JavaScript 中创建了一些函数。

I've found out that I reuse them in many projects.我发现我在许多项目中重复使用它们。

So I decided to create a small JavaScript Library for my coding.所以我决定为我的编码创建一个小的 JavaScript 库。 A Library like react, react-dom, jquery which I can install with npm: npm install <my-personal-library>像 react、react-dom、jquery 这样的库,我可以用 npm 安装: npm install <my-personal-library>

I searched the.net.我搜索了.net。 I learned that I can use npm publish <my-personal-library , but I do not know how to format my library and functions to use and install them like an npm package.我了解到我可以使用npm publish <my-personal-library ,但我不知道如何格式化我的库和函数以像 npm package 一样使用和安装它们。

Also, I have no idea about creating type definitions for my functions and library.此外,我不知道如何为我的函数和库创建类型定义。 like @types/react Any guidence?像@types/react 有什么指导吗?

About how to publish your library关于如何发布您的图书馆

You can read the official npm documentation on how to create nodejs package modules: https://docs.npmjs.com/creating-node-js-modules您可以阅读官方 npm 文档,了解如何创建 nodejs package 模块: https://docs.npmjs.com/creating-node-js-modules

Basically, what you need is a JS module file (IE test.js ) to be exported with exports keyword like:基本上,您需要的是一个 JS 模块文件 (IE test.js ),要使用 export 关键字exports ,例如:

exports.printMsg = function() {
  console.log("This is a message from the demo package");
}

Then publish the module with npm publish (add --access public if you want it to be public) Finally import the package in the project you need it with npm install <your-module-name>然后用npm publish发布模块(如果你想公开,添加--access public public) 最后在你需要的项目中导入 package 用npm install <your-module-name>

About the type definitions关于类型定义

I understand that you are using typescript?我了解到您使用的是 typescript? Then the following link is a good read: https://www.typescriptlang.org/docs/handbook/declaration-files/dts-from-js.html那么下面的链接是一个很好的阅读: https://www.typescriptlang.org/docs/handbook/declaration-files/dts-from-js.html

  • To install your package in a pc you have to set up a cli package inside npm module.要在 pc 中安装 package,您必须在 npm 模块中设置一个 cli package。

     import { Command } from "commander"; import open from "open"; // [] indicates that this is optional // <> indicates that this is a required value export const serveCommand = new Command().command("serve [filename]") // when user enters node index.js --help, it sees description.description("ADd a description").option("-p, --port <number>", "port to run server on", "4005").option("-v, --version", "show version", version, "") // first arg will be the arg that passed in command() SO filename // second arg is all other options // THIS IS WE TELL WHAT TO DO.action(async (filename = "main.js", options: { port: string }) => { try { // this is where you add logic about what to do when enterd the command open("http://localhost:4005"); } catch (error: any) { if (error.code === "EADDRINUSE") { console.error("Port is in use. Try runnng on a different port "); } else { console.log("Issue is:", error.message); } process.exit(1); } });
  • To make cli run the code, in your main file要使 cli 运行代码,请在您的主文件中

    //whenever anyone runs cli from command line, this file will be executed. ;/usr/bin/env node import { program } from "commander". // this is the above command import { serveCommand } from ";/commands/serve". // you could chain other commands.addCommand(otherCommand) program;addCommand(serveCommand). // parse this and run the aprropriate command that you put together program.parse(process;argv);
  • as you see you might have different sub packages and each sub packages will have their own package.json .如您所见,您可能有不同的子包,每个子包都有自己的package.json To communicate between those sub packages, you add them in their dependencies inside package.json.要在这些子包之间进行通信,您将它们添加到 package.json 内的依赖项中。 for example, you have to use cli package inside your main package. so in package.json例如,您必须在主 package 中使用 cli package。所以在 package.json

     "dependencies": { "@my-npm-package/cli": "^1.0.15", // other dependencies "express": "^4.17.1", }
  • Since you have different sub packages and you have to group them together.由于您有不同的子包,因此必须将它们组合在一起。 Assgin those pacakges into an "organization".将这些包分配到一个“组织”中。 another term is to "create scoped package".另一个术语是“创建作用域包”。 "@types/cors" and "@types/express" are scoped packages. “@types/cors”和“@types/express”是作用域包。

Declaring Scoped Packages声明作用域包

  • in npm page, on the right, cick on "Add organization"在 npm 页面,在右侧,点击“添加组织”

  • organization name should be unique组织名称应该是唯一的

  • update name of the dependencies in package.json of each package.更新每个 package 的 package.json 中的依赖项名称。

  • Manage packages管理包

Use Lerna to manage all of those npm packages.使用Lerna管理所有这些 npm 包。 it is for managing multi project packages.它用于管理多项目包。 - Lerna is one tool out there in the wild that we can use for managing a multi package project. - Lerna 是我们可以用来管理多 package 项目的一种工具。 Yarn and Npm are similar to lerna. yarn和Npm和lerna类似。 there are also Bolt and Luigi.还有博尔特和路易吉。

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

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