Lyo aim to fill that gap. This tool could turn any node.js package into a single file browser library.
Let's walk through an example to see it running.
Since npx exists, I don't even have to install Lyo, I can just run:
npx lyo get sha1
From top to bottom, we can see that :
- Lyo was installed in 6.5s
- run over sha1 version 1.1.1
- it read the file sha1.js
- output a file named sha1.min.js
- named the library
- Browserify, Babel and Uglify was successfully ran
- Lyo terminated without error
Then, I can simply add the output file into my HTML page.
<script src="sha1.min.js"></script> <script> console.log(sha1("P@ssWord")); // => 56355fa0c17cb7f9dc0d936f6c7aa385114ee097 </script>
How easy is this !
Not easy enough ? You don't even want to open your terminal ? Lyo got you covered. Open lyo.now.sh on your favorite browser and enter the name of the package you want to compile. Lyo will take care of everything and output how you can use this package over a CDN or node.js.
On the other side of the spectrum, why not provide your user with a browser version of your node.js package ?
Of course, you might already use Browserify or Webpack, but you can now delegate all the work to Lyo.
Open your project folder, then run:
npx lyo init npm install
Lyo will edit the
package.json file to add itself as a dependency and add a script to execute it.
Now, every time you publish your work on NPM, it will be packed in a single file. This file can then be directly downloaded or through a CDN like jsDelivr or unpkg.
A few caveat, Lyo can't ensure that its output is working under the browser environment. For example, a package using the file system or any node-only capability, won't be able to run on a browser. Also, Browserify is quite opinionated on the use of ES6 features like import/export. It means you can't use any syntax not already supported by the node.js' last version.
I really love how a so complex process can be used so easily. Whether you like or not the extreme modularity of NPM, Lyo allow you to harness this power.
See you all next month.