[英]Do I need to bundle files when publishing a library to npm?
My project structure very very roughly looks like this:我的项目结构非常非常粗略地看起来像这样:
dist/
- helper.compiled.js
- entrypoint.compiled.js
src/
- helper.js
- entrypoint.js
I was reading the npm publishing guidelines and it says to provide a single index.js file.我正在阅读 npm 发布指南,它说要提供一个 index.js 文件。 But is this really necessary?
但这真的有必要吗? Afterall, my
entrypoint.compiled.js
can just require helper.compiled.js
.毕竟,我的
entrypoint.compiled.js
可以只需要helper.compiled.js
。 What is the benefit of providing a single index.js
file?提供单个
index.js
文件有什么好处?
What is the recommended procedure for publishing a library to npm in this situation?在这种情况下,将库发布到 npm 的推荐过程是什么? I tried using
npm pack
, but I don't quite understand what it is doing.我尝试使用
npm pack
,但我不太明白它在做什么。
The best way to bundle just the compiled files is to put the directory in the files
section of your package.json
.仅捆绑已编译文件的最佳方法是将目录放在
package.json
的files
部分。 This will then only package the files that npm uses such as package.json
, README.md
, and other files that your package requires. This will then only package the files that npm uses such as
package.json
, README.md
, and other files that your package requires.
An example package.json
looks something like this: package.json
示例如下所示:
{
"name": "my-library",
"version": "1.0.0",
"description": "My Library.",
"main": "dist/entrypoint.compiled.js",
"scripts": {},
"author": "",
"license": "ISC",
"dependencies": {},
"files": [
"dist"
]
}
You need only to publish the compiled files.您只需要发布已编译的文件。 If you compile them correctly then there is no need to include your
src/
folder in the package.如果您正确编译它们,则无需在 package 中包含您的
src/
文件夹。
Here is my .npmignore
file for my tree in react package: .npmignore
You can see what is in the package here .这是我在反应 package 中的树的
.npmignore
文件: .npmignore
您可以在此处查看 package 中的内容。
As you can see, I publish only the dist
directory and every file in the root.如您所见,我仅发布
dist
目录和根目录中的每个文件。 The bare minimum is the package.json
and the dist
directory.最低限度是
package.json
和dist
目录。
npm publish
command essentially just creates a package from your files and uploads it to the npm registry. npm publish
命令本质上只是从您的文件中创建一个 package 并将其上传到 npm 注册表。 After running the command you will be able to find the npm package and use it as any other package.运行命令后,您将能够找到 npm package 并将其用作任何其他 package。
After you run npm publish
I recommend downloading published the package from the registry and try out if all the required files are there and verify that you can use everything.运行
npm publish
后,我建议从注册表下载已发布的 package 并尝试是否所有必需的文件都在那里并验证您是否可以使用所有内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.