简体   繁体   English

如何将 Babel 与 Electron 一起使用?

[英]How do I use Babel with Electron?

I want to build an Electron app with ES6 import syntax so I can re-use modules between my Node.js and browser side JS without code duplication and found Electron is frustratingly behind the times on ES6 syntax support.我想用 ES6 导入语法构建一个 Electron 应用程序,这样我就可以在我的 Node.js 和浏览器端 JS 之间重复使用模块,而无需重复代码,并发现 Electron 在语法上令人沮丧。

I learned about this magical solution only to discover it was no longer maintained.我了解了这个神奇的解决方案,却发现它不再被维护。

So Babel to the rescue, or so I thought.所以巴别塔来救援,至少我是这么想的。 Google isn't exactly fruitful on the topic of Babel + Electron tutorials. Google 在 Babel + Electron 教程的主题上并不完全富有成果。 I also wanted to throw in Nodemon.我还想加入 Nodemon。

Here's my setup:这是我的设置:

package.json 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"
  }
}

As you're going to see in the following output and debugging logs, the problem here is we're trying to compile the node module to use ES6 syntax, but any Electron app is dependent on the Electron module, which seems to not export in a traditional way, resolving the electron executable path (string) instead of a Node.js module.正如您将在以下 output 和调试日志中看到的那样,这里的问题是我们正在尝试编译节点模块以使用 ES6 语法,但是任何 Electron 应用程序都依赖于导出模块中的 Z0DF2DA9CF88450E6758Z56DA4 中的 Z0DF2DA9CF88450E6758356DA5一种传统的方法,解析 electron 可执行路径(字符串)而不是 Node.js 模块。 It's a circular problem.这是一个循环问题。

app.js应用程序.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 .babelrc

{
  "presets": [
    "@babel/preset-env"
  ]
}

I'm running:我在跑:

npm run compile

Which gives the error:这给出了错误:

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)

So to debug I tried this app.js :所以调试我尝试了这个app.js

import electron from 'electron'
console.log("typeof electron:", typeof electron, "\nelectron:", electron)

Output: Output:

typeof electron: string
electron: C:\Users\jonat\documents\github\infinitum\node_modules\electron\dist\electron.exe

And as further clarification, an app.js like this:作为进一步的澄清,一个 app.js 像这样:

import * as name from 'electron'
console.log({ name })

logs:日志:

{
  name: {
    default: 'C:\\Users\\jonat\\documents\\github\\infinitum\\node_modules\\electron\\dist\\electron.exe'
  }
}

I'm realizing this is probably because the "electron."我意识到这可能是因为“电子”。 does something special in the parsing pipeline.在解析管道中做了一些特别的事情。 But I've definitely heard of Babel being the solution to using ES6 import syntax in Electron, I just cant find an actual guide on doing it.但我绝对听说过 Babel 是在 Electron 中使用 ES6 导入语法的解决方案,我只是找不到实际的指南。 So how can I use Babel with Electron?那么如何将 Babel 与 Electron 一起使用?

I think the problem is about your usage of babel-node .我认为问题在于您对babel-node的使用。 babel-node is a node cli clone that does babel transformations before executing JS code. babel-node是一个node cli 克隆,它在执行 JS 代码之前进行 babel 转换。 It is not a compiler.它不是编译器。 There is no --out-file flag defined for this cli.没有为此 cli 定义--out-file标志。 It's a pity that it does not warn you for using an unrecognized flag.遗憾的是,它没有警告您使用无法识别的标志。

In order to compile your ES6 files, you need to use babel cli.为了编译你的 ES6 文件,你需要使用babel cli。 Replacing babel-node with babel at your compile task should do the trick.在您的compile任务中用babel替换babel-node应该可以解决问题。

Also you need to replace your imports to import * as... from... syntax:您还需要替换导入以import * as... from...语法:

import * as url from 'url'
import * as path from 'path'

You can also check Electron 12 previews.您还可以查看 Electron 12 预览。 They support Node 14, which supports ES modules.它们支持支持 ES 模块的 Node 14。 So when Electron 12 is out, it should theoretically be possible to use ES modules without Babel.所以当 Electron 12 出时,理论上应该可以不用 Babel 使用 ES 模块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM