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同级创建。

JavaScript 全选
module.exports = {
    pluginOptions: {
        electronBuilder: {
            externals: ['electron-edge-js']
        }
    }
}

当配置未开启时,将不能使用 __dirname __filename等nodejs内置变量

Uncaught (in promise) ReferenceError: __dirname is not defined

首先需要配置 new BrowserWindow

C# 全选
{      
    // 如果使用到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 的配置项

JavaScript 全选
module.exports = {
    pluginOptions: {
        electronBuilder: {
            externals: ['electron-edge-js'],
            // 此处同样需要开启,将会在编译阶段将引用关系写入
            nodeIntegration: true, 
        }
    }
}

如果单独配置此项,但是并没有开启 new BrowserWindow的 nodeIntegration: true ,则会发生 

Uncaught ReferenceError: require is not defined

此外,对于dll放置的文件夹,一般在项目根目录创resources用于存放资源,并且项目打包过程中会不会直接打包资源目录,所以需要增加资源配置,以防止出错。对于文件的引用,在开发环境下,为当前所看到的结构,当编译打包后,为安装目录下resources目录,所以生产和开发的引用文件存在一定区别,可以实验后再看文件引用

JavaScript 全选
 module.exports = {
     pluginOptions: {
         electronBuilder: {
             externals: ['electron-edge-js'],
             // 此处同样需要开启,将会在编译阶段将引用关系写入
             nodeIntegration: true, 
             builderOptions:{
                 extraResources: {
                     // 拷贝静态文件到指定位置,否则会提示文件找不到
                     from: 'resources/',
                     to: './'
                 },
             }
         }
     }
 }

提供文件获取目录方法,可以直接获取不同操作系统下app的resource目录,如果是window即 process.platform==='win32'

JavaScript 全选
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

 

版权声明:本文为YES开发框架网发布内容,转载请附上原文出处连接
张国生
上一篇:重新生成package-lock.json
下一篇:Electron调用C#类库dll
评论列表

发表评论

评论内容
昵称:
关联文章

Electron edge.js配置
YESWEB POS开发 Electron操作appdata配置信息
Electron调用C#类库dll
electron与node.js的版本对应关系
YESWEB POS开发 Electron运行生成报错解决办法
权限配置
three.js
vue打包js文件添加hash
深入理解js中的yield
路由配置
初始化配置-数据库配置
Node.js历史下载
C#爬虫:使用Selenium,Selenium配置指南
Razor模板编辑高亮CodeMirror和格式化JS-Beautify
js Copy剪切板
.net Core项目.csproj配置指南
Anaconda配置
.NET MVC加载从后台加载JS代码块
iNeuOS工业互联网操作系统,发布3.6.4版本:云端安全控制和实时日志功能,附Chrome、Firefox和Edge浏览器测试性能对比
html+js上传文件

联系我们
联系电话:15090125178(微信同号)
电子邮箱:garson_zhang@163.com
站长微信二维码
微信二维码