Skip to content
This repository has been archived by the owner on Jan 13, 2018. It is now read-only.

Commit

Permalink
Merge pull request #429 from bem/BEM-930
Browse files Browse the repository at this point in the history
Add ability to write tech module as a function (closes #363, BEM-930)
  • Loading branch information
Sergej Tatarincev committed Sep 11, 2013
2 parents 3ae967e + a3d5bc8 commit 8a41b46
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
15 changes: 12 additions & 3 deletions lib/tech/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,26 @@ var TechV1 = exports.Tech = require('./v1').Tech,
Q = require('q'),
U = require('../util'),

BEM,
getBem = function() {
if (!BEM) BEM = require('../..');

return BEM;
};
/**
* Return tech class for the tech module path.
*
* @param {String|Object} module Path to tech module or module object.
* @param {Level} [level] Level object to resolve techs by name.
* @returns {Tech} Tech class.
*/
getTechClass = exports.getTechClass = function(module, level) {
var getTechClass = exports.getTechClass = function(module, level) {

var tech = typeof module === 'string'? require(module) : module;

if (typeof tech === 'function') tech = tech(getBem());

var tech = typeof module === 'string'? require(module) : module,
TechClass = tech.API_VER > 1 ? TechV2 : TechV1;
var TechClass = tech.API_VER > 1 ? TechV2 : TechV1;

// link to tech class found in Tech property
if (tech.Tech) return tech.Tech;
Expand Down
11 changes: 11 additions & 0 deletions test/data/techs/function-tech.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';

module.exports = function(BEM) {
return {
techMixin: {
getBuildResults: function() {
return BEM.require.resolve('./techs/js');
}
}
};
};
6 changes: 6 additions & 0 deletions test/tech.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,12 @@ describe('tech', function() {

});

it('for module with function-style code', function() {
var T = getTechClass(require.resolve('./data/techs/function-tech.js'));
T = new T('function-tech', 'function-tech');
assert.equal(T.getBuildResults(), BEM.require.resolve('./techs/js.js'));
});

});

describe('v2', function() {
Expand Down

0 comments on commit 8a41b46

Please sign in to comment.