Reac+vite t打包部署后报错: require is not defined?

React+Antd打包部署之后报错: ReferenceError: require is not defined
看错误信息应该是react-dom文件里用到了require,但是路由好像没问题,是点击按钮的时候报错了,整个页面不能看
错误信息截图:
image.png
vite.config.js代码:

import { defineConfig,splitVendorChunkPlugin } from 'vite'
import react from '@vitejs/plugin-react-swc'
import { resolve } from "path"
import { theme } from 'antd';
import lessToJs from 'less-vars-to-js';
import { readFileSync } from 'fs'
import svgr from "vite-plugin-svgr";

const { defaultAlgorithm, defaultSeed } = theme;
const varLessPath = "./theme/var.less"
const mapToken = defaultAlgorithm(defaultSeed);
const varLessStr = readFileSync(varLessPath, 'utf-8')
const customVarLessJson = lessToJs(varLessStr, { resolveVariables: true, stripPrefix: true });
const prefixBase=process.env.ITLA_IMH_VIEW_ROUTERBASE;


//https://vitejs.dev/config/
export default defineConfig({
  base: prefixBase,
  build: {   
    assetsDir:'statics',
    rollupOptions: {
      input: {
        // 配置所有页面路径,使得所有页面都会被打包
        index: resolve(__dirname,'index.html')
      },
      output:{
        dir:'build',
        //file:'[name].html'
      }
    },
    sourcemap:true
  },
  define: {
    CUSTOMVARLESSDATA: `${JSON.stringify(customVarLessJson)}`
  },
  plugins: [
    // ReactRouterGenerator({
    //   outputFile: resolve(".", "./src/router/auto.jsx"),
    //   isLazy: true,
    //   comKey: "components"
    // }),
    react(),
    splitVendorChunkPlugin(),
    svgr({include: "**/*.svg?react",})
  ],
  resolve: {
    alias: {
      "@": resolve(".", "./src"),
      "~": resolve(".", "./node_modules")
    },
    extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', ".less",".jsonc"]
  },
  css: {
    preprocessorOptions: {
      less: {
        modifyVars: { ...mapToken, ...customVarLessJson },
        javascriptEnabled:true,
      },
    },
    modules:{
      localsConvention:"camelCase",
      generateScopedName:'[name]_[local]_[hash:6]',
      //globalModulePaths:['./theme/*.scss']
    }
  },
  server: {
    port: 3187,
    open: false,
    host: true,
    proxy: {
      '^/api/imh': {
        // target: "http://localhost:8088",
        //target: "http://10.14.0.26:8088",
        //target: "http://10.14.0.147:8088",
        //target: "http://10.14.0.116:8088",
        //target: "http://192.168.120.11:8811",
        target: "http://10.14.0.61:8812",
        //target: "http://10.14.0.223:9999",
        //target: "http://192.168.120.11:8812",
        changeOrigin: true,
        rewrite: (path) => {
          return path.replace("/api/imh", "/imh")
        }
      },
    },
  },
  envPrefix: "ITLA_IMH_VIEW_",
})

期望消除错误

阅读 1.4k
1 个回答

按钮的点击回调中,你做了什么?贴出来看看

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题