成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

通過npm或yarn自動(dòng)生成vue組件

masturbator / 2210人閱讀

摘要:不知道大家每次新建組件的時(shí)候,是不是都要?jiǎng)?chuàng)建一個(gè)目錄,然后新增一個(gè)文件,然后寫這些東西,如果是公共組件,是不是還要新建一個(gè)用來(lái)導(dǎo)出組件雖然有有代碼片段能實(shí)現(xiàn)自動(dòng)補(bǔ)全,但還是很麻煩,今天靈活運(yùn)用工作流,自動(dòng)生成文件和目錄。

不知道大家每次新建組件的時(shí)候,是不是都要?jiǎng)?chuàng)建一個(gè)目錄,然后新增一個(gè).vue文件,然后寫template、script、style這些東西,如果是公共組件,是不是還要新建一個(gè)index.js用來(lái)導(dǎo)出vue組件、雖然有vscode有代碼片段能實(shí)現(xiàn)自動(dòng)補(bǔ)全,但還是很麻煩,今天靈活運(yùn)用scripts工作流,自動(dòng)生成vue文件和目錄。

實(shí)踐步驟

安裝一下chalk,這個(gè)插件能讓我們的控制臺(tái)輸出語(yǔ)句有各種顏色區(qū)分
npm install chalk --save-dev
yarn add chalk --save-dev

在根目錄中創(chuàng)建一個(gè) scripts 文件夾

新增一個(gè)generateComponent.js文件,放置生成組件的代碼

新增一個(gè)template.js文件,放置組件模板的代碼

template.js文件,里面的內(nèi)容可以自己自定義,符合當(dāng)前項(xiàng)目的模板即可

// template.js
module.exports = {
  vueTemplate: compoenntName => {
    return `





`
  },
  entryTemplate: `import Main from "./main.vue"
export default Main`
}

generateComponent.js生成vue目錄和文件的代碼

// generateComponent.js`
const chalk = require("chalk") // 控制臺(tái)打印彩色
const path = require("path")
const fs = require("fs")
const resolve = (...file) => path.resolve(__dirname, ...file)
const log = message => console.log(chalk.green(`${message}`))
const successLog = message => console.log(chalk.blue(`${message}`))
const errorLog = error => console.log(chalk.red(`${error}`))
const { vueTemplate, entryTemplate } = require("./template")
const _ = process.argv.splice(2)[0] === "-com"

const generateFile = (path, data) => {
  if (fs.existsSync(path)) {
    errorLog(`${path}文件已存在`)
    return
  }
  return new Promise((resolve, reject) => {
    fs.writeFile(path, data, "utf8", err => {
      if (err) {
        errorLog(err.message)
        reject(err)
      } else {
        resolve(true)
      }
    })
  })
}

// 公共組件目錄src/base,全局注冊(cè)組件目錄src/base/global,頁(yè)面組件目錄src/components
_ ? log("請(qǐng)輸入要生成的組件名稱、如需生成全局組件,請(qǐng)加 global/ 前綴") : log("請(qǐng)輸入要生成的頁(yè)面組件名稱、會(huì)生成在 components/目錄下")
let componentName = ""
process.stdin.on("data", async chunk => {
  const inputName = String(chunk).trim().toString()

  // 根據(jù)不同類型組件分別處理
  if (_) {
    // 組件目錄路徑
    const componentDirectory = resolve("../src/base", inputName)
    // vue組件路徑
    const componentVueName = resolve(componentDirectory, "main.vue")
    // 入口文件路徑
    const entryComponentName = resolve(componentDirectory, "index.js")

    const hasComponentDirectory = fs.existsSync(componentDirectory)
    if (hasComponentDirectory) {
      errorLog(`${inputName}組件目錄已存在,請(qǐng)重新輸入`)
      return
    } else {
      log(`正在生成 component 目錄 ${componentDirectory}`)
      await dotExistDirectoryCreate(componentDirectory)
    }

    try {
      if (inputName.includes("/")) {
        const inputArr = inputName.split("/")
        componentName = inputArr[inputArr.length - 1]
      } else {
        componentName = inputName
      }
      log(`正在生成 vue 文件 ${componentVueName}`)
      await generateFile(componentVueName, vueTemplate(componentName))
      log(`正在生成 entry 文件 ${entryComponentName}`)
      await generateFile(entryComponentName, entryTemplate)
      successLog("生成成功")
    } catch (e) {
      errorLog(e.message)
    }
  } else {
    const inputArr = inputName.split("/")
    const directory = inputArr[0]
    let componentName = inputArr[inputArr.length - 1]

    // 頁(yè)面組件目錄
    const componentDirectory = resolve("../src/components", directory)

    // vue組件
    const componentVueName = resolve(componentDirectory, `${componentName}.vue`)

    const hasComponentDirectory = fs.existsSync(componentDirectory)
    if (hasComponentDirectory) {
      log(`${componentDirectory}組件目錄已存在,直接生成vue文件`)
    } else {
      log(`正在生成 component 目錄 ${componentDirectory}`)
      await dotExistDirectoryCreate(componentDirectory)
    }

    try {
      log(`正在生成 vue 文件 ${componentName}`)
      await generateFile(componentVueName, vueTemplate(componentName))
      successLog("生成成功")
    } catch (e) {
      errorLog(e.message)
    }
  }

  process.stdin.emit("end")
})

process.stdin.on("end", () => {
  log("exit")
  process.exit()
})

function dotExistDirectoryCreate (directory) {
  return new Promise((resolve) => {
    mkdirs(directory, function () {
      resolve(true)
    })
  })
}

// 遞歸創(chuàng)建目錄
function mkdirs (directory, callback) {
  var exists = fs.existsSync(directory)
  if (exists) {
    callback()
  } else {
    mkdirs(path.dirname(directory), function () {
      fs.mkdirSync(directory)
      callback()
    })
  }
}

配置package.json,scripts新增兩行命令,其中-com是為了區(qū)別是創(chuàng)建頁(yè)面組件還是公共組件

"scripts": {
    "new:view":"node scripts/generateComponent",
    "new:com": "node scripts/generateComponent -com"
  },

執(zhí)行

    npm run new:view // 生成頁(yè)組件
    npm run new:com // 生成基礎(chǔ)組件
    或者
    yarn run new:view // 生成頁(yè)組件
    yarn run new:com // 生成基礎(chǔ)組件

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/101598.html

相關(guān)文章

  • vue-cli3 搭建的前端項(xiàng)目基礎(chǔ)模板

    摘要:基于搭建的前端模板,本倉(cāng)庫(kù),即可搭建完成一個(gè)新項(xiàng)目的基礎(chǔ)模板,源碼地址,歡迎或特性預(yù)編譯語(yǔ)言,做了一定的封裝,詳見雪碧圖移動(dòng)的適配方案引入了及,可以自由地用去開發(fā)常用的工具類引用全局注入相關(guān)的文件,如通用的及等常用的的集合支持 基于 vue-cli3 搭建的前端模板,clone 本倉(cāng)庫(kù),即可搭建完成一個(gè)新項(xiàng)目的基礎(chǔ)模板,源碼地址,歡迎 star 或 fork 特性 CSS 預(yù)編譯語(yǔ)言...

    william 評(píng)論0 收藏0
  • Vue現(xiàn)有項(xiàng)目改造為Nuxt項(xiàng)目

    摘要:好了,項(xiàng)目啟動(dòng)了,目錄結(jié)構(gòu)也清楚了,接下來(lái)就是整個(gè)現(xiàn)有項(xiàng)目的遷移了目前正在改造項(xiàng)目,文章尚未寫完,會(huì)抽時(shí)間不定期的繼續(xù)更新項(xiàng)目的改造過程及分享改造過程中遇到的問題 公司項(xiàng)目,最初只為了實(shí)現(xiàn)前后端分離式開發(fā),直接選擇了vue框架進(jìn)行開發(fā),然而現(xiàn)在項(xiàng)目基本完成了,發(fā)現(xiàn)蜘蛛根本就抓取不到網(wǎng)站數(shù)據(jù),搜索引擎搜出來(lái),都是一片空白沒有數(shù)據(jù),需要對(duì)項(xiàng)目做SEO優(yōu)化。 本人第一次接觸SEO的優(yōu)化,完全...

    Invoker 評(píng)論0 收藏0
  • Vue-Donut——專用于構(gòu)建Vue的UI組件庫(kù)的開發(fā)框架

    摘要:相信不少使用的開發(fā)者和公司都有定制一套屬于自己的組件庫(kù)的需求。針對(duì)這個(gè)問題,我搭建了一個(gè)專門用來(lái)構(gòu)建的組件庫(kù)的開發(fā)框架,以節(jié)省搭建環(huán)境的勞動(dòng)力,專心于組件庫(kù)的開發(fā)。首先我們嘗試了使用的方案,就是把組件庫(kù)直接作為項(xiàng)目的子模塊使用。 showImg(https://segmentfault.com/img/bVNais?w=1226&h=1159); 相信不少使用Vue的開發(fā)者和公司都有定...

    stormgens 評(píng)論0 收藏0
  • ElementUI的構(gòu)建流程

    摘要:下面一步步拆解上述流程。切換至分支檢測(cè)本地和暫存區(qū)是否還有未提交的文件檢測(cè)本地分支是否有誤檢測(cè)本地分支是否落后遠(yuǎn)程分支發(fā)布發(fā)布檢測(cè)到在分支上沒有沖突后,立即執(zhí)行。 背景 最近一直在著手做一個(gè)與業(yè)務(wù)強(qiáng)相關(guān)的組件庫(kù),一直在思考要從哪里下手,怎么來(lái)設(shè)計(jì)這個(gè)組件庫(kù),因?yàn)闃I(yè)務(wù)上一直在使用ElementUI(以下簡(jiǎn)稱Element),于是想?yún)⒖剂艘幌翬lement組件庫(kù)的設(shè)計(jì),看看Element構(gòu)...

    wudengzan 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<