我正在尝试将 botframework-webchat 集成到 gatsby.js 网站,gatsby develop 构建成功,但是当我使用 gatsby build 命令运行生产构建时,它抛出以下错误。
> 15 | const store = createStore({}, ({ dispatch }) => next => action => {
[1] | ^
[1] 16 | if (action.type === 'DIRECT_LINE/CONNECT_FULFILLED') {
[1] 17 | dispatch({
[1] 18 | type: 'WEB_CHAT/SEND_EVENT',
[1]
[1]
[1] WebpackError: TypeError: Object(...) is not a function
[1]
[1] - MinimizableWebChat.js:15
[1] src/lib/webchat/MinimizableWebChat.js:15:31
运行开发构建:gatsby develop(构建成功)。
运行生产构建:gatsby build(抛出错误)
以下是 MinimizableWebChat.js 的完整代码,摘自 microsoft BotFramework-Webchat
import React, { useCallback, useEffect, useMemo, useState } from 'react';
import { createStore } from 'botframework-webchat';
import classNames from 'classnames';
import WebChat from './WebChat';
import { checkTokenExpirationDate, createToken } from './utils/tokenDefs';
import './utils/fabric-icons-inline.css';
import './MinimizableWebChat.scss';
const USR_BOT_TKN = 'USR_TKN';
const MinimizableWebChat = ({ lang = 'en', darkMode = false }) => {
useEffect(() => {
return () => {
localStorage.removeItem(USR_BOT_TKN);
setToken(null);
};
}, [lang]);
const store = useMemo(
() =>
createStore({}, ({ dispatch }) => next => action => {
if (action.type === 'DIRECT_LINE/CONNECT_FULFILLED') {
dispatch({
type: 'WEB_CHAT/SEND_EVENT',
payload: {
name: 'webchat/join',
value: {
language: lang,
},
},
});
} else if (action.type === 'DIRECT_LINE/INCOMING_ACTIVITY') {
if (action.payload.activity.from.role === 'bot') {
setNewMessage(true);
}
}
return next(action);
}),
[lang]
);
const [loaded, setLoaded] = useState(false);
const [minimized, setMinimized] = useState(true);
const [newMessage, setNewMessage] = useState(false);
const [token, setToken] = useState();
const handleFetchToken = useCallback(async () => {
if (!token) {
const userToken = localStorage.getItem(USR_BOT_TKN);
const isValidToken = checkTokenExpirationDate(userToken);
if (userToken && isValidToken) {
return setToken(userToken);
} else {
const res = await fetch(createToken().uriGenerateToken, createToken().params);
const { token } = await res.json();
localStorage.setItem(USR_BOT_TKN, token);
setToken(token);
}
}
}, [setToken, token]);
const handleMaximizeButtonClick = useCallback(async () => {
setLoaded(true);
setMinimized(false);
setNewMessage(false);
}, [setMinimized, setNewMessage]);
const handleMinimizeButtonClick = useCallback(() => {
setMinimized(true);
setNewMessage(false);
}, [setMinimized, setNewMessage]);
return (
<div className='minimizable-web-chat'>
<button
className={classNames(
lang === 'ar' ? 'maximize left' : 'maximize right',
darkMode ? 'dark' : ''
)}
onClick={minimized ? handleMaximizeButtonClick : handleMinimizeButtonClick}>
{minimized ? (
<img
src={darkMode ? '/open_chat_icon_black.svg' : '/open_chat_icon.svg'}
alt='Open chat button icon'
/>
) : (
<span className='ms-Icon ms-Icon--Cancel' />
)}
{newMessage && minimized && (
<span className='ms-Icon ms-Icon--CircleShapeSolid red-dot' />
)}
</button>
{loaded && (
<div
className={classNames(
lang === 'ar' ? 'chat-box left' : 'chat-box right',
// side === 'left' ? 'chat-box left' : 'chat-box right',
minimized ? 'hide' : '',
darkMode ? 'dark' : ''
)}>
<header>
<img src='/logo.svg' alt='Logo' />
</header>
{lang && (
<WebChat
className='react-web-chat'
onFetchToken={handleFetchToken}
store={store}
token={token}
lang={lang}
darkMode={darkMode}
/>
)}
</div>
)}
</div>
);
};
export default MinimizableWebChat;