ejs-electron无法与电子伪造

时间:2018-09-16 14:03:13

标签: electron ejs electron-forge

在运行于Windows 10的电子锻造应用程序中,尽管看不到任何错误,但未呈现ejs模板。 我可以使用

创建的应用程序重现该问题。
electron-forge init ejs-test

我正在使用电子伪造5.2.2和ejs-电子2.03 这是我的index.js文件:

import { app, BrowserWindow } from 'electron'

import * as ejse from 'ejs-electron'

// Handle creating/removing shortcuts on Windows when installing/uninstalling.
if (require('electron-squirrel-startup')) { // eslint-disable-line global-require
  app.quit()
}

// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow

const createWindow = () => {
  // Create the browser window.
  mainWindow = new BrowserWindow({
    width: 800,
    height: 600,
  })

  ejse.data('testdata', 'Generated throuh EJS')
  // and load the index.html of the app.
  mainWindow.loadFile(`${__dirname}/index.ejs`)

  // Open the DevTools.
  mainWindow.webContents.openDevTools()

  // Emitted when the window is closed.
  mainWindow.on('closed', () => {
    // Dereference the window object, usually you would store windows
    // in an array if your app supports multi windows, this is the time
    // when you should delete the corresponding element.
    mainWindow = null
  })
}

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow)

// Quit when all windows are closed.
app.on('window-all-closed', () => {
  // On OS X it is common for applications and their menu bar
  // to stay active until the user quits explicitly with Cmd + Q
  if (process.platform !== 'darwin') {
    app.quit()
  }
})

app.on('activate', () => {
  // On OS X it's common to re-create a window in the app when the
  // dock icon is clicked and there are no other windows open.
  if (mainWindow === null) {
    createWindow()
  }
})

这是我的index.ejs文件:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title></title>
  </head>
  <body>
    <%= testdata %>
  </body>
</html>

当我用     npm start

我没有收到任何错误,但是<%= testdata%>按原样显示,而不是被替换。 在加载我的ejs文件之前调用ejse.listening()返回true。

对于非伪造的电子应用程序,相同的代码可以正常工作。

你能帮忙吗?

做更多的调查,我发现ejs-electron中的临界线

protocol.interceptBufferProtocol('file', protocolListener)

返回以下错误: 错误:该方案已被拦截

1 个答案:

答案 0 :(得分:1)

这是与https://github.com/electron-userland/electron-forge/issues/291相同的问题。 鉴于目前缺乏文档,升级到电子锻造v6太困难了,所以我找到了解决方法。

我需要使用ejs的原因是根据多种语言的消息文件动态生成html代码。我首先尝试动态生成它,并将loadURL与'data:text / html'协议一起使用。但是,这会产生很多问题,因为html与文件路径没有关联,因此任何引用(例如

所以我转而在构建时为每种语言生成一个html文件,然后在运行时根据语言选择要加载的一个。

所以有两个步骤: 我在我的package.json中添加了以下内容:

  "scripts": {
    "build" : "node build.js"
  },

而build.js是:

const ejs = require('ejs')
const fs = require('fs')
let langs = ['en', 'fr']
langs.forEach((lang) => {
  let msgdata = JSON.parse(fs.readFileSync(`${__dirname}/src/messages/${lang}.json`, 'utf8'))

  ejs.renderFile(`${__dirname}/src/views/pages/mindmap.ejs`,
    {
      'messages': msgdata,
      'otherlanguage': 'fr',
    },
    {},
    (err, str) => {
      fs.writeFileSync(`${__dirname}/src/mindmap-${lang}.html`, str, 'utf8')
    })
})

因此,npm run build会生成与我使用的语言文件一样多的html文件。

然后我的应用程序的index.js文件几乎是样板电子锻造:

const { app, BrowserWindow, ipcMain } = require('electron')
const ejs = require('ejs')
const fs = require('fs')

// Handle creating/removing shortcuts on Windows when installing/uninstalling.
if (require('electron-squirrel-startup')) { // eslint-disable-line global-require
  app.quit()
}

// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow

const createWindow = () => {
  // Create the browser window.
  mainWindow = new BrowserWindow()

  // and load the index.html of the app.
  mainWindow.loadURL(`file://${__dirname}/mindmap-${lang}.html`)

  // Emitted when the window is closed.
  mainWindow.on('closed', () => {
    // Dereference the window object, usually you would store windows
    // in an array if your app supports multi windows, this is the time
    // when you should delete the corresponding element.
    mainWindow = null
  })
}

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow)

// Quit when all windows are closed.
app.on('window-all-closed', () => {
  // On OS X it is common for applications and their menu bar
  // to stay active until the user quits explicitly with Cmd + Q
  if (process.platform !== 'darwin') {
    app.quit()
  }
})

app.on('activate', () => {
  // On OS X it's common to re-create a window in the app when the
  // dock icon is clicked and there are no other windows open.
  if (mainWindow === null) {
    createWindow()
  }
})

// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and import them here.
ipcMain.on('request-messages', (event, arg) => {
  mainWindow.webContents.send('messages', msgdata)
})
相关问题