admin管理员组文章数量:1432187
When publishing some javascript to npm as a library, should I set the "main" in pacakge.json to "dist/index.js" or my "src/index.js"?
Suppose that the library is built with webpack, and may be used with projects in webpack.
what will be the difference between two options. will webpack be able to do tree shaking in both options?
Thanks!
When publishing some javascript to npm as a library, should I set the "main" in pacakge.json to "dist/index.js" or my "src/index.js"?
Suppose that the library is built with webpack, and may be used with projects in webpack.
what will be the difference between two options. will webpack be able to do tree shaking in both options?
Thanks!
Share Improve this question asked Apr 17, 2017 at 23:39 ryangryang 1611 silver badge5 bronze badges1 Answer
Reset to default 6If your library is designed to be used in the browser, then it's important to remember that not everyone is using a module bundler.
It's good practice to set the main
property to the bundled file (in your case dist/index.js
) and make sure that you have a prepublish
script that performs your build step before you publish it.
To support tree-shaking with bundlers like Rollup, you can use the module
property and ensure that it points to a module that uses ES2015 imports.
For example:
{
"main": "dist/index.js",
"module": "src/index.js"
}
Rollup will respect this, but getting Webpack to tree-shake your code is a little more involved.
本文标签: javascriptwhen publishing npm packageshould I use src of distStack Overflow
版权声明:本文标题:javascript - when publishing npm package, should I use src of dist - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745558671a2663339.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论