It kills me that in my universal JavaScript app, I have DLL libraries extracted via webpack DLL plugin, but I have to choose between the default export (for use by browsers) or commonjs
(for use by Node).
In all the thousands of lines of vendor code, literally the only difference between the two exports is the very first line:
- module.exports =
+ var vendor =
My express app will only allow me to require
the module.exports
variant because it expects commonjs
modules, and my browser has no idea what to do unless it gets the var vendor
variant.
I don't want 99.999999...% identical copies of the same file.
Instinctively, this feels insane: I think both environments should be able to consume the same DLL libraries, since the app code is identical between them.
So I've been brainstorming options, and I've come up with the following possibilities:
- Use a client-side CommonJS loader like RequireJS (really don't like this option, as I'd have to force the download on the client at the cost of load time)
- Transform the DLLs on-the-fly to replace the first line when the client requests them, via Express (or Nginx, if that's possible); unsure what performance penalties I'd incur with this approach.
- Require the DLLs from a FileStream that does the find/replace when the Express app starts (again, no idea if its possible, but this feels like the least-invasive of the three options).
- Accept the fact that there's no way to avoid having two copies of the DLLs, and that each new dependency I add to my DLLs will require × 2 disk space et al.
I've been unsuccessful in finding implementation details to the middle two points, or in finding alternate approaches. I'm looking for answers beyond "just have two copies and move on with your life." If you know how to do the middle two points, I'd love to get implementation details from you. If you have an idea or approach I haven't listed here, I'm all ears!