从零到一搭建:vue3+vite7+antfu+stylelint+githooks,全流程配置,附带源码,集成css变量使用,下载即用

news/2025/10/8 21:23:35/文章来源:https://www.cnblogs.com/qinfenfeifei/p/19130131

@

目录
  • 0 基础环境
    • 0.1 node版本
    • 0.2 包管理器
    • 0.3 vscode插件
  • 1 创建项目——vue官网方式
    • 1.1 创建命令
    • 1.2 初始化git
  • 2 语法检查:antfu组合eslint和prettier
    • 2.1 安装命令
    • 2.2 安装依赖
    • 2.3 在package.json中添加脚本
    • 2.4 修改eslint配置文件
  • 3 语法检查:stylelint
    • 3.1 安装命令
    • 3.2 在package.json中添加脚本
    • 3.3 修改.vscode/settings.json
    • 3.4 创建stylelint配置文件
    • 3.5 创建stylelint忽略文件
  • 4 提交检查:simple-git-hooks
    • 4.1 安装命令
    • 4.2 在package.json中最后添加如下代码
    • 4.3 更新git hooks
    • 4.4 verifyCommit配置(也可使用commitlint)
    • 4.5 安装chalk
  • 5 Lint-staged
    • 5.1 安装命令
    • 5.2 配置文件
    • 5.3 在package.json中配置脚本
  • 6 完毕,测试lint功能,提交lint功能
    • 6.1 提交格式不对
    • 6.2 提交完成
  • 7 在项目中测试css
    • 7.1 在项目中创建css文件
    • 7.2 vue文件中使用

0 基础环境

0.1 node版本

node 最好>20 因为eslint9的需要 本次项目node为22.14.0

0.2 包管理器

这里采用pnpm

0.3 vscode插件

eslint prettier vue-official stylelint

在这里插入图片描述

1 创建项目——vue官网方式

1.1 创建命令

pnpm create vue

在这里插入图片描述

1.2 初始化git

git init
git add .
git commit -m 'first'

2 语法检查:antfu组合eslint和prettier

2.1 安装命令

npx @antfu/eslint-config@latest

在这里插入图片描述

2.2 安装依赖

pnpm  i

2.3 在package.json中添加脚本

"lint": "eslint .",
"lint:fix": "eslint . --fix"

2.4 修改eslint配置文件

import antfu from '@antfu/eslint-config'export default antfu({// 1. 基本配置// type: 'lib', // 表示当前项目是一个库(library),而不是应用(app)// 2. 代码风格配置(Stylistic)stylistic: true, // 或者你可以更加细粒度的设置// 3. 格式化工具formatters: true, // 使用外部格式化工具(如 Prettier)// 4. ts支持typescript: true, // 启用 TypeScript 支持// 5. vue支持// vue: true, // 或者你可以更加细粒度的设置vue: {overrides: {// 强制 Vue 文件的顺序为  <script> -> <template> -> <style>'vue/block-order': ['error',{order: ['script', 'template', 'style'],},],},},// 6. 规则覆盖(Rules)/*** 规则 https://www.wenjiangs.com/docs/eslint,vue规则:https://eslint.vuejs.org/rules/* 主要有如下的设置规则,可以设置字符串也可以设置数字,两者效果一致*/rules: {'eslint-comments/no-unlimited-disable': 'off','ts/no-use-before-define': 'off','style/no-mixed-operators': 'off','no-console': 'warn','ts/no-unused-expressions': 'off','style/max-statements-per-line': 'off','ts/prefer-namespace-keyword': 'off','antfu/top-level-function': 'off','node/prefer-global/process': 'off','ts/consistent-type-definitions': 'off','ts/ban-ts-comment': 'off','vue/singleline-html-element-content-newline': 'off', // vue 标签强制换行// 关闭一些耗时的规则'import/no-cycle': 'off','import/no-deprecated': 'off','import/no-named-as-default': 'off','prefer-promise-reject-errors': 'off',},// 7. 忽略文件(Ignores)// 9x版本 忽略文件这种配置方式 废弃掉eslintignore// 【注意:ignores 是 @antfu/eslint-config 的配置方式,替代了传统的 .eslintignore 文件】ignores: ['*.sh','node_modules','*.md','*.woff','*.ttf','.vscode','.idea','/public','/docs','.husky','.local','/bin','Dockerfile','/dist','/src/libs','src/mock','*.min.js','/*.json',],// 8. 其他配置// 关闭对 JSON 和 YAML 的支持// jsonc: false, // 关闭 JSON 支持// yaml: false, // 关闭 YAML 支持// isInEditor: false, // 保存删除未引入的代码// unocss: true, // unocss 检测&格式化 暂时注释 等配置了unocss再开放为true
})

3 语法检查:stylelint

3.1 安装命令

pnpm i -D postcss postcss-html stylelint stylelint-config-recess-order stylelint-config-standard

3.2 在package.json中添加脚本

"stylelint": "stylelint  \"**/*.{css,scss,less,vue,html}\"",
"stylelint:fix": "stylelint  \"**/*.{css,scss,less,vue,html}\" --fix"

3.3 修改.vscode/settings.json

  // Auto fix"editor.codeActionsOnSave": {"source.fixAll.eslint": "explicit","source.organizeImports": "never","source.fixAll.stylelint": "explicit"},// stylelint配置"stylelint.enable": true,// 关闭编辑器内置样式检查(避免与stylelint冲突)"css.validate": false,"less.validate": false,"scss.validate": false,"stylelint.validate": ["css","less","postcss","scss","sass","vue"],

3.4 创建stylelint配置文件

stylelint.config.js

export default {// 1. 基础配置,继承两个官方推荐的配置// stylelint-config-standard:Stylelint 的标准规则集,涵盖常见的 CSS 规范。// stylelint-config-recess-order:强制 CSS 属性按照特定顺序排列(如 position -> display -> margin 等),提高代码可读性。extends: ['stylelint-config-standard', 'stylelint-config-recess-order'],// 2. 文件解析器overrides: [{files: ['**/*.(html|vue)'],customSyntax: 'postcss-html', // 解析 CSS、HTML、Vue(如 Vue 的 <style> 块) 文件},// 选less可以注释scss// {//   files: ['*.less', '**/*.less'],//   customSyntax: 'postcss-less', // 解析 Less 文件// },// 选sass可以注释上面的less// {//   files: ['*.scss', '**/*.scss'],//   customSyntax: 'postcss-scss', // 解析 SCSS 文件//   rules: {//     'scss/percent-placeholder-pattern': null, // 关闭 SCSS 占位符检查//     'scss/at-mixin-pattern': null, // 关闭 SCSS Mixin 命名检查//   },// },],// 3. 自定义规则rules: {// (1) 忽略某些规则: 这些规则被设为 null,表示不检查,通常是为了兼容项目特殊情况。'media-feature-range-notation': null, // 允许 `min-width: 768px` 或 `width >= 768px`'selector-not-notation': null, // 允许 `:not(.class)` 或 `:not(.class, .other)`'import-notation': null,'function-no-unknown': null, // 允许未知的 CSS 函数(如自定义函数)'selector-class-pattern': null, // 允许任意类名(默认强制 kebab-case)'no-empty-source': null, // 允许空样式文件'no-descending-specificity': null, // 允许低优先级选择器覆盖高优先级'font-family-no-missing-generic-family-keyword': null, // 允许 `font-family: Arial`(不强制加 `sans-serif`)'named-grid-areas-no-invalid': null, //  禁用对 CSS Grid 命名区域有效性的检查。// (2) 特殊伪类/伪元素处理: 允许 Vue 特有的伪类(如 :global)和伪元素(如 ::v-deep),避免误报。'selector-pseudo-class-no-unknown': [true,{ignorePseudoClasses: ['global', 'deep'], // 允许 `:global` 和 `:deep`(Vue Scoped CSS)},],'selector-pseudo-element-no-unknown': [true,{ignorePseudoElements: ['v-deep', ':deep'], // 允许 `::v-deep` 和 `::deep`(Vue 深度选择器)},],// (3) 忽略未知的 @ 规则: 允许 TailwindCSS、SCSS/Less 的特殊语法(如 @apply、@mixin)'at-rule-no-unknown': [true,{ignoreAtRules: [// TailwindCSS'tailwind','apply','variants',// SCSS/Less 控制语句'function','if','each',// SCSS Mixins'include','mixin','extend','use','responsive','screen','return',],},],// (4) 其他重要规则'unit-no-unknown': [true, { ignoreUnits: ['rpx'] }], // 允许微信小程序的 `rpx` 单位'rule-empty-line-before': [// 规则前空行(注释后除外)'always',{ignore: ['after-comment', 'first-nested'],},],'property-no-vendor-prefix': [ // 允许 `-webkit-` 前缀true,{ignoreProperties: ['background-clip'],},],},// 4. 忽略文件: 不检查 .js、.jsx、.tsx、.ts 文件(Stylelint 只处理样式文件)ignoreFiles: ['**/*.js', '**/*.jsx', '**/*.tsx', '**/*.ts'],
}

3.5 创建stylelint忽略文件

.stylelintignore

