@a6kme/create-npm-package 中文文档教程

发布于 5年前 浏览 23 项目主页 更新于 3年前

CLI tool to create npm package (Currently Work In Progress. Not published to NPM.)

lerna

Why?

我经常不得不创建 NPM 包,而且我发现自己一次又一次地创建相同的样板代码。 对于非常简单和微小的 CommonJS 模块,我必须以不同于我必须为 UMD 编译的库的方式配置 webpackTypeScript 配置和 devDependencies 与 ES6 不同。 所以,我想更好地创建一个 CLI,它可以开箱即用。 只需回答几个提示,初始设置就完成了。

注意:如果您不打算使用 TypeScript 或不受支持的 NodeJs 功能,例如 ES6 import,您可能完全不需要它。 您可以简单地在 JavaScript 中创建您的文件,并将其直接发布到 NPM,而无需使用任何捆绑器或编译器。

当您计划在浏览器中将库公开为

How?

  • npm install -g @a6kme/create-npm-package
  • Run create-npm-package <YOUR_PACKAGE_NAME> command, and answer some prompts to create a folder with your package name.

在终端窗口执行npm login后,可以直接运行npm publish --access public。 它将测试和构建包,并将其发布到 npm 存储库中。

What?

目前,CLI 工具对其使用的工具的选择非常自以为是。 它使用 webpack 进行打包,使用 jest 进行测试,使用 eslint 进行 linting。 它为您执行以下操作

  • Sets up the package directory with name of your package
  • Creates package.json, with standard fields, like repository path, package name, and scripts for testing, linting etc.
  • Initializes the git repository with .gitignore file
  • Sets git remote origin with https://github.com/<YOUR_GIT_USERNAME>/<YOUR_PACKAGE_NAME>.git
  • Adds eslint configuration file with appropriate parser
  • Adds webpack builder configuration file

查看我关于如何使用此 CLI 的博客文章,网址为 https://a6k.me/articles/how-to-create-and-publish-an-npm-package/

CLI tool to create npm package (Currently Work In Progress. Not published to NPM.)

lerna

Why?

Often I had to create NPM packages, and I found myself creating the same boilerplate code again and again. For very simple and tiny CommonJS modules, I had to configure webpack differently than a library which I had to compile for UMD. TypeScript configuration and devDependencies were different than that of ES6. So, I figured better to create a CLI, which takes care of all that out of the box. Just answer few propmts, and the initial setup is done.

NOTE: You might not need it altogether, if you do not plan to use TypeScript or unsupported NodeJs features, like ES6 import. You can simply create your files in JavaScript, and publish it directly to NPM, without using any bundler or compiler.

It is needed when you are planning to expose the library as a <script> tag in browser, since you would want to use bundler to output one file which you can include in your HTML file. Or using experimental NodeJs features, and would want to use Babel compiler to convert it into more widely supported JavaScript.

How?

  • npm install -g @a6kme/create-npm-package
  • Run create-npm-package <YOUR_PACKAGE_NAME> command, and answer some prompts to create a folder with your package name.

After doing npm login in the terminal window, you can directly run npm publish --access public. It will test and build the package, and publish it on npm repository.

What?

Currently, the CLI tool is highly opinionated about the choice of tools it uses. It uses webpack for bundling, jest for testing, eslint for linting. It does the following for you

  • Sets up the package directory with name of your package
  • Creates package.json, with standard fields, like repository path, package name, and scripts for testing, linting etc.
  • Initializes the git repository with .gitignore file
  • Sets git remote origin with https://github.com/<YOUR_GIT_USERNAME>/<YOUR_PACKAGE_NAME>.git
  • Adds eslint configuration file with appropriate parser
  • Adds webpack builder configuration file

Check out my blog post about how to use this CLI at https://a6k.me/articles/how-to-create-and-publish-an-npm-package/

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文