跳转到主内容

宣布 Electron 中的 TypeScript 支持

· 阅读时间:约 6 分钟

electron npm 包现在包含一个 TypeScript 定义文件,提供整个Electron API的详细注释。 这些注释可以改进您的 Electron 开发 感受 ,即使您正在编写原版JavaScript 只需要运行 npm install electron 就可以在您的项目中获得最新的 Electron 类型注释。


TypeScript 是一种由Microsoft创建的开源编程语言。 它是一个添加了对 静态类型的支持并扩展了语言的 JavaScript 超集。 TypeScript 社区近年来迅速增长。 TypeScript 在 最近的Stack Overflow开发者调查 名列最受开发者喜爱的编程语言中。 TypeScript 被描述为 "增强的JavaScript", GitHub, Slack, 和 Microsoft 都用它来写成可缩放的 Electron 应用,这些应用被数以百万计的人使用 。

TypeScript 支持 JavaScript 中的许多较新的语言功能,如 类,对象析构, 异步,但其真正的不同的 功能是 类型注释。 声明程序所期望的输入和输出数据类型可以通过帮助您在编译时找到错误降低错误,类型注释还可以作为程序工作方式的正式声明

当库使用原版 Javascript 编写时,类型常常很模糊 定义通常是写文档时的事后思考。 函数通常可以 接受更多的类型,而不是文件记录的类型, 或者函数可能有隐藏的 未被记录的约束,因此可能导致运行时错误。

TypeScript 用 定义文件 解决了这个问题。 TypeScript 定义文件描述了库的所有函数和它的 预期输入和输出类型。 当库作者将一个 TypeScript 定义文件与其已发布的库捆绑在一起时, 该库的使用者可以在他们的 编辑器 中探索它的API,并立即开始使用它。 常常不需要查阅图书馆的 文档。

很多受欢迎的项目,例如 , Vue. s, node-github (现在是 Electron! )编译他们自己的定义文件并将它与他们的 已发布的 npm 软件包捆绑在一起。 对于那些不将自己的定义文件捆绑在一起的项目, 通常有 DefinitelyTyped, 社区维护定义文件的第三方生态系统。

安装

从 1.6.10版本开始,Electron 的每次版本更新都包含它自己的 TypeScript 定义文件。 当您从 npm 安装 electron 软件包时, electron.d.ts 文件会自动与 安装的软件包捆绑在一起。

最安全的 安装 Electron 的方式是使用准确的版本号:

npm install electron --save-dev --save-exact

或者如果您正在使用 yarn

yarn add electron --dev --exact

如果您已经使用了第三方定义,例如 @types/electron@types/node, 您应该将它们从您的 Electron 项目中删除,以防止任何冲突

定义文件来自我们的 结构化 API 文档, 因此它将始终与 Electron 的 API 文档 一致。 只要安装electron,你就能获取到和你的版本一致的最新TypeScript定义

用法

关于如何安装和使用Electron新的 TypeScript 定义的概要, 观看这个简短的演示:

如果您正在使用 Visual Studio Code,您已经 得到了内置的 TypeScript 支持。 还有社区维护的 插件用于 Atom, Sublime, vim, 和 其他编辑器

一旦您的编辑器配置好了 TypeScript ,您将开始看到更多的 基于上下文的行为,例如自动补全,内嵌方法引用, 参数检查等等。

Method autocompletion

Method reference

Argument checking

开始使用 TypeScript

如果你刚刚知道 TypeScript 并想了解更多信息, 此 来自Microsoft 的介绍视频 提供了关于为何创建这门语言的很好的概述。 如何工作, 如何使用它, 以及它的未来。

在官方的 TypeScript 网站上还有 HandbookPlayground

因为TypeScript 是 JavaScript 的超集,您现有的 JavaScript 代码 已经是有效的 TypeScript。 这意味着您可以根据需要逐步将现有的 JavaScript 项目转换为 TypeScript 并使用新语言功能。

谢谢!

没有Electron的 开源社区维护者的帮助,这个项目是不可能实现的。 感谢 Samuel Attard, Felix Rieseberg, Birunthan Mohanathas, Milan Burda, Brendan Forster, 和许多其他人的错误修复、文件改进、 和技术指导。

支持

如果您在使用 Electron 新的 TypeScript 定义文件时遇到任何问题, 请在 electron-typescript-definition 仓库中提交一个Issue。

尽情使用 TypeScript 吧!

Project of the Week: Jasper

· 阅读时间:约 5 分钟

本周我们会见了 Jasper的创建者,这是一个基于 Electron 的 管理GitHub 通知的工具。


Hello! Who are you?

I'm Ryo Maruyama, a software developer in Japan. I am developing Jasper and ESDoc.

What is Jasper?

Jasper 是 GitHub 灵活而强大的问题阅读器。 It supports issues and pull requests on github.com and GitHub Enterprise.

Jasper App Screenshot

Why did you make it?

When people use GitHub in their job or OSS activities, they tend to receive many notifications on a daily basis. As a way to subscribe to the notifications, GitHub provides email and web notifications. I used these for a couple of years, but I faced the following problems:

  • It's easy to overlook issues where I was mentioned, I commented, or I am watching.
  • I put some issues in a corner of my head to check later, but I sometimes forget about them.
  • To not forget issues, I keep many tabs open in my browser.
  • It's hard to check all issues that are related to me.
  • It's hard to grasp all of my team's activity.

I was spending a lot of time and energy trying to prevent those problems, so I decided to make an issue reader for GitHub to solve these problems efficiently, and started developing Jasper.

Who's using Jasper?

Jasper is used by developers, designers, and managers in several companies that are using GitHub. Of course, some OSS developers also are using it. And it is also used by some people at GitHub!

How does Jasper work?

Once Jasper is configured, the following screen appears. From left to right, you can see "streams list", "issues list" and "issue body".

Jasper Start Screen

This "stream" is the core feature of Jasper. For example, if you want to see "issues that are assigned to @zeke in the electron/electron repository", you create the following stream:

repo:electron/electron assignee:zeke is:issue

Jasper Start Screen 2

After creating the stream and waiting for a few seconds, you can see the issues that meet the conditions.

Jasper Start Screen 3

What can we do with streams?

I will introduce what kind of conditions can be used for stream.

Users and Teams

Stream问题
mentions:cat mentions:dogIssues that mention user cat or dog
author:cat author:dogIssues created by user cat or dog
assignee:cat assignee:dogIssues assigned to cat or dog
commenter:cat commenter:dogIssues that cat or dog commented on
involves:cat involves:dogIssues that "involve" cat or bob
team:animal/white-cat team:animal/black-dogIssues that animal/white-cat or animal/black-dog are mentioned in

involves means mention, author, assignee or commenter

Repositories and Organizations

Stream问题
repo:cat/jump repo:dog/runIssues in cat/jump or dog/run
org:electron user:cat user:dogIssues in electron, cat or dog

org is same as user

属性

Stream问题
repo:cat/jump milestone:v1.0.0 milestone:v1.0.1Issues that are attached to v1.0.0 or v1.0.1 in cat/jump
repo:cat/jump label:bug label:blockerIssues that are attached bug and blocker in cat/jump
electron OR atomshellIssues that include electron or atomshell

Review Status

Stream问题
is:pr review:requiredIssues that are required review in cat/jump
is:pr review-requested:catIssues that are requested review by cat.
But these are not reviewed yet.
is:pr reviewed-by:catIssues that are reviewed by cat

As you may have noticed by looking at these, streams can use GitHub's search queries. For details on how to use streams and search queries, see the following URLs.

Jasper also has features for unread issue management, unread comment management, marking stars, notification updating, filtering issues, keyboard shortcuts, etc.

Is Jasper a paid product? How much does it cost?

Jasper is $12. However you can use the free trial edition for 30 days.

Why did you choose to build Jasper on Electron?

I like the following aspects of Electron:

  • Apps can be developed with JavaScript/CSS/HTML.
  • Apps can be built for Windows, Mac, and Linux platforms.
  • Electron is actively developed and has a large community.

These features enable rapid and simple desktop application development. It is awesome! If you have any product idea, you should consider using Electron by all means.

What are some challenges you've faced while developing Jasper?

I had a hard time figuring out the "stream" concept. 首先,我考虑使用 GitHub 的 通知 API。 However I noticed that it does not support certain use cases. 此后,除了通知API外,我考虑使用 问题 API合并请求 API。 But it never became what I wanted. 然后在思考各种方法时,我认识到轮询GitHub的 搜索 API 将提供最大的灵活性。 It took about a month of experimentation to get to this point, then I implemented a prototype of Jasper with the stream concept in two days.

Note: The polling is limited to once every 10 seconds at most. This is acceptable enough for the restriction of GitHub API.

What's coming next?

I have a plan to develop the following features:

  • A filtered stream: A stream has some filtered stream that filter issues in the stream. It is like as view of SQL.
  • Multiple accounts: you will be able to use both github.com and GHE
  • Improve performance: For now the loading a issue in WebView is low speed than normal browser.

Follow @jasperappio on Twitter for updates.

Project of the Week: WebTorrent

· 阅读时间:约 9 分钟

This week we caught up with @feross and @dcposch to talk about WebTorrent, the web-powered torrent client that connects users together to form a distributed, decentralized browser-to-browser network.


What is WebTorrent?

WebTorrent is the first torrent client that works in the browser. It's written completely in JavaScript and it can use WebRTC for peer-to-peer transport. No browser plugin, extension, or installation is required.

Using open web standards, WebTorrent connects website users together to form a distributed, decentralized browser-to-browser network for efficient file transfer.

You can see a demo of WebTorrent in action here: webtorrent.io.

webtorrent homepage

Why is this cool?

Imagine a video site like YouTube, but where visitors help to host the site's content. The more people that use a WebTorrent-powered website, the faster and more resilient it becomes.

Browser-to-browser communication cuts out the middle-man and lets people communicate on their own terms. No more client/server – just a network of peers, all equal. WebTorrent is the first step in the journey to re-decentralize the Web.

Where does Electron come into the picture?

About one year ago, we decided to build WebTorrent Desktop, a version of WebTorrent that runs as a desktop app.

WebTorrent Desktop player window

We created WebTorrent Desktop for three reasons:

  1. We wanted a clean, lightweight, ad-free, open source torrent app
  2. We wanted a torrent app with good streaming support
  3. We need a "hybrid client" that connects the BitTorrent and WebTorrent networks

If we can already download torrents in my web browser, why a desktop app?

First, a bit of background on the design of WebTorrent.

webtorrent desktop logo

In the early days, BitTorrent used TCP as its transport protocol. Later, uTP came along promising better performance and additional advantages over TCP. Every mainstream torrent client eventually adopted uTP, and today you can use BitTorrent over either protocol. The WebRTC protocol is the next logical step. It brings the promise of interoperability with web browsers – one giant P2P network made up of all desktop BitTorrent clients and millions of web browsers.

“Web peers” (torrent peers that run in a web browser) make the BitTorrent network stronger by adding millions of new peers, and spreading BitTorrent to dozens of new use cases. WebTorrent follows the BitTorrent spec as closely as possible, to make it easy for existing BitTorrent clients to add support for WebTorrent.

Some torrent apps like Vuze already support web peers, but we didn't want to wait around for the rest to add support. So basically, WebTorrent Desktop was our way to speed up the adoption of the WebTorrent protocol. By making an awesome torrent app that people really want to use, we increase the number of peers in the network that can share torrents with web peers (i.e. users on websites).

What are some interesting use cases for torrents beyond what people already know they can do?

One of the most exciting uses for WebTorrent is peer-assisted delivery. Non-profit projects like Wikipedia and the Internet Archive could reduce bandwidth and hosting costs by letting visitors chip in. Popular content can be served browser-to-browser, quickly and cheaply. Rarely-accessed content can be served reliably over HTTP from the origin server.

The Internet Archive actually already updated their torrent files so they work great with WebTorrent. So if you want to embed Internet Archive content on your site, you can do it in a way that reduces hosting costs for the Archive, allowing them to devote more money to actually archiving the web!

There are also exciting business use cases, from CDNs to app delivery over P2P.

What are some of your favorite projects that use WebTorrent?

gaia app screenshot

The coolest thing built with WebTorrent, hands down, is probably Gaia 3D Star Map. It's a slick 3D interactive simulation of the Milky Way. The data loads from a torrent, right in your browser. It's awe-inspiring to fly through our star system and realize just how little we humans are compared to the vastness of our universe.

You can read about how this was made in Torrenting The Galaxy, a blog post where the author, Charlie Hoey, explains how he built the star map with WebGL and WebTorrent.

brave logo

We're also huge fans of Brave. Brave is a browser that automatically blocks ads and trackers to make the web faster and safer. Brave recently added torrent support, so you can view traditional torrents without using a separate app. That feature is powered by WebTorrent.

So, just like how most browsers can render PDF files, Brave can render magnet links and torrent files. They're just another type of content that the browser natively supports.

One of the co-founders of Brave is actually Brendan Eich, the creator of JavaScript, the language we wrote WebTorrent in, so we think it's pretty cool that Brave chose to integrate WebTorrent.

Why did you choose to build WebTorrent Desktop on Electron?

WebTorrent Desktop main window

Electron应用程序“繁忙”因为在每个应用程序中包含整个Chrome内容模块。 在某些情况下,这部分是真的 (Electron 应用程序安装器通常是 ~40MB, 其中,指定的 OST 安装器通常是 ~20MB)。

However, in the case of WebTorrent Desktop, we use nearly every Electron feature, and many dozens of Chrome features in the course of normal operation. If we wanted to implement these features from scratch for each platform, it would have taken months or years longer to build our app, or we would have only been able to release for a single platform.

Just to get an idea, we use Electron's dock integration (to show download progress), menu bar integration (to run in the background), protocol handler registration (to open magnet links), power save blocker (to prevent sleep during video playback), and automatic updater. As for Chrome features, we use plenty: the <video> tag (to play many different video formats), the <track> tag (for closed captions support), drag-and-drop support, and WebRTC (which is non-trivial to use in a native app).

Not to mention: our torrent engine is written in JavaScript and assumes the existence of lots of Node APIs, but especially require('net') and require('dgram') for TCP and UDP socket support.

Basically, Electron is just what we needed and had the exact set of features we needed to ship a solid, polished app in record time.

What are your favorite things about Electron?

The WebTorrent library has been in development as an open source side project for two years. We made WebTorrent Desktop in four weeks. Electron is the primary reason that we were able to build and ship our app so quickly.

Just as Node.js made server programming accessible to a generation of jQuery-using front-end programmers, Electron makes native app development accessible to anyone familiar with Web or Node.js development. Electron is extremely empowering.

Do the website and the Desktop client share code?

Yes, the webtorrent npm package works in Node.js, in the browser, and in Electron. The exact same code can run in all environments – this is the beauty of JavaScript. It's today's universal runtime. Java Applets promised "Write Once, Run Anywhere" apps, but that vision never really materialized for a number of reasons. Electron, more than any other platform, actually gets pretty darn close to that ideal.

What are some challenges you've faced while building WebTorrent?

In early versions of the app, we struggled to make the UI performant. We put the torrent engine in the same renderer process that draws the main app window which, predictably, led to slowness anytime there was intense CPU activity from the torrent engine (like verifying the torrent pieces received from peers).

We fixed this by moving the torrent engine to a second, invisible renderer process that we communicate with over IPC. This way, if that process briefly uses a lot of CPU, the UI thread will be unaffected. Buttery-smooth scrolling and animations are so satisfying.

Note: we had to put the torrent engine in a renderer process, instead of a "main" process, because we need access to WebRTC (which is only available in the renderer.)

应在哪些领域改进Electron?

One thing we'd love to see is better documentation about how to build and ship production-ready apps, especially around tricky subjects like code signing and auto-updating. We had to learn about best practices by digging into source code and asking around on Twitter!

Is WebTorrent Desktop done? If not, what's coming next?

We think the current version of WebTorrent Desktop is excellent, but there's always room for improvement. We're currently working on improving polish, performance, subtitle support, and video codec support.

If you're interested in getting involved in the project, check out our GitHub page!

Any Electron development tips that might be useful to other developers?

Feross, one of the WebTorrent Desktop contributors, recently gave a talk "Real world Electron: Building Cross-platform desktop apps with JavaScript" at NodeConf Argentina that contains useful tips for releasing a polished Electron app. 如果你处于一个基本的工作应用程序的阶段,并且你正在试图将它带到一个更高水平的打造和专业水平上,这个话尤其有用。

Watch here:

Slides here:

DC, another WebTorrent contributor, wrote a checklist of things you can do to make your app feel polished and native. It comes with code examples and covers things like macOS dock integration, drag-and-drop, desktop notifications, and making sure your app loads quickly.

Touch Bar Support

· 阅读时间:约 3 分钟

Electron 1.6.3 beta 版本包含对macOS Touch Bar 的初始支持。


The new Touch Bar API allows you to add buttons, labels, popovers, color pickers, sliders, and spacers. These elements can be dynamically updated and also emit events when they are interacted with.

This is the first release of this API so it will be evolving over the next few Electron releases. Please check out the release notes for further updates and open issues for any problems or missing functionality.

You can install this version via npm install electron@beta and learn more about it in the TouchBar and BrowserWindow Electron docs.

Big thanks to @MarshallOfSound for contributing this to Electron. 🎉

Touch Bar Example

Touch Bar Gif

Below is an example of creating a simple slot machine game in the touch bar. It demonstrates how to create a touch bar, style the items, associate it with a window, handle button click events, and update the labels dynamically.

const { app, BrowserWindow, TouchBar } = require('electron');

const { TouchBarButton, TouchBarLabel, TouchBarSpacer } = TouchBar;

let spinning = false;

// Reel labels
const reel1 = new TouchBarLabel();
const reel2 = new TouchBarLabel();
const reel3 = new TouchBarLabel();

// Spin result label
const result = new TouchBarLabel();

// Spin button
const spin = new TouchBarButton({
label: '🎰 Spin',
backgroundColor: '#7851A9',
click: () => {
// Ignore clicks if already spinning
if (spinning) {
return;
}

spinning = true;
result.label = '';

let timeout = 10;
const spinLength = 4 * 1000; // 4 seconds
const startTime = Date.now();

const spinReels = () => {
updateReels();

if (Date.now() - startTime >= spinLength) {
finishSpin();
} else {
// Slow down a bit on each spin
timeout *= 1.1;
setTimeout(spinReels, timeout);
}
};

spinReels();
},
});

const getRandomValue = () => {
const values = ['🍒', '💎', '7️⃣', '🍊', '🔔', '⭐', '🍇', '🍀'];
return values[Math.floor(Math.random() * values.length)];
};

const updateReels = () => {
reel1.label = getRandomValue();
reel2.label = getRandomValue();
reel3.label = getRandomValue();
};

const finishSpin = () => {
const uniqueValues = new Set([reel1.label, reel2.label, reel3.label]).size;
if (uniqueValues === 1) {
// All 3 values are the same
result.label = '💰 Jackpot!';
result.textColor = '#FDFF00';
} else if (uniqueValues === 2) {
// 2 values are the same
result.label = '😍 Winner!';
result.textColor = '#FDFF00';
} else {
// No values are the same
result.label = '🙁 Spin Again';
result.textColor = null;
}
spinning = false;
};

const touchBar = new TouchBar([
spin,
new TouchBarSpacer({ size: 'large' }),
reel1,
new TouchBarSpacer({ size: 'small' }),
reel2,
new TouchBarSpacer({ size: 'small' }),
reel3,
new TouchBarSpacer({ size: 'large' }),
result,
]);

let window;

app.once('ready', () => {
window = new BrowserWindow({
frame: false,
titleBarStyle: 'hidden-inset',
width: 200,
height: 200,
backgroundColor: '#000',
});
window.loadURL('about:blank');
window.setTouchBar(touchBar);
});

Project of the Week: Voltra

· 阅读时间:约 6 分钟

This week we met with Aprile Elcich and Paolo Fragomeni to talk about Voltra, an Electron-powered music player.


What is Voltra?

Voltra is a music player for people who want to own their music. It’s also a store where you can discover and buy new music based on what you already own. It’s ad-free, cross-platform for desktop and mobile. It also doesn’t spy on you.

voltra-artistview

Who is Voltra for?

Anyone who listens to music.

What motivated you to create Voltra?

Radio has has always had a big share of listeners. It’s moving off the airwaves and onto the Internet. Now you can rent music on demand — it’s a radio revival! A lot of new products and services have emerged because of this, but streaming radio still leaves someone else in control of your music and how you experience it.

We wanted a product that was entirely focused on music you own. Something that made it easy to discover and buy new music directly from artists or labels.

Is there a free version?

The desktop player is completely free. Selling your music is also free! We are not ad-supported.

Since the app is free, we may open source it later on. Right now we don’t have the bandwidth to manage that. We also have very specific ideas for features and the direction we want to take things. We have an active beta community and we take our feedback to heart.

How do you make money?

We have premium features!

Our Voltra Audio Archive is a cloud-backup service designed specifically for music. We don’t compress or share data blocks. Your music collection is physically backed up for you.

For artists and labels, our Pro Membership offers tools to help them reach more relevant audiences, such as analytics and professional artist webpages.

What makes Voltra different?

Design and usability are incredibly important to us. We want to give listeners a distraction-free listening experience! There are a some interesting music players and stores out there. But many of them are more advanced and harder to use than their creators realize. We want to make Voltra accessible to as many people as possible.

We also don't take a cut from the artist or the label. That’s a key differentiator for us. It’s really important because it lowers the barrier for artists to get their music to market.

What are some design & technical decisions you made?

While designing Voltra, we considered UI conventions from native apps and the web, we also thought a lot about what we could remove. We have an active private beta group who have given us critical feedback over the last few months.

We found that album art and photography are really important to people. Many players are just lists of files. One of the cool things about owning physical albums is the album art, and we wanted to put emphasis on this in the Voltra desktop app.

voltra-albumview

We also made sure not to mess with people's files. We use file watching so you can put your files wherever you want, and we don't rename them or move them for you. We have an embedded database to track the state of the watched directories so that we can track what's new, even when the process isn't running.

What are some challenges you've faced while building Voltra?

We spend a lot of time focused on performance. We started with frameworks but moved to vanilla Javascript. In our experience, the generalized abstractions they provide outweigh the performance penalties and ceremony that they introduce.

We handle very large collections pretty well at this point. Large collections means possibly tens of thousands of images! Having Node.js’ file system module directly available from the render process made it really easy to lazy load and unload lots of images super quickly based on DOM events.

一般来说, 设置立即requestIdleCallback 已经成为在保持界面响应的同时进行大量处理的超重要工具。 More specifically, distributing CPU-bound tasks into separate processes really helps to keep the user interface responsive. 例如,我们将实际音频环境移动到一个单独的过程中。 通过 IPC 与它通信,以避免忙碌用户界面可能出现的中断。

Why did you choose to build Voltra on Electron?

浏览器的沙盒对我们的应用过于限制。 但我们也正在开发一个 web 播放器。 So it’s a huge win that we can share almost 100% of the code between the two implementations.

We actually started by building a native app with Swift. The main problem we found was that we were reinventing a lot of things. The web has the world’s largest open source eco-system. So we pretty quickly switched to Electron.

Also, and most importantly, with Electron you develop once and it should Just Work™ on all the major platforms. It’s not guaranteed, but the cost of coding natively for each platform definitely outweighs any other costs that electron introduces.

What are your favorite things about Electron?

GTD!: Having Node.js’ networking stack and Chromium’s presentation layer packaged together is a recipe for getting things done.

Competency: It’s just the web stack, so literally our whole team is involved in actually building the product.

Community: There is a highly organized community that knows how to communicate really well! We feel pretty great about developing with support like that.

In what areas could Electron be improved?

We would like to see Electron endorse a single packager. The packager is as important to Electron what the package manager is to Node. There are multiple packagers in user-land, each with interesting features but each with bugs. Consensus by the community would help to direct the energy being spent by contributors.

