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)
Conflicts:
	lib/tech/index.js
	test/tech.js
  • Loading branch information
Sergej Tatarincev authored and Сергей Сергеев committed Sep 13, 2013
1 parent 817b224 commit aa7fb92
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
16 changes: 12 additions & 4 deletions lib/tech/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +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,
requiredVersion = tech.API_VER,
TechClass = requiredVersion > 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');
}
}
};
};
7 changes: 7 additions & 0 deletions test/tech.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,13 @@ describe('tech', function() {
SINON.match.any,
"source_suffix");
});

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 aa7fb92

Please sign in to comment.