"style" package.json 中的字段

"style" field in package.json

我注意到 Bootstrap and Normalize.css 在它们的 package.json 中都有一个 "style" 字段。

为什么他们有这个?如果我不得不猜测,它允许用户像导入定义的样式表一样容易require('bootstrap'),但事实并非如此。

来自 Techwraith 的 pull request,将其添加到 Bootstrap:

Many modules in npm are starting to expose their css entry files in their package.json files. This allows tools like npm-css, rework-npm, and npm-less to import bootstrap from the node_modules directory. [...]

It's actually not written anywhere but in the code for these modules right now. We're hoping to get this standardized at some point, but we've all reached this convention separately, so I'm inclined to just go with it. [...]

If you want to read about this style of css development, I wrote a thing:

http://techwraith.com/your-css-needs-a-dependency-graph-too/

其他工具也有支持,例如 browserify 插件 parcelify:

Add css to your npm modules consumed with browserify.

  • Just add a style key to your package.json to specify the package's css file(s). [...]

Parcelify will concatenate all the css files in the modules on which main.js depends -- in this case just myModule.css -- in the order of the js dependency graph, and write the output to bundle.css.