What's coming next?

We‘re currently developing a mobile app, and working with artists and labels to add their music to the Voltra shop. Hey! If you’re an artist or label, sign up now! We plan on opening up the shop when we reach our goal of 10 million tracks.

Electron Internals: Building Chromium as a Library

· 阅读时间:约 7 分钟

Electron is based on Google's open-source Chromium, a project that is not necessarily designed to be used by other projects. This post introduces how Chromium is built as a library for Electron's use, and how the build system has evolved over the years.


Using CEF

The Chromium Embedded Framework (CEF) is a project that turns Chromium into a library, and provides stable APIs based on Chromium's codebase. Very early versions of Atom editor and NW.js used CEF.

To maintain a stable API, CEF hides all the details of Chromium and wraps Chromium's APIs with its own interface. So when we needed to access underlying Chromium APIs, like integrating Node.js into web pages, the advantages of CEF became blockers.

So in the end both Electron and NW.js switched to using Chromium's APIs directly.

Building as part of Chromium

Even though Chromium does not officially support outside projects, the codebase is modular and it is easy to build a minimal browser based on Chromium. The core module providing the browser interface is called Content Module.

To develop a project with Content Module, the easiest way is to build the project as part of Chromium. This can be done by first checking out Chromium's source code, and then adding the project to Chromium's DEPS file.

NW.js and very early versions of Electron are using this way for building.

The downside is, Chromium is a very large codebase and requires very powerful machines to build. For normal laptops, that can take more than 5 hours. So this greatly impacts the number of developers that can contribute to the project, and it also makes development slower.

Building Chromium as a single shared library

As a user of Content Module, Electron does not need to modify Chromium's code under most cases, so an obvious way to improve the building of Electron is to build Chromium as a shared library, and then link with it in Electron. In this way developers no longer need to build all off Chromium when contributing to Electron.

libchromiumcontent 项目是由 @aroben 为此目的创建的。 It builds the Content Module of Chromium as a shared library, and then provides Chromium's headers and prebuilt binaries for download. 它构建Chromium的内容 模块作为共享的库,然后提供Chromium的标题 并预建二进制二进制文件供下载。

亮度 项目也是作为libchromiumcontent的一部分生来的, 它提供了内容模块周围的薄层。

By using libchromiumcontent and brightray together, developers can quickly build a browser without getting into the details of building Chromium. And it removes the requirement of a fast network and powerful machine for building the project.

Apart from Electron, there were also other Chromium-based projects built in this way, like the Breach browser.

Filtering exported symbols

On Windows there is a limitation of how many symbols one shared library can export. As the codebase of Chromium grew, the number of symbols exported in libchromiumcontent soon exceeded the limitation.

The solution was to filter out unneeded symbols when generating the DLL file. It worked by providing a .def file to the linker, and then using a script to judge whether symbols under a namespace should be exported.

By taking this approach, though Chromium kept adding new exported symbols, libchromiumcontent could still generate shared library files by stripping more symbols.

Component build

Before talking about the next steps taken in libchromiumcontent, it is important to introduce the concept of component build in Chromium first.

As a huge project, the linking step takes very long in Chromium when building. Normally when a developer makes a small change, it can take 10 minutes to see the final output. To solve this, Chromium introduced component build, which builds each module in Chromium as separated shared libraries, so the time spent in the final linking step becomes unnoticeable.

Shipping raw binaries

With Chromium continuing to grow, there were so many exported symbols in Chromium that even the symbols of Content Module and Webkit were more than the limitation. It was impossible to generate a usable shared library by simply stripping symbols.

最后,我们必须 运送原始二进制的 Chromium 而不是 生成一个单一的共享库。

As introduced earlier there are two build modes in Chromium. As a result of shipping raw binaries, we have to ship two different distributions of binaries in libchromiumcontent. One is called static_library build, which includes all static libraries of each module generated by the normal build of Chromium. The other is shared_library, which includes all shared libraries of each module generated by the component build.

In Electron, the Debug version is linked with the shared_library version of libchromiumcontent, because it is small to download and takes little time when linking the final executable. And the Release version of Electron is linked with the static_library version of libchromiumcontent, so the compiler can generate full symbols which are important for debugging, and the linker can do much better optimization since it knows which object files are needed and which are not.

So for normal development, developers only need to build the Debug version, which does not require a good network or powerful machine. Though the Release version then requires much better hardware to build, it can generate better optimized binaries.

The gn update

Being one of the largest projects in the world, most normal systems are not suitable for building Chromium, and the Chromium team develops their own build tools.

Earlier versions of Chromium were using gyp as a build system, but it suffers from being slow, and its configuration file becomes hard to understand for complex projects. After years of development, Chromium switched to gn as a build system, which is much faster and has a clear architecture.

One of the improvements of gn is to introduce source_set, which represents a group of object files. In gyp, each module was represented by either static_library or shared_library, and for the normal build of Chromium, each module generated a static library and they were linked together in the final executable. By using gn, each module now only generates a bunch of object files, and the final executable just links all the object files together, so the intermediate static library files are no longer generated.

This improvement however made great trouble to libchromiumcontent, because the intermediate static library files were actually needed by libchromiumcontent.

The first try to solve this was to patch gn to generate static library files, which solved the problem, but was far from a decent solution.

第二次尝试由 @alespergll从对象文件列表中生成自定义静态库。 It used a trick to first run a dummy build to collect a list of generated object files, and then actually build the static libraries by feeding gn with the list. It only made minimal changes to Chromium's source code, and kept Electron's building architecture still.

摘要

As you can see, compared to building Electron as part of Chromium, building Chromium as a library takes greater efforts and requires continuous maintenance. However the latter removes the requirement of powerful hardware to build Electron, thus enabling a much larger range of developers to build and contribute to Electron. The effort is totally worth it.

本周项目:WordPress Desktop

· 阅读时间:约 6 分钟

本周我们在 Automattic 平台上聊到了 WordPress Desktop, 一个开源的桌面客户端, 负责管理 WordPress 的内容.


WordPress 应用程序

每个人都了解 WordPress,但 WordPress 桌面究竟是什么?

