[TOC]
> [electron初探问题总结](https://www.cnblogs.com/wonyun/p/10991984.html)
# npm 安装 electron 依赖时下载失败(缓慢)
## 解决方式二
这种方式的弊端就是只针对当前项目。换一个项目又要再次修改。
1.修改项目下`node_modules/electron/install.js`文件,原文件有一段代码为:
~~~javascript
// downloads if not cached
...
arch: process.env.npm_config_arch || process.arch
}).then((zipPath) => extractFile(zipPath)).catch((err) => onerror(err))
~~~
2.在原来的代码里添加代码(注意 `process.arch` 后需添加一个逗号)
~~~javascript
// downloads if not cached
...
arch: process.env.npm_config_arch || process.arch, // 此处加逗号
/****添加下面代码****/
mirrorOptions:{
mirror: 'https://npm.taobao.org/mirrors/electron/',
customDir: version
}
/****添加上面代码****/
}).then((zipPath) => extractFile(zipPath)).catch((err) => onerror(err))
~~~
3.此时在 `node_modules/electron/` 目录下执行命令
~~~bash
node install.js
~~~
注意:执行完后无打印内容,此时直接运行项目即可。
运行应用
~~~bash
npm start
~~~
# pouchdb 打包出错!
webpack 在打包时会将会出错:
<b style="color:red">Error: No native build was found for platform=win32 arch=x64 runtime=electron abi=80 uv=1 libc=glibc</b>
这种情况下,可以不将它打包在内:
~~~
const webpack = require("webpack");
{
...
plugins: [new webpack.ExternalsPlugin("commonjs", ["leveldown"])]
}
~~~
或者这样:
~~~
{
...
externals: {
"pouchdb": "require('pouchdb')"
}
}
~~~
如果你非要打包在一起: [https://github.com/nolanlawson/pouchdb-electron](https://github.com/nolanlawson/pouchdb-electron)
> [Using Node.js addons in Electron's renderer with Webpack](https://stackoverflow.com/questions/50547649/using-node-js-addons-in-electrons-renderer-with-webpack)
# 关于实现本地模块的导入:
生成一个不会被 webpack 解析的`require`函数:
```js
const requireFunc = typeof __webpack_require__ === 'function' ? __non_webpack_require__ : require
```
preload.js:
```
window.require = __non_webpack_require__ ; // 原始的 require
```
# electron 无边框窗口拖拽
```js
import { screen, ipcMain } from 'electron';
//在一些机器上会出现拖动窗口导致的窗口大小莫名其妙改变的bug,这里采用在移动窗口的时候锁死窗口大小
export const windowMove = (win, id) => {
let moving_interval = null;
ipcMain.on('window-move-evt', (sender, data) => {
if(data['id'] === id && data['move']){
const win_size = win.getSize();
const win_pos = win.getPosition();
const mos_pos = screen.getCursorScreenPoint();
if(moving_interval){
clearInterval(moving_interval);
}
moving_interval = setInterval(() => {
const current_pos = screen.getCursorScreenPoint();
const x = win_pos[0] + current_pos.x - mos_pos.x;
const y = win_pos[1] + current_pos.y - mos_pos.y;
win.setBounds({ x: x, y: y, width: win_size[0], height: win_size[1] });
}, 15);
}else{
clearInterval(moving_interval);
}
});
}
```
> [Electron 无边框窗口的拖动](https://www.jianshu.com/p/96327b044e85)
> [electron 无边框窗口拖拽](https://zhuanlan.zhihu.com/p/112564936)