最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - when publishing npm package, should I use src of dist - Stack Overflow

programmeradmin0浏览0评论

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 badges
Add a ment  | 

1 Answer 1

Reset to default 6

If 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.

发布评论

评论列表(0)

  1. 暂无评论