SocketCluster / asyngular

Highly scalable realtime framework compatible with SocketCluster
https://asyngular.io/
MIT License
77 stars 5 forks source link

Solution for using with FuseBox #7

Closed hollowcat closed 4 years ago

hollowcat commented 5 years ago

Hello. It appears that Asyngular loads wsEngine dynamically with a computed require:

[86] let wsEngine = typeof opts.wsEngine === 'string' ? require(opts.wsEngine) : opts.wsEngine;

This causes two problems with the FuseBox bundler. First, it cannot detect that it needs to bundle the ws package, which thus needs to be added to imports. Second, its runtime api then cannot resolve: https://fuse-box.org/docs/production-builds/quantum-configuration#computed-statement-resolution

Here is how to fix it.

Inside the template produced by Asyngular, remove everything express (as FuseBox provides its own dev server) and add the explicit import:

require('ws'); // otherwise fuse-box won't see that it is used and bundle it

Inside the FuseBox script, apply the api patch:

fuse.js ``` ... const fuse = FuseBox.init({ homeDir : ".", output : "dist/$name.js", target : "server@esnext", useTypescriptCompiler : true, plugins: [ JSONPlugin(), QuantumPlugin({ bakeApiIntoBundle: "server", target: "server", /*--- begin patch ---*/ api: core => { core.solveComputed("asyngular-server/server.js", { mapping: "^ws/index.js", fn: (statement, core) => { statement.setExpression(" 'ws/index.js' ") } }); //core.solveComputed("express/lib/view.js", ...); } /*--- end patch ---*/ }) ] }); fuse .bundle("server") .instructions("> server/server.js") .completed(proc => proc.start()); fuse.run(); fuse.dev({ root: "dist", fallback: "index.html" }); ```

express/lib/view.js was another file that caused trouble, should be fixable by analogy if you need express.

Perhaps this issue could be addressed to work readily by avoiding the dynamic require.

jondubois commented 4 years ago

Dynamic require is a very useful and commonly used feature. I think this is likely a problem with FuseBox and not with this project.