electron应用打包成功纪念一下,以前曾经行过后来打包各种报错,现在有空就尝试解决一下
首先安装nvm能够方便切换node版本
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash
顺利安装后你用nvm list查看node列表时会告诉你这个nvm不存在
解决办法: source ~/.bashrc
接着执行nvm list可以,然后安装需要的版本
npm install 8.16.2
回到最初版本然后打包看各种错误
error while loading shared libraries: libgconf-2.so.4
sudo apt-get install libgconf-2-4
Error retrieving accessibility bus address: org.freedesktop.DBus.Error.ServiceUnknown: The name org.a11y.Bus was not provided by any .service
sudo apt-get install at-spi2-core
还有一种错误是定位到js文件catch后面的{,这时只需要再catch后增加(e)
联系更新了好多个js文件
运行报错 ReferenceError: process is not defined
打开webpack.renderer.config.js文件,如下所示进行修改
new HtmlWebpackPlugin({
filename: 'index.html',
template: path.resolve(__dirname, '../src/index.ejs'),
minify: {
collapseWhitespace: true,
removeAttributeQuotes: true,
removeComments: true
},
# 增加templateParameters配置项 start
templateParameters(compilation, assets, options) {
return {
compilation: compilation,
webpack: compilation.getStats().toJson(),
webpackConfig: compilation.options,
htmlWebpackPlugin: {
files: assets,
options: options
},
process,
};
},
# 增加templateParameters配置项 end
nodeModules: process.env.NODE_ENV !== 'production'
? path.resolve(__dirname, '../node_modules')
: false
}),
打开webpack.web.config.js文件,如下所示进行修改
new HtmlWebpackPlugin({
filename: 'index.html',
template: path.resolve(__dirname, '../src/index.ejs'),
# 增加templateParameters配置项 start
templateParameters(compilation, assets, options) {
return {
compilation: compilation,
webpack: compilation.getStats().toJson(),
webpackConfig: compilation.options,
htmlWebpackPlugin: {
files: assets,
options: options
},
process,
};
},
# 增加templateParameters配置项 end
minify: {
collapseWhitespace: true,
removeAttributeQuotes: true,
removeComments: true
},
nodeModules: false
}),
最后终于改完错误,开始下载依赖,发现很慢
npm config set registry https://registry.npmmirror.com
electron 下载慢 修改 .npmrc 增加
electron_mirror="https://npm.taobao.org/mirrors/electron/"
ELECTRON_MIRROR="https://npm.taobao.org/mirrors/electron/"
第一次打包下载https://github.com/electron/electron/releases/download/v2.0.18/electron-v2.0.18-linux-x64.zip 如果比较慢,自己下载放到.cache/electron文件夹下
最后终于打包成功