easemob / webim-uniapp-demo

79 stars 57 forks source link

vue3引入失败 #28

Closed wangshuaixue closed 1 year ago

wangshuaixue commented 1 year ago

'default' is not exported by src/newSDK/uni_sdk3.6.3.js, imported by src/utils/WebIM.js at utils/WebIM.js:1:7

Wster11 commented 1 year ago

Vite 的开发服务器将所有代码视为原生 ES 模块。因此,Vite 必须先将作为 CommonJS 或 UMD 发布的依赖项转换为 ESM。 https://cn.vitejs.dev/guide/dep-pre-bundling.html#dependency-pre-bundling

解决方案,手动指定sdk文件进行预构建

根目录新建 vite.config.js

import

{defineConfig} from 'vite'; import uni from '@dcloudio/vite-plugin-uni';

export default defineConfig({ // 本地dev optimizeDeps:

{ include: [ "@/assets/Easemob-chat.js", // 手动指定sdk文件进行预构建 ], } , // build配置 build: { commonjsOptions:

{ include: [] } , }, plugins: [uni()], });

wangshuaixue commented 1 year ago

Vite 的开发服务器将所有代码视为原生 ES 模块。因此,Vite 必须先将作为 CommonJS 或 UMD 发布的依赖项转换为 ESM。 https://cn.vitejs.dev/guide/dep-pre-bundling.html#dependency-pre-bundling

解决方案,手动指定sdk文件进行预构建

根目录新建 vite.config.js

import

{defineConfig} from 'vite'; import uni from '@dcloudio/vite-plugin-uni';

export default defineConfig({ // 本地dev optimizeDeps:

{ include: [ "@/assets/Easemob-chat.js", // 手动指定sdk文件进行预构建 ], } , // build配置 build: { commonjsOptions:

{ include: [] } , }, plugins: [uni()], });

我直接把我的vite.config.js替换成这个了 还是不管用呢

Wster11 commented 1 year ago

include: [ "@/assets/Easemob-chat.js"

报什么错?include: [ "@/assets/Easemob-chat.js"] 需要替换成你本地目录的sdk地址, 更改完配置,需要重启项目

wangshuaixue commented 1 year ago

include: [ "@/assets/Easemob-chat.js"

报什么错?include: [ "@/assets/Easemob-chat.js"] 需要替换成你本地目录的sdk地址, 更改完配置,需要重启项目

optimizeDeps:{ include: [ "./src/newSDK/uniapp-sdk-4.1.2.js"], }, 这个是我引入sdk的目录。是这个路径的问题吗 相对路径不行吗 还是一样的错误 'default' is not exported by src/newSDK/uniapp-sdk-4.1.2.js, imported by src/utils/WebIM.js

Wster11 commented 1 year ago

"./src/newSDK/uniapp-sdk-4.1.2.js" 改成 "@/src/newSDK/uniapp-sdk-4.1.2.js" 试试

wangshuaixue commented 1 year ago

"./src/newSDK/uniapp-sdk-4.1.2.js" 改成 "@/src/newSDK/uniapp-sdk-4.1.2.js" 试试

这个@是不是要先命名一下 export default defineConfig({ resolve: { alias: { '@': resolve(__dirname, './src') } }, // 本地dev // 手动指定sdk文件进行预构建 optimizeDeps:{ include: [ "@/newSDK/uniapp-sdk-4.1.2.js"], }, // build配置 build:{ commonjsOptions:{ include: ["@/newSDK/uniapp-sdk-4.1.2.js",] }, }, plugins: [uni()],

})
我的文件
Wster11 commented 1 year ago

build: { commonjsOptions:

{ include: [] } , },

@ 是HBuilderX内置的不用配置,vue3引入uniapp sdk的 demo

wangshuaixue commented 1 year ago

build: { commonjsOptions: { include: [] } , },

@ 是HBuilderX内置的不用配置,vue3引入uniapp sdk的 demo

我没有用 HBuilderX 用的vscode 请问这能解吗

Wster11 commented 1 year ago

测了一下cli创建的项目, 没有问题 cli_demo

wangshuaixue commented 1 year ago

测了一下cli创建的项目, 没有问题 cli_demo

需要配置下 build:{ commonjsOptions:{ include: [/sdk/,/node_modules/] }, }, 解决了感谢