vue3+ts+uniapp+vite+pinia项目配置

开发环境: node >=18,npm >=8.10.2,vue <= 3.2.31

安装项目

npx degit dcloudio/uni-preset-vue#vite-ts vue3-uniapp

1、引入样式规范

npm add -D eslint eslint-config-airbnb-base eslint-config-prettier eslint-import-resolver-typescript eslint-plugin-import eslint-plugin-prettier eslint-plugin-vue @typescript-eslint/eslint-plugin @typescript-eslint/parser vue-global-api 
  1.  .editorconfig

    # .editorconfig 文件
    root = true
    
    [*] # 表示所有文件适用
    charset = utf-8 # 设置文件字符集为 utf-8
    indent_style = space # 缩进风格(tab | space)
    indent_size = 2 # 缩进大小
    end_of_line = lf # 控制换行类型(lf | cr | crlf)
    trim_trailing_whitespace = true # 去除行首的任意空白字符
    insert_final_newline = true # 始终在文件末尾插入一个新行
    
    [*.md] # 表示仅 md 文件适用以下规则
    max_line_length = off # 关闭最大行长度限制
    trim_trailing_whitespace = false # 关闭末尾空格修剪
  2.  .prettierrc.cjs
    // @see https://prettier.io/docs/en/options
    module.exports = {
      singleQuote: true,
      printWidth: 100,
      tabWidth: 2,
      useTabs: false,
      semi: false,
      trailingComma: 'all',
      endOfLine: 'auto',
    }
  3.  .eslintrc.cjs
    // .eslintrc.cjs 文件
    module.exports = {
      env: {
        browser: true,
        es2021: true,
        node: true,
      },
      extends: [
        'eslint:recommended',
        'plugin:@typescript-eslint/recommended',
        'plugin:vue/vue3-essential',
        // eslint-plugin-import 插件, @see https://www.npmjs.com/package/eslint-plugin-import
        'plugin:import/recommended',
        // eslint-config-airbnb-base 插件, tips: 本插件也可以替换成 eslint-config-standard
        'airbnb-base',
        // 1. 接入 prettier 的规则
        'prettier',
        'plugin:prettier/recommended',
        'vue-global-api',
      ],
      overrides: [
        {
          env: {
            node: true,
          },
          files: ['.eslintrc.{js,cjs}'],
          parserOptions: {
            sourceType: 'script',
          },
        },
      ],
      parserOptions: {
        ecmaVersion: 'latest',
        parser: '@typescript-eslint/parser',
        sourceType: 'module',
      },
      plugins: [
        '@typescript-eslint',
        'vue',
        // 2. 加入 prettier 的 eslint 插件
        'prettier',
        // eslint-import-resolver-typescript 插件,@see https://www.npmjs.com/package/eslint-import-resolver-typescript
        'import',
      ],
      rules: {
        'vue/multi-word-component-names': 'off',
        // 3. 注意要加上这一句,开启 prettier 自动修复的功能
        'prettier/prettier': 'error',
        // turn on errors for missing imports
        'import/no-unresolved': 'off',
        // 对后缀的检测,否则 import 一个ts文件也会报错,需要手动添加'.ts', 增加了下面的配置后就不用了
        'import/extensions': [
          'error',
          'ignorePackages',
          { js: 'never', jsx: 'never', ts: 'never', tsx: 'never' },
        ],
        // 只允许1个默认导出,关闭,否则不能随意export xxx
        'import/prefer-default-export': ['off'],
        'no-console': ['off'],
        // 'no-unused-vars': ['off'],
        // '@typescript-eslint/no-unused-vars': ['off'],
        // 解决vite.config.ts报错问题
        'import/no-extraneous-dependencies': 'off',
        'no-plusplus': 'off',
        'no-shadow': 'off',
      },
      // eslint-import-resolver-typescript 插件,@see https://www.npmjs.com/package/eslint-import-resolver-typescript
      settings: {
        'import/parsers': {
          '@typescript-eslint/parser': ['.ts', '.tsx'],
        },
        'import/resolver': {
          typescript: {},
        },
      },
    }
  4.  .stylelintrc.cjs
    npm add -D stylelint stylelint-config-html stylelint-config-recess-order stylelint-config-recommended-vue stylelint-config-standard stylelint-config-standard-scss postcss postcss-html postcss-scss sass
    module.exports = {
      root: true,
      extends: [
        'stylelint-config-standard',
        'stylelint-config-standard-scss', // tips: 本插件也可以替换成 stylelint-config-recommended-scss
        'stylelint-config-recommended-vue/scss',
        'stylelint-config-html/vue',
        'stylelint-config-recess-order',
      ],
      overrides: [
        // 扫描 .vue/html 文件中的<style>标签内的样式
        {
          files: ['**/*.{vue,html}'],
          customSyntax: 'postcss-html',
        },
        {
          files: ['**/*.{css,scss}'],
          customSyntax: 'postcss-scss',
        },
      ],
      // 自定义规则
      rules: {
        // 允许 global 、export 、v-deep等伪类
        'selector-pseudo-class-no-unknown': [
          true,
          {
            ignorePseudoClasses: ['global', 'export', 'v-deep', 'deep'],
          },
        ],
        'unit-no-unknown': [
          true,
          {
            ignoreUnits: ['rpx'],
          },
        ],
      },
    }

