[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)