Electron edge.js配置
使用edge需要同时增加三处配置
当什么都没有配置时,将会发生报错
Uncaught (in promise) Error: Cannot find module '...\node_modules\electron\dist\resources\electron.asar\renderer\native\win32\x64\14.16.0\edge_nativeclr'
此时需要在vue.config.js文件增加,如果没有配置文件,则需要在package.json同级创建。
module.exports = {
pluginOptions: {
electronBuilder: {
externals: ['electron-edge-js']
}
}
}
当配置未开启时,将不能使用 __dirname __filename等nodejs内置变量
Uncaught (in promise) ReferenceError: __dirname is not defined
首先需要配置 new BrowserWindow
{
// 如果使用到nodejs的api,则打包时需要将此设置为true
nodeIntegration: true,
// 同时,需要设置此项为false
// 未设置时报 Uncaught ReferenceError: require is not defined
contextIsolation: false
}
以上配置完成后会报
Uncaught (in promise) TypeError: fs.existsSync is not a function
此时需要继续增加 vue.config.js 的配置项
module.exports = {
pluginOptions: {
electronBuilder: {
externals: ['electron-edge-js'],
// 此处同样需要开启,将会在编译阶段将引用关系写入
nodeIntegration: true,
}
}
}
如果单独配置此项,但是并没有开启 new BrowserWindow的 nodeIntegration: true ,则会发生
Uncaught ReferenceError: require is not defined
此外,对于dll放置的文件夹,一般在项目根目录创resources用于存放资源,并且项目打包过程中会不会直接打包资源目录,所以需要增加资源配置,以防止出错。对于文件的引用,在开发环境下,为当前所看到的结构,当编译打包后,为安装目录下resources目录,所以生产和开发的引用文件存在一定区别,可以实验后再看文件引用
module.exports = {
pluginOptions: {
electronBuilder: {
externals: ['electron-edge-js'],
// 此处同样需要开启,将会在编译阶段将引用关系写入
nodeIntegration: true,
builderOptions:{
extraResources: {
// 拷贝静态文件到指定位置,否则会提示文件找不到
from: 'resources/',
to: './'
},
}
}
}
}
提供文件获取目录方法,可以直接获取不同操作系统下app的resource目录,如果是window即 process.platform==='win32'
const path = require('path')
export function getAppResourcePath (filePath:string) {
if (process.platform === 'darwin' || process.platform === 'linux') {
if (process.env.NODE_ENV === 'development') {
return path.resolve('resources/' + filePath)
} else {
return path.join(__dirname, '..') + filePath
}
} else {
return path.resolve('resources/' + filePath)
}
}
使用setup语法时,需使用require('electron-edge-js')引入,不可以使用import,否则会报
Syntax Error: TypeError: Cannot read property 'content' of null 而非 setup语法
则可以直接import
参考:https://www.yisu.com/zixun/761445.html