博客 / 詳情

返回

Vue3 企業級優雅實戰 - 組件庫框架 - 9 實現組件庫 cli - 上

上文搭建了組件庫 cli 的基礎架子,實現了創建組件時的用户交互,但遺留了 cli/src/command/create-component.ts 中的 createNewComponent 函數,該函數要實現的功能就是上文開篇提到的 —— 創建一個組件的完整步驟。本文咱們就依次實現那些步驟。(友情提示:本文內容較多,如果你能耐心看完、寫完,一定會有提升)

1 創建工具類

在實現 cli 的過程中會涉及到組件名稱命名方式的轉換、執行cmd命令等操作,所以在開始實現創建組件前,先準備一些工具類。

cli/src/util/ 目錄上一篇文章中已經創建了一個 log-utils.ts 文件,現繼續創建下列四個文件:cmd-utils.tsloading-utils.tsname-utils.tstemplate-utils.ts

1.1 name-utils.ts

該文件提供一些名稱組件轉換的函數,如轉換為首字母大寫或小寫的駝峯命名、轉換為中劃線分隔的命名等:

/**
 * 將首字母轉為大寫
 */
export const convertFirstUpper = (str: string): string => {
  return `${str.substring(0, 1).toUpperCase()}${str.substring(1)}`
}
/**
 * 將首字母轉為小寫
 */
export const convertFirstLower = (str: string): string => {
  return `${str.substring(0, 1).toLowerCase()}${str.substring(1)}`
}
/**
 * 轉為中劃線命名
 */
export const convertToLine = (str: string): string => {
  return convertFirstLower(str).replace(/([A-Z])/g, '-$1').toLowerCase()
}
/**
 * 轉為駝峯命名(首字母大寫)
 */
export const convertToUpCamelName = (str: string): string => {
  let ret = ''
  const list = str.split('-')
  list.forEach(item => {
    ret += convertFirstUpper(item)
  })
  return convertFirstUpper(ret)
}
/**
 * 轉為駝峯命名(首字母小寫)
 */
export const convertToLowCamelName = (componentName: string): string => {
  return convertFirstLower(convertToUpCamelName(componentName))
}

1.2 loading-utils.ts

在命令行中創建組件時需要有 loading 效果,該文件使用 ora 庫,提供顯示 loading 和關閉 loading 的函數:

import ora from 'ora'

let spinner: ora.Ora | null = null

export const showLoading = (msg: string) => {
  spinner = ora(msg).start()
}

export const closeLoading = () => {
  if (spinner != null) {
    spinner.stop()
  }
}

1.3 cmd-utils.ts

該文件封裝 shelljs 庫的 execCmd 函數,用於執行 cmd 命令:

import shelljs from 'shelljs'
import { closeLoading } from './loading-utils'

export const execCmd = (cmd: string) => new Promise((resolve, reject) => {
  shelljs.exec(cmd, (err, stdout, stderr) => {
    if (err) {
      closeLoading()
      reject(new Error(stderr))
    }
    return resolve('')
  })
})

1.4 template-utils.ts

由於自動創建組件需要生成一些文件,template-utils.ts 為這些文件提供函數獲取模板。由於內容較多,這些函數在使用到的時候再討論。

2 參數實體類

執行 gen 命令時,會提示開發人員輸入組件名、中文名、類型,此外還有一些組件名的轉換,故可以將新組件的這些信息封裝為一個實體類,後面在各種操作中,傳遞該對象即可,從而避免傳遞一大堆參數。

2.1 component-info.ts

src 目錄下創建 domain 目錄,並在該目錄中創建 component-info.ts ,該類封裝了組件的這些基礎信息:

import * as path from 'path'
import { convertToLine, convertToLowCamelName, convertToUpCamelName } from '../util/name-utils'
import { Config } from '../config'

export class ComponentInfo {
  /** 中劃線分隔的名稱,如:nav-bar */
  lineName: string
  /** 中劃線分隔的名稱(帶組件前綴) 如:yyg-nav-bar */
  lineNameWithPrefix: string
  /** 首字母小寫的駝峯名 如:navBar */
  lowCamelName: string
  /** 首字母大寫的駝峯名 如:NavBar */
  upCamelName: string
  /** 組件中文名 如:左側導航 */
  zhName: string
  /** 組件類型 如:tsx */
  type: 'tsx' | 'vue'

  /** packages 目錄所在的路徑 */
  parentPath: string
  /** 組件所在的路徑 */
  fullPath: string

  /** 組件的前綴 如:yyg */
  prefix: string
  /** 組件全名 如:@yyg-demo-ui/xxx */
  nameWithLib: string