2、引入 husky + lint-staged + commitlint

  1. 先安装git   在终端输入命令  git init        
  2. npm i -D husky lint-staged commitlint @commitlint/cli @commitlint/config-conventional
  3. npx husky install
  4. 在 package.json的scripts里面增加 "prepare": "husky install"
  5. package.json  
    "lint-staged": {
      "**/*.{html,vue,ts,cjs,json,md}": [
        "prettier --write"
      ],
      "**/*.{vue,js,ts,jsx,tsx}": [
        "eslint --fix"
      ],
      "**/*.{vue,css,scss,html}": [
        "stylelint --fix"
      ]
    },
    // commitlint.config.cjs 文件
    module.exports = {
      extends: ['@commitlint/config-conventional'],
    }
    npx husky add .husky/pre-commit "npx --no-install -- lint-staged"
    npx husky add .husky/commit-msg "npx --no-install commitlint --edit $1"

 3、vite.config.ts 优化 

  1. vite.config.ts
  2. import path from 'node:path'
    import { defineConfig, loadEnv } from 'vite'
    import uni from '@dcloudio/vite-plugin-uni'
    import dayjs from 'dayjs'
    import vue from '@vitejs/plugin-vue'
    import svgLoader from 'vite-svg-loader'
    import { visualizer } from 'rollup-plugin-visualizer'
    import ViteRestart from 'vite-plugin-restart'
    import Components from 'unplugin-vue-components/vite'
    // ElementPlusResolver,
    // AntDesignVueResolver,
    // VantResolver,
    // HeadlessUiResolver,
    // ElementUiResolver
    import {} from 'unplugin-vue-components/resolvers'
    import AutoImport from 'unplugin-auto-import/vite'
    import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
    import viteCompression from 'vite-plugin-compression'
    import viteImagemin from 'vite-plugin-imagemin'
    import vueSetupExtend from 'vite-plugin-vue-setup-extend'
    import UnoCSS from 'unocss/vite'
    import autoprefixer from 'autoprefixer'
    
    const htmlPlugin = () => {
      return {
        name: 'html-transform',
        transformIndexHtml(html) {
          return html.replace('%BUILD_DATE%', dayjs().format('YYYY-MM-DD HH:mm:ss'))
        },
      }
    }
    
    // https://vitejs.dev/config/
    export default ({ mode }) => {
      // mode: 区分生产环境还是开发环境
      // process.cwd(): 获取当前文件的目录跟地址
      // loadEnv(): 返回当前环境env文件中额外定义的变量
      const env = loadEnv(mode, path.resolve(process.cwd(), 'env'))
      console.log(env)
      return defineConfig({
        plugins: [
          uni(),
          UnoCSS(),
          htmlPlugin(),
          svgLoader(),
          // 打包分析插件
          visualizer(),
          ViteRestart({
            // 通过这个插件,在修改vite.config.js文件则不需要重新运行也生效配置
            restart: ['vite.config.js'],
          }),
          vueSetupExtend(),
          // 原先引用组件的时候需要在目标文件里面import相关组件,现在就可以直接使用无需在目标文件import了
          Components({
            dirs: ['src/components'], // 目标文件夹
            extensions: ['vue'], // 文件类型
            dts: 'src/components.d.ts', // 输出文件,里面都是一些import的组件键值对
            // ui库解析器,也可以自定义,需要安装相关UI库
            resolvers: [
              // VantResolver(),
              // ElementPlusResolver(),
              // AntDesignVueResolver(),
              // HeadlessUiResolver(),
              // ElementUiResolver()
            ],
          }),
          AutoImport({
            imports: ['vue'],
            dts: 'src/auto-import.d.ts',
          }),
          createSvgIconsPlugin({
            // 指定要缓存的文件夹
            iconDirs: [path.resolve(process.cwd(), 'src/assets/svg')],
            // 指定symbolId格式
            symbolId: 'icon-[dir]-[name]',
          }),
          viteCompression(), // 会多出一些.gz文件,如xxx.js.gz,这里默认是不会删除xxx.js文件的,如果想删除也可以增加配置
          // 这个图片压缩插件比较耗时,希望仅在生产环境使用
          viteImagemin({
            gifsicle: {
              // gif图片压缩
              optimizationLevel: 3, // 选择1到3之间的优化级别
              interlaced: false, // 隔行扫描gif进行渐进式渲染
              // colors: 2 // 将每个输出GIF中不同颜色的数量减少到num或更少。数字必须介于2和256之间。
            },
            optipng: {
              // png
              optimizationLevel: 7, // 选择0到7之间的优化级别
            },
            mozjpeg: {
              // jpeg
              quality: 20, // 压缩质量,范围从0(最差)到100(最佳)。
            },
            pngquant: {
              // png
              quality: [0.8, 0.9], // Min和max是介于0(最差)到1(最佳)之间的数字,类似于JPEG。达到或超过最高质量所需的最少量的颜色。如果转换导致质量低于最低质量,图像将不会被保存。
              speed: 4, // 压缩速度,1(强力)到11(最快)
            },
            svgo: {
              // svg压缩
              plugins: [
                {
                  name: 'removeViewBox',
                },
                {
                  name: 'removeEmptyAttrs',
                  active: false,
                },
              ],
            },
          }),
        ],
        css: {
          postcss: {
            plugins: [
              autoprefixer({
                // 指定目标浏览器
                overrideBrowserslist: ['> 1%', 'last 2 versions'],
              }),
            ],
          },
        },
    
        resolve: {
          alias: {
            '@': path.join(process.cwd(), './src'),
          },
        },
        server: {
          host: '0.0.0.0',
          hmr: true,
          port: 7001,
          // 自定义代理规则
          proxy: {
            // 选项写法
            '/api': {
              target: 'http://localhost:6666',
              changeOrigin: true,
              rewrite: (path) => path.replace(/^\/api/, ''),
            },
          },
        },
      })
    }
    npm i -D vite-svg-loader rollup-plugin-visualizer vite-plugin-restart unplugin-vue-components unplugin-auto-import vite-plugin-svg-icons vite-plugin-compression vite-plugin-vue-setup-extend unocss autoprefixer

    vite-plugin-imagemin目前无法安装

  3. uno.config.ts

    // uno.config.ts
    import {
      defineConfig,
      presetAttributify,
      presetUno,
      presetIcons,
      transformerDirectives,
      transformerVariantGroup,
    } from 'unocss'
    
    export default defineConfig({
      presets: [
        presetUno(),
        // 支持css class属性化,eg: `<button bg="blue-400 hover:blue-500 dark:blue-500 dark:hover:blue-600" text="sm white">attributify Button</button>`
        presetAttributify(),
        // 支持图标,需要搭配图标库,eg: @iconify-json/carbon, 使用 `<button class="i-carbon-sun dark:i-carbon-moon" />`
        presetIcons({
          scale: 1.2,
          warn: true,
          extraProperties: {
            display: 'inline-block',
            'vertical-align': 'middle',
          },
        }),
      ],
      transformers: [
        transformerDirectives(),
        // 支持css class组合,eg: `<div class="hover:(bg-gray-400 font-medium) font-(light mono)">测试 unocss</div>`
        transformerVariantGroup(),
      ],
    })
  4. main.ts

    import 'virtual:svg-icons-register'
    import 'virtual:uno.css'
  5. tsconfig.json

    {
      "compilerOptions": {
        "sourceMap": true,
        "baseUrl": ".",
        "paths": {
          "@/*": ["./src/*"]
        },
        "lib": ["esnext", "dom"],
        "types": ["@dcloudio/types", "@types/wechat-miniprogram", "@uni-helper/uni-app-types"]
      },
      "vueCompilerOptions": {
        "nativeTags": ["block", "template", "component", "slot"]
      },
      "include": ["src/**/*.ts", "src/**/*.d.ts", "src/**/*.tsx", "src/**/*.vue"],
      "references": [{ "path": "./tsconfig.node.json" }]
    }
  6. .eslintrc.cjs

    globals: {
         uni: true,
    },
    npm i -D @types/wechat-miniprogram @uni-helper/uni-app-types
    npm i -S pinia pinia-plugin-persistedstate
    // src/store/index.ts
    import { createPinia } from 'pinia'
    import { createPersistedState } from 'pinia-plugin-persistedstate' // 数据持久化
    
    const store = createPinia()
    store.use(
      createPersistedState({
        storage: {
          getItem: uni.getStorageSync,
          setItem: uni.setStorageSync,
        },
      }),
    )
    export default store
    
    // src.main.ts
    import { createSSRApp } from 'vue'
    import App from './App.vue'
    import store from './store'
    import 'virtual:svg-icons-register'
    import 'virtual:uno.css'
    
    export function createApp() {
      const app = createSSRApp(App)
      app.use(store)
      return {
        app,
      }
    }

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

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

机器学习与现代医疗设备的结合:革新医疗健康的未来

&#x1f3ac; 鸽芷咕&#xff1a;个人主页 &#x1f525; 个人专栏: 《C干货基地》《粉丝福利》 ⛺️生活的理想&#xff0c;就是为了理想的生活! 引言 随着技术的不断进步&#xff0c;机器学习&#xff08;Machine Learning, ML&#xff09;在现代医疗设备中的应用正在改变着…

7.5cf

Problem - D - Codeforces 大致题目意思&#xff1a;找#的圆心 #include<bits/stdc.h> typedef long long ll;#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0) const ll N1e21; char a[N][N]; using namespace std;int main() {IOS;int t;cin>>t;whi…

含并行连结的网络

一、Inception块 1、白色部分通过降低通道数来控制模型复杂度&#xff0c;蓝色做特征提取工作&#xff0c;每条路上的通道数可能不同&#xff0c;大概我们会把更重要的那部分特征分配更多的通道数 2、Inception只改变高宽&#xff0c;不改变通道数 3、在不同的情况下需要选择…

gitee项目上不同的项目分别使用不用的用户上传

最近使用根据需要&#xff0c;希望不同的项目使用不同的用户上传&#xff0c;让不同的仓库展示不同的用户名&#xff01;&#xff01;&#xff01; 第一步查看全局的用户信息&#xff1a; # 查看目前全局git配置信息 git config -l #会输出全局的git配置信息 第二步进入到要设…

【MySQL】1.初识MySQL

初识MySQL 一.MySQL 安装1.卸载已有的 MySQL2.获取官方 yum 源3.安装 MySQL4.登录 MySQL5.配置 my.cnf 二.MySQL 数据库基础1.MySQL 是什么&#xff1f;2.服务器&#xff0c;数据库和表3.mysqld 的层状结构4.SQL 语句分类 一.MySQL 安装 1.卸载已有的 MySQL //查询是否有相关…

【ubuntu】安装(升级)显卡驱动,黑屏|双屏无法使用问题解决方法

every blog every motto: You can do more than you think. https://blog.csdn.net/weixin_39190382?typeblog 0. 前言 ubuntu 安装(升级)显卡驱动&#xff0c;黑屏|双屏无法使用问题解决方法 由于项目需要&#xff0c;对显卡驱动进行升级。升级完就黑屏。。。。&#xff0…

平台稳定性里程碑 | Android 15 Beta 3 已发布

作者 / 产品管理副总裁、Android 开发者 Matthew McCullough 从近期发布的 Beta 3 开始&#xff0c;Android 15 达成了平台稳定性里程碑版本&#xff0c;这意味着开发者 API 和所有面向应用的行为都已是最终版本&#xff0c;您可以查阅它们并将其集成到您的应用中&#xff0c;并…

qt 开发笔记堆栈布局的应用

1.概要 画面中有一处位置&#xff0c;有个按钮点击后&#xff0c;这片位置完全换成另一个画面&#xff0c;这中情况特别适合用堆栈布局。 //堆栈布局的应用 #include <QStackedLayout> QStackedLayout *layout new QStackedLayout(this); layout->setCurrentIndex(…

无法下载cuda

cuda下载不了 一、台式机电脑浏览器打不开cuda下载下面二、解决办法 一、台式机电脑浏览器打不开cuda下载下面 用360、chrome、Edge浏览器都打不开下载页面&#xff0c;有的人说后缀com改成cn&#xff0c;都不行。知乎上说是网络问题&#xff0c;电信换成换成移动/联通的网络会…

文心一言最常用的20条指令及指令说明,含增强指令

