使用 Vite 发布 npm 包

2024年06月13日星期四


项目准备

准备好一个vite项目,在vite.config.js中做好打包配置。

vite.config.js
export default {
  build: {
    lib: {
      // 配置库的入口文件
      entry: [
        resolve(__dirname, 'src/index.js'),
        resolve(__dirname, 'src/log.js')
      ],
      // 库的全局变量名
      name: 'Pluck',
      // 定义输出文件的名称
      fileName: (format, name) => {
        if (format === 'es') {
          return `${name}.js`
        }
        return `${name}.${format}`
      }
    },
    // Rollup 配置选项
    rollupOptions: {
      // 外部依赖,不打包到库中
      external: ['collect.js']
    }
  }
}

然后在package.json中做好发包配置。

package.json
{
  "name": "pluck",
  "version": "0.0.6",
  "type": "module",
  "files": [  //指定发布到 npm 的文件或目录。
    "dist"  
  ],
  "main": "./dist/index.cjs",  //包的 CommonJS 入口文件。
  "module": "./dist/index.js", //包的 ES 模块入口文件。
 
 //...
}

做好配置之后,就可以发布npm包了。

  • 根据需要设置registry源。
> npm config get registry  //获取当前配置的 npm 注册表
> npm config set registry https://xxxxxx.xxxxx.xxx  //配置npm注册表
> npm login //登录npm
> npm publish //发布npm包