  constructor (componentName: string, description: string, componentType: string) {
    this.prefix = Config.COMPONENT_PREFIX
    this.lineName = convertToLine(componentName)
    this.lineNameWithPrefix = `${this.prefix}-${this.lineName}`
    this.upCamelName = convertToUpCamelName(this.lineName)
    this.lowCamelName = convertToLowCamelName(this.upCamelName)
    this.zhName = description
    this.type = componentType === 'vue' ? 'vue' : 'tsx'
    this.parentPath = path.resolve(__dirname, '../../../packages')
    this.fullPath = path.resolve(this.parentPath, this.lineName)
    this.nameWithLib = `@${Config.COMPONENT_LIB_NAME}/${this.lineName}`
  }
}

2.2 config.ts

上面的實體中引用了 config.ts 文件,該文件用於設置組件的前綴和組件庫的名稱。在 src 目錄下創建 config.ts

export const Config = {
  /** 組件名的前綴 */
  COMPONENT_PREFIX: 'yyg',
  /** 組件庫名稱 */
  COMPONENT_LIB_NAME: 'yyg-demo-ui'
}

3 創建新組件模塊

3.1 概述

上一篇開篇講了,cli 組件新組件要做四件事:

  1. 創建新組件模塊;
  2. 創建樣式 scss 文件並導入;
  3. 在組件庫入口模塊安裝新組件模塊為依賴,並引入新組件;
  4. 創建組件庫文檔和 demo。

本文剩下的部分分享第一點,其餘三點下一篇文章分享。

src 下創建 service 目錄,上面四個內容拆分在不同的 service 文件中,並統一由 cli/src/command/create-component.ts 調用,這樣層次結構清晰,也便於維護。

首先在 src/service 目錄下創建 init-component.ts 文件,該文件用於創建新組件模塊,在該文件中要完成如下幾件事:

  1. 創建新組件的目錄;
  2. 使用 pnpm init 初始化 package.json 文件;
  3. 修改 package.json 的 name 屬性;
  4. 安裝通用工具包 @yyg-demo-ui/utils 到依賴中;
  5. 創建 src 目錄;
  6. 在 src 目錄中創建組件本體文件 xxx.tsx 或 xxx.vue;
  7. 在 src 目錄中創建 types.ts 文件;
  8. 創建組件入口文件 index.ts。

3.2 init-component.ts

上面的 8 件事需要在 src/service/init-component.ts 中實現,在該文件中導出函數 initComponent 給外部調用:

/**
 * 創建組件目錄及文件
 */
export const initComponent = (componentInfo: ComponentInfo) => new Promise((resolve, reject) => {
  if (fs.existsSync(componentInfo.fullPath)) {
    return reject(new Error('組件已存在'))
  }

  // 1. 創建組件根目錄
  fs.mkdirSync(componentInfo.fullPath)

  // 2. 初始化 package.json
  execCmd(`cd ${componentInfo.fullPath} && pnpm init`).then(r => {
    // 3. 修改 package.json
    updatePackageJson(componentInfo)

    // 4. 安裝 utils 依賴
    execCmd(`cd ${componentInfo.fullPath} && pnpm install @${Config.COMPONENT_LIB_NAME}/utils`)

    // 5. 創建組件 src 目錄
    fs.mkdirSync(path.resolve(componentInfo.fullPath, 'src'))

    // 6. 創建 src/xxx.vue 或s src/xxx.tsx
    createSrcIndex(componentInfo)

    // 7. 創建 src/types.ts 文件
    createSrcTypes(componentInfo)

    // 8. 創建 index.ts
    createIndex(componentInfo)

    g('component init success')

    return resolve(componentInfo)
  }).catch(e => {
    return reject(e)
  })
})

上面的方法邏輯比較清晰,相信大家能夠看懂。其中 3、6、7、8抽取為函數。

修改 package.json :讀取 package.json 文件,由於默認生成的 name 屬性為 xxx-xx 的形式,故只需將該字段串替換為 @yyg-demo-ui/xxx-xx 的形式即可,最後將替換後的結果重新寫入到 package.json。代碼實現如下:

const updatePackageJson = (componentInfo: ComponentInfo) => {
  const { lineName, fullPath, nameWithLib } = componentInfo
  const packageJsonPath = `${fullPath}/package.json`
  if (fs.existsSync(packageJsonPath)) {
    let content = fs.readFileSync(packageJsonPath).toString()
    content = content.replace(lineName, nameWithLib)
    fs.writeFileSync(packageJsonPath, content)
  }
}

創建組件的本體 xxx.vue / xxx.tsx:根據組件類型(.tsx 或 .vue)讀取對應的模板,然後寫入到文件中即可。代碼實現:

