设置我的昵称 發表於 2020-6-15 13:57:00

React+electron项目搭建 打包

<h1><strong>React+electron项目搭建 打包</strong></h1>
<h1><strong><span style="font-family: 宋体">一.搭建</span>react+electron项目</strong></h1>
<h2><strong>1.<span style="font-family: 黑体">创建一个</span><span style="font-family: Arial">react</span><span style="font-family: 黑体">项目</span></strong></h2>
<p>create-react-app my-app</p>
<p>cd my-app</p>
<p>npm start</p>
<p>看下页面是否打开,是否运行正确。</p>
<p>注意:如果页面没有src文件夹,</p>
<p>第一种:卸载全局安装包:</p>
<blockquote>
<p align="left">npm uninstall -g create-react-app</p>
<p align="left">yarn global remove create-react-app</p>
</blockquote>
<p>然后 npm start,浏览器会打开页面。</p>
<p>如果第一种不行,用第二种</p>
<p>第二种:忽视掉本地的create-react-app已存在的版本进行项目的创建</p>
<pre class="prettyprint"><code class="prism language-javascript has-numbering">npx <span class="token operator">--ignore<span class="token operator">-existing create<span class="token operator">-react<span class="token operator">-app my<span class="token operator">-app</span></span></span></span></span></code></pre>
<h2>2.&nbsp;<strong><span style="font-family: 黑体">引入</span>electron</strong></h2>
<p align="justify">npm install electron --save-dev</p>
<p align="justify">&nbsp;</p>
<h2>3.&nbsp;<strong>配置</strong></h2>
<h3>1)<strong><span style="font-family: 宋体">:在</span>package.json<span style="font-family: 宋体">配置入口文件,具体如下:</span></strong></h3>
<p align="justify">“main”:”main.js”</p>
<p align="justify">&nbsp;<img src="https://img2020.cnblogs.com/blog/1120896/202006/1120896-20200615134250342-1627666503.png"></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p align="justify">修改启动命令:</p>
<p>&nbsp;<img src="https://img2020.cnblogs.com/blog/1120896/202006/1120896-20200615134303123-646212425.png"></p>
<p>&nbsp;</p>
<h3>2)<strong><span style="font-family: 宋体">:</span>main.js<span style="font-family: 宋体">文件编写</span></strong></h3>
<div class="cnblogs_code">
<pre>const { app, BrowserWindow } = require('electron'<span style="color: rgba(0, 0, 0, 1)">)
const path </span>= require('path'<span style="color: rgba(0, 0, 0, 1)">)
const pkg </span>= require('./package.json'<span style="color: rgba(0, 0, 0, 1)">);

let mainWindow

</span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> createWindow() {
mainWindow </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> BrowserWindow({
  width: </span>800<span style="color: rgba(0, 0, 0, 1)">,
  height: </span>600<span style="color: rgba(0, 0, 0, 1)">,
  webPreferences: {
    preload: path.join(__dirname, </span>'preload.js'<span style="color: rgba(0, 0, 0, 1)">)
  }
 })

  </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 加载应用----react 打包</span>
  <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">mainWindow.loadURL(url.format({</span>
  <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">pathname: path.join(__dirname, './build/index.html'),</span>
  <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">protocol: 'file:',</span>
  <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">slashes: true</span>
  <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">}))</span>
  <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 加载应用----适用于 react 项目和开发阶段npm run electron</span><span style="color: rgba(0, 0, 0, 1)">
  mainWindow.loadURL(</span>'http://localhost:3000/'<span style="color: rgba(0, 0, 0, 1)">);
  mainWindow.on(</span>'closed', <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> () {
    mainWindow </span>= <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">
  })
}

app.on(</span>'ready'<span style="color: rgba(0, 0, 0, 1)">, createWindow)

app.on(</span>'window-all-closed', <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> () {
  </span><span style="color: rgba(0, 0, 255, 1)">if</span> (process.platform !== 'darwin'<span style="color: rgba(0, 0, 0, 1)">) app.quit()
})

app.on(</span>'activate', <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> () {
  </span><span style="color: rgba(0, 0, 255, 1)">if</span> (mainWindow === <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">) createWindow()
})</span></pre>
</div>
<p>&nbsp;</p>
<p>&nbsp;</p>
<h2><strong>启动:</strong></h2>
<p>npm start &nbsp;&nbsp;npm run dev/electron &nbsp;<span style="font-family: 宋体">两个命令都执行时,浏览器打开页面,会打开桌面应用。</span></p>
<p>&nbsp;</p>
<h1>二.<strong><span style="font-family: 宋体">搭建</span>react路由</strong></h1>
<p>npm install --save react-router-dom</p>
<p>&nbsp;</p>
<h3><strong>src/Index.js</strong></h3>
<div class="cnblogs_code">
<pre>import React from 'react'<span style="color: rgba(0, 0, 0, 1)">;
import ReactDOM from </span>'react-dom'<span style="color: rgba(0, 0, 0, 1)">;
import ReactMap from </span>'./router/routerMap'<span style="color: rgba(0, 0, 0, 1)">;
import </span>* as serviceWorker from './serviceWorker'<span style="color: rgba(0, 0, 0, 1)">;

ReactDOM.render(
  </span>&lt;div&gt;
    &lt;ReactMap /&gt;
  &lt;/div&gt;<span style="color: rgba(0, 0, 0, 1)">
, document.getElementById(</span>'root'<span style="color: rgba(0, 0, 0, 1)">));
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> If you want your app to work offline and load faster, you can change</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> unregister() to register() below. Note this comes with some pitfalls.</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> Learn more about service workers: https://bit.ly/CRA-PWA</span><span style="color: rgba(0, 0, 0, 1)">
serviceWorker.unregister();</span></pre>
</div>
<pre><strong>
src/router/routerMap.js
</strong></pre>
<div class="cnblogs_code">
<pre>import React from 'react'<span style="color: rgba(0, 0, 0, 1)">
import { HashRouter as Router, Route } from </span>'react-router-dom'<span style="color: rgba(0, 0, 0, 1)">
import Login from </span>'../pages/Login/Login'<span style="color: rgba(0, 0, 0, 1)">

class ReactMap extends React.Component {
  updateHandle() {
    console.log(</span>"每次router变化后触发"<span style="color: rgba(0, 0, 0, 1)">)
  }
  render() {
    </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> (
      </span>&lt;Router history={<span style="color: rgba(0, 0, 255, 1)">this</span>.props.history}&gt;
        &lt;Route exact path="" component={Login} /&gt;
      &lt;/Router&gt;
    <span style="color: rgba(0, 0, 0, 1)">)
  }
}
export </span><span style="color: rgba(0, 0, 255, 1)">default</span> ReactMap;</pre>
</div>
<p>&nbsp;</p>
<h1><strong><span style="font-family: 宋体">三.搭建</span>redux</strong></h1>
<p>npm install --save react-redux</p>
<p>npm install --save redux</p>
<p>npm install --save redux-thunk</p>
<p>&nbsp;</p>
<h3><strong>src/Index.js</strong></h3>
<div class="cnblogs_code">
<pre>import React from 'react'<span style="color: rgba(0, 0, 0, 1)">;
import ReactDOM from </span>'react-dom'<span style="color: rgba(0, 0, 0, 1)">;
import ReactMap from </span>'./router/routerMap'<span style="color: rgba(0, 0, 0, 1)">;
import </span>* as serviceWorker from './serviceWorker'<span style="color: rgba(0, 0, 0, 1)">;
import { Provider } from </span>'react-redux'<span style="color: rgba(0, 0, 0, 1)">
import { createStore, applyMiddleware, compose } from </span>'redux'<span style="color: rgba(0, 0, 0, 1)">
import rootRedux from </span>'./redux'<span style="color: rgba(0, 0, 0, 1)">
import thunk from </span>'redux-thunk'<span style="color: rgba(0, 0, 0, 1)">
const composeEnhancers </span>= window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ ?<span style="color: rgba(0, 0, 0, 1)">
window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({}) : compose
const enhancer </span>=<span style="color: rgba(0, 0, 0, 1)"> composeEnhancers(applyMiddleware(thunk));
const store </span>=<span style="color: rgba(0, 0, 0, 1)"> createStore(
  rootRedux,
  enhancer
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> applyMiddleware(thunk),</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> window.__REDUX_DEVTOOLS_EXTENSION__ &amp;&amp; window.__REDUX_DEVTOOLS_EXTENSION__()</span><span style="color: rgba(0, 0, 0, 1)">
)

ReactDOM.render(
  </span>&lt;div&gt;
    &lt;Provider store={store}&gt;
    &lt;ReactMap /&gt;
    &lt;/Provider&gt;
  &lt;/div&gt;<span style="color: rgba(0, 0, 0, 1)">
, document.getElementById(</span>'root'<span style="color: rgba(0, 0, 0, 1)">));
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> If you want your app to work offline and load faster, you can change</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> unregister() to register() below. Note this comes with some pitfalls.</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> Learn more about service workers: https://bit.ly/CRA-PWA</span><span style="color: rgba(0, 0, 0, 1)">
serviceWorker.unregister();</span></pre>
</div>
<p>&nbsp;</p>
<h3><strong>src/redux/index.js</strong></h3>
<div class="cnblogs_code">
<pre>import { combineReducers } from 'redux'<span style="color: rgba(0, 0, 0, 1)">
import set from </span>'./set'<span style="color: rgba(0, 0, 0, 1)">
export </span><span style="color: rgba(0, 0, 255, 1)">default</span><span style="color: rgba(0, 0, 0, 1)"> combineReducers({
data: set
})</span></pre>
</div>
<p>&nbsp;</p>
<h3><strong>src/redux/set.js</strong></h3>
<p>&nbsp;</p>
<div class="cnblogs_code">
<pre>const set = (state = [], action) =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 0, 255, 1)">switch</span><span style="color: rgba(0, 0, 0, 1)"> (action.type) {
    </span><span style="color: rgba(0, 0, 255, 1)">case</span> 'GET_LIST'<span style="color: rgba(0, 0, 0, 1)">:
      </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> {
      ...state,
      list: action.list
      }
    </span><span style="color: rgba(0, 0, 255, 1)">case</span> 'SET_USERNAME'<span style="color: rgba(0, 0, 0, 1)">:
      </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> {
      ...state,
      userName: action.list
      }
    </span><span style="color: rgba(0, 0, 255, 1)">default</span>: <span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> state
}
}

