Angular鍜孍lectron杩欐牱缁撳悎锛屽紑鍙戞晥鐜囩洿鎺ユ媺婊?馃お
Posted 闈掓湪
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Angular鍜孍lectron杩欐牱缁撳悎锛屽紑鍙戞晥鐜囩洿鎺ユ媺婊?馃お相关的知识,希望对你有一定的参考价值。
鍏徃浣跨敤Angular+Electron杩涜PC绔簲鐢ㄥ紑鍙戯紝浣跨敤寮€婧愰」鐩?code>angular-electron浣滀负妯℃澘锛?a href="https://link.segmentfault.com/?url=https%3A%2F%2Fgithub.com%2Fmaximegris%2Fangular-electron" rel="nofollow">https://github.com/maximegris... 锛夛紝浣嗛殢鐫€椤圭洰鐨勮繘琛岋紝琚涓嬬殑鍑犱釜鐥涚偣鎶樼(寰椾笉琛岋細
- [ ] 浣跨敤tsc缂栬瘧涓昏繘绋嬬殑浠g爜锛屾棤娉曡涓昏繘绋嬬殑浠g爜鐑洿鏂帮紙闇€瑕侀噸鍚紝闈炲父楹荤儲+鑰楁椂锛?/li>
- [ ] 涓昏繘绋嬪拰娓叉煋杩涚▼鐨勫惎鍔ㄦ病鏈夌粨鍚堝ソ锛屼互鑷充簬鍦ㄥ叧闂簲鐢ㄧ殑鏃跺€欙紝娓叉煋杩涚▼闇€瑕佸彟澶栧幓鏉€姝伙紙涓嶆柟渚匡級
- [ ] 娓叉煋杩涚▼娌℃湁娣诲姞HMR锛屾瘡娆′唬鐮佸彉鏇达紝瀵艰嚧鏁翠釜椤圭洰Reload锛堥潪甯歌€楁椂锛?/li>
Attention: 鏈枃瀵圭収婧愮爜椋熺敤鏇翠匠 : https://github.com/simo-an/ng... 馃榿
鎺ヤ笅鏉ユ垜浠?del>鍦ㄤ笂鐝懜楸兼椂闂?/del>鍐欎竴涓墦鍖呰剼鏈潵瑙e喅涓婅堪鐨勯棶棰?/p>
鍚姩娓叉煋杩涚▼锛屼娇鐢?code>angular-cli鐨勫懡浠?/h4>import NgCli from "@angular/cli";
import * as WaitOn from "wait-on";
function startRenderer(): Promise<void> {
NgCli({ cliArgs: [\'serve\', \'--configuration\', \'development\'] });
return WaitOn({ resources: [\'tcp:4200\'] }); // 鐩戝惉榛樿 4200 绔彛
}
缂栬瘧涓昏繘绋嬶紝鎴戜滑浣跨敤Webpack瀵瑰叾鎵撳寘
import NgCli from "@angular/cli";
import * as WaitOn from "wait-on";
function startRenderer(): Promise<void> {
NgCli({ cliArgs: [\'serve\', \'--configuration\', \'development\'] });
return WaitOn({ resources: [\'tcp:4200\'] }); // 鐩戝惉榛樿 4200 绔彛
}
Webpack
鐨勯厤缃唬鐮佽缁嗚 https://github.com/simo-an/ng...
馃檮 閭f垜浠€庢牱鍋氬埌涓昏繘绋嬬殑鐑洿鏂板憿锛?/p>
- 鍚姩涓昏繘绋嬶紝骞惰褰曞叾杩涚▼淇℃伅
- 褰?code>Webpack妫€鏌ュ埌鏈変唬鐮佹洿鏂版椂锛屾牴鎹繘绋?code>ID鏉€姝诲師杩涚▼
- 閲嶆柊鍚姩涓昏繘绋嬶紝鏇存柊璁板綍鐨勮繘绋嬩俊鎭?/li>
杩欐牱鏄笉鏄氨寰堝畬缇庯紙鎬濊矾鏉ヨ嚜electron-vue
馃槢锛?/p>
缂栬瘧鎵撳寘涓昏繘绋嬬殑浠g爜濡備笅
import * as webpack from \'webpack\';
import mainConfig from \'./webpack.main.config\'; // 鑾峰彇 Webpack 閰嶇疆鏂囦欢
let electronProcess = null; // 璁颁綇鐨勪富杩涚▼淇℃伅
let manualRestart = false; // 璁板綍涓昏繘绋嬭鏉€姝绘槸涓嶆槸鎵嬪姩鏉€姝荤殑锛堝嵆鏄笉鏄儹鍚姩锛?
function startMain(): Promise<void> {
return new Promise<void>((resolve, reject) => {
const compiler = webpack.webpack(mainConfig);
compiler.watch({}, (err, stats) => {
if (err) { return ; } // 鍑洪敊
if (!electronProcess || electronProcess.kill) return resolve(); // 杩涚▼宸茬粡鏉€姝?
// 鎵嬪姩閲嶅惎锛屽浜庝笂闈㈢殑涓変釜姝ラ
manualRestart = true;
process.kill(electronProcess.pid);
electronProcess = null;
startElectron(); // 鍚姩 Electron
setTimeout(() => manualRestart = false, 5000);
});
});
}
鍚姩搴旂敤鐨勪唬鐮佸涓?code>startElectron()锛屾敞鎰忦煣愪笅闈㈡垜浠紑濮嬪惎鍔?code>Electron鍟?/p>
let electronProcess = null; // 璁颁綇鐨勪富杩涚▼淇℃伅
let manualRestart = false; // 璁板綍涓昏繘绋嬭鏉€姝绘槸涓嶆槸鎵嬪姩鏉€姝荤殑锛堝嵆鏄笉鏄儹鍚姩锛?
function startElectron(): void {
const args = [
\'--inspect=5858\',
path.join(__dirname, \'../../dist/electron-main/main.js\') // 涓昏繘绋嬪叆鍙f枃浠? ];
// 璁颁綇涓昏繘绋嬶紙涓嬫鏈変唬鐮佹洿鏂版椂灏辨潃姝伙級
electronProcess = spawn(require(\'electron\'), args);
electronProcess.on(\'close\', () => { if (!manualRestart) process.exit(); });
}
杩欓儴鍒嗙殑浠g爜閲岋紝鎴戝鏋滀富鍔ㄩ€€鍑哄簲鐢紝浼氬皢鏁翠釜Process
鍏ㄩ儴鏉€姝伙紝瑙e喅浜嗕富杩涚▼鍏抽棴锛屾覆鏌撹繘绋嬭繕鍦ㄨ繍琛岀殑闂銆?/p>
鏈€鍚庯紝鍚姩锛岃繍琛?/h4>Promise.all([ startRenderer(), startMain() ]).then(startElectron)
Promise.all([ startRenderer(), startMain() ]).then(startElectron)
鎾掕姳锛屽畬缁擄紝鎴戜滑涓婇潰鐨勪袱涓棝鐐硅В鍐冲暒 馃槅
- [x] 浣跨敤webpack鎵撳寘缂栬瘧涓昏繘绋嬩唬鐮佸苟娣诲姞鐑惎鍔紙鏃犻渶閲嶅惎锛岀畝鍗?鐪佹椂锛?/li>
- [x] 涓汇€佹覆鏌撹繘绋嬬浉浜掗厤缃紝鍚岀敓鍚屾锛堟柟渚匡級
鐒惰€屾渶鍚庝竴涓棶棰樺湪Webpack11 +
鍙樺緱闈炲父瀹规槗瑙e喅鍟︼紝鍙渶瑕佸湪angular.json
涓厤缃ā鍧楃儹鏇存柊鍗冲彲 馃
{
// ...
"serve": {
"development": {
"hmr": true,
"browserTarget": "ng-electron:build:development"
}
}
// ...
}
// ...
}
鑷虫锛屾垜浠」鐩腑鐨勪笁涓棝鐐瑰叏閮ㄨВ鍐?/p>
- [x] 浣跨敤webpack鎵撳寘缂栬瘧涓昏繘绋嬩唬鐮佸苟娣诲姞鐑惎鍔紙鏃犻渶閲嶅惎锛岀畝鍗?鐪佹椂锛?/li>
- [x] 涓汇€佹覆鏌撹繘绋嬬浉浜掗厤缃紝鍚岀敓鍚屾锛堟柟渚匡級
- [x] 娓叉煋杩涚▼娣诲姞HMR锛屾瘡娆′唬鐮佸彉鏇达紝鍙噸鏂版覆鏌撶浉搴旀ā鍧楋紙鐪佹椂锛?/li>
馃構馃構馃構馃構馃構馃構馃構馃構馃構馃構馃構馃構馃構灏忛粍浜哄垎鍓茬嚎馃構馃構馃構馃構馃構馃構馃構馃構馃構馃構馃構馃構馃構
鐪嬪埌杩欓噷浣犱細涓嶄細鏈夌枒闂煒?/p>
- 浣犵敤 ts 鍐欑殑浠g爜锛屾€庝箞鍙互褰撲綔鑴氭湰鐢╪ode杩愯鍛紵
- 閭f垜瑕佹€庢牱浣跨敤
electron-builder
鎵撳寘鍛? - 浣犺繖涓湡鐨勬瘮鑰佸鍐欑殑閭d釜寮€婧愮殑鏈?code>4k+ star 鐨勬洿濂界敤鍚?
- ......
閭?code>clone涓嬫潵鐪嬬湅浠g爜锛屽湪鐢佃剳涓婅窇涓€涓嬶紝涓婇潰鐨勭枒鎯戝氨鍏ㄨВ鍟?/p>
娆㈣繋璁块棶锛宻tar or fork锛?https://github.com/simo-an/ng...
以上是关于Angular鍜孍lectron杩欐牱缁撳悎锛屽紑鍙戞晥鐜囩洿鎺ユ媺婊?馃お的主要内容,如果未能解决你的问题,请参考以下文章
鍓嶇涔嬭矾锛氱敤 Angular 瑙勮寖鏉ョ害鏉熷洟闃?git 鎻愪氦