const createSrcIndex = (componentInfo: ComponentInfo) => {
  let content = ''
  if (componentInfo.type === 'vue') {
    content = sfcTemplate(componentInfo.lineNameWithPrefix, componentInfo.lowCamelName)
  } else {
    content = tsxTemplate(componentInfo.lineNameWithPrefix, componentInfo.lowCamelName)
  }
  const fileFullName = `${componentInfo.fullPath}/src/${componentInfo.lineName}.${componentInfo.type}`
  fs.writeFileSync(fileFullName, content)
}

這裏引入了 src/util/template-utils.ts 中的兩個生成模板的函數:sfcTemplate 和 tsxTemplate,在後面會提供。

創建 src/types.ts 文件:調用 template-utils.ts 中的函數 typesTemplate 得到模板,再寫入文件。代碼實現:

const createSrcTypes = (componentInfo: ComponentInfo) => {
  const content = typesTemplate(componentInfo.lowCamelName, componentInfo.upCamelName)
  const fileFullName = `${componentInfo.fullPath}/src/types.ts`
  fs.writeFileSync(fileFullName, content)
}

創建 index.ts:同上,調用 template-utils.ts 中的函數 indexTemplate 得到模板再寫入文件。代碼實現:

const createIndex = (componentInfo: ComponentInfo) => {
  fs.writeFileSync(`${componentInfo.fullPath}/index.ts`, indexTemplate(componentInfo))
}

init-component.ts 引入的內容如下:

import { ComponentInfo } from '../domain/component-info'
import fs from 'fs'
import * as path from 'path'
import { indexTemplate, sfcTemplate, tsxTemplate, typesTemplate } from '../util/template-utils'
import { g } from '../util/log-utils'
import { execCmd } from '../util/cmd-utils'
import { Config } from '../config'

3.3 template-utils.ts

init-component.ts 中引入了 template-utils.ts 的四個函數:indexTemplatesfcTemplatetsxTemplatetypesTemplate,實現如下:

import { ComponentInfo } from '../domain/component-info'

/**
 * .vue 文件模板
 */
export const sfcTemplate = (lineNameWithPrefix: string, lowCamelName: string): string => {
  return `<template>
  <div>
    ${lineNameWithPrefix}
  </div>
</template>

<script lang="ts" setup name="${lineNameWithPrefix}">
import { defineProps } from 'vue'
import { ${lowCamelName}Props } from './types'

defineProps(${lowCamelName}Props)
</script>

<style scoped lang="scss">
.${lineNameWithPrefix} {
}
</style>
`
}

/**
 * .tsx 文件模板
 */
export const tsxTemplate = (lineNameWithPrefix: string, lowCamelName: string): string => {
  return `import { defineComponent } from 'vue'
import { ${lowCamelName}Props } from './types'

const NAME = '${lineNameWithPrefix}'

export default defineComponent({
  name: NAME,
  props: ${lowCamelName}Props,
  setup (props, context) {
    console.log(props, context)
    return () => (
      <div class={NAME}>
        <div>
          ${lineNameWithPrefix}
        </div>
      </div>
    )
  }
})
`
}

/**
 * types.ts 文件模板
 */
export const typesTemplate = (lowCamelName: string, upCamelName: string): string => {
  return `import { ExtractPropTypes } from 'vue'

export const ${lowCamelName}Props = {
} as const

export type ${upCamelName}Props = ExtractPropTypes<typeof ${lowCamelName}Props>
`
}

/**
 * 組件入口 index.ts 文件模板
 */
export const indexTemplate = (componentInfo: ComponentInfo): string => {
  const { upCamelName, lineName, lineNameWithPrefix, type } = componentInfo

  return `import ${upCamelName} from './src/${type === 'tsx' ? lineName : lineName + '.' + type}'
import { App } from 'vue'
${type === 'vue' ? `\n${upCamelName}.name = '${lineNameWithPrefix}'\n` : ''}
${upCamelName}.install = (app: App): void => {
  // 註冊組件
  app.component(${upCamelName}.name, ${upCamelName})
}

export default ${upCamelName}
`
}

這樣便實現了新組件模塊的創建,下一篇文章將分享其餘的三個步驟,並在 createNewComponent 函數中調用。

user avatar laughingzhu 頭像 jidongdehai_co4lxh 頭像 weirdo_5f6c401c6cc86 頭像 pugongyingxiangyanghua 頭像 waweb 頭像 huanjinliu 頭像 warn 頭像 mmmy_a 頭像 caideheirenyagao 頭像 xiaojt 頭像 niaonao 頭像 user_kim 頭像
20 位用戶收藏了這個故事!

發佈 評論

Some HTML is okay.