vue3+ts項目之安裝eslint、prettier和sass的詳細過程
創(chuàng)建項目
安裝eslint
yarn add eslint -D
生成配置文件
npx eslint --init








安裝其他插件
yarn add -D eslint-plugin-import eslint-plugin-vue eslint-plugin-node eslint-plugin-prettier eslint-config-prettier eslint-plugin-node @babel/eslint-parser vue-eslint-parser
修改.eslintrc.cjs
module.exports = {
env: {
browser: true,
es2021: true,
node: true,
jest: true,
},
/* 指定如何解析語法 */
parser: "vue-eslint-parser",
parserOptions: {
ecmaVersion: "latest",
parser: "@typescript-eslint/parser",
sourceType: "module",
},
extends: [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"plugin:vue/vue3-essential",
],
overrides: [
{
env: {
node: true,
},
files: [".eslintrc.{js,cjs}"],
parserOptions: {
sourceType: "script",
},
},
],
plugins: ["@typescript-eslint", "vue"],
rules: {
// 參考 https://typescript-eslint.io/
// 禁止// @ts-ignore
"@typescript-eslint/ban-ts-ignore": "off",
//要求函數(shù)和類方法有顯式返回類型。
"@typescript-eslint/explicit-function-return-type": "off",
//禁用any類型
"@typescript-eslint/no-explicit-any": "off",
//除 import 語句外,不允許使用 require 語句。
"@typescript-eslint/no-var-requires": "off",
//禁止空函數(shù)
"@typescript-eslint/no-empty-function": "off",
//在定義變量之前禁止使用變量。
"@typescript-eslint/no-use-before-define": "off",
//禁止 @ts-<directive> 注釋或要求指令后有描述。
"@typescript-eslint/ban-ts-comment": "off",
//禁止某些類型。
"@typescript-eslint/ban-types": "off",
//禁止使用 ! 進行非空斷言后綴運算符。
"@typescript-eslint/no-non-null-assertion": "off",
//要求導(dǎo)出函數(shù)和類的公共類方法顯式返回和參數(shù)類型。
"@typescript-eslint/explicit-module-boundary-types": "off",
// 參考 https://eslint.vuejs.org/rules/
//強制執(zhí)行自定義事件名稱的特定大小寫
"vue/custom-event-name-casing": "off",
//強制執(zhí)行屬性順序
"vue/attributes-order": "off",
//強制每個組件都應(yīng)位于自己的文件中
"vue/one-component-per-file": "off",
//不允許在標簽的右括號之前換行
"vue/html-closing-bracket-newline": "off",
//強制每行的最大屬性數(shù)
"vue/max-attributes-per-line": "off",
//需要在多行元素的內(nèi)容之前和之后換行
"vue/multiline-html-element-content-newline": "off",
//需要在單行元素的內(nèi)容之前和之后換行
"vue/singleline-html-element-content-newline": "off",
//對模板中的自定義組件強制執(zhí)行屬性命名樣式
"vue/attribute-hyphenation": "off",
//強制執(zhí)行自關(guān)閉風格
"vue/html-self-closing": "off",
//禁止向模板添加多個根節(jié)點
"vue/no-multiple-template-root": "off",
"vue/require-default-prop": "off",
//禁止向自定義組件中使用的 v-model 添加參數(shù)
"vue/no-v-model-argument": "off",
//禁止使用箭頭函數(shù)來定義觀察者
"vue/no-arrow-functions-in-watch": "off",
//禁止 <template> 上的key屬性
"vue/no-template-key": "off",
//禁止使用v-html以防止XSS攻擊
"vue/no-v-html": "off",
//支持 <template> 中的注釋指令
"vue/comment-directive": "off",
//禁止 <template> 中出現(xiàn)解析錯誤
"vue/no-parsing-error": "off",
//禁止使用已棄用的 .native 修飾符(在 Vue.js 3.0.0+ 中)
"vue/no-deprecated-v-on-native-modifier": "off",
//要求組件名稱始終為多個單詞
"vue/multi-word-component-names": "off",
// 參考 http://eslint.cn/docs/rules/
//禁止添加論據(jù)v-model 用于定制組件
"no-v-model-argument": "off",
//禁止使用不必要的轉(zhuǎn)義字符
"no-useless-escape": "off",
//禁止稀疏數(shù)組
"no-sparse-arrays": "off",
//禁止直接在對象上調(diào)用某些 Object.prototype 方法
"no-prototype-builtins": "off",
//禁止條件中的常量表達式
"no-constant-condition": "off",
//在定義變量之前禁止使用變量
"no-use-before-define": "off",
//禁止指定的全局變量
"no-restricted-globals": "off",
//不允許指定的語法
"no-restricted-syntax": "off",
//在生成器函數(shù)中圍繞*運算符強制執(zhí)行一致的間距
"generator-star-spacing": "off",
//不允許在return、throw、continue和break語句之后出現(xiàn)無法訪問的代碼
"no-unreachable": "off",
//vue2只有一個節(jié)點但是vue3支持多個
"no-multiple-template-root": "off",
//該規(guī)則旨在消除未使用的變量,函數(shù)和函數(shù)的參數(shù)。
"no-unused-vars": "error",
//禁止case聲明
"no-case-declarations": "off",
//禁止console
"no-console": "error",
},
};添加.eslintignore
*.sh node_modules lib *.md *.scss *.woff *.ttf .vscode .idea dist mock public bin build config index.html src/assets
安裝prettier
yarn add -D eslint-plugin-prettier prettier eslint-config-prettier
添加.prettierrc.cjs
module.exports = {
// 一行最多多少個字符
printWidth: 150,
// 指定每個縮進級別的空格數(shù)
tabWidth: 2,
// 使用制表符而不是空格縮進行
useTabs: true,
// 在語句末尾打印分號
semi: true,
// 使用單引號而不是雙引號
singleQuote: true,
// 更改引用對象屬性的時間 可選值"<as-needed|consistent|preserve>"
quoteProps: 'as-needed',
// 在JSX中使用單引號而不是雙引號
jsxSingleQuote: false,
// 多行時盡可能打印尾隨逗號。(例如,單行數(shù)組永遠不會出現(xiàn)逗號結(jié)尾。) 可選值"<none|es5|all>",默認none
trailingComma: 'es5',
// 在對象文字中的括號之間打印空格
bracketSpacing: true,
// jsx 標簽的反尖括號需要換行
jsxBracketSameLine: false,
// 在單獨的箭頭函數(shù)參數(shù)周圍包括括號 always:(x) => x \ avoid:x => x
arrowParens: 'always',
// 這兩個選項可用于格式化以給定字符偏移量(分別包括和不包括)開始和結(jié)束的代碼
rangeStart: 0,
rangeEnd: Infinity,
// 指定要使用的解析器,不需要寫文件開頭的 @prettier
requirePragma: false,
// 不需要自動在文件開頭插入 @prettier
insertPragma: false,
// 使用默認的折行標準 always\never\preserve
proseWrap: 'preserve',
// 指定HTML文件的全局空格敏感度 css\strict\ignore
htmlWhitespaceSensitivity: 'css',
// Vue文件腳本和樣式標簽縮進
vueIndentScriptAndStyle: false,
// 換行符使用 lf 結(jié)尾是 可選值"<auto|lf|crlf|cr>"
endOfLine: 'lf',
};添加.prettierignore
/dist/* /html/* .local /node_modules/** **/*.svg **/*.sh /public/*
安裝sass
yarn add sass sass-loader stylelint postcss postcss-scss postcss-html stylelint-config-prettier stylelint-config-recess-order stylelint-config-recommended-scss stylelint-config-standard stylelint-config-standard-vue stylelint-scss stylelint-order stylelint-config-standard-scss -D
配置.stylelintrc.cjs
// @see https://stylelint.bootcss.com/
module.exports = {
extends: [
'stylelint-config-standard', // 配置stylelint拓展插件
'stylelint-config-html/vue', // 配置 vue 中 template 樣式格式化
'stylelint-config-standard-scss', // 配置stylelint scss插件
'stylelint-config-recommended-vue/scss', // 配置 vue 中 scss 樣式格式化
'stylelint-config-recess-order', // 配置stylelint css屬性書寫順序插件,
'stylelint-config-prettier', // 配置stylelint和prettier兼容
],
overrides: [
{
files: ['**/*.(scss|css|vue|html)'],
customSyntax: 'postcss-scss',
},
{
files: ['**/*.(html|vue)'],
customSyntax: 'postcss-html',
},
],
ignoreFiles: [
'**/*.js',
'**/*.jsx',
'**/*.tsx',
'**/*.ts',
'**/*.json',
'**/*.md',
'**/*.yaml',
],
/**
* null => 關(guān)閉該規(guī)則
* always => 必須
*/
rules: {
'value-keyword-case': null, // 在 css 中使用 v-bind,不報錯
'no-descending-specificity': null, // 禁止在具有較高優(yōu)先級的選擇器后出現(xiàn)被其覆蓋的較低優(yōu)先級的選擇器
'function-url-quotes': 'always', // 要求或禁止 URL 的引號 "always(必須加上引號)"|"never(沒有引號)"
'no-empty-source': null, // 關(guān)閉禁止空源碼
'selector-class-pattern': null, // 關(guān)閉強制選擇器類名的格式
'property-no-unknown': null, // 禁止未知的屬性(true 為不允許)
'block-opening-brace-space-before': 'always', //大括號之前必須有一個空格或不能有空白符
'value-no-vendor-prefix': null, // 關(guān)閉 屬性值前綴 --webkit-box
'property-no-vendor-prefix': null, // 關(guān)閉 屬性前綴 -webkit-mask
'selector-pseudo-class-no-unknown': [
// 不允許未知的選擇器
true,
{
ignorePseudoClasses: ['global', 'v-deep', 'deep'], // 忽略屬性,修改element默認樣式的時候能使用到
},
],
},
}配置忽略文件.stylelintignore
/node_modules/* /dist/* /html/* /public/*
package.json增加配置
"format": "prettier --write \"./**/*.{html,vue,ts,js,json,md}\"",
"lint:eslint": "eslint src/**/*.{ts,vue} --cache --fix",
"lint:style": "stylelint src/**/*.{css,scss,vue} --cache --fix",執(zhí)行yarn format會自動格式化css、js、html、json還有markdown代碼
到此這篇關(guān)于vue3+ts項目02-安裝eslint、prettier和sass的文章就介紹到這了,更多相關(guān)vue3 ts安裝eslint、prettier和sass內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue el-date-picker 開始日期不能大于結(jié)束日期的實現(xiàn)代碼
這篇文章主要介紹了vue el-date-picker 開始日期不能大于結(jié)束日期的實現(xiàn)代碼,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧2024-01-01
在Vue中創(chuàng)建可重用的 Transition的方法
這篇文章主要介紹了在Vue中創(chuàng)建可重用的 Transition,本文通過示例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-06-06

