✏️
ougege
  • README
  • Docs
    • index
    • Articles
      • AI
        • 体验Chrome AI
        • 体验Cloudflare Workers AI
        • 体验deepseek
      • CSS
        • CSS优化-PurgeCSS
        • 实用效果
        • 开发常用样式
      • Deepin
        • deepin20安装mysql
        • deepin使用tensorflow入门机器学习
        • deepin安装cuda和cuDNN
        • deepin安装lamp
        • deepin安装nvidia驱动
        • deepin安装oh my Zsh
        • deepin安装p7zip
        • deepin换源
        • 安装deepin系统后要做的事
      • Docker
        • CI/CD搭建配置
        • deepin搭建docker环境
        • docker安装和使用gitlab
        • docker搭建nginx+php环境
      • Essay
        • IOS申请邓白氏编码
        • Markdown-Mermaid
        • Markdown Use
        • webview白屏的问题查找和修复
        • 前端开发对接问题和解决办法汇总
        • 国务院机构改革方案
        • 国家级智库
        • 实用网站推荐
        • 常用Markdown数学公式语法
        • 强烈推荐前端要安装的vscode扩展
        • 新建销售计划-页面卡死问题分析
        • 海淘入坑手册
        • 竞品研究
        • 足球知识速成
      • Git
        • GitBook安装和常用命令
        • GitKraken免费版本
        • Git安装和配置
        • Git异常处理
        • Git Worktree使用
        • 前端工程化相关的实用git命令
      • JS
        • ESM模块导出方式对比
        • Emoji多端统一处理
        • JS发布订阅模式
        • JS性能优化
        • JS标准内置对象
        • JS链式调用原理
        • Promise介绍和使用
        • Range的使用
        • Vue+Oauth登录实现
        • Vue实现富文本插入Emoji
        • chrome扩展入门
        • es5新特性
        • es6常用特性
        • es常用片段
        • uniapp使用eslint校验代码
        • 与移动端通信
        • 优秀js库moment
        • 使用vue-socketio
        • 实现一个中间件
        • 小程序webview调试
        • 常用snippets
        • 常用正则
        • 常用的设计模式
        • 微信jssdk封装使用
        • 浏览器宏任务和微任务
        • 浏览器的5种Observer
        • 深入理解赋值、浅拷贝、深拷贝
        • 解析vue指令clickoutside源码
        • 键盘事件与KeyBoardWrapper交互
        • 高德地图常用方法封装
        • 高阶函数片段
      • Network
        • 使用Lighthouse分析前端性能
        • 前后端启用https
        • 宝塔nginx安装ngx_brotli
        • 比较gz与br加载速度
        • 浏览器https提示不安全
        • 浏览器提示HSTS
        • 简单使用tcpdump
        • 静态资源gzip优化
      • Node
        • CommonJS模块导出方式对比
        • Taro command not found 多平台解决方案
        • koa使用和API实现
        • node安装报错Unexpected-token
        • 使用nvm和nrm
        • 使用uniapp给小程序添加云函数
        • 使用verdaccio搭建本地npm仓库
        • 使用vue-cli搭建vue项目
        • 安装Node.js和npm配置
        • 编译成cjs和mjs的思路解析
        • 让你的npmPackage同时支持cjs和mjs
        • 通过GithubAction将内容部署到vps
      • Python
        • Python源管理
        • Python版本管理
        • mitmproxy抓包
        • 微信公众平台开发爬坑经历
      • Shell
        • Ubuntu安装deepin桌面环境
        • Ubuntu安装flatpak软件
        • Ubuntu安装wireshark
        • Ubuntu常见问题汇总
        • dell G3装系统无法识别第二块硬盘
        • linux下virtualbox用gho还原系统
        • mysql常用命令
        • navicat连接一键集成环境的mysql
        • nginx常用命令
        • pm2常用命令
        • virtualbox虚拟机和宿主机互相复制粘贴
        • vps内资源通过mega快传到本地
        • vps报错temporary failure in name resolution
        • vscode修改文件监控数
        • windows+linux双系统引导修复
        • zsh常用插件和命令
        • 一键搭建ChatGPT web版
        • 使用V2ray,CloudFlare Warp解锁GPT
        • 使用vscode进行java开发
        • 利用zx和SSHKey发布代码到服务器
        • 反爬虫一些方案总结和尝试
        • 安装1Panel
        • 安装Bt面板
        • 安装Ubuntu22.04后要做的事
        • 无显示器linux设置默认分辨率
        • 特别实用的shell命令
        • 解决linux安装xmind缺少依赖报错
      • Standards
        • CSS格式化之stylelint
        • CSS规范
        • HTML规范
        • JS规范
        • commit规范
        • 使用husky+commitlint规范代码提交
        • 使用semantic-release自动管理版本号
        • 命名规范
        • 图片规范
        • 版本编号规范
      • Wall
        • 科学上网-Cloudflare-Pages
        • 科学上网-Cloudflare-Warp
        • 科学上网-Geph
        • 科学上网-RackNerd
        • 科学上网-Slicehosting
        • 科学上网-Surfshark
        • 科学上网-Tor
        • 科学上网-XX-NET
        • 科学上网-heroku
        • 科学上网-shadowsock
        • 科学上网-v2ray使用
        • 科学上网-v2ray搭建
        • 科学上网-浏览器代理
        • 科学上网-让终端走代理
      • Windows
        • SourceTree破解免登录(windows版)
        • git bash交互提示符不工作
        • nexus 7 2013 wifi 刷机
        • tree命令生成文件目录
        • 利用charles抓包app
        • 安装Openssl
        • 安装msi文件报错2503和2502
        • 神器vimium使用说明
        • 自用host
        • 解决win10扩展出来的屏幕模糊
        • 解决安装Adobe Air时发生错误
    • Snippets
      • zsh
        • docker
        • extract
        • git-commit
        • git
        • mysql-macports
        • npm
        • nvm
        • pip
        • pm2
        • systemd
        • ubuntu
        • vscode
