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

Javascript 将库发布到npm时是否需要捆绑文件?,javascript,npm,Javascript,Npm,我的项目结构大致如下: dist/ - helper.compiled.js - entrypoint.compiled.js src/ - helper.js - entrypoint.js { "name": "my-library", "version": "1.0.0", "description": "My Library.", "main": "dist/entrypoint.compiled.js", "scripts": {}

我的项目结构大致如下:

dist/
  - helper.compiled.js
  - entrypoint.compiled.js
src/
 - helper.js
 - entrypoint.js
{
    "name": "my-library",
    "version": "1.0.0",
    "description": "My Library.",
    "main": "dist/entrypoint.compiled.js",
    "scripts": {},
    "author": "",
    "license": "ISC",
    "dependencies": {},
    "files": [
        "dist"
    ]
}
我正在阅读npm发布指南,上面说要提供一个index.js文件。但这真的有必要吗?毕竟,我的
entrypoint.compiled.js
只需要
helper.compiled.js
。提供单个
index.js
文件的好处是什么


在这种情况下,向npm发布库的建议步骤是什么?我尝试使用npm包,但我不太明白它在做什么。

您只需要发布编译后的文件。如果编译正确,则无需在包中包含
src/
文件夹

这是react包中我的树的
.npmignore
文件:您可以看到包中有什么

如您所见,我只发布
dist
目录和根目录中的每个文件。最简单的是
package.json
dist
目录

npm publish
命令实际上只是从文件中创建一个包并将其上载到npm注册表。运行该命令后,您将能够找到npm包并将其用作任何其他包


运行
npm publish
后,我建议从注册表下载已发布的包,并尝试是否所有必需的文件都在那里,并验证您可以使用所有文件。

仅捆绑已编译文件的最佳方法是将目录放在
包.json
的部分。然后,这将只打包npm使用的文件,如
package.json
README.md
,以及包所需的其他文件

示例
package.json
如下所示:

dist/
  - helper.compiled.js
  - entrypoint.compiled.js
src/
 - helper.js
 - entrypoint.js
{
    "name": "my-library",
    "version": "1.0.0",
    "description": "My Library.",
    "main": "dist/entrypoint.compiled.js",
    "scripts": {},
    "author": "",
    "license": "ISC",
    "dependencies": {},
    "files": [
        "dist"
    ]
}

不要使用
.npmignore
在包中包含文件,在发布库时使用
包.json
中的属性,包括
包.json
部分中的文件或文件夹,因此,我不需要将所有编译的文件捆绑到一个索引.js中。你不必这样做。如果编译后的文件在本地工作,那么它在下载时就会工作。我在我的包中这样做:(单击右上角的“浏览文件”)。我想我担心没有
index.js
文件,用户将不得不执行
require('my-lib-name/entrypoint.compiled.js')
而不仅仅是
require('my-lib-name'))
因为我知道node将“index.js”作为需要目录时所需的默认文件。它基于
package.json
中的
main
属性。只需将它指向您想要充当
index.js
的文件。