export </span><span style="color: rgba(0, 0, 255, 1)">default</span> set</pre>
</div>
<p>&nbsp;</p>
<p>&nbsp;</p>
<h3><strong>src/actions/index.js</strong></h3>
<div class="cnblogs_code">
<pre>export const getList = () =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
   </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> {
    type: </span>'GET_LIST'<span style="color: rgba(0, 0, 0, 1)">,
    list: </span>'1111'<span style="color: rgba(0, 0, 0, 1)">
  }
}

export const setUserName </span>= (list) =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
  </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> {
    type: </span>'SET_USERNAME'<span style="color: rgba(0, 0, 0, 1)">,
    list: list
  }
}</span></pre>
</div>
<p>&nbsp;</p>
<h1><strong><span style="font-family: 宋体">四.不抽离</span> webpack配置的方案antd(npm run build 后找不到图片路径,暂未解决)</strong></h1>
<p>cnpm install --save react-app-rewired customize-cra</p>
<p>cnpm install --save babel-plugin-import</p>
<p>&nbsp;</p>
<h2><strong>1.<span style="font-family: 黑体">配置</span><span style="font-family: Arial">less,less-loader</span></strong></h2>
<p>cnpm install --save less less-loader</p>
<p>&nbsp;</p>
<h2>2.&nbsp;<strong><span style="font-family: 黑体">根目录新建</span>config-overrides.js<span style="font-family: 黑体">文件</span></strong></h2>
<div class="cnblogs_code">
<pre>const { override, fixBabelImports, addWebpackAlias, addLessLoader } = require('customize-cra'<span style="color: rgba(0, 0, 0, 1)">)
const path </span>= require('path'<span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> resolve(dir) {
</span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> path.join(__dirname, dir)
}

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> addLessLoader 添加less的引用</span><span style="color: rgba(0, 128, 0, 1)">
//</span><span style="color: rgba(0, 128, 0, 1)"> fixBabelImports 按需加载antd组件</span><span style="color: rgba(0, 128, 0, 1)">
//</span><span style="color: rgba(0, 128, 0, 1)"> addWebpackAlias 路径别名配置</span><span style="color: rgba(0, 128, 0, 1)">
/*</span><span style="color: rgba(0, 128, 0, 1)"> 路径别名配置 </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
module.exports </span>=<span style="color: rgba(0, 0, 0, 1)"> override(
addWebpackAlias({
    </span>'@': resolve('src'<span style="color: rgba(0, 0, 0, 1)">),
    components: resolve(</span>'./src/components'<span style="color: rgba(0, 0, 0, 1)">),
    assets: resolve(</span>'./src/assets'<span style="color: rgba(0, 0, 0, 1)">),
    static: resolve(</span>'./src/static'<span style="color: rgba(0, 0, 0, 1)">),
    img: resolve(</span>'./src/static/img'<span style="color: rgba(0, 0, 0, 1)">),
    js: resolve(</span>'./src/static/js'<span style="color: rgba(0, 0, 0, 1)">),
    css: resolve(</span>'./src/static/css'<span style="color: rgba(0, 0, 0, 1)">),
}),
</span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)"> antd组件按需加载 </span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
fixBabelImports(</span>'import'<span style="color: rgba(0, 0, 0, 1)">, {
    libraryName: </span>'antd'<span style="color: rgba(0, 0, 0, 1)">,
    libraryDirectory: </span>'es'<span style="color: rgba(0, 0, 0, 1)">,
    style: </span><span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">,
}),
addLessLoader({
    javascriptEnabled: </span><span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">,
    modifyVars: { </span>'@primary-color': '#1DA57A'<span style="color: rgba(0, 0, 0, 1)"> },
}),

);</span></pre>
</div>
<p>&nbsp;</p>
<h2>3.&nbsp; 修改package.json文件,目录结构</h2>
<p>&nbsp;<img src="https://img2020.cnblogs.com/blog/1120896/202012/1120896-20201211163638270-1824149486.png"></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<h2>4.&nbsp; 使用:页面引用less</h2>
<p align="left">import&nbsp;'@/static/css/index.less'</p>
<p align="left">&nbsp;</p>
<h1><strong><span style="font-family: 宋体">五.抽离</span> webpack配置的方案</strong></h1>
<p>npm run eject&nbsp;</p>
<p>&nbsp;<img alt="" loading="lazy" src="https://img2020.cnblogs.com/blog/1120896/202006/1120896-20200615134552606-1378280926.png"></p>
<p><span style="font-family: 宋体">运行后会出现</span>config<span style="font-family: 宋体">文件夹,在里面配置</span><span style="font-family: &quot;Lucida Console&quot;">less </span><span style="font-family: 宋体">和路径别名配置</span></p>
<h2><strong>1.<span style="font-family: 黑体">配置</span><span style="font-family: Arial">less,less-loader</span></strong></h2>
<p>cnpm install --save less less-loader</p>
<p><span style="font-family: 宋体">在</span>webpack.config.js<span style="font-family: 宋体">里面进行</span><span style="font-family: &quot;Lucida Console&quot;">less</span><span style="font-family: 宋体">的配置</span></p>
<p>&nbsp;<img src="https://img2020.cnblogs.com/blog/1120896/202006/1120896-20200615134631160-366954168.png"></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;<img src="https://img2020.cnblogs.com/blog/1120896/202006/1120896-20200615134643570-642405530.png"></p>
<p>&nbsp;</p>
<h2><strong>2.<span style="font-family: 黑体">配置路径别名</span></strong></h2>
<p><span style="font-family: 宋体">在</span>webpack.config.js<span style="font-family: 宋体">里面进行路径别名的配置</span></p>
<p>&nbsp;<img src="https://img2020.cnblogs.com/blog/1120896/202006/1120896-20200615134653395-918250327.png"></p>
<p>&nbsp;</p>
<h1><strong>六.打包</strong></h1>
<h2><strong>1.<span style="font-family: 黑体">打包的准备工作:</span></strong></h2>
<p><span style="font-family: 宋体">在</span>package.json,<span style="font-family: 宋体">里面加上</span>”homepage”:”.”</p>
<p>&nbsp;<img src="https://img2020.cnblogs.com/blog/1120896/202006/1120896-20200615134710265-2104485766.png"></p>
<p>&nbsp;</p>
<p><span style="font-family: 宋体">在</span>public<span style="font-family: 宋体">里面加上</span><span style="font-family: &quot;Lucida Console&quot;">main.js &nbsp;</span><span style="font-family: 宋体">和 </span><span style="font-family: &quot;Lucida Console&quot;">package.json </span><span style="font-family: 宋体">和 </span><span style="font-family: &quot;Lucida Console&quot;">preload.js</span></p>
<p>package.json</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">{
    </span>"name":"crh_chat"<span style="color: rgba(0, 0, 0, 1)">,
    </span>"version":"1.0-test"<span style="color: rgba(0, 0, 0, 1)">,
    </span>"main":"main.js"<span style="color: rgba(0, 0, 0, 1)">,
    </span>"author"<span style="color: rgba(0, 0, 0, 1)">: {
      </span>"name":"YOURNAME"<span style="color: rgba(0, 0, 0, 1)">
    },
    </span>"license": "MIT"<span style="color: rgba(0, 0, 0, 1)">
}</span></pre>
</div>
<p>&nbsp;</p>
<p>main.js</p>
<p><span style="font-family: 宋体">如果需要引用一些模块就加入</span>preload:&nbsp;path.join(__dirname,&nbsp;'preload.js'),不用可以注释掉这段</p>
<div class="cnblogs_code">
<pre>const electron = require('electron'<span style="color: rgba(0, 0, 0, 1)">);
const path </span>= require('path'<span style="color: rgba(0, 0, 0, 1)">);
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 控制应用生命周期的模块</span>
const { app } =<span style="color: rgba(0, 0, 0, 1)"> electron;
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 创建本地浏览器窗口的模块</span>
const { BrowserWindow } =<span style="color: rgba(0, 0, 0, 1)"> electron;

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 指向窗口对象的一个全局引用,如果没有这个引用,那么当该javascript对象被垃圾回收的</span><span style="color: rgba(0, 128, 0, 1)">
//</span><span style="color: rgba(0, 128, 0, 1)"> 时候该窗口将会自动关闭</span>
<span style="color: rgba(0, 0, 0, 1)">let win;

</span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> createWindow() {
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 创建一个新的浏览器窗口</span>
win = <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> BrowserWindow({
width: </span>1920<span style="color: rgba(0, 0, 0, 1)">,
height: </span>1080<span style="color: rgba(0, 0, 0, 1)">,
autoHideMenuBar: </span><span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">,
webPreferences: {
      preload: path.join(__dirname, </span>'preload.js'<span style="color: rgba(0, 0, 0, 1)">)
}
});

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 并且装载应用的index.html页面</span>
win.loadURL(`file:<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">${__dirname}/index.html`);</span>

<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 打开开发工具页面</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">win.webContents.openDevTools();</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 当窗口关闭时调用的方法</span>
win.on('closed', () =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 解除窗口对象的引用,通常而言如果应用支持多个窗口的话,你会在一个数组里</span>
    <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 存放窗口对象,在窗口关闭的时候应当删除相应的元素。</span>
    win = <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">;
});
}


</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 当Electron完成初始化并且已经创建了浏览器窗口,则该方法将会被调用。</span><span style="color: rgba(0, 128, 0, 1)">
//</span><span style="color: rgba(0, 128, 0, 1)"> 有些API只能在该事件发生后才能被使用。</span>
app.on('ready'<span style="color: rgba(0, 0, 0, 1)">, createWindow);
</span><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)"> var mainWindow = new BrowserWindow({
webPreferences: {
    nodeIntegration: false
}
}); </span><span style="color: rgba(0, 128, 0, 1)">*/</span>

<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 当所有的窗口被关闭后退出应用</span>
app.on('window-all-closed', () =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 对于OS X系统,应用和相应的菜单栏会一直激活直到用户通过Cmd + Q显式退出</span>
<span style="color: rgba(0, 0, 255, 1)">if</span> (process.platform !== 'darwin'<span style="color: rgba(0, 0, 0, 1)">) {
    app.quit();
}
});


app.on(</span>'activate', () =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 对于OS X系统,当dock图标被点击后会重新创建一个app窗口,并且不会有其他</span>
<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 窗口打开</span>
<span style="color: rgba(0, 0, 255, 1)">if</span> (win === <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">) {
    createWindow();
}

});</span></pre>
</div>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>preload.js <span style="font-family: 宋体">这里引入了</span><span style="font-family: &quot;Lucida Console&quot;">electron</span><span style="font-family: 宋体">,需要用的时候,</span><span style="font-family: &quot;Lucida Console&quot;">window.electron</span></p>
<div class="cnblogs_code">
<pre>global.electron = require('electron'<span style="color: rgba(0, 0, 0, 1)">)

window.addEventListener(</span>'DOMContentLoaded', () =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
const replaceText </span>= (selector, text) =&gt;<span style="color: rgba(0, 0, 0, 1)"> {
    const element </span>=<span style="color: rgba(0, 0, 0, 1)"> document.getElementById(selector)
    </span><span style="color: rgba(0, 0, 255, 1)">if</span> (element) element.innerText =<span style="color: rgba(0, 0, 0, 1)"> text
}


</span><span style="color: rgba(0, 0, 255, 1)">for</span> (const type of ['chrome', 'node', 'electron'<span style="color: rgba(0, 0, 0, 1)">]) {
    replaceText(`${type}</span>-<span style="color: rgba(0, 0, 0, 1)">version`, process.versions)
}
})</span></pre>
</div>
<p>&nbsp;</p>
<p><span style="font-family: 宋体">有两个地方需要</span>preload.js <span style="font-family: 宋体">外面那个是开发时要用的,</span><span style="font-family: &quot;Lucida Console&quot;">public</span><span style="font-family: 宋体">里面是打包后需要用到的。</span></p>
<p>&nbsp;<img src="https://img2020.cnblogs.com/blog/1120896/202006/1120896-20200615134805059-1289668382.png"></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<h2><strong>2.npm run build</strong></h2>
<p><span style="font-family: 宋体">成功后会出现</span>build<span style="font-family: 宋体">文件夹,双击</span><span style="font-family: &quot;Lucida Console&quot;">index.html</span><span style="font-family: 宋体">在浏览器打开,可以正常运行</span></p>
<p>&nbsp;<img src="https://img2020.cnblogs.com/blog/1120896/202006/1120896-20200615134825151-1999748602.png"></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<h2><strong>3.npm run package</strong></h2>
<p><span style="font-family: 宋体">在</span>pacakge.json<span style="font-family: 宋体">里面的</span><span style="font-family: &quot;Lucida Console&quot;">scripts</span><span style="font-family: 宋体">配置</span></p>
<p>&nbsp;</p>
<p>"package":&nbsp;"electron-packager&nbsp;./build&nbsp;package0514&nbsp;--win&nbsp;--out&nbsp;package/&nbsp;--arch=x64&nbsp;--app-version=1.1.0&nbsp;--electron-version=8.0.0"</p>
<p>&nbsp;</p>
<p><span style="font-family: 宋体">然后</span> npm run package</p>
<p><span style="font-family: 宋体">成功后页面会生成一个</span>package<span style="font-family: 宋体">文件,里面有我们打包好的文件</span></p>
<p>&nbsp;<img src="https://img2020.cnblogs.com/blog/1120896/202006/1120896-20200615134842704-1699406709.png"></p>
<p>&nbsp;</p><br><br>
来源:https://www.cnblogs.com/miam/p/13130509.html
頁: [1]
查看完整版本: React+electron项目搭建 打包