How may I import a Node module in the client code of my Electron app? - electron

I'm building a board game from ES6 modules using Electron 2 (for Chromium 61+) and the esm shim on the server side of things. This is the first time I've written isomorphic JavaScript, let alone ES6 modules; I intend to be able to run game logic on the client in single-player mode, and on the server in networked play mode. So far so good, I'm happy to report! And it's satisfying to not rely on any heavy transpilers.
Now, though, I have a problem: I intend to use types from Immutable JS on the client as well as the server, and I only know how to import them into the server code. Until now, all the import statements in the isomorphic code referred to other JS modules in the app, not to dependencies from npm. A module like the one below causes an "Uncaught TypeError: Failed to resolve module specifier 'immutable'" runtime error in the client when the app loads:
import Immutable from "immutable";
Immutable.List.of([]);
export { foo: {} };
In fact, I'm virtually certain that the import statement is failing because Chromium can't resolve "immutable" to a JS file. But how am I supposed to go about resolving it? And is there a way to resolve it that would work for any node module that is written to be isomorphic?

TL;DR - You can't without help of bundler like webpack as long as you're using npm modules.
Most of node.js package ecosystem is not ready for native module yet. About 99% of published package in npm currently using node.js's CommonJS module system, while there are very few module written to support esm (ES module syntax as well).
esm shim is intended to help latter - if module's written in esm and to be imported in current node.js version doesn't support it helps to resolve those modules. Opposite case doesn't work. Chromium can import your code directly which is written in native syntax, then try to resolve dependency module you specified and failed to resolve as 1. it doesn't know where to resolve (as it doesn't follow node.js's module resolution rules) 2. when it's available to resolve, actual import will fail cause module'll be cjs export instead of native.
Get back to TL;DR above - if the intention is achieving isomorphic code to run on both processes, use bundler accordingly.

Related

Rollup: globals & external

I'm trying to rollup my completely es6 module repo which has both local imports/export for the projects, and imports to dependencies that are also either scripts or modules.
I'm also trying to have a dual build which creates legacy iife modules via rollup.
This works fine for just my project, no problems. The difficulty is that I have imports for my dependencies.
Rollup's globals and external options are supposed to help but thus far I haven't succeeded in exposing these and rolling up to an iffe. I get
http://backspaces.github.io/asx/libs/three.module.js' is imported by src/Three.js, but could not be resolved – treating it as an external dependency
errors and others. The resulting rollups are not what I want: converting the iife rollup to expect the dependencies to be globals thus removed from the rollup.
I realize this is a pretty general question, but I just want to know how to use these two options to manage my repo so that I have imports to dependencies and can "remove" them in the rollup.
Can anyone clearly explain them and what they do? The rollup wiki is slightly helpful but not complete enough.
For Rollup to be able to include a dependency, it has to be able to find it. It doesn't have any built-in logic for fetching a remote URL such as http://backspaces.github.io/asx/libs/three.module.js (that could be done as a plugin, but AFAIK that plugin hasn't been written, and I'd probably advise against it anyway).
Instead, you'd be better off importing the module from node_modules like so...
import THREE from 'three';
...and adding node-resolve and commonjs to the config that generates the IIFE.
For the config that generates the non-IIFE build where Three.js is kept external, you would need to use the paths config to point three back to the URL:
// rollup.config.js
export default {
entry: 'src/main.js', // or whatever
// ...
external: ['three'], // so it's not included
paths: {
three: http://backspaces.github.io/asx/libs/three.module.js
}
};

Other tran transpiling to JS, does google dart compile to object code?

Google dart compiles to javascript code but dart is also a standalone language. I know Dart can also run without javascript (on the vm), but to what does it compile to? Does it compile to C or object code? I couldnt find any information about it.
Im also asking because dart works on mobile devices and ive also read it will be used in some experimental OS called fuchsia.
As far as I know
it's compiled to binary code on-the-fly in the VM,
it's compiled to binary code ahead of time with AoT.

Rascal slow at importing modules

I am running Rascal from the REPL and it seems like it takes a pretty long time to import some modules. For example import lang::java::\syntax::Java15; takes seconds to run.
I've also noticed cases where modules that depend on other modules don't appear to be reloaded if they are changed. For example:
program 1:
module A::A
....
program 2:
module B::B
import A::A;
...
REPL:
import A::A;
import B::B;
Now I've made some changes to A and B and I import B again. I would imagine the changes to A would get propagated to the new version of B (since it is importing A) but this doesn't seem to happen.
Why is importing this slow and is there a way to speed this up?
How does importing packages with dependencies in the REPL work?
Thanks!
We recently changed quite a bit about this part of the implementation. So could you tell us which version you are using?
Importing is slow right now because we have a bottleneck in the parsing infrastructure, as far as I can remember. Speeding it up; you can do by not using a console in Debug mode (i.e. use Run As...), using more memory for Eclipse also helps (I use 1.8Gb heap and an 80mb stack).
The REPL works in Eclipse by monitoring which modules have changed since running the previous command on the REPL. When a new command is entered, such as an import command, first all modules which have changed and the modules they depend on are purged, this produces an initial worklist for reloading, which is then executed in a fixpoint fashion to load the new modules (each module only once), then finally the command is executed.

Writing an eCAP plugin for Squid: How to link OpenSSL and other libraries

I'm writing an eCAP adapter (in C++) for Squid. I've seen that libtool is required in order to create a library and import it into Squid.
I started from the adapter_modifying example (that can be found here http://www.measurement-factory.com/tmp/ecap/ecap_adapter_sample-0.2.0.tar.gz) and added some features (encryption of JSON objects).
In order to do so, I'm using this library https://code.google.com/p/rapidjson/ and this OpenSSL wrapper https://github.com/shanet/Crypto-Example
After compiling and installing the adapter (it involves the creation of a library with libtool), I've run squid but the adapter crashes as soon as I instanciate an object of the OpenSSL wrapper.
My plugin stops running as soon as I instantiate the Crypto wrapper:
Crypto crypto;
If I don't execute the adaptation method and just buffer and forward the chunks, everything works fine. Do you see anything in the code of this library https://github.com/shanet/Crypto-Example that may cause this issue?
Do you know how I can access the error log (or standard error) of my plugin?
How can I correctly link these two libraries to my adapter? Which commands should I execute?

How do I know which modules to include when packaging with py2app?

I'm trying to package the Mac version of an open source application that I didn't write (I'm not much of a coder). I'm using py2app 0.6.4. The application builds on my system properly, but I'm unsure of what to list for the includes in the setup.py file.
The dependencies include qt4, PyQt, matplotlib, cherrypy, and sip.
When I looked at this article on handling PyQt applications, I noticed the dependencies were not listed simply as PyQt but rather *PyQt4._qt* etc. How can I determine what to insert in the includes statement from the code of the application?
When py2app runs, it's going to look at each of your scripts, automatically grabbing any modules or packages imported by your scripts. In many cases, this will suffice and you won't need to list anything in the includes variable. Some packages have extra files such as data files that aren't used by the import statement, but must be present for the package to run correctly. Then you need to explicitly include it so py2app will grab it as well. Try to use your app; if you get an error that some module or file isn't found then worry about putting it in the includes variable.

Resources