简体   繁体   English

使用 React viteJS 构建问题并被放大

[英]Build problem with React viteJS and was amplify

my question is quite simple to explain.我的问题很容易解释。 I've initialized a React project with ViteJS and then added aws-amplify for the backend.我已经用 ViteJS 初始化了一个 React 项目,然后为后端添加了 aws-amplify。 I developed the project and everything works in my local environment running npm run dev.我开发了这个项目,一切都在我的本地环境中运行 npm run dev。 The problem is that I cannot build it.问题是我无法构建它。 You can see the error in the text below.您可以在下面的文本中看到错误。 Do you have any idea?你有什么主意吗?

'request' is not exported by __vite-browser-external, imported by node_modules/@aws-sdk/credential-provider-imds/dist/es/remoteProvider/httpRequest.js 'request' 不是由 __vite-browser-external 导出的,而是由 node_modules/@aws-sdk/credential-provider-imds/dist/es/remoteProvider/httpRequest.js 导入的

Error logs错误日志

In vite.config.js add:在 vite.config.js 添加:

resolve: {
    alias: {
      './runtimeConfig': './runtimeConfig.browser',
    },
}

in define field在定义字段中

Working config for React polyfilled for AWS SDK and Amplify适用于 AWS SDK 和 Amplify 的 React polyfill 的工作配置

import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
import rollupNodePolyFill from "rollup-plugin-node-polyfills";
// https://vitejs.dev/config/
export default defineConfig({
  plugins: [react()],
  optimizeDeps: {
    esbuildOptions: {
      // Node.js global to browser globalThis
      define: {
        global: "globalThis", //<-- AWS SDK 
      },
    },
  },
  build: {
    rollupOptions: {
      plugins: [
        // Enable rollup polyfills plugin
        // used during production bundling
        rollupNodePolyFill(),
      ],
    },
  },
  resolve: {
    alias: {
      './runtimeConfig': './runtimeConfig.browser', // <-- Fix from above
    },
  }
});

when using an array of aliases.使用别名数组时。


  resolve: {
    alias: [
      {
        find: '@', replacement: path.resolve(__dirname, './src'),
      },
      {
        find: './runtimeConfig', replacement: './runtimeConfig.browser',
      }

    ]
  }

For the issue "'request' is not exported by __vite-browser-external", just install the http package (eg 'npm i http')对于“__vite-browser-external 未导出'请求'”的问题,只需安装 http package(例如 'npm i http')

It appears root cause of this type of issue is that aws-amplify JS lib is relying on Node specific features.此类问题的根本原因似乎是aws-amplify JS lib 依赖于 Node 特定的功能。 There is a two part workaround:有一个两部分的解决方法:

  1. To solve errors like 'xxxxx' is not exported by __vite-browser-external add additional alias for ./runtimeConfig to the vite.config.ts file.要解决'xxxxx' is not exported by __vite-browser-external类的错误,请将vite.config.ts ./runtimeConfig中。 So it will look something like:所以它看起来像:
alias: {
      "@": fileURLToPath(new URL("./src", import.meta.url)),
      './runtimeConfig': './runtimeConfig.browser',
    },
  1. To solve error like Uncaught ReferenceError: global is not defined , declare corresponding global variable in your topmost html file ( index.html )要解决Uncaught ReferenceError: global is not defined之类的错误,请在最顶层的 html 文件( index.html )中声明相应的全局变量
<script>
    var global = global || window;
    var Buffer = Buffer || [];
    var process = process || {
      env: { DEBUG: undefined },
      version: []
    };
</script>

There is github issue open for more than a year now: https://github.com/aws-amplify/amplify-js/issues/9639 github 问题已开放一年多: https://github.com/aws-amplify/amplify-js/issues/9639

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM