dcloudio / uni-app

A cross-platform framework using Vue.js
https://uniapp.dcloud.io
Apache License 2.0
40.15k stars 3.64k forks source link

js里面无法获取scss变量[plugin:commonjs--resolver] Cannot find module #5223

Open Harvey-Andrew opened 1 week ago

Harvey-Andrew commented 1 week ago

控制台 Snipaste_2024-11-14_17-13-03

App.vue Snipaste_2024-11-14_17-12-38

uni.scss Snipaste_2024-11-14_17-13-19

tsconfig.json

{
  "compilerOptions": {
    "target": "esnext",
    "useDefineForClassFields": true,
    "module": "esnext",
    "moduleResolution": "node",
    "strict": true,
    "jsx": "preserve",
    "sourceMap": true,
    "resolveJsonModule": true,
    "esModuleInterop": true,
    "lib": ["esnext", "dom"],
    "types": ["@dcloudio/types", "vite/client", "@types/node", "nutui-uniapp/global.d.ts"],
    "baseUrl": "./src",
    "paths": {
      "@/*": ["*"],
      "@components/*": ["src/components/*"]
    },
    "ignoreDeprecations": "5.0",
    "suppressImplicitAnyIndexErrors": true,
    "allowJs": true,
    // 对声明文件不进行类型检查
    "skipLibCheck": true
  },
  //加入配置,将标签视为原始组件
  "vueCompilerOptions": {
    "nativeTags": ["block", "component", "template", "slot"]
  },
  "include": ["src/**/*.ts", "src/**/*.d.ts", "src/**/*.tsx", "src/**/*.vue"]
}

vite.config.ts


import { defineConfig, loadEnv } from 'vite'
import uni from '@dcloudio/vite-plugin-uni'
import path from 'path'
import fs from 'fs-extra'
import Components from '@uni-helper/vite-plugin-uni-components'
import { NutResolver } from 'nutui-uniapp'
// https://vitejs.dev/config/
export default defineConfig(async ({ mode }) => {
  // 根据当前工作目录中的 `mode` 加载 .env 文件
  // 设置第三个参数为 '' 来加载所有环境变量,而不管是否有 `VITE_` 前缀。
  // "dev:mp-weixin:student": "uni -p mp-weixin  -m  studentDev", pnpm run dev:mp-weixin:student
  const env = loadEnv(mode, `${process.cwd()}/env`, '')
  await fs.copy(path.resolve(__dirname, env.VITE_APP_BUILD_CONFIG), path.join(__dirname, 'src'))
  return {
    plugins: [
      Components({
        resolvers: [NutResolver()],
      }),
      uni(),
    ],
    resolve: {
      alias: {
        '@': path.resolve(__dirname, './src'),
        '@components': path.resolve(__dirname, './src/components'),
      },
    },

    css: {
      // 配置`scss`和`less`全局变量
      preprocessorOptions: {
        scss: {
          additionalData: '@import "nutui-uniapp/styles/variables.scss";',
        },
        less: {
          additionalData: '@import "@/styles/vars/_base.less";',
        },
      },
    },
    envDir: path.resolve(__dirname, 'env'),
  }
})