最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - What does it mean for Electron to combine Node.js and Chromium contexts? - Stack Overflow

programmeradmin0浏览0评论

In a blog post the author mentions that Electron combines Node and Chromium into a "single context" which implies that we don't have to use Browserify to transform code.

I understand that one implication of Electron is you can build cross-platform desktop apps using web technologies. I also understand the reason why we're able to write to the filesystem is because Electron has Node baked in. Also, the reason we're able to use HTML/CSS/JS/DevTools is because Chromium is baked in. However, I don't think this is what the author is talking about.

  • How does Electron to combine Node and Chromium into a "single context"?
  • Why don't have to use Browserify anymore?

In a blog post the author mentions that Electron combines Node and Chromium into a "single context" which implies that we don't have to use Browserify to transform code.

I understand that one implication of Electron is you can build cross-platform desktop apps using web technologies. I also understand the reason why we're able to write to the filesystem is because Electron has Node baked in. Also, the reason we're able to use HTML/CSS/JS/DevTools is because Chromium is baked in. However, I don't think this is what the author is talking about.

  • How does Electron to combine Node and Chromium into a "single context"?
  • Why don't have to use Browserify anymore?
Share Improve this question edited Feb 23, 2017 at 7:02 mbigras asked Jul 3, 2016 at 4:01 mbigrasmbigras 8,05513 gold badges62 silver badges117 bronze badges 1
  • I think this means that there is no difference where your code currently run, server or client. So you should not write smthng like this: if (typeof window==='object') { // code for browser. – Ilya Novojilov Commented Jul 3, 2016 at 4:53
Add a comment  | 

2 Answers 2

Reset to default 14

Chromium is a Webkit based web browser with the V8 javascript engine. It supports all the usual browser and DOM APIs and thus is good for making web pages and not good at interacting with the underlying system.

Node.js was built by striping out the V8 engine, making a headless command line application, and adding extensive APIs to access the file system, require() other files, run other shell programs, etc. (things you'd expect of a true scripting language.

Electron in a simplified way is an attempt to replace the V8 engine used in Chromium with the new more general purpose oriented one of Node.js. It exposes a few extra APIs to node.js to allow for opening chromium windows, but also every chromium window using a <script> tag will interpret it with the node.js engine.

Why Electron? The reason that Chromium can't do this by itself is because it was originally designed to be a web browser and in web browsers file system APIs would be unheard of as typically files are hosted on a remote server and accessing files on a user's computer would be a security risk (because why should any single webpage have access to all your files?).

require statements now work out of the box because node.js has filesystem support will allows them to be synchronously read from the disk without the need for bundling them into the same javascript file or requesting them from a server.

So under normal circumstances, Node.js and the web browser are two separate contexts, which is why one would normally have to use Browserify to 'compile' the Node.js code for use with a web browser.

Same thing with a PHP script requiring some sort of handler by the web server to be able to properly execute within a web browser. Versus HTML & CSS and even JavaScript being able to execute within a web browser without any further intervention, as the web browser already contains all the necessary tools to parse HTML and interpret and run the JS.

With Electron, it's kinda of the same deal of how a modern Web Browser is able to execute JavaScript. With Electron, the Chromium has been modified to be able to execute Node. Electron is the Browserify, Electron is the container that allows Node.js and the Chromium to work together without any further modification or intervention.

So by saying that Node and Chromium have been combined into a single context, all that means is that Node and Chomium are able to work and interface with eachother as if they are one, without requiring any other tools or steps to 'link' the two together.

发布评论

评论列表(0)

  1. 暂无评论