Powered by GitBook
On this page
  • 介绍
  • 全局安装vue-cli
  • 项目初始化
  • VUE CLI 4.x安装使用
  • 启动程序
  • 安装使用ESLint
  • Eslint 关闭校验
  • 使用Mocha进行单元测试
  • 使用Nightmare进行功能测试
  • 分析文件依赖
  • 参考

Was this helpful?

  1. Docs
  2. Articles
  3. Node

使用vue-cli搭建vue项目

Previous使用verdaccio搭建本地npm仓库Next安装Node.js和npm配置

Last updated 10 months ago

Was this helpful?

介绍

在工作中,我们常常面临要同时创建和开发多个 vue 项目.于是,产生一个想法,把这些重复的操作流程化,以便于以后 CTRL+C 和 CTRL+V

全局安装vue-cli

建议这里使用 ,切换到 taobao 源会比较快

npm i -g vue-cli

项目初始化

vue init webpack vue-project
# 如下图所示:这里节省时间没有安装ESLint,Unit,Nightwatch
# 由于国内网络,安装photomjs和electron真的很慢,后面我们使用另外方法单独安装
初始化项目

VUE CLI 4.x安装使用

# 卸载旧
npm uninstall vue-cli -g
# 安装新
npm i -g @vue/cli
# 升级
npm update -g @vue/cli
# 创建
vue create hello-world
# help
vue create --help
# 使用图形界面
vue ui

启动程序

# 如果缺少依赖
npm i
# 运行
npm run dev

至此,一个简单的 vue 项目就创建完成了. 接下来我们来锦上添花,增加一些附加功能.

安装使用ESLint

# 安装eslint和规则模块
# 注意:这里指定了10.2.1这个版本(跟随vue-cli推荐版本);是因为之后的版本验证更严格,只使用一次的变量会报错.提示让你用const定义
npm i eslint eslint-config-standard@10.2.1 eslint-friendly-formatter eslint-loader eslint-plugin-import eslint-plugin-node eslint-plugin-promise eslint-plugin-standard eslint-plugin-vue babel-eslint --save-dev
  1. 创建 eslint 配置文件 .eslintrc.js

  2. 创建 .eslintignore

  3. 修改 package.json 文件,增加 Eslint 校验

  4. 修改 webpack.base.conf.js

  5. 最后在 index.js 配置使用 eslint

::: code-group

// https://eslint.org/docs/user-guide/configuring
module.exports = {
    root: true,
    parserOptions: {
      parser: 'babel-eslint'
    },
    env: {
      browser: true,
    },
    extends: [
      // https://github.com/vuejs/eslint-plugin-vue#priority-a-essential-error-prevention
      // consider switching to `plugin:vue/strongly-recommended` or `plugin:vue/recommended` for stricter rules.
      'plugin:vue/essential', 
      // https://github.com/standard/standard/blob/master/docs/RULES-en.md
      'standard'
    ],
    // required to lint *.vue files
    plugins: [
      'vue'
    ],
    // add your custom rules here
    rules: {
      // allow async-await
      'generator-star-spacing': 'off',
      // allow debugger during development
      'no-debugger': process.env.NODE_ENV === 'production' ? 'error' : 'off'
    }
}
/build/
/config/
/dist/
/*.js
{
  ...
  "scripts": {
    "dev": "webpack-dev-server --inline --progress --config build/webpack.dev.conf.js",
    "start": "npm run dev",
    "build": "node build/build.js"
  },
  ...
}
// 增加
const createLintingRule = () => ({
  test: /\.(js|vue)$/,
  loader: 'eslint-loader',
  enforce: 'pre',
  include: [resolve('src'), resolve('test')],
  options: {
    formatter: require('eslint-friendly-formatter'),
    emitWarning: !config.dev.showEslintErrorsInOverlay
  }
})

module.exports = {
  ... // 省略内容
  module: {
    rules: [
      // 注意:下面是扩展运算符
      ...(config.dev.useEslint ? [createLintingRule()] : []),
      {
        test: /\.vue$/,
        loader: 'vue-loader',
        options: vueLoaderConfig
      },
      ... // 省略内容
    ]
  }
  ... // 省略内容
}
// 省略内容
...
// Use Eslint Loader?
// If true, your code will be linted during bundling and
// linting errors and warnings will be shown in the console.
useEslint: true,
// If true, eslint errors and warnings will also be shown in the error overlay
// in the browser.
showEslintErrorsInOverlay: false,

/**
  * Source Maps
  */