下面是20条文心一言的指令及其说明&#xff0c;每条指令尽量简洁明了&#xff0c;以便在有限的字数内提供尽可能多的信息。以下是这些指令及其说明&#xff1a; 1. 查询天气 指令&#xff1a;今天北京的天气怎么样&#xff1f;说明&#xff1a;此指令用于查询特定城市&#xf…

Python结合MobileNetV2:图像识别分类系统实战

一、目录 算法模型介绍模型使用训练模型评估项目扩展 二、算法模型介绍 图像识别是计算机视觉领域的重要研究方向&#xff0c;它在人脸识别、物体检测、图像分类等领域有着广泛的应用。随着移动设备的普及和计算资源的限制&#xff0c;设计高效的图像识别算法变得尤为重要。…

数据结构基础--------【二叉树基础】

二叉树基础 二叉树是一种常见的数据结构&#xff0c;由节点组成&#xff0c;每个节点最多有两个子节点&#xff0c;左子节点和右子节点。二叉树可以用来表示许多实际问题&#xff0c;如计算机程序中的表达式、组织结构等。以下是一些二叉树的概念&#xff1a; 二叉树的深度&a…

高考选专业,兴趣与就业前景该如何平衡?

从高考结束的那一刻开始&#xff0c;有些家长和学生就已经变得焦虑了&#xff0c;因为他们不知道成绩出来的时候学生应该如何填报志愿&#xff0c;也不知道选择什么样的专业&#xff0c;毕竟大学里面的专业丰富多彩&#xff0c;如何选择确实是一门学问&#xff0c;而对于学生们…

Zynq7000系列FPGA中DMA引擎编程指南

DMA引擎的编程指南通常涉及一系列步骤和API调用&#xff0c;以确保数据在内存之间的高效传输&#xff0c;而无需CPU的直接干预。 DMA引擎的编程指南包括以下部分&#xff1a; 一、编写微代码为AXI事务编写CCRx程序 通道微码用于设置dmac.CCRx寄存器以定义AXI事务的属性。这是…

Node.js-path 模块

path 模块 path 模块提供了 操作路径 的功能&#xff0c;如下是几个较为常用的几个 API&#xff1a; 代码实例&#xff1a; const path require(path);//获取路径分隔符 console.log(path.sep);//拼接绝对路径 console.log(path.resolve(__dirname, test));//解析路径 let pa…

java反射介绍

Java反射API允许你在运行时检查和修改程序的行为。这意味着你可以动态地创建对象、查看类的字段、方法和构造函数&#xff0c;甚至调用它们。这是一个强大的特性&#xff0c;但也应该谨慎使用&#xff0c;因为它可以破坏封装性。 以下是使用Java反射的一些常见用途&#xff1a;…

041基于SSM+Jsp的高校校园点餐系统

开发语言&#xff1a;Java框架&#xff1a;ssm技术&#xff1a;JSPJDK版本&#xff1a;JDK1.8服务器&#xff1a;tomcat7数据库&#xff1a;mysql 5.7&#xff08;一定要5.7版本&#xff09;数据库工具&#xff1a;Navicat11开发软件&#xff1a;eclipse/myeclipse/ideaMaven包…

OPENCV(图像入门笔记)

使用OpenCV读取图像 使用cv.imread()函数读取图像。 第一个参数为图像名称 第二个参数是一个标志&#xff0c;它指定了读取图像的方式。分别有三种 cv.IMREAD_COLOR&#xff1a; 加载彩色图像。任何图像的透明度都会被忽视。它是默认标志。 cv.IMREAD_GRAYSCALE&#xff1a;以…

什么是 HTTP POST 请求?初学者指南与示范

在现代网络开发领域&#xff0c;理解并应用 HTTP 请求 方法是基本的要求&#xff0c;其中 "POST" 方法扮演着关键角色。 理解 POST 方法 POST 方法属于 HTTP 协议的一部分&#xff0c;主旨在于向服务器发送数据以执行资源的创建或更新。它与 GET 方法区分开来&…

Linux:Ubuntu18.04下开机自启动QT图形化界面

Linux&#xff1a;Ubuntu18.04下开机自启动QT图形化界面 Chapter1 Linux&#xff1a;Ubuntu18.04下开机自启动QT图形化界面一、创建rc.local文件二、建立rc-local.service文件三、启动服务查看启动状态四、重启 Chapter2 将QT应用作为开机自启动&#xff08;Linux系统&#xff…