问题
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?
回答1:
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.
回答2:
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.
来源:https://stackoverflow.com/questions/38166617/what-does-it-mean-for-electron-to-combine-node-js-and-chromium-contexts