我想用ES6的import语法来构建Electron应用程序,这样我就可以在Node.js和浏览器端JS之间重复使用模块而不会出现代码重复问题。但是我发现,Electron在支持ES6语法方面严重滞后。
我了解到这个神奇的解决方案,点击这里去看,结果发现它已经不再维护了。
因此,我想到了Babel。然而,关于Babel + Electron的教程并不多见。我还想加入Nodemon。
以下是我的设置: package.json
正如您将在以下的输出和调试日志中看到的那样,这里的问题在于我们试图编译使用ES6语法的节点模块,但任何Electron应用都依赖于Electron模块,后者似乎不是以传统的方式导出,而是解决了electron可执行路径(字符串),而不是一个Node.js模块。这是一个循环问题。
app.js
所以为了调试,我尝试了这个
而进一步的澄清,像这样的app.js:
我了解到这个神奇的解决方案,点击这里去看,结果发现它已经不再维护了。
因此,我想到了Babel。然而,关于Babel + Electron的教程并不多见。我还想加入Nodemon。
以下是我的设置: package.json
{
"name": "infinitum",
"version": "1.0.0",
"description": "",
"main": "compiled.js",
"directories": {
"test": "tests"
},
"scripts": {
"start": " electron .",
"compile": "nodemon --exec babel-node app.js --out-file compiled.js"
},
"author": "",
"license": "ISC",
"devDependencies": {
"@babel/core": "^7.12.10",
"@babel/node": "^7.12.10",
"@babel/preset-env": "^7.12.11",
"electron": "^11.1.0",
"nodemon": "^2.0.6"
}
}
正如您将在以下的输出和调试日志中看到的那样,这里的问题在于我们试图编译使用ES6语法的节点模块,但任何Electron应用都依赖于Electron模块,后者似乎不是以传统的方式导出,而是解决了electron可执行路径(字符串),而不是一个Node.js模块。这是一个循环问题。
app.js
import {app, BrowserWindow} from 'electron'
import 'url'
import 'path'
let win
function createWindow() {
win = new BrowserWindow({width: 800, height: 600})
win.loadURL(url.format ({
pathname: path.join(__dirname, 'index.html'),
protocol: 'file:',
slashes: true
}))
}
app.on('ready', createWindow)
.babelrc文件{
"presets": [
"@babel/preset-env"
]
}
我正在运行:
npm run compile
导致错误的原因是:
C:\Users\jonat\documents\github\infinitum\app.js:23
_electron.app.on('ready', createWindow);
^
TypeError: Cannot read property 'on' of undefined
at Object.<anonymous> (C:\Users\jonat\documents\github\infinitum\/app.js:16:5)
at Module._compile (internal/modules/cjs/loader.js:1076:30)
at Module._compile (C:\Users\jonat\Documents\GitHub\infinitum\node_modules\pirates\lib\index.js:99:24)
at Module._extensions..js (internal/modules/cjs/loader.js:1097:10)
at Object.newLoader [as .js] (C:\Users\jonat\Documents\GitHub\infinitum\node_modules\pirates\lib\index.js:104:7)
at Module.load (internal/modules/cjs/loader.js:941:32)
at Function.Module._load (internal/modules/cjs/loader.js:782:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
at Object.<anonymous> (C:\Users\jonat\Documents\GitHub\infinitum\node_modules\@babel\node\lib\_babel-node.js:172:21)
at Module._compile (internal/modules/cjs/loader.js:1076:30)
所以为了调试,我尝试了这个
app.js
:import electron from 'electron'
console.log("typeof electron:", typeof electron, "\nelectron:", electron)
输出:
typeof electron: string
electron: C:\Users\jonat\documents\github\infinitum\node_modules\electron\dist\electron.exe
而进一步的澄清,像这样的app.js:
import * as name from 'electron'
console.log({ name })
日志:
{
name: {
default: 'C:\\Users\\jonat\\documents\\github\\infinitum\\node_modules\\electron\\dist\\electron.exe'
}
}
我意识到这可能是因为“electron .”在解析流水线中有特殊作用。但我确实听说过Babel是在Electron中使用ES6导入语法的解决方案,只是找不到实际操作指南。那么我该如何在Electron中使用Babel?