⬆️ ⬇️

Simple CommonJS Packer Modules for use in the browser - clinch

If you use stitch and you don't have enough of it, and browserify seemed complicated in settings - try a clinch .



What is in the box:





The main feature I wanted to solve is the native use of CommonJS modules, including dependencies installed via npm. It is clear that modules using IO or having C-bindings will not work, but a large layer of simple modules can be transferred to the client.

The second trick - I like the development through testing, ideally - all client code would be good to run through mocha , in an automated version.

Well, nice bonuses - a reasonable amount of “binding” code, unambiguous resolution of dependencies, versioning, elimination of duplicates, the ability to have any number of bundles on the page.



In my opinion, all the ideas have been quite successfully implemented.

')

Installation - local npm install clinch .



Well, a simple example of use:

 #!/usr/bin/env coffee Clinch = require 'clinch' packer = new Clinch() pack_config = bundle : main : "#{__dirname}/hello_world" packer.buldPackage 'my_package', pack_config, (err, data) -> console.log 'Builder, data: \n', data 


for 'hello_world.coffee' containing

 module.exports = hello_world : -> 'Hello World!' 


on output will give something like this

 (function() { <... skip clinch header ...> dependencies = {}; sources = { "2377150448": function(exports, module, require) { module.exports = { hello_world: function() { return 'Hello World!'; } }; }}; this.my_package = { "main": require(2377150448)}; }).call(this); 


And in the browser the function will be available like this.

 hello_world = my_package.main.hello_world 


If you don’t like the idea of ​​injecting into the global object of properties, you can turn it off, besides there are several settings for the jade compiler.

The project has a Russian-language manual , which will probably be better thanks to your questions and comments.

In addition, there is a demo project in which you can see the code and results on the client.



Ps. I will be immensely grateful to anyone who teaches how to write good manuals.



Update: since 0.2.5 it is possible to register your own handler (or override the embedded ones), the method is called registerProcessor() .

It works like this:

 # add .econ processor packer.registerProcessor '.econ', (file_content, filename, cb) -> content = Eco.precompile file_content cb null, "module.exports = #{content}" 




An example of how to configure and use Handlebars - look here .

Source: https://habr.com/ru/post/172831/



All Articles