Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Harvey-Andrew opened this issue Nov 15, 2024 · 0 comments

Comments

@Harvey-Andrew
Copy link

Harvey-Andrew commented Nov 15, 2024

控制台
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'),
  }
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant