Vite 从零搭建项目
guide
注意下面的 pm 命令是 pnpm 的别名。
pm init ; pm add -D vite; pm add -D vue; pm add -D @vitejs/plugin-vue;
text
1// vue() 让 vite 支持解析 vue3 的语法
2// vite.config.js
3import {defineConfig} from 'vite'
4import vue from '@vitejs/plugin-vue';
5
6export default defineConfig({
7 plugins: [vue()],
8})
9
10vite默认支持 ts
请注意,Vite 仅执行 .ts 文件的转译工作,并不执行 任何类型检查。并假定类型检查已经被你的 IDE 或构建过程处理了。 所以建议安装下面依赖,避免编辑器告警。
- pm add -D typescript
- pm add -D @types/node
否则会提示
path & __dirname 找不到,红色告警。
json5
1{
2 "compilerOptions": {
3 "rootDir": "./",
4 "baseUrl": "./",
5 "target": "ES2020",
6 "module": "esnext",
7 /* 用于选择模块解析策略,有'node'和'classic'两种类型' */
8 "moduleResolution": "node",
9 "esModuleInterop": true,
10 "forceConsistentCasingInFileNames": true,
11 "strict": true,
12 "allowJs": true,
13 /* allowJs设置的值为true或false,用来指定是否允许编译js文件,默认是false,即不编译js文件 */
14 "checkJs": true,
15 /* checkJs的值为true或false,用来指定是否检查和报告js文件中的错误,默认是false */
16 "skipLibCheck": true,
17 "paths": {
18 "@/*": [
19 "src/*"
20 ]
21 },
22 "lib": [
23 "esnext",
24 "dom"
25 ],
26 "include": [
27 "./",
28 "src/**/*",
29 "src/**/*.vue"
30 ],
31 "exclude": [
32 "node_modules"
33 ]
34 }
35}
36安装prettier格式化
- pm add --save-dev --save-exact prettier ;
- node --eval "fs.writeFileSync('.prettierrc','{} ')";
- pnpm exec prettier . --write ; 或者在直接使用 webstorm的插件,直接在格式化时候执行。
prettier有几种写法
- package.json 文件中 key: prettier
- .prettierrc 用 json or yaml
text
1.prettierrc.json, .prettierrc.yml, .prettierrc.yaml, or .prettierrc.json5 file.
2.prettierrc.json、.prettierrc.yml、.prettierrc.yaml 或 .prettierrc.json5- .prettierrc.js, or prettier.config.js
使用 export default 或 module.exports 导出对象的
安装 eslint
text
1 "@typescript-eslint/eslint-plugin": "^5.4.0",
2 "@typescript-eslint/parser": "^5.4.0",
3 "@vue/compiler-sfc": "^3.2.22",
4 "@vue/eslint-config-typescript": "^9.1.0",
5 "typescript-eslint": "0.0.1-alpha.0",
6 "eslint": "^8.2.0",
7 "eslint-plugin-vue": "^8.0.3",
8 "eslint-config-prettier": "^9.1.0",
9 "eslint-plugin-prettier": "^5.2.1",js
1// https://eslint.org/docs/latest/rules/array-bracket-spacing
2module.exports = {
3 extends: [
4 'plugin:vue/vue3-recommended',
5 '@vue/typescript/recommended',
6 'eslint:recommended',
7 'plugin:prettier/recommended',
8 'prettier',
9 ],
10 plugins: [
11 '@typescript-eslint',
12 ],
13 'env': {
14 'node': true,
15 },
16 rules: {
17 'vue/multi-word-component-names': 'off',
18 'no-var': 'error',
19 'vue/no-unused-vars': 'error',
20 '@typescript-eslint/consistent-type-definitions': [
21 'error',
22 'interface',
23 ],
24 '@typescript-eslint/no-unused-vars': 'error', // 使用 ts 未使用变量的规则 比如枚举类型在es中会报错
25 'no-extend-native': 0,
26 'no-new': 0,
27 'no-useless-escape': 0,
28 'no-useless-constructor': 0,
29 'no-trailing-spaces': ['error', { 'skipBlankLines': true }],
30 'space-infix-ops': ['error', { 'int32Hint': false }],
31 'space-before-function-paren': ['error', {
32 'anonymous': 'always',
33 'named': 'always',
34 'asyncArrow': 'always',
35 }],
36 'semi': ['error', 'always'],
37 'comma-dangle': 0,
38 'no-console': 0,
39 'no-debugger': 0,
40 'id-length': 0,
41 'eol-last': 0,
42 'indent': 'off',
43 'object-curly-spacing': ['error', 'always'],
44 'array-bracket-spacing': ['error', 'never'],
45 'arrow-spacing': 'error',
46 'no-multiple-empty-lines': 'error',
47 'no-unused-vars': 'error',
48 'spaced-comment': 'error',
49 // 'quotes': ['error', 'single', { 'allowTemplateLiterals': true }],
50 'no-unreachable': 'error',
51 'keyword-spacing': 'error',
52 'space-before-blocks': 'error',
53 'semi-spacing': 'error',
54 'comma-spacing': 'error',
55 'key-spacing': 'error',
56 'prefer-const': ['error', {
57 'destructuring': 'any',
58 'ignoreReadBeforeAssign': false,
59 }],
60 'no-irregular-whitespace': 2, // 不规则的空白不允许
61 'vue/require-default-prop': 'off',
62 },
63};
64eslint不同的版本用法不同
如果 ESLint < v9 中使用 .eslintrc.* 文件配置规则。 另请参阅:https://eslint.org/docs/latest/use/configure/。
text
1module.exports = {
2 extends: [
3 // add more generic rulesets here, such as:
4 // 'eslint:recommended',
5 'plugin:vue/vue3-recommended',
6 // 'plugin:vue/recommended' // Use this if you are using Vue.js 2.x.
7 ],
8 rules: {
9 // override/add rules settings here, such as:
10 // 'vue/no-unused-vars': 'error'
11 }
12}从 V9后面开始,文件就变了
- eslint.config.js
- .eslintrc.js
- eslint.config.cjs
text
1// eslint.config.js
2export default [
3 {
4 rules: {
5 semi: "error",
6 "prefer-const": "error"
7 }
8 }
9];别名配置
告诉 vite 别名 编译不报错
text
1// vite.config.js
2// https://cn.vitejs.dev/config/shared-options.html
3import { defineConfig } from 'vite';
4import vue from '@vitejs/plugin-vue';
5import { resolve } from 'path';
6
7export default defineConfig({
8 plugins: [vue()],
9 resolve: {
10 alias: [
11 {
12 find: '@',
13 replacement: resolve(__dirname, './src'),
14 },
15 ],
16 },
17});
18告诉 ts 语法不报错
text
1{
2 "compilerOptions": {
3 ...
4 "paths": {
5 "@/*": [
6 "src/*"
7 ]
8 },
9 ...
10 }
11}
12