Reputation: 123
In my electron-forge application, running on Windows 10, the ejs templates are not rendered, although no error is visible. I can reproduce the issue with an application created with
electron-forge init ejs-test
I am using electron-forge 5.2.2 and ejs-electron 2.03 Here is my index.js file:
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()
}
})
And here is my index.ejs file:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<%= testdata %>
</body>
</html>
When i run this with npm start
I get no error but the <%= testdata %> is displayed as-is, instead of being substituted. Calling ejse.listening() just before loading my ejs file returns true.
The same code works fine with a non-forge electron app.
Can you help?
Doing some more investigation, I have found out that the critical line in ejs-electron
protocol.interceptBufferProtocol('file', protocolListener)
was returning the following error: Error: The scheme has been intercepted
Upvotes: 0
Views: 1188
Reputation: 123
This is the same issue as https://github.com/electron-userland/electron-forge/issues/291. Upgrading to electron-forge v6 was too difficult given the current lack of documentation, so I found a workaround.
The reason I needed to use ejs wasto dynamically generate my html code based on a message file in multiple languages. I first tried to generate it dynamically and use loadURL with the 'data:text/html' protocol. However, this creates a lot of issues because the html is not associated with a file path and therefore any reference like
So I turned to generate one html file per language, at build time instead, and chose the one to load depending on the language, at runtime.
So two steps: I added the following to my package.json:
"scripts": {
"build" : "node build.js"
},
and the build.js is:
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')
})
})
Thus, npm run build generates as many html files as I have languages files.
Then my application's index.js file is pretty much boiler-plate electron-forge:
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)
})
Upvotes: 1