From 6e3c52c05a6fd7c7a7d13609e6223bc865971de9 Mon Sep 17 00:00:00 2001 From: jaywcjlove Date: Fri, 11 Nov 2022 05:45:16 +0000 Subject: [PATCH] doc: update `package.json.md`. (#46) c4694ed8322c36a86e43350f56ae44d7eb54b04c --- docs/package.json.html | 161 ++++++++++++++++++++--------------------- 1 file changed, 80 insertions(+), 81 deletions(-) diff --git a/docs/package.json.html b/docs/package.json.html index 1278726c..fa47d940 100644 --- a/docs/package.json.html +++ b/docs/package.json.html @@ -41,12 +41,14 @@

这个快速参考备忘清单,显示了关于 package.json 文件中所需内容的全部内容。

重要字段

介绍

本快速参考备忘清单是您需要了解的关于 package.json 文件中所需内容的全部内容。 它必须是实际的 JSON,而不仅仅是 JavaScript 对象字面量。

name

{
@@ -263,9 +265,7 @@
 }
 

字段由模块作者提供,作为 JavaScript 包或组件工具的提示,用于打包模块以供客户端使用。 提案就在这里

-

exports

- -

所有包的版本都支持 main 字段,但它的功能是有限的。现在在 package.json 可以使用最新的 exports 字段导出。具体参考:https://nodejs.org/api/packages.html

+

exports

{
   "name": "mod",
   "exports":{
@@ -274,18 +274,8 @@
   }
 }
 
-

导出和导入

-
    -
  1. -

    如果同时出现 main 和 exports 字段,只会读取 exports 字段

    -
    {
    -  "main": "./index.js",
    -  "exports": "./index.js"
    -} 
    -
    -
  2. -
  3. -

    导出子路径中的模块

    +

    使用最新的 exports 字段导出,可规避 main 字段局限性 具体参考

    +

exports 导出子路径中的模块

{
   "name": "mod",
   "exports": {
@@ -294,100 +284,105 @@
   }
 }
 
-
// 导入
-import sub from "mod/sub"
+

导入

+
import sub from "mod/sub"
 
- -
  • -

    如果 . 是唯一的导出时,其提供了语法糖

    +
  • exports 简写 (. 唯一的导出)

    {
       "exports": {
         ".": "./dist/index.js"
       }
    -  //简写
    -  //"exports": "./dist/index.js"
     }
     
    - -
  • -

    条件导出。根据导出包的格式不同而设置的情况。

    -
      -
    • 注意:由于 require 和 import 互斥,所以 require 不能加载 es 的模块,export 不能加载 cjs 模块
    • -
    - - - - - - - - - - - - - - - - - - - - - - - - - -
    :--
    export通过 import 或 import() 或 es 模块加载的任何顶层导入或解析操作加载时,匹配。
    require当包通过 require() 加载时匹配。预期的格式包括 CommonJS、JSON 和本地插件。
    node匹配任何 Node.js 环境。可以是 cjs 或 es 模块文件。必须在 import 或 require 之后。
    default默认的降级条件。可以是一个 cjs 或 es 模块文件。这个条件应该总是排在最后。
    +

    简写

    {
    -  "exports": {
    -    ".": {
    -      "import":"./dist/index.mjs",
    -      "require":"./dist/index.cjs",
    -      "node": "./dist/ployfill.js",
    -      "default": "./dist/default.js"
    +  "exports": "./dist/index.js"
    +}
    +
    +
  • 条件导出(exports)

    + + + + + + + + + + + + + + + + + + + + + + + + + + +
    :--
    export通过 importimport()es 模块加载的任何顶层导入或解析操作加载时,匹配。
    require当包通过 require() 加载时匹配。预期的格式包括 CommonJS、JSON 和本地插件。
    node匹配任何 Node.js 环境。可以是 cjses 模块文件。必须在 importrequire 之后。
    default默认的降级条件。可以是一个 cjses 模块文件。这个条件应该总是排在最后。
    +
    {
    +  "exports": {
    +    ".": {
    +      "import":"./dist/index.mjs",
    +      "require":"./dist/index.cjs",  // 当包通过 `require()` 加载时匹配
    +      "node": "./dist/ployfill.js",  // 匹配任何 `Node.js` 环境
    +      "default": "./dist/default.js" // 默认的降级条件
         }
       }
     }
     
    - - +

    注意:由于 requireimport 互斥,所以 require 不能加载 es 的模块,export 不能加载 cjs 模块

    +

    导出和导入

    +
    {
    +  "main": "./index.js",
    +  "exports": "./index.js"
    +} 
    +
    +

    如果同时出现 mainexports 字段,只会读取 exports 字段

    任务类字段

    包里还可以包含一些可执行脚本或者其他配置信息。

    scripts

    +

    脚本是定义自动化开发相关任务的好方法,比如使用一些简单的构建过程或开发工具。 在 scripts 字段里定义的脚本,可以通过 yarn run <script> 命令来执行。 例如,下面 build-project 脚本可以通过 yarn run build-project 调用,并执行 node build-project.js

    {
       "scripts": {
         "build-project": "node build-project.js"
       }
     }
     
    -

    脚本是定义自动化开发相关任务的好方法,比如使用一些简单的构建过程或开发工具。 在 scripts 字段里定义的脚本,可以通过 yarn run <script> 命令来执行。 例如,上述 build-project 脚本可以通过 yarn run build-project 调用,并执行 node build-project.js

    有一些特殊的脚本名称。 如果定义了 preinstall 脚本,它会在包安装前被调用。 出于兼容性考虑,installpostinstallprepublish 脚本会在包完成安装后被调用。

    +

    start 脚本的默认值为 node server.js

    +

    参考文档:npm docs

    特定的 scripts

    对于以下脚本,npm 支持 package.json 文件的 scripts 默认命令字段:

      -
    • prepublish: 在打包并发布包之前运行,以及在没有任何参数的本地 npm 安装之前运行。 (见下文)
    • -
    • prepare: 在打包和发布包之前运行,在没有任何参数的本地 npm install 上运行,以及安装 git 依赖项时(见下文)。 这是在 preublish 之后运行,但是在 preublishOnly 之前运行。
    • -
    • prepublishOnly: 在包准备和打包之前运行,仅限于npm发布。 (见下文。)
    • +
    • prepublish: 在打包并发布包之前运行,以及在没有任何参数的本地 npm 安装之前运行
    • +
    • prepare: 在打包和发布包之前运行,在没有任何参数的本地 npm install 上运行,以及安装 git 依赖项时。 这是在 preublish 之后运行,但是在 preublishOnly 之前运行
    • +
    • prepublishOnly: 在包准备和打包之前运行,仅限于npm发布
    • prepack: 在打包 tarball 之前运行(在 npm packnpm publish,以及安装 git 依赖项时)
    • -
    • postpack: 在生成 tarball 之后运行并移动到其最终目标。
    • -
    • publish, postpublish: 在包发布后运行。
    • -
    • preinstall: 在安装软件包之前运行。
    • -
    • install, postinstall: 安装包后运行。
    • -
    • preuninstall, uninstall: 在卸载软件包之前运行。
    • -
    • postuninstall: 在卸载软件包之后运行。
    • -
    • preversion: 在改变包版本之前运行。
    • -
    • version: 改变包版本后运行,但提交之前。
    • -
    • postversion: 改变包版本后运行,然后提交。
    • -
    • pretest, test, posttest: 由 npm test 命令运行。
    • -
    • prestop, stop, poststop: 由 npm stop 命令运行。
    • -
    • prestart, start, poststart: 由 npm start 命令运行。
    • -
    • prerestart, restart, postrestart: 由 npm restart 命令运行。 注意:如果没有提供重启脚本,npm restart 将运行 stopstart 脚本。
    • -
    • preshrinkwrap, shrinkwrap, postshrinkwrap: 由 npm shrinkwrap 命令运行。
    • +
    • postpack: 在生成 tarball 之后运行并移动到其最终目标
    • +
    • publish, postpublish: 在包发布后运行
    • +
    • preinstall: 在安装软件包之前运行
    • +
    • install, postinstall: 安装包后运行
    • +
    • preuninstall, uninstall: 在卸载软件包之前运行
    • +
    • postuninstall: 在卸载软件包之后运行
    • +
    • preversion: 在改变包版本之前运行
    • +
    • version: 改变包版本后运行,但提交之前
    • +
    • postversion: 改变包版本后运行,然后提交
    • +
    • pretest, test, posttest: 由 npm test 命令运行
    • +
    • prestop, stop, poststop: 由 npm stop 命令运行
    • +
    • prestart, start, poststart: 由 npm start 命令运行
    • +
    • prerestart, restart, postrestart: 由 npm restart 命令运行。 注意:如果没有提供重启脚本,npm restart 将运行 stopstart 脚本
    • +
    • preshrinkwrap, shrinkwrap, postshrinkwrap: 由 npm shrinkwrap 命令运行

    参考文档:npm docs.

    config

    @@ -583,6 +578,10 @@

    另见

    © 2022 Kenny Wang, All rights reserved.