/dist/*
/public/*node_modules
.husky
.vscode
.idea
*.sh
*.mdstats.html

4 提交检查:simple-git-hooks

4.1 安装命令

pnpm i simple-git-hooks -D

4.2 在package.json中最后添加如下代码

"simple-git-hooks": {"pre-commit": "pnpm lint-staged","commit-msg": "node scripts/verifyCommit.js"
}

4.3 更新git hooks

npx simple-git-hooks

4.4 verifyCommit配置(也可使用commitlint)

scripts/verifyCommit.js

// @ts-check
import { readFileSync } from 'node:fs'
import path from 'node:path'
import chalk from 'chalk'const msgPath = path.resolve('.git/COMMIT_EDITMSG')
const msg = readFileSync(msgPath, 'utf-8').trim()const commitRE// eslint-disable-next-line regexp/no-unused-capturing-group= /^(revert: )?(feat|fix|docs|dx|style|refactor|perf|test|workflow|build|ci|chore|types|wip|release)(\(.+\))?: .{1,50}/chalk.level = 1
if (!commitRE.test(msg)) {console.error(`  ${chalk.bgRed.white(' ERROR ')} ${chalk.red('invalid commit message format.',)}\n\n${chalk.red('  Proper commit message format is required for automated changelog generation. Examples:\n\n',)}    ${chalk.green('feat(compiler): add \'comments\' option')}\n`+ `    ${chalk.green('fix(v-model): handle events on blur (close #28)',)}\n\n${chalk.red('  See .github/commit-convention.md for more details.\n')}`,)process.exit(1)
}

4.5 安装chalk

pnpm add chalk -D

5 Lint-staged

5.1 安装命令

pnpm i lint-staged -D

5.2 配置文件

lint-staged.config.js

/**  @type {import('lint-staged').Config} */
export default {'*.{js,jsx,ts,tsx}': ['eslint --fix'],'*.json': ['eslint --fix'],'*.{scss,less,styl,html,vue}': ['eslint --fix', 'stylelint --fix --allow-empty-input'],'*.md': ['prettier --write'],
}

5.3 在package.json中配置脚本

"staged:fix": "lint-staged"

6 完毕,测试lint功能,提交lint功能

6.1 提交格式不对

在这里插入图片描述

6.2 提交完成

在这里插入图片描述

7 在项目中测试css

7.1 在项目中创建css文件

styles/variables.css

/* Variables */
:root {--color1: #45f3ff;--color3: #23242a;--color2: #1c1c1c;
}

styles/global.css

/* Global CSS */
* {box-sizing: border-box;padding: 0;margin: 0;
}html,
body {height: 100%;background-color: purple;
}

styles/index.css

/* 引入 css 文件 */
@import './global.css';
@import './variables.css';

main.ts

import './styles/index.css'

7.2 vue文件中使用

App.vue

<script setup lang="ts">
// 动态修改CSS 变量
document.documentElement.style.setProperty('--color1', '#00f')
</script><template><h1>You did it!</h1>
</template><style scoped>
h1 {color: var(--color1);
}
</style>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/931955.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

云梦网如何做网站广东万高建设网站

在程序操作过程中偶尔会出现如标题所示的错误&#xff0c;开始一直不知道怎么回事&#xff0c;后来仔细调试了下&#xff0c;发现是在浏览器可以点击“前进/后退”按钮时&#xff0c;同时按住shift鼠标滚轮滚动会出现这个Bug。 上网查找了下&#xff0c;有些浏览器Shift鼠标滚轮…

网站添加百度搜索哈尔滨优化seo外包公司

文章目录 概要一、Es二、kibana三、dcoker compose管理四、参考 概要 在工作过程中&#xff0c;经常需要测试环境搭建Es环境&#xff0c;本文基于Es V8.12.2来演示如何快速搭建单节点Es和kibana。 服务器默认已按装docker 一、Es 1&#xff1a;拉取镜像 docker pull elast…

晶台光耦在手机PD快充上的应用 - 实践

pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", …

bat批处理脚本文件-获取当前时间的几种方法

前言全局说明获取当前时间的几种方法一、说明 1.1 环境: Windows 7 旗舰版二、方法一 2.1 源码 @echo off@REM 获取当前时间 https://www.cnblogs.com/wutou/p/19130116 SET year=%date:~0,4% SET month=%date:~5,2% S…

二分图最大权完美匹配 KM算法

#include<iostream> #include<cstdio> #include<cmath> #include<cstring> using namespace std; #define LL long long #define N 510 #define INF 1e12 int n,m; int match[N];//右点匹配了…

做一张简单的app网站多钱黔东南建设厅官方网站

对于初学者来说&#xff0c;java中的"\t"和空格总是让人迷惑&#xff0c;其实很简单&#xff0c;通过一个例子就能很快明白&#xff01;比如说打印九九乘法表&#xff01;先来看用空格的效果&#xff01;1 package cn.edu.nwpu.java;23 public class MultiplicationT…

2025.10.8模拟赛

赛时 看了T1,然后差不多想到做法了,但是没想明白是怎么判a_i相等的 后来写二分+哈希,切了,但是由于没有考虑到可以进行合并,60pts 遂开T2,画了一张特别大的图,然后唐完了 发现了在连续的一段下降是最优的,又发…

Python 中的排序排序函数及区别

Python 中的排序排序函数及区别 在 Python 中,常用的排序功能主要通过两种方式实现:sorted() 内置函数和列表的 sort() 方法。它们都能完成排序任务,但在使用场景和特性上有显著区别。 一、两种核心排序工具 1. sor…

房地产网站建设哪家有效果网站优化公司排行

free命令 ​free是指查看当前系统内存的使用情况&#xff0c;它显示系统中剩余及已用的物理内存和交换内存&#xff0c;以及共享内存和被核心使用的缓冲区。 作者 作者&#xff1a;Brian Edmonds。 语法 free [参数] free 命令 -Linux手册页 命令选项及作用 执行令 &am…

wordpress 表说明长沙网站包年优化

awk&#xff08;是一种处理文本文件的应用程序&#xff0c;它依次处理文件的每一行&#xff0c;并读取里面的每一个字段。&#xff09; awk 包含几个特殊的内建变量&#xff08;可直接用&#xff09;如下所示&#xff1a; 1、获取根分区剩余大小 #可以使用df -h命令来查看所有…

岗贝路网站建设产品设计英文

2024年8月7日至9日&#xff0c;一场生物制造领域的盛会将在上海新国际博览中心闪亮登场。那就是2024上海国际合成生物学与绿色生物制造展览会。这场盛会将同期举办2024第13届上海国际生物发酵展&#xff0c;形成了一个强大的“生物军团”&#xff0c;展现出生物科技领域的无限魅…

搭建asp虚拟主机网站wordpress同分类文章

碰到的问题 上传Datafrane格式的数据到数据库 会碰见很多错误 举几个很普遍遇到的问题(主要以SqlServer举例) 这里解释下 将截断字符串或二进制数据 这个是字符长度超过数据库设置的长度 然后还有字符转int失败 或者字符串转换日期/或时间失败 这个是碰到的需要解决的最多的问…

长春餐饮网站建设wordpress页面构造器

分类目录&#xff1a;《自然语言处理从入门到应用》总目录 在本文中&#xff0c;我们将学习如何在LangChain中创建简单的链式连接并添加组件以及运行它。链式连接允许我们将多个组件组合在一起&#xff0c;创建一个统一的应用程序。例如&#xff0c;我们可以创建一个链式连接&a…

RL | 速读 IJCAI 2025 的强化学习论文

速读一下 IJCAI 2025 的 RL 相关论文。目录359 Multi-granularity Knowledge Transfer for Continual Reinforcement Learning - 为持续 RL 而设计的多粒度知识迁移一、 研究背景与核心痛点(The Gap)二、 动机与故事…

Superhumanism

The civilizeds support Superhumanism. But they didnt support the Nazi Germany! Because they dont see the similar. Because they dont see the consequences. A normal human cannot become a superman, but if…

IDM弹窗解决 - -一叶知秋

IDM弹窗解决1、打开任务管理器,结束IDM任务(一定要结束全部的IDM任务)2、在控制面板中,打开 管理工具(有搜索)3、然后打开 本地安全策略4、找到 软件限制策略->其它规则,如果 软件限制策略 下面没有选项,就…

PHP+MySQL开发语言 在线下单订水送水小脚本源码及搭建指南

PHP+MySQL开发语言 在线下单订水送水小脚本源码及搭建指南pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consola…

Sliding Window Algorithm

核心目的 滑动窗口技术的主要目的是优化算法。它能将一个通常需要用两层嵌套循环(时间复杂度为 \(O(N^2)\))才能解决的问题,转化为只需一次单循环(时间复杂度为 \(O(N)\)),从而大大提高代码的执行效率。工作原理…

微信企业号可以做微网站吗宿州建设银行网站

对数值进行调整在Python中对整数和浮点数进行数字计算是很容易的。但是&#xff0c;如果你需要对分数&#xff0c;数组或者日期和时间进行计算&#xff0c;这就会稍微复杂点。对于简单的取整操作&#xff0c;我们可以使用内建的round(value, ndigits)函数就可&#xff0c;举个例…

网站建设所需人员上海如何优化网站

动态通过网络获取json来tabbar图片和文字或其他信息转载于:https://www.cnblogs.com/TheYouth/p/6488843.html