Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making a browser and node compatible Javascript library

I am attempting to make a javascript library which I would prefer to be compatible with both browsers and node. However, there is some functionality offered in the node API that isn't offered in browsers (such as compression). I know it would be possible to code this functionality in javascript so it would be cross-compatible, but the node native compression will probably perform much better as it is much lower level.

How should I split between browser-compatible code and code that uses node API?

The way I see it, I could do one of the following:

  • make 2 separate scripts, one for node and one for browsers
  • make my code figure out the environment it is in and act accordingly
  • make all my code the same, but lose some performance improvements I would have had in node

What should I do to solve this?

like image 922
Matt Bell Avatar asked Mar 05 '26 07:03

Matt Bell


1 Answers

I know this is an old question, however, today it is possible easily with Browserify. Browserify lets you write nodejs modules with require() syntax and have them converted to browser complain code easily!

They even ported zlib which you mention to work with it, so that dependency is OK.

I hope this helps future readers, browserify helped me :)

like image 188
Benjamin Gruenbaum Avatar answered Mar 07 '26 21:03

Benjamin Gruenbaum



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!