WordPress.com Desktop 应用 提供了一个平滑的跨平台体验, 它可以让你将精力集中在你的内容和设计上, 而不需要让浏览器标签打乱你的思绪 — 或者维持网站原始的样子而且还可以继续访问. 结合我们的浏览器支持和移动应用程序,您可以随时随地构建您的网站,以任何有助于您完成工作的方式。

为什么要构建一个桌面应用来管理 WordPress 站点? 就不能全部基于网页吗?

它实际上使用了你在浏览器中访问 WordPress.com 时获得的完全相同的技术。 然而,它都是本地托管的,因此它的加载时间最短。 得益于本地特性的支持, 比如托盘和通知等等都可以使用, 你就可以专注于 WordPress 的站点和博客.

你为什么选择在 Electron 上构建 WordPress Desktop?

2015 年末,我们通过 Calypso 重建了 WordPress.com 站点, Calypso 是一个开源的使用 React 开发的现代化 JavaScript 应用. 我们开始研究 Electron,并且对 Calypso 进行了一些更改,使其能够在本地运行。 It was a compelling experience and we thought there was a lot of value in developing it further.

我们有几个团队在 Calypso 上工作。 要使用传统的桌面技术制作一个完整的多平台GUI客户端,将需要更多的工作。 通过使用Electron,我们2-4人的小团队能够利用其他团队的努力,并在几个月内构建桌面应用程序。

构建 WordPress 桌面时遇到了哪些挑战?

我们很快就有了一个运行应用的起始版本, 但让它表现的和本地应用一样则花费了很多时间. 这其中有一个很大的挑战, 这个过程需要在你自己的机器上运行一个 Calypso 的备份, 它就是一个 API 驱动的 UI. 这方面涉及许多衔接工作,各种变化都反馈给了 Calypso 本身。

此外,我们花费了大量精力为不同的平台打包应用程序—— 我们提供了Windows, macOS 和 Linux 版本 - 并且他们之间有很大的不同。

当时 Electron 还比较新,我们不断遇到一些可以很快得到解决的问题(有时是同一天!)

应在哪些领域改进Electron?

Electron 已经提供了我们构建桌面应用所需要的的大部分内容,并且从我们开始使用它以来就已经进展很快了。 尽管如此,也一些领域在桌面应用中被认为是理所当然的功能,比如拼写检查和查找/替换,就很难在 Electron 中复现。

我们也希望看到一些较新的 Chrome 技术渗透到 Electron 中。 我们特别热衷于尝试 WebVR 技术。

What are your favorite things about Electron?

我们选择Electron的主要原因,也是它最大的优势,是非常活跃和开放的社区。 Automattic 一直相信开源。 这是我们的核心原则之一,Electron 项目和社区遵循许多非常开放和积极的核心信念。

WordPress 桌面下一步是什么?

我们的模型的伟大之处在于,Desktop 应用程序受益于任何新的 Calypso 功能 - 它将不断改进。 我们希望可以为应用程序添加额外特性,例如离线支持和更好的系统通知,这将真正将应用程序带入本机领域

Automattic是否有团队在开发其他 Electron 应用程序?

当前,经过我们在桌面应用上的努力, Simpenote 团队决定使用 Electron 为 Windows 和 Linux 构建桌面应用程序(原生的 Mac 客户端已经存在)。 Simpenote Electron 应用 也是开源的,可在 Github上访问到。

我们也有一个即将使用 Electron 开发树莓派集成的团队。

如果其中任何一个听起来很有趣,那么我们 将很乐意听到你的想法

Any Electron tips that might be useful to other developers?

交付签名桌面软件的过程对我们来说相对较新,特别是对于Windows。 我们为 Windows 应用代码签名 撰写了一篇文章,其中包括该过程以及我们为正确完成此操作而经历的一些困难。

每周项目:Dat

· 阅读时间:约 9 分钟

本周的精选项目是 Dat,一个用于分发数据集的开源分布式 拨款资助的 工具。 Dat 由一个分布在世界各地的的团队编写和运营,团队中的许多人帮助撰写了这篇文章。


A screenshot of the main view of dat-desktop, showing a few rows of shared
dats

首先,什么是 Dat?

我们希望将点对点和分布式系统的最佳部分引入数据共享。 我们从科学数据共享开始,然后开始向研究机构、政府、公共服务部门和开源团队扩展。

另一种想法是 Dropbox 或 BitTorrent Sync 等同步和上传软件,但 Dat 是 开源的。 我们的目标是成为一个强大的、开源的、非营利的数据共享软件,用于不同大小的数据。

要使用 dat CLI 工具,你需要输入:

dat share path/to/my/folder

然后 Dat 会创建一个可以用来将该文件夹发送给他人的链接 —— 没有中央服务器或第三方可以访问您的数据。 与 BitTorrent不同的是,他人也不可能嗅探到谁在分享什么(查看 Dat 白皮书了解更多详情)。

现在我们知道什么是Dat。 Dat 桌面如何适应?

Dat Desktop 是一种让无法或不想使用命令行的人可以访问 Dat 的方式。 您可以在您的机器上托管多个 Dat ,并通过您的网络提供数据。

你能分享一些很酷的使用案例吗?

DataRefuge 和 Svalbard 项目

我们正致力于一个代号为 Svalbard 的与 DataRefuge 有关的项目, 一个致力于备份有消失危险的政府气候数据的小组。 Svalbard 以北极的斯瓦尔巴全球种子库命名,该库有一个位于地下的植物DNA大型备份库。 我们的版本是一个大型的有版本控制的公共科学数据集。 一旦我们知道并可以信任元数据,我们就可以建立其他很酷的项目,例如 分布式志愿数据存储网络

加利福尼亚公民数据联盟

CACivicData 是一个开源的存档,每天从加利福尼亚的CAL-ACCESS数据库中下载,追踪政治活动中的资金。 他们进行 每日发布,这意味着在他们的 zip 文件中储存大量重复的数据。 我们正在将他们的数据托管为一个 Dat 存储库,减少检索特定版本或更新到更新的版本所需的带宽和复杂度。

Electron 更新

虽然还没有明确是否实现,但我们认为一个有趣的使用案例是把编译后的 Electron 应用程序放入一个 Dat 储存库,然后使用 Electron 内的 Dat 客户端下载已完成编译的应用程序的最新更新变化文件,以节省下载时间并同时减少服务器的带宽成本。

谁应该使用 Dat Desktop?

任何想要通过 P2P 网络分享和更新数据的人。 包括数据科学家、开放数据黑客、研究人员、开发人员。 如果任何人都有我们尚未想到的很酷的使用情况,我们完全愿意接受反馈。 你可以进入我们的Gitter 聊天并问我们任何事情!

Dat 和 Dat Desktop 的下一步是什么?

用户账户和元数据的发布。 我们正在开发一个 Dat 注册表网络应用,部署在 datproject.org,基本上将成为数据集中的 “npm”, 但应注意的是,我们只是一个元数据目录,而数据可以在任何线上服务存放(而不是像 npm 或像 GitHub 因为源代码太小而可以在一个系统中存放全部而选择在中央服务器上存放数据)。 由于许多数据集是巨大的,我们需要一个联合注册表(类似于BitTorrent 追踪器的工作原理)。 我们想让人们更容易从Dat Desktop找到或发布注册表中的数据集, 尽可能减少数据共享进程中的阻碍。

另一个功能是多作者/协作文件夹。 我们有开展协作工作流的大计划,或许包括围绕数据集协作设计的类 git 式的分支管理。 当然,我们目前仍在为提升总体稳定度和使我们的协议标准化而工作!

为何选择在 Electron 上构建 Dat 桌面?

Dat是使用Node.js建造的,所以它对我们的集成来说是十分自然和合适的。 除此之外,我们的用户使用各种机器,因为科学家, 研究人员和政府官员可能被迫为其机构使用某些设置——这意味着我们需要能够服务 Windows 和 Linux 以及 Mac。 Dat Desktop 让我们很容易做到这一点。

建造 Dat 和 Dat Desktop 时你面临什么挑战?

搞清楚人们想要什么。 We started with tabular datasets, but we realized that it was a bit of a complicated problem to solve and that most people don't use databases. So half way through the project, we redesigned everything from scratch to use a filesystem and haven't looked back.

We also ran into some general Electron infrastructure problems, including:

  • Telemetry - how to capture anonymous usage statistics
  • Updates - It's kind of piecemeal and magic to set up automatic updates
  • Releases - XCode signing, building releases on Travis, doing beta builds, all were challenges.

We also use Browserify and some cool Browserify Transforms on the 'front end' code in Dat Desktop (which is kind of weird because we still bundle even though we have native require -- but it's because we want the Transforms). To better help manage our CSS we switched from Sass to using sheetify. It's greatly helped us modularize our CSS and made it easier to move our UI to a component oriented architecture with shared dependencies. For example dat-colors contains all of our colors and is shared between all our projects.

We've always been a big fan of standards and minimal abstractions. Our whole interface is built using regular DOM nodes with just a few helper libraries. We've started to move some of these components into base-elements, a library of low-level reusable components. As with most of our technology we keep iterating on it until we get it right, but as a team we have a feeling we're heading in the right direction here.

应在哪些领域改进Electron?

We think the biggest pain point is native modules. Having to rebuild your modules for Electron with npm adds complexity to the workflow. Our team developed a module called prebuild which handles pre-built binaries, which worked well for Node, but Electron workflows still required a custom step after installing, usually npm run rebuild. It was annoying. To address this we recently switched to a strategy where we bundle all compiled binary versions of all platforms inside the npm tarball. This means tarballs get larger (though this can be optimized with .so files - shared libraries), this approach avoids having to run post-install scripts and also avoids the npm run rebuild pattern completely. It means npm install does the right thing for Electron the first time.

What are your favorite things about Electron?

它的API似乎相当深思熟虑,它相对稳定。而且它在做到实时跟进上游 Node 发布的最新版本这方面做得不错,我们也没别的要求了!

Any Electron tips that might be useful to other developers?

If you use native modules, give prebuild a shot!

关注 Dat 开发的最佳途径是什么?

在 Twitter 上关注 @dat_project 或者 订阅我们的 电子邮箱

Project of the Week: Ghost

· 阅读时间:约 5 分钟

This week we chatted with Felix Rieseberg, desktop engineer at Slack and maintainer of Ghost Desktop, an Electron client for the Ghost publishing platform.


Ghost Desktop Screenshot

What is Ghost?

Ghost is a fully open source, hackable platform for building and running a modern online publication. We power blogs, magazines and journalists from Zappos to Sky News.

What makes it different from other publishing platforms?

Ghost was founded in April 2013, after a very successful Kickstarter campaign to create a new platform focused solely on professional publishing. Our mission is to create the best open source tools for independent journalists and writers across the world, and have a real impact on the future of online media. It offers a simpler, more focussed experience: Our editor is designed solely around providing the best possible writing experience.

Compared to the all-time classic WordPress, it offers a simpler, more streamlined experience - it is easier to setup and maintain, comes with all important features out-of-the-box, and is dramatically faster. Compared to other online platforms, Ghost gives writers full ownership and control over their content, allows full customization, and enables authors to build a business around their publication.

Is Ghost a for-profit company?

This one is important to us: Ghost is an independent non-profit organisation. We build publishing tools for modern journalism & blogging because we believe freedom of speech is important. Our software is released under a free open source license, our business model is completely transparent, and our legal structure means that 100% of the money we make is reinvested into making Ghost better.

What is Ghost Desktop?

Ghost Desktop allows writers to manage multiple blogs at once - and to focus on their writing. 像常见的写作快捷方式这样的简单东西无法在浏览器中实现,但我们的桌面应用程序中可以使用。 它允许其他应用程序通过 deeplinks 与博客直接联系

What is Ghost for Journalism?

This year we're very excited to be dedicating our entire 10 person full-time Ghost team to helping grow three independent publications, along with $45,000 in resources toward their efforts. We're calling it Ghost for Journalism.

We've been building Ghost as the web's next great platform for independent publishers for about three and half years now, and we've now reached a really interesting inflection point. We started this journey to create a simple, well designed blogging platform which could be used by just about anyone. That was always going to be step one.

Long term, we want Ghost to be an incredible platform for the world's best journalism, and that means we need to build features to attract exactly those people. This year we're making a very conscious decision to focus on just that.

Why did you choose to build Ghost Desktop on Electron?

Ghost uses JavaScript and Node.js on both the backend and frontend, so being able to utilize the same technology and skillset enables our team to move faster, build more, and ultimately deliver a better experience. In addition, being able to share more than 95% of code between the macOS, Windows, and Linux version of the app allows us to focus on building a great core user experience, without having to maintain one code base for each platform.

What are some challenges you've faced while building Ghost Desktop?

Spellchecking is likely still one of the most difficult services offered - we could easily utilize one of the many online services, but correctly spellchecking text in multiple languages while guarding the privacy and autonomy of our users is not an easy task.

应在哪些领域改进Electron?

We would love to see Electron bring the operating system's native spellchecking capabilities to their apps. We're dreaming about a world in which an <input> field receives the same services as a NSTextView, but we are also intimately aware how difficult that is.

What are your favorite things about Electron?

JavaScript is famous for being a vast ecosystem, involving countless tools and frameworks - but the convenience it affords us is hard to overstate. Building an app with Electron is only slightly harder than building a web app, which is an amazing feat.

Is Ghost done? If not, what's coming next?

Ghost Desktop is also an ongoing project - we're pretty far from being done. We have been talking for a while about bringing a full offline mode to our users, and we're getting fairly close. Other notable work areas are the extension and integration with other text editing apps (like Word or Atom), ultimately allowing people to write posts using their favorite tools. In general, once we've shipped the offline mode feature, we're looking for deeper integration with the operating system. If that sounds interesting to you, join us!

What are some of your favorite Electron apps?

I'm a big fan of Kap, Felony, and Visual Studio Code.

👻

本周主题:Beaker浏览器

· 阅读时间:约 6 分钟

本周我们约见了Paul FrazeeBeaker浏览器的创始人 。 Beaker是一个实验性的,在客户端使用Dat协议托管网站的对等网络浏览器。


Beaker是什么以及创造它的理由?

Beaker是一个参与式浏览器。 它是为独立骇客们打造的浏览器。

互联网是一个封闭的信息源。 如果你想对社交媒体的运转方式施加影响的话,你必须要在Facebook或Twitter里工作。 对搜索引擎巨头Google来说, 控制权掌握在公司而不是用户自己手中。

通过Beaker,我们有了一个新的Web协议: Decentralized Archive Transport。 "Dat"协议 免费创建站点,然后从设备中共享它们, 无需服务器。 这就是我们的革新之处。

Beaker协议

当你用Beaker访问一个Dat站点时,你会下载该站点的文件。 这个站点会永远是你的。 你可以保存它,复刻它,修改它,并自由地分享你的新版本。 这些都是开源的。

所以这是我们的目的:我们正在打造一个支持开放式网站的浏览器。 我们希望它成为社交骇客的一个工具。

谁会使用Beaker?

骇客们。 改装者。 创意者们。 喜欢折腾的人。

如何创建一个使用Dat协议的新项目?

我们有个类似于git加npm的 命令行工具bkr 以下是如何创建一个站点:

$ bkr fork dat://0ff7d4c7644d0aa19914247dc5dbf502d6a02ea89a5145e7b178d57db00504cd/ ~/my-fork
$ cd ~/my-fork
echo "我的叉不考虑上一个索引。 tml!" > index.html
$ bkr 发布

以下是如何复刻一个站点:

$ bkr fork dat://0ff7d4c7644d0aa19914247dc5dbf502d6a02ea89a5145e7b178d57db00504cd/ ~/my-fork
$ cd ~/my-fork
$ echo "My fork has no regard for the previous index.html!" tml!" > index.html
$ bkr 发布

这些网站随后用你的浏览器进行托管。 这有点像BitTorren;你在一个P2P网格中分享网站。

如果你想要一个用户界面,我们在浏览器中内置了一些基础工具,但我们正在将这些工具推送到用户手中。 这些以后都将成为可更改的用户应用程序。

为何选择使用Electron来构建Beaker?

对这个项目来说是显而易见的。 如果我独自复刻了Chrome,我这会儿就应该在写C++了! 没人想要那么做。 我了解整个Web技术栈,并且我可以很快速地用它来工作。 这是个不用多想就能做出的选择。

事实是,我不太确定在没有Electron的情况下我能做这件事。 这是一个伟大的软件。

在构建Beaker的过程中遇到过哪些难题?

其中一半都是在捣鼓工具,我能解决的都尽量解决了。

构建一个浏览器是非常容易的。 Electron基本上就是个构建浏览器的工具包。 ...除了浏览器的页签;这花了我很长时间才改正过来。 我最终奔溃了,并且学会了如何创建SVG。 这看起来好多了,但花了我3,4次迭代才弄好。

应在哪些领域改进Electron?

如果我可以将devtools停靠在一个webview中,那真是太好了。

Beaker的后续进展?

用于Dat站点的安全DNS域名。 一个名为"app scheme."的公共配置化URL方案。更多Dat的API。

对于那些可能有兴趣为本项目作出贡献的人们来说,Beaker在哪些方面需要帮助?

我们有很多未解决的问题。 不要害怕联系我。 在freenode的#beakerbrowser频道上。 我们为贡献者保留了一个页面,同时我们将会把你添加进来。 并且如果你访问奥斯汀,我会为你买一杯啤酒。

Any Electron tips that might be useful to other developers?

  1. 使用现成的构建工具。 相信我,你不会想纠结于你自身的解决方案中。 使用electron-builder。 使用样板仓库。
  2. 如果你需要在Electron仓库中创建一个issue,请花些功夫使其易于复现。 你将会更快地得到响应,团队也会对此表示感谢。 如果你能试着自己去修复它,那是更好的。 事实上,看到问题的核心是非常有意思的一件事。
  3. 至少阅读一次所有的指南和进阶文档。
  4. 不要构造浏览器,这是一个已经饱和的市场。