Skip to content

Instantly share code, notes, and snippets.

@Sengulair
Created February 2, 2024 00:38
Show Gist options
  • Save Sengulair/437a07bbcf14f9b07c99943f11244030 to your computer and use it in GitHub Desktop.
Save Sengulair/437a07bbcf14f9b07c99943f11244030 to your computer and use it in GitHub Desktop.
Vue 3.4+ lint+prettier+ts+vite starter
module.exports = {
root: true,
env: {
browser: true,
node: true,
},
parser: 'vue-eslint-parser',
parserOptions: {
parser: '@typescript-eslint/parser',
sourceType: 'module',
},
extends: [
'eslint:recommended',
'plugin:vue/vue3-recommended',
'plugin:prettier/recommended',
'plugin:import/recommended',
'plugin:import/typescript',
],
rules: {
// https://github.com/import-js/eslint-plugin-import/blob/HEAD/docs/rules/order.md
'import/order': [
'error',
{
pathGroups: [
{
pattern: '@/**',
group: 'internal',
},
],
'newlines-between': 'always',
groups: ['builtin', 'external', 'internal', 'index', 'sibling', 'parent', 'object', 'type'],
},
],
// https://github.com/import-js/eslint-plugin-import/blob/HEAD/docs/rules/newline-after-import.md
'import/newline-after-import': [
'error',
{
count: 1,
},
],
/* Disable some repeated and low performance rules https://github.com/typescript-eslint/typescript-eslint/blob/f335c504bcf75623d2d671e2e784b047e5e186b9/docs/getting-started/linting/FAQ.md#eslint-plugin-import */
'import/named': 'off',
'import/namespace': 'off',
'import/default': 'off',
'import/no-named-as-default-member': 'off',
'import/no-unused-modules': 'off',
'import/no-named-as-default': 'off',
'import/no-cycle': 'off',
'import/no-deprecated': 'off',
// https://eslint.vuejs.org/rules/padding-line-between-blocks.html#vue-padding-line-between-blocks
'vue/padding-line-between-blocks': ['error', 'always'],
// https://eslint.vuejs.org/rules/html-self-closing.html#vue-html-self-closing
'vue/html-self-closing': [
'error',
{
html: {
void: 'always',
normal: 'never',
component: 'always',
},
svg: 'always',
math: 'always',
},
],
// https://eslint.vuejs.org/rules/block-lang.html#vue-block-lang
'vue/block-lang': [
'error',
{
script: {
lang: 'ts',
},
},
],
// https://eslint.vuejs.org/rules/new-line-between-multi-line-property.html#vue-new-line-between-multi-line-property
'vue/new-line-between-multi-line-property': [
'error',
{
minLineOfMultilineProperty: 2,
},
],
// https://eslint.vuejs.org/rules/no-undef-properties.html#vue-no-undef-properties
'vue/no-undef-properties': [
'error',
{
ignores: ['/^\\$/'],
},
],
// https://eslint.vuejs.org/rules/no-useless-v-bind.html#vue-no-useless-v-bind
'vue/no-useless-v-bind': [
'error',
{
ignoreIncludesComment: true,
ignoreStringEscape: true,
},
],
// https://eslint.vuejs.org/rules/prefer-true-attribute-shorthand.html#vue-prefer-true-attribute-shorthand
'vue/prefer-true-attribute-shorthand': ['error', 'always'],
// https://eslint.vuejs.org/rules/no-unused-properties.html#vue-no-unused-properties
'vue/no-unused-properties': [
'error',
{
groups: ['props', 'data', 'computed', 'methods', 'setup'],
deepData: false,
ignorePublicMembers: true,
},
],
// https://eslint.vuejs.org/rules/component-name-in-template-casing.html#vue-component-name-in-template-casing
'vue/component-name-in-template-casing': [
'error',
'PascalCase',
{
registeredComponentsOnly: false,
ignores: ['/^component|client-only|keep-alive|router-view|router-link|i18n$/'],
},
],
// https://eslint.vuejs.org/rules/component-api-style.html
'vue/component-api-style': [
'error',
['script-setup', 'composition'], // "script-setup", "composition", "composition-vue2", or "options"
],
// https://eslint.vuejs.org/rules/custom-event-name-casing.html
'vue/custom-event-name-casing': [
'error',
'kebab-case', // 'camelCase' | 'kebab-case'
{
ignores: [],
},
],
// https://eslint.vuejs.org/rules/define-emits-declaration#vue-define-emits-declaration
'vue/define-emits-declaration': ['error', 'type-literal'],
// https://eslint.vuejs.org/rules/match-component-file-name.html
'vue/match-component-file-name': [
'error',
{
extensions: ['vue'],
shouldMatchCase: true,
},
],
// https://eslint.vuejs.org/rules/next-tick-style.html
'vue/next-tick-style': ['error', 'promise'],
// https://eslint.vuejs.org/rules/no-duplicate-attr-inheritance.html
'vue/no-duplicate-attr-inheritance': ['error'],
// https://eslint.vuejs.org/rules/no-required-prop-with-default.html
'vue/no-required-prop-with-default': [
'error',
{
autofix: false,
},
],
// https://eslint.vuejs.org/rules/no-useless-mustaches.html#vue-no-useless-mustaches
'vue/no-useless-mustaches': [
'error',
{
ignoreIncludesComment: false,
ignoreStringEscape: true,
},
],
// https://eslint.vuejs.org/rules/prefer-define-options.html
'vue/prefer-define-options': ['error'],
// https://eslint.vuejs.org/rules/require-macro-variable-name.html
'vue/require-macro-variable-name': [
'error',
{
defineProps: 'props',
defineEmits: 'emit',
defineSlots: 'slots',
useSlots: 'slots',
useAttrs: 'attrs',
},
],
// https://eslint.vuejs.org/rules/valid-define-options.html
'vue/valid-define-options': ['error'],
/*
* Below are rules that is more depends on personal preference
* and may not be suitable for all projects
*/
// https://eslint.vuejs.org/rules/component-tags-order.html#vue-component-tags-order
'vue/block-order': [
'error',
{
order: ['template', 'script', 'style'],
},
],
// https://eslint.vuejs.org/rules/require-emit-validator.html#vue-require-emit-validator
'vue/require-emit-validator': ['error'],
// https://eslint.vuejs.org/rules/v-on-handler-style.html
'vue/v-on-handler-style': [
'error',
['method', 'inline-function'], // ["method", "inline-function"] | ["method", "inline"] | "inline-function" | "inline"
{
ignoreIncludesComment: false,
},
],
// https://eslint.vuejs.org/rules/no-ref-object-reactivity-loss.html
'vue/no-ref-object-reactivity-loss': ['error'],
// https://eslint.vuejs.org/rules/define-macros-order.html
'vue/define-macros-order': [
'error',
{
order: ['defineOptions', 'defineModel', 'defineProps', 'defineEmits', 'defineSlots'],
defineExposeLast: true,
},
],
// https://eslint.vuejs.org/rules/define-props-declaration.html
'vue/define-props-declaration': ['error', 'type-based'],
// https://eslint.vuejs.org/rules/match-component-import-name.html
'vue/match-component-import-name': ['error'],
// https://eslint.vuejs.org/rules/no-static-inline-styles.html
'vue/no-static-inline-styles': [
'error',
{
allowBinding: false,
},
],
},
};
{
"arrowParens": "always",
"semi": true,
"useTabs": false,
"tabWidth": 2,
"bracketSpacing": true,
"singleQuote": true,
"printWidth": 120,
"endOfLine": "lf",
"trailingComma": "all"
}