// https://webpack.js.org/configuration/devtool/#development
devtool: 'cheap-module-eval-source-map',
...
// 省略内容

:::

Eslint 关闭校验

// 关闭段落校验
/* eslint-disable */
  代码块
/* eslint-enable */

// 关闭当前行校验
一行代码 // eslint-disable-line

// 关闭下一行校验
// eslint-disable-next-line
下一行的代码

// 关闭对单一文件的校验
// 在文件头部加上注释,eslint在校验的时候会跳过后续的代码
/* eslint-disable */

使用Mocha进行单元测试

# 建议全局安装mocha
npm i -g mocha
# 安装断言库chai
npm i chai --save-dev
  1. 查看 add.js 文件源码

  2. 编写一个测试脚本 add.test.js ::: code-group

function add(x, y) {
    return x + y
}
module.exports = add
let add = require('./add.js')
let expect = require('chai').expect
// describe称为"测试套件"
describe('加法函数测试', function() {
  // it 称为"测试用例"
  it('1加1应该等于2', function() {
    // expect是一句断言
    expect(add(1, 1)).to.be.equal(2)
  })
})

:::

mocha 默认运行 test 子目录里面的测试脚本

mocha test

当然,你也可以自定义漂亮的报告格式,例如 mochawesome

npm i -g mochawesome

打开 package.json 文件,改写 scripts 字段的 test 脚本 ::: code-group

// 改成
"scripts": {
  "test": "mocha --recursive ./test/* --reporter mochawesome"
}

:::

运行测试用例

npm test

最后在 mochawesome-report 子目录查看 mocha 常用钩子函数

before(function() {
  // 在本区块的所有测试用例之前执行
})
after(function() {
  // 在本区块的所有测试用例之后执行
})
beforeEach(function() {
  // 在本区块的每个测试用例之前执行
})
afterEach(function() {
  // 在本区块的每个测试用例之后执行
})

使用Nightmare进行功能测试

  1. 安装依赖

    ::: code-group

    # nightmare会先安装electron
    # 为了保证速度,这里使用淘宝的镜像
    $ set ELECTRON_MIRROR=https://npm.taobao.org/mirrors/electron/
    
    # 安装electron和nightmare
    npm i -g electron nightmare
    # nightmare会先安装electron
    # 为了保证速度,这里使用淘宝的镜像
    $ export  ELECTRON_MIRROR=https://npm.taobao.org/mirrors/electron/
    
    # 安装electron和nightmare
    npm i -g electron nightmare
    # nightmare会先安装electron
    # 为了保证速度,这里使用淘宝的镜像
    $ export  ELECTRON_MIRROR=https://npm.taobao.org/mirrors/electron/
    
    # 安装electron和nightmare
    npm i -g electron nightmare

    :::

  2. 然后新建个 demo.js

    ::: code-group

    const Nightmare = require('nightmare')
    const nightmare = Nightmare({ show: true })
    
    nightmare
      .goto('http://www.ckcest.cn/default/es/search?page=kgoes&dbtypecode=type_all&dbid=1000&keyword=')
      .type('#txt_search', '机械')
      .click('#btn_search')
      .wait('.search-result-list')
      .evaluate(getData)
      .end()
      .then(function(res) {
        console.log(res)
      })
      .catch(error => {
        console.error('Search failed:', error)
      })
    
    function getData () {
      let contentHtml = document.getElementById('result-content').innerHTML
      return contentHtml
    }

    :::

  3. 运行程序

node demo.js

当然, nightmare 也可以和 macha 一起使用

const Nightmare = require('nightmare')
let expect = require('chai').expect
describe('测试nightmare', function() {
  it('nightmare使用', function() {
    nightmare = Nightmare({ show: true })
    nightmare
      .goto('http://www.ckcest.cn/default/es/search?page=kgoes&dbtypecode=type_all&dbid=1000&keyword=')
      .type('#txt_search', '机械')
      .click('#btn_search')
      .wait('.search-result-list')
      .evaluate(getData)
      .end()
      .then(res => {
        // expect是一句断言
        expect(res).to.be.equal('浪潮集团有限公司')
      }).catch(error => {
        console.error('Search failed:', error)
      })
    })
})

function getData () {
  let contentHtml = document.getElementById('technicalSupport').innerHTML
  return contentHtml
}

分析文件依赖

# 安装
npm -g install madge

# 使用
madge src/main.js

参考

VUE-CLI使用
jsTraining
ESLint
测试框架 Mocha 实例教程
Nightmare
解决npm安装electron很慢的问题
前端的UI自动化测试
madge
nrm