javascript 'vite build'正在引发'无法从'../内部/窗口?commonjs-external '解析'`./内部/窗口'`

tjjdgumg  于 2023-01-29  发布在  Java
关注(0)|答案(1)|浏览(383)

我最近从CRA迁移到了Vite。一切都很顺利,但是构建脚本抛出了一个错误。似乎是一个RollupError Could not resolve "../internals/window" from "../internals/window?commonjs-external"
当我从配置中删除define对象时,构建成功,但开发服务器停止工作
我正在使用的vite 4这里是我的配置

import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
import viteTsconfigPaths from "vite-tsconfig-paths";
import svgrPlugin from "vite-plugin-svgr";

// https://vitejs.dev/config/
export default defineConfig(({ command, mode }) => {
  return {
    plugins: [react(), viteTsconfigPaths(), svgrPlugin()],
    preview: {
      port: 3000,
    },
    build: {
      outDir: "build",
      rollupOptions: {
        external: ["jss-plugin-window"],
      },
    },
    define: {
      global: "window"
    },
    server: {
      open: true,
      port: 3000,
    },
    envPrefix: "REACT_APP_",
  };
});
h5qlskok

h5qlskok1#

我使用以下方法绕过该问题,从这里得到了解决方案:https://dev.to/lico/issues-that-i-encountered-and-how-to-deal-with-them-while-migrating-from-cra-to-vite-51pg

define: {
    ...(process.env.NODE_ENV === 'development' ? {global: 'window'} : {})
  }

相关问题