英文:
@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: '({})',
},
resolve: {
alias: {
'./runtimeConfig': './runtimeConfig.browser',
},
},
and in index.html
, we have the following script:
<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>
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: '({})',
},
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论