Vue 3 + TypeScript + Vite

This template should help get you started developing with Vue 3 and TypeScript in Vite. The template uses Vue 3 <script setup> SFCs, check out the script setup docs to learn more.

Recommended IDE Setup

Type Support For .vue Imports in TS

TypeScript cannot handle type information for .vue imports by default, so we replace the tsc CLI with vue-tsc for type checking. In editors, we need TypeScript Vue Plugin (Volar) to make the TypeScript language service aware of .vue types.

If the standalone TypeScript plugin doesn't feel fast enough to you, Volar has also implemented a Take Over Mode that is more performant. You can enable it by the following steps:

  1. Disable the built-in TypeScript Extension

    1. Run Extensions: Show Built-in Extensions from VSCode's command palette
    2. Find TypeScript and JavaScript Language Features, right click and select Disable (Workspace)
  2. Reload the VSCode window by running Developer: Reload Window from the command palette.

  3. Install template with vite: npm create vite@latest my-vue-app -- --template vue

  4. Update deps

  5. npm i -D eslint eslint-config-prettier eslint-plugin-import eslint-plugin-prettier eslint-plugin-vue prettier @typescript-eslint/parser

  6. add .eslintrc.cjs and .prettierrc and content from gist

  7. update VSCode settings.json in local folder according gist

{
"name": "template-lint-rules",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "vue-tsc && vite build",
"typecheck": "vue-tsc --noEmit",
"lint": "eslint --ext .js,.ts,.vue .",
"preview": "vite preview"
},
"dependencies": {
"vue": "^3.4.15"
},
"devDependencies": {
"@typescript-eslint/parser": "^6.20.0",
"@vitejs/plugin-vue": "^5.0.3",
"eslint": "^8.56.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-import": "^2.29.1",
"eslint-plugin-prettier": "^5.1.3",
"eslint-plugin-vue": "^9.21.1",
"prettier": "^3.2.4",
"typescript": "^5.3.3",
"vite": "^5.0.12",
"vue-tsc": "^1.8.27"
}
}
{
"typescript.tsdk": "node_modules/typescript/lib",
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment