vite打包时添加打包标记的打包插件

一、插件代码

// buildSignPlugin.js
import { Plugin, loadEnv } from 'vite'
import fs from 'fs'
import path from 'path'
interface Config {
  output?: string
  filename?: string
  fileType?: 'text' | 'ts' | 'js' | 'json' | string
  [keyname: string]: any
}
const buildSignPlugin: (options: Config) => Plugin = function (options?: Config) {
  const { dir = './dist', filtname = 'build-sign', fileType = 'text' } = options || {}
  return {
    name: 'vite-build-sign',
    // Vite 独有钩子
    config(config) {
      // console.log('config')
    },
    // Vite 独有钩子
    configResolved(resolvedCofnig) {
      // console.log('configResolved')
    },
    // 通用钩子
    options(opts) {
      console.log('options')
      return opts
    },
    // Vite 独有钩子
    configureServer(server) {
      // console.log('configureServer')
      // setTimeout(() => {
      //   // 手动退出进程
      //   process.kill(process.pid, 'SIGTERM')
      // }, 3000)
    },
    // 通用钩子
    buildStart() {
      // console.log('buildStart')
      // 获取当前时间
    },
    // 通用钩子
    buildEnd() {
      // console.log('buildEnd')
    },
    // 通用钩子
    closeBundle() {
      const root = process.cwd()
      const env = process.env
      const packageJsonPath = root + '/package.json'

      const buildTime = new Date().toISOString()
      // 读取并解析 package.json 文件
      let npm_package_version = (env && env.npm_package_version) || ''

      if (!npm_package_version) {
        try {
          const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'))
          if (packageJson) {
            npm_package_version = packageJson.version
          }
        } finally {
        }
      }
      // 构建文件内容,导出当前时间
      const fileContent = `build time:${buildTime}\n version: ${npm_package_version}\n ${JSON.stringify(env)}`
      // 写入文件
      const outputDir = path.resolve(dir) // 假设文件写入到输出目录
      const filePath = path.join(outputDir, `${filtname}.${fileType}`)
      fs.writeFileSync(filePath, fileContent)
      // console.log('closeBundle')
    },
  }
}

export default buildSignPlugin

二、标题使用插件

// vite.config.js
import buildSignPlugin from './buildSignPlugin'; // 引入你的插件

export default {
  // ... 其他配置
  plugins: [
    // ... 其他插件
    buildSignPlugin(), // 使用你的插件
  ],
};

三、封装成npm包

  1. 创建npm项目
    npm init -y
    2.编辑package.json
{
  "name": "vite-build-sign-plugin",
  "version": "1.0.0",
  "description": "A Vite plugin to create a build-sign.js file with the current build time.",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [
    "vite",
    "plugin",
    "build",
    "sign"
  ],
  "author": "Your Name",
  "license": "ISC"
}
  1. 测试
    在发布之前,确保插件在本地工作正常。可以通过在一个Vite项目中安装你的插件(使用npm linknpm install ../path-to-your-plugin)来测试它。
  2. 发布
    登录npm账号,发布
    npm publish
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值