floatdrop / gulp-bem

BEM toolset for gulp
26 stars 2 forks source link

Propose a way for working with javascript, that exported by block #9

Closed floatdrop closed 10 years ago

floatdrop commented 10 years ago

For now gulp-bem has pretty obvious way to build css files, but in JavaScript land there is no cascade declarations in the core (but there is prototypes).

I propose a draft for including javascript files and building them together - browserify all JavaScript files with some additional sugar, that will handle require of bem identifiers. This could be done with exporting builded code in {bem} into require cache of browserify. It will fix issue with unclear require path's, but not with levels of definition (thou order of building will be handled by tree.deps, which is nice).

Example:

// base/omnibutton.js
// block/omnibutton.js
var Class = require('jsface').Class;

var Omnibutton = require('omnibutton');
var MyOmnibutton = Class(Omnibutton, { /*...*/ });

module.exports = MyOmnibutton;

Notice, that require('jsface') should automaticly work without any additional preparations (besides npm i jsface --save).

If we won't add extra features and use browserify as it is, require in block/omnibutton.js will export {} (by rules, that NodeJS using for resolving recursive dependencirs) and will not have base/omnibutton.js instance as we want.

May be it could be solved by forcing new cache entity after building concrete JavaScript file in block.

floatdrop commented 10 years ago

Possible drawback: name collisions.

floatdrop commented 10 years ago

Proof of concept - https://github.com/floatdrop/gulp-bem-js-pack

floatdrop commented 10 years ago

For AMD approach simple concatenation is working.