简体   繁体   English

将库发布到 npm 时是否需要捆绑文件?

[英]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.jsonfiles部分。 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.jsondist目录。

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.

相关问题 发布库时我们是否需要 Angular 库 tslib 对等依赖项? - Do we need the Angular Library tslib peer dependency when publishing a library? 捆绑和发布 NPM 库。 解析所有依赖项并将它们包含到包中是否很常见? - bundling and publishing an NPM library. Is it common to resolve all dependencies and include them into the bundle? 在 npm 上发布之前,我是否必须构建 React Javascript 项目? - Do I have to build the React Javascript project before publishing on npm? 在发布npm包时,我应该使用dist的src吗? - when publishing npm package, should I use src of dist 尽管已经使用npm安装了,但我是否需要在lib或vendor文件夹中保留js库的副本? - Do I need to keep a copy of js library in lib or vendor folder though already installed using npm? 从Google图书馆加载时是否需要加载jsapi? - Do I need to load the jsapi when loading from google library? 如何捆绑模块而不依赖于发布npm包 - How to bundle module without dependencies for publishing npm package 我们是否需要在生产结束时捆绑我们的js文件 - DO we need to bundle our js files at the end of production 我是否需要为未找到模块的错误安装 npm - Do I need to do npm install for module not found error Angular 2,开发环境中非常慢(我是否需要捆绑开发才能使用?) - Angular 2, very slow in Development Env (Do I need a bundle for Dev?)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM