记录nuxt3爬坑,vitest配置,autoimport、naive-ui配置等问题

记录日期2022.11.15
项目基于vitesse-nuxt3
package.jsonnuxt.config.tsvitest.config.tstsconfig.json配置放在结尾

技术栈

目前使用到 nuxt3、pinia、unocss、vueuse、naive-ui、vitest

项目版本

nuxt版本:3.0.0-rc.13-27772354.a0a59e2
@nuxt/test-utils-edge版本:3.0.0-rc.13-27772354.a0a59e2

学习记录

nuxt3 文档
nuxt2 unit test
vitest 文档
在nuxt3中使用vitest和vueuse
vitest配置
在vitest中配置autoimport
naiveUI服务端渲染SSR
naiveUI nuxt support

解决的问题

  1. 全局css
    • 参考 nuxt.config.ts css 配置
  2. test.ts 文件中自动引入 vue3 vueuse vitest
    • 参考 vitest.config.tstsconfig.json
  3. 配置 vitest 路径和自动引入的组件与 nuxt 一致
    • 参考 vitest.config.ts
  4. 配置 naive-ui 自动引入及主题配置

学习中遇到的问题

  1. No context is available. (Forgot calling setup or createContext?) 错误
    • nuxt2中是用setuptest 在nuxt3是使用setup demo test
  2. Vitest was initialized with native Node instead of Vite Node. 错误
    • 在vitest 中配置 @nuxt/test-utils-edge
    • vitest.config.ts中配置test.deps.inline(参考底下配置) nuxt issue
  3. Component inside <Transition> renders non-element root node that cannot be animated. 错误
    • 在nuxt中每个页面 <template> </temnplate> 都需要一个根节点(不同于 vue3 不限制)nuxt issue
  4. Adding tailwind reset hide button style of unocss
    • naiveUI 的按钮样式被 style 中的tainwind.css 样式所影响,需要在nuxt.config.tsunocss.preflight设置为falseunocss issue

// package.json
{
  "private": true,
  "packageManager": "pnpm@7.9.0",
  "scripts": {
    "build": "nuxi build",
    "dev": "nuxi dev",
    "start": "node .output/server/index.mjs",
    "lint": "eslint .",
    "test": "vitest",
    "postinstall": "nuxi prepare",
    "generate": "nuxi generate",
    "up": "taze major -I"
  },
  "devDependencies": {
    "@antfu/eslint-config": "^0.29.4",
    "@css-render/vue3-ssr": "^0.15.11",
    "@iconify-json/carbon": "^1.1.9",
    "@iconify-json/twemoji": "^1.1.5",
    "@nuxt/test-utils-edge": "3.0.0-rc.13-27772354.a0a59e2",
    "@nuxtjs/color-mode": "^3.1.8",
    "@pinia/nuxt": "^0.4.3",
    "@unocss/nuxt": "^0.46.4",
    "@vueuse/nuxt": "^9.5.0",
    "dayjs": "^1.11.6",
    "eslint": "^8.27.0",
    "jsdom": "^20.0.2",
    "naive-ui": "^2.33.5",
    "nuxt": "npm:nuxt3@3.0.0-rc.13-27772354.a0a59e2",
    "pinia": "^2.0.23",
    "playwright": "^1.27.1",
    "sass": "^1.56.1",
    "typescript": "^4.8.4",
    "unplugin-auto-import": "^0.11.4",
    "unplugin-vue-components": "^0.22.9",
    "vite": "^3.2.3",
    "vitest": "^0.25.1",
    "vue": "^3.2.44"
  },
  "pnpm": {
    "peerDependencyRules": {
      "ignoreMissing": [
        "webpack"
      ]
    }
  }
}

// nuxt.config.ts
import dayjs from 'dayjs'
import Components from 'unplugin-vue-components/vite'
import { NaiveUiResolver } from 'unplugin-vue-components/resolvers'

export default defineNuxtConfig({
  modules: [
    '@vueuse/nuxt',
    '@unocss/nuxt',
    '@pinia/nuxt',
    '@nuxtjs/color-mode',
  ],
  alias: {
    style: '/<rootDir>/assets/style',
  },
  // nuxt auto import components
  components: {
    dirs: [
      '~/components',
      '~/layoutComponents',
      '~/pagesComponents',
    ],
  },
  // global css
  css: [
    'assets/style/root.css',
  ],

  // antfu/vitesse-nuxt3 default config
  experimental: {
    reactivityTransform: true,
  },
  unocss: {
    // Injecting `@unocss/reset/tailwind.css` entry
    preflight: false,
  },
  colorMode: {
    classSuffix: '',
  },
  // naive-ui
  build: {
    transpile:
      process.env.NODE_ENV === 'production'
        ? ['naive-ui', 'vueuc', '@css-render/vue3-ssr', '@juggle/resize-observer']
        : ['@juggle/resize-observer'],
  },
  vite: {
  // need add declare from xxx.d.ts
    define: {
      __BUILD_TIME__: JSON.stringify(dayjs().format('YYYY/MM/DD HH:mm')),
    },
    ssr: {
      noExternal: ['naive-ui'],
    },
    plugins: [
      Components({
        resolvers: [NaiveUiResolver()], // 全自动按需引入naive-ui组件
      }),
    ],
    // 解决在开发模式下降低 naive-ui 引起的打包缓慢
    optimizeDeps: {
      include:
        process.env.NODE_ENV === 'development'
          ? ['naive-ui', 'vueuc', 'date-fns-tz/esm/formatInTimeZone']
          : [],
    },
  },
})

// vitest.config.ts
import { resolve } from 'path'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { defineConfig } from 'vitest/config'

export default defineConfig({
  test: {
    globals: true,
    environment: 'jsdom',
    // fix bug: Vitest was initialized with native Node instead of Vite Node
    // https://github.com/nuxt/framework/issues/3252#issuecomment-1126771193
    deps: {
      inline: ['@nuxt/test-utils-edge'],
    },
  },
  resolve: {
    alias: {
      '@': resolve(__dirname, '.'),
    },
  },
  plugins: [
    // auto import vue、vueuse
    AutoImport({
      imports: ['vue', '@vueuse/core'],
      vueTemplate: true,
      dts: true, // not matter
    }),
    // auto import vue component
    Components({
      dirs: [
        './components',
        './layoutComponents',
        './pagesComponents',
      ],
    }),
  ],
})
// tsconfig.json
{
  "extends": "./.nuxt/tsconfig.json",
  "compilerOptions": {
    "strict": true,
    "types": ["vitest/globals", "naive-ui/volar"]
  }
}

posted @ 2022-11-12 12:35  WongDaWEEE  阅读(2471)  评论(0编辑  收藏  举报