ci010 / electron-vue-next

A starter template for using vue-next with the electron.
https://ci010.github.io/electron-vue-next/
191 stars 27 forks source link

Can not import third party module in main process? #52

Closed pandaranker closed 3 years ago

pandaranker commented 3 years ago

抱歉我不是很懂rollup,也许这是个很蠢的问题

Enviroment:

Reproduce :

我试着在主进程里引入mqtt与websocket,mqtt.js自带types文件,ws安装了@types/ws

一开始我是这样写的

import Mqtt from 'mqtt' 
import WS from 'ws'

this.mqttClient = Mqtt.connect(url)
this.wsClient = new WS(url)

以上写法在dev模式下两个都会报同样的错误

image

在编译寻址时会多一个.ts的后缀

如果换成以下写法可以正常执行dev,但是在production下会找不到module

import type MqttTypes from 'mqtt' 
import type WSTypes from 'ws'

const Mqtt = require('mqtt') as typeof MqttTypes
const WS = require('ws') as typeof WSTypes
this.mqttClient = Mqtt.connect(url)
this.wsClient = new WS(url)

这样的写法仅仅能够在dev下运行,在我以前的electron项目里也不能这样写,build后会出现找不到module的问题:

image

notes:

ci010 commented 3 years ago

不知道你有没有试过文档里的 https://ci010.github.io/electron-vue-next/zh/index.html#%E7%AE%A1%E7%90%86%E4%BE%9D%E8%B5%96 这个 section。

你可能需要把他们加到external里再在main里面用,就不会有编译和打包的问题了。 然后 import type 的话…… 理论上是在编译期做类型检查用的,runtime是没有的。

pandaranker commented 3 years ago

@ci010 真的十分感谢,加入到external了之后没有问题了...果然是非常蠢的一个issues...我总觉得vite应该是不会影响主进程的,回想我朋友说的话...看来还是对vite不够了解,ps:impor0t type写法下我是用了require跳过了vite的npm包检查,所以在dev下运行起来了,现在想想当时就应该意识到的...抱歉

ci010 commented 3 years ago

@ci010 真的十分感谢,加入到external了之后没有问题了...果然是非常蠢的一个issues...我总觉得vite应该是不会影响主进程的,回想我朋友说的话...看来还是对vite不够了解,ps:impor0t type写法下我是用了require跳过了vite的npm包检查,所以在dev下运行起来了,现在想想当时就应该意识到的...抱歉

不没关系,非常欢迎问问题,我总感觉现在还有各种corner case的问题【【