Skip to content
JSON
{
  "name": "dano-test-publish",
  "private": false,
  "version": "0.0.4",
  "keywords": ["dano","demo"],
  "description": "测试包发布、开发的demo",
  "main": "./ui/lib/index.js",
  "module": "./ui/es/index.js",
  "type": "module",
  "publishConfig": {
    "access": "public"
  },
  "license":"ISC",
  "files": [
    "ui"
  ],
  "scripts": {
    "dev": "vite",
    "build": "vite build",
    "preview": "vite preview"
  },
  "dependencies": {
    "vue": "^3.5.22"
  },
  "devDependencies": {
    "@vitejs/plugin-vue": "^6.0.1",
    "sass": "^1.93.2",
    "vite": "^7.1.7",
    "vite-plugin-vue-setup-extend": "^0.4.0"
  }
}

npm publish 即可发布包

1. 基本信息

json
{
  "name": "dano-test-publish",
  "private": false,
  "version": "0.0.4",
  "keywords": ["dano", "demo"],
  "description": "测试包发布、开发的demo",
  "main": "./ui/lib/index.js",
  "module": "./ui/es/index.js",
  "type": "module",
  "license": "ISC"
}
  • name: dano-test-publish — 这个字段指定了包的名称。发布到 npm 时,会用这个名称来区分不同的包。
  • private: false — 默认情况下,privatefalse,表示这个包可以发布到 npm。如果是 true,这个包将不会被发布。
  • version: 0.0.4 — 当前包的版本号,符合 semver(语义化版本控制)的规范。版本号通常遵循 MAJOR.MINOR.PATCH 的形式。
  • keywords: ["dano", "demo"] — 一些关键词,方便别人通过搜索找到这个包。
  • description: 测试包发布、开发的demo — 包的简短描述,便于别人了解这个包的功能。
  • main: ./ui/lib/index.js — 包的入口文件,对于 CommonJS 环境使用。指向的是编译后的文件。
  • module: ./ui/es/index.js — 包的入口文件,用于 ES Module 环境。通常这是源码或是经过适当转换的 ES 模块代码。
  • type: module — 这表示包使用 ECMAScript 模块格式(import/export)。这对于支持 ES Module 的项目非常重要。
  • license: "ISC" — 使用 ISC 许可证,通常用于开源项目。

2. 发布配置

json
"publishConfig": {
  "access": "public"
}
  • publishConfig: 这里配置了包发布到 npm 时的访问权限。"public" 表示这个包是公开的,任何人都可以访问。如果你将其设置为 "restricted",那么包就会限制为私有发布。

3. 文件字段

json
"files": [
  "ui"
]
  • files: 这个字段指定了发布包时包含的文件。在这里,它指定只有 ui 目录下的文件会被发布到 npm 仓库,其他文件如源码、配置文件等不会被包含。

4. 脚本字段

json
"scripts": {
  "dev": "vite",
  "build": "vite build",
  "preview": "vite preview"
}
  • dev: vite — 启动 Vite 开发服务器,用于本地开发。你可以在本地访问组件并实时查看效果。
  • build: vite build — 使用 Vite 构建项目,通常会将源代码编译为生产环境可用的格式(如 ES Module 或 UMD 格式)。
  • preview: vite preview — 用于预览构建后的代码。

5. 依赖

json
"dependencies": {
  "vue": "^3.5.22"
}
  • dependencies: 项目运行时依赖的包。这里指定了 Vue 作为项目的核心依赖,且版本要求大于等于 3.5.22,但小于 4.0.0(由于使用了 ^,即符合语义化版本规则)。

6. 开发依赖

json
"devDependencies": {
  "@vitejs/plugin-vue": "^6.0.1",
  "sass": "^1.93.2",
  "vite": "^7.1.7",
  "vite-plugin-vue-setup-extend": "^0.4.0"
}
  • devDependencies: 项目在开发阶段使用的工具和库。它们不会随着包的发布而包含在生产环境中。

    • @vitejs/plugin-vue: Vite 用于处理 Vue 文件的插件,允许 Vite 编译 .vue 文件。
    • sass: 用于支持 Sass/SCSS 语法,方便你使用 Sass 来编写样式。
    • vite: 构建工具,用于开发、构建和预览。
    • vite-plugin-vue-setup-extend: 一个 Vite 插件,用于扩展 Vue 3 的 <script setup> 语法。

常见的补充配置项

  1. Babel 配置: 如果你的项目中使用了 Babel 来转换 JavaScript,你可能需要在 devDependencies 中添加 @babel/preset-env@babel/core,并在 package.json 中配置 babel 字段。

    json
    "babel": {
      "presets": ["@babel/preset-env"]
    }
  2. eslint 和 prettier: 用于代码风格检查和格式化。可以添加 eslintprettier 配置项。

    json
    "eslintConfig": {
      "extends": ["eslint:recommended", "plugin:vue/vue3-essential"]
    },
    "prettier": {
      "singleQuote": true,
      "semi": false
    }
  3. PostCSS 配置: PostCSS 是用于处理 CSS 的工具,可以在项目中进行自动前缀、CSS 压缩等处理。你可能需要在 devDependencies 中安装 postcss 和相关插件。

    json
    "devDependencies": {
      "postcss": "^8.3.6",
      "autoprefixer": "^10.4.1"
    },
    "postcss": {
      "plugins": {
        "autoprefixer": {}
      }
    }
  4. 环境变量: 为了配置不同的环境(开发、生产等),你可以使用 .env 文件。在 Vite 中,vite 会自动加载 .env 文件中的环境变量。你可以在 package.json 中配置 env 文件的读取规则:

    json
    "scripts": {
      "dev": "vite",
      "build": "vite build",
      "preview": "vite preview",
      "serve": "vite preview --port 5000"
    }
  5. TypeScript 配置: 如果你的项目使用 TypeScript,可以添加 tsconfig.json 文件来配置 TypeScript 的行为。

    json
    "devDependencies": {
      "typescript": "^4.5.0"
    }
  6. npm 和 yarn 配置: 你还可以设置一些关于 npm 或 yarn 的配置,例如自动安装依赖、注册表地址等。

    json
    "publishConfig": {
      "registry": "https://registry.npmjs.org/"
    }