@aws-amplify 与 vite 构建的依赖问题

huangapple go评论66阅读模式
英文:

@aws-amplify dependency issue with vite build

问题

我目前正在将我们的React项目转换为使用Vite。到目前为止,开发过程非常出色,但当我们尝试运行vite build(npm run build)时,我们遇到了以下与@aws-amplify相关的问题:

[commonjs--resolver] /Users/directoryPlaceholder/node_modules/@aws-amplify/pubsub/lib-esm/vendor/paho-mqtt.js中的意外标记(97:27)
文件:/Users/directoryPlaceholder/node_modules/@aws-amplify/pubsub/lib-esm/vendor/paho-mqtt.js:97:27
95:   }
96: })(this, function LibraryFactory() {
97:   var PahoMQTT = (function (global) {
                              ^
98:     // 以下是私有变量,仅在函数闭包内可见
99:     // 用于定义模块的函数闭包内可见。

我们的vite.config.js已经具有以下配置:

define: {
    global: '({})',
},
resolve: {
    alias: {
        './runtimeConfig': './runtimeConfig.browser',
    },
},

index.html中,我们有以下脚本:

<script>
  const isBrowser = () => typeof window !== "undefined";
  const isGlobal = () => typeof global !== "undefined";
  if (!isGlobal() && isBrowser()) {
    var global = window;
  }
</script>
<script type="module" src="/src/index.tsx"></script>

如果有任何关于如何解决这个问题的想法,我们将非常感激!

英文:

I'm currently working on converting our React project over to use Vite. It is great for development so far but when we try to run vite build (npm run build), we run into the following issue with @aws-amplify

[commonjs--resolver] Unexpected token (97:27) in /Users/directoryPlaceholder/node_modules/@aws-amplify/pubsub/lib-esm/vendor/paho-mqtt.js
file: /Users/directoryPlaceholder/node_modules/@aws-amplify/pubsub/lib-esm/vendor/paho-mqtt.js:97:27
95:   }
96: })(this, function LibraryFactory() {
97:   var PahoMQTT = (function (global) {
                                ^
98:     // Private variables below, these are only visible inside the function closure
99:     // which is used to define the module.

Our vite.config.js already has the following configurations:

    define: {
        global: &#39;({})&#39;,
    },
    resolve: {
        alias: {
            &#39;./runtimeConfig&#39;: &#39;./runtimeConfig.browser&#39;,
        },
    },

and in index.html, we have the following script:

&lt;script&gt;
  const isBrowser = () =&gt; typeof window !== &quot;undefined&quot;;
  const isGlobal = () =&gt; typeof global !== &quot;undefined&quot;;
  if (!isGlobal() &amp;&amp; isBrowser()) {
    var global = window;
  }
&lt;/script&gt;
&lt;script type=&quot;module&quot; src=&quot;/src/index.tsx&quot;&gt;&lt;/script&gt;

If anyone has any ideas on how to solve this we would be very grateful!

答案1

得分: 1

vite.config.ts 中移除此选项修复了该问题

define: {
    global: '({})',
},
英文:

Removing this option from vite.config.ts fixed the issue

 define: {
        global: &#39;({})&#39;,
    },

huangapple
  • 本文由 发表于 2023年6月1日 09:20:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76378125.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定