npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

electron-view-renderer-spc

v2.11.0

Published

View rendering engine for electron framework.

Downloads

119

Readme

Electron View Renderer

npm npm node node electron

This package is intended to work with the electron framework for building desktop apps. It allows developers to parse templates on the fly vs creating static HTML files, or pre-rendering any templates.

In addition this package exposes a way to add additional renderer types beyond the ones that are added by default.

Currently the following are added by default:

['ejs']

Features

  • Simplified view templating
  • Custom view render engines
  • Custom asset protocol

Install

npm install --save electron-view-renderer

Usage

This package accomplishes its goal by registering custom protocols. This means that rather than listening to a protocol such as 'file://' or 'https://' it listens to the specified protocol (default is "view"). This matters because it allows us to do our template rendering without having to take over an existing protocol such as "file://" and yet still be able to treat it as such.

ElectronViewRenderer can be instantiated at any time, even before app is ready, but will ultimately wait for the app to be ready before registering new protocols. The new protocols will then be available throughout the lifetime of the process.

// require ElectronViewRenderer
const ElectronViewRenderer = require('electron-view-renderer')

// instantiate ElectronViewRenderer with
const viewRenderer = new ElectronViewRenderer({
  viewPath: 'views',            // default 'views'
  viewProtcolName: 'view',      // default 'view'
  useAssets: true               // default false
  assetsPath: 'assets'          // default 'assets'
  assetsProtocolName: 'asset'   // default 'asset'
})

// use favorite renderer (currently preloaded with ejs, but more coming
// and custom renderers can be plugged in)
viewRenderer.use('ejs')

...

// when instantiating a browser window
const window = new BrowserWindow({
  backgroundColor: '#fff',
  center: true,
  height: 600,
  width: 600,
})

viewRenderer.load(window, 'index', {myLocalVar: "value"})

Using the electron Quick Start Example

Given a basic file structure:

project/
├── assets/
│   └── css/
│       └── main.css
├── views/
│   └── index.ejs
├── main.js
└── package.json

project/main.js:

const {app, BrowserWindow} = require('electron')
const ElectronViewRenderer = require('electron-view-renderer')
const path = require('path')
const url = require('url')

const viewRenderer = new ElectronViewRenderer({
  viewPath: 'views',
  viewProtcolName: 'view',
  useAssets: true,
  assetsPath: 'assets',
  assetsProtocolName: 'asset',
})

viewRenderer.use('ejs')

// 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 win

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

  // // and load the index.html of the app.
  // win.loadURL(url.format({
  //   pathname: path.join(__dirname, 'index.html'),
  //   protocol: 'file:',
  //   slashes: true
  // }))

  // NOTE: instead of loadig a url as the Quick Start example shows, we are
  //       going to use the viewRenderer helper
  viewRenderer.load(win, 'index', {name: "Bob"})

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

  // Emitted when the window is closed.
  win.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.
    win = 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 macOS 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 macOS 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 (win === 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 require them here.

project/views/index.ejs:

<!doctype html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>Hello From <%= name %></title>
    <link href="asset://css/main.css" rel="stylesheet" type="text/css" />
  </head>
  <body>
    <h1>Hello From <%= name %>!</h1>
    We are using node <script>document.write(process.versions.node)</script>,
    Chrome <script>document.write(process.versions.chrome)</script>,
    and Electron <script>document.write(process.versions.electron)</script>.
  </body>
</html>

project/assets/css/main.css:

body {
  background-color: #bada55;
}

Make sure electron command is available

npm install -g electron

Add the package.json

npm init
npm install --save electron electron-view-renderer

Run the example

electron .

Adding custom renderers

The best example given could be with how ejs is added internally. The main requirement is that there is a rendererAction and that its callback is called with the rendered HTML:

viewRenderer.add('ejs', {
  extension: '.ejs',
  viewPath: 'views',
  rendererAction: (filePath, viewData, callback) => {
    ejs.renderFile(filePath, viewData, {}, (error, html) => {
      if (error) {
        if (error.file) error.message += `\n\nERROR @(${error.file}:${error.line}:${error.column})`
        throw new Error(error)
      }

      callback(html)
    })
  }
})

Dependencies

NOTE: this package does not officialy support electron-prebuilt. Please see https://stackoverflow.com/questions/41574586/what-is-the-difference-between-electron-and-electron-prebuilt

Package | Version --- |:---: electron | 1.6.11 ejs | 2.5.6 haml | 0.4.3 pug | 2.0.0-rc.2 captains-log | 1.0.2

Author

Dennis Baskin [email protected] http://github.com/dennisbaskin/electron-view-renderer