简体   繁体   English

package.json的main,module,browser属性是应该指向minified还是source?

[英]Should main, module, browser properties of package.json point to minified or source?

For a JS library which is published in a structure like ... 对于一个以...结构发布的JS库

my-package\
  dist\
    my-package.cjs.js
    my-package.cjs.min.js
    my-package.cjs.min.js.map
    my-package.esm.js
    my-package.esm.min.js
    my-package.esm.min.js.map
    my-package.umd.js
    my-package.umd.min.js
    my-package.umd.min.js.map
  package.json

Eg built to CJS, ESM, and UMD bundles, each having a "source", minified and map file. 例如,构建为CJS,ESM和UMD捆绑包,每个捆绑包具有“源”,缩小和映射文件。

package.json 的package.json

{ // ...
  "main": "dist/my-package.cjs.js",
  "module": "dist/my-package.esm.js",
  "browser": "dist/my-package.umd.js"
}

My assumption is that these properties should point to the "source" file, and tools used to bundle my library (eg Webpack) into an external project are smart enough to choose the minified file if the build is non-debug/non-dev mode. 我的假设是这些属性应该指向“源”文件,并且用于将我的库(例如Webpack)捆绑到外部项目中的工具足够聪明,如果构建是非调试/非开发模式,则选择缩小的文件。

Or, am I wrong and these properties should point to the minified file? 或者,我错了,这些属性应该指向缩小的文件?

I think you are right. 我想你是对的。 If you are only releasing, for example, ESM and UMD, you could then stick to 如果您只是发布ESM和UMD,那么您可以坚持下去

{ // ...
  "main": "dist/my-package.umd.js",
  "module": "dist/my-package.esm.js"
}

The most important thing is that, using " module ", you can provide a version of your bundle that is efficiently consumed by an app that is compiled through Webpack or Rollup. 最重要的是,使用“ 模块 ”,您可以提供捆绑版本,该版本由通过Webpack或Rollup编译的应用程序有效使用。 Tree shaking can be applied in that case so all the dead code is not included in the final bundle. 在这种情况下可以应用树摇动,因此所有死代码都不包含在最终捆绑中。

This is useful, for example, for a React components library. 例如,这对于React组件库很有用。 You can export it this way so the apps that use it only get the code for the components being consumed. 您可以通过这种方式导出它,以便使用它的应用程序只获取正在使用的组件的代码。

This was answered before here: 这是在此之前回答的:

What is the "module" package.json field for? 什么是“module”package.json字段?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM