📜 ⬆️ ⬇️

ImEx.js will decorate your code

Did you write large projects using JS? Did you have many objects in object objects (eg LIB.Module.object.param)? I had this often and it caused quite a lot of inconvenience and a bad kind of code. Similarly, anonymous functions often had to be used; they do not look particularly neat.

ImEx.js


I wanted some very simple and accurate solution to the problem. It so happened that the project I am working on has a node.js module that brings together all the .js files into one build.js. And here a thought arose, and why not add a certain pre-processor, which modifies the input code somewhat, thereby allowing to make non-standard functions (lightweight CoffeeScript or TypeScript). This is how the small preprocessor ImEx.js appeared

purpose


Avoid long constructions

Suppose we have three entities World, World.Animal, World.animals.Dog, let's imagine how it will look like on pure JS:

// World.js var World = { animals: {} }; 

 // Animal.js World.Animal = function ( name, legNum ) { this.name = name; this.legNum = legNum; }; 

 // Dog.js World.animals.Dog = function () { this.barking = false; }; World.animals.Dog.prototype = Object.create( World.Animal ); World.animals.Dog.prototype.bark = function () { this.barking = true; }; World.animals.Dog.prototype.stopBark = function () { this.barking = false; }; 

')
If you use ImEx.js, the code will look like this:

 // World.js namespace Global; export World = { animals: {} }; 

 // Animal.js namespace World; export Animal = function ( name, legNum ) { this.name = name; this.legNum = legNum; }; 

 // Dog.js namespace World.animals; import World.Animal; export Dog = function () { this.barking = false; }; Dog.prototype = Object.create( Animal ); Dog.prototype.bark = function () { this.barking = true; }; Dog.prototype.stopBark = function () { this.barking = false; }; 


Three directives have been added 'namespace', 'import', 'export'. Everything is so simple that I think does not require explanation.

Make modular more explicit

This is achieved using import, export directives.

Completely avoid anonymous functions, but have their advantages.

If you delve into the implementation, each module turns into an anonymous function and all variables that are not marked with the import or export prefix do not become global. This is exactly what we wanted to achieve.

On completion

I think something similar already exists and if someone pokes a finger for comparison I will be grateful.

The code is open here.
Easy to install: npm install imex -g
More complete instructions for installing and running with examples are in the readme .

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


All Articles