main
我们来看npm官方的定义:
1 | The main field is a module ID that is the primary entry point to your program. That is, if your package is named foo, and a user installs it, and then does require("foo"), then your main module's exports object will be returned. |
简单说main
字段定义了我们程序的主要入口,用户在require
我们模块的时候,程序会从main
字段中找到我们程序的主要入口,从而输出我们的代码依赖.
在这里,main
字段用来指向commonjs的模块
jsnext:main 与 module
jsnext:main
与 module
的作用是一样的, 但是由于module
更直白一点,所以现在使用module
. 在这里我们讨论module
module
字段会指向一个ES2015 模块的入口, 当使用rollup
这些模块的bundler时,由于使用了ES2015 的模块,可以再做tree shaking
优化
参考
http://shuheikagawa.com/blog/2017/01/05/main-jsnext-main-and-module/
https://github.com/jsforum/jsforum/issues/5
https://www.reddit.com/r/javascript/comments/5jwg9c/confused_about_fields_module_and_jsnextmain_in/
rollup wiki
https://github.com/rollup/rollup/wiki/pkg.module
https://github.com/rollup/rollup/wiki/jsnext:main