From 62071408377aea7867f52134c477bef48d27720b Mon Sep 17 00:00:00 2001 From: Vladimir Alaev Date: Mon, 19 Aug 2013 17:18:43 +0300 Subject: [PATCH 1/3] Add ability to write tech module as a function (closes #363, BEM-930) --- lib/tech/index.js | 15 ++++++++++++--- test/data/techs/function-tech.js | 12 ++++++++++++ test/tech.js | 6 ++++++ 3 files changed, 30 insertions(+), 3 deletions(-) create mode 100644 test/data/techs/function-tech.js diff --git a/lib/tech/index.js b/lib/tech/index.js index 6a61be3a..b194f3dc 100644 --- a/lib/tech/index.js +++ b/lib/tech/index.js @@ -6,6 +6,12 @@ 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. * @@ -13,10 +19,13 @@ var TechV1 = exports.Tech = require('./v1').Tech, * @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; diff --git a/test/data/techs/function-tech.js b/test/data/techs/function-tech.js new file mode 100644 index 00000000..2dc9ce9c --- /dev/null +++ b/test/data/techs/function-tech.js @@ -0,0 +1,12 @@ +'use strict'; + +module.exports = function(BEM) { + return { + techMixin: { + + getBuildResults: function() { + return BEM.require.resolve('./techs/js'); + } + } + }; +}; diff --git a/test/tech.js b/test/tech.js index 017df34f..684063ca 100644 --- a/test/tech.js +++ b/test/tech.js @@ -125,6 +125,12 @@ describe('tech', function() { }); + it('for module with function-style code', function() { + var T = getTechClass(require.resolve(PATH.resolve(__dirname, '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() { From c94e4475f0c0cd409e441d7aec49833929fd8fd6 Mon Sep 17 00:00:00 2001 From: Vladimir Alaev Date: Fri, 23 Aug 2013 16:52:41 +0300 Subject: [PATCH 2/3] Remove redundant PATH.resolve() --- test/tech.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/tech.js b/test/tech.js index 684063ca..d2de2bed 100644 --- a/test/tech.js +++ b/test/tech.js @@ -126,7 +126,7 @@ describe('tech', function() { }); it('for module with function-style code', function() { - var T = getTechClass(require.resolve(PATH.resolve(__dirname, 'data/techs/function-tech.js'))); + 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')); }); From a3d5bc84ebfc8c6273bd4da490ad1f48b5e823dd Mon Sep 17 00:00:00 2001 From: Vladimir Alaev Date: Tue, 10 Sep 2013 12:47:26 +0300 Subject: [PATCH 3/3] Fir formatting --- test/data/techs/function-tech.js | 1 - 1 file changed, 1 deletion(-) diff --git a/test/data/techs/function-tech.js b/test/data/techs/function-tech.js index 2dc9ce9c..d1aaafad 100644 --- a/test/data/techs/function-tech.js +++ b/test/data/techs/function-tech.js @@ -3,7 +3,6 @@ module.exports = function(BEM) { return { techMixin: { - getBuildResults: function() { return BEM.require.resolve('./techs/js'); }