From d93d204d78f0c102285df05e87d6d9058cda2c9c Mon Sep 17 00:00:00 2001 From: Vladimir Alaev Date: Mon, 19 Aug 2013 18:18:21 +0300 Subject: [PATCH 1/2] Add ability to write level config module as a function (closes #364, BEM-931) --- lib/level.js | 10 ++++ test/data/make/project/pages/.bem/level.js | 61 +++++++++++----------- 2 files changed, 41 insertions(+), 30 deletions(-) diff --git a/lib/level.js b/lib/level.js index 2a48df3b..33c5ec4a 100644 --- a/lib/level.js +++ b/lib/level.js @@ -8,8 +8,18 @@ var PATH = require('./path'), LOGGER = require('./logger'), isRequireable = bemUtil.isRequireable, + BEM, + getBem = function() { + if (!BEM) BEM = require('..'); + + return BEM; + }, + getLevelClass = function(path, optional) { var level = optional && !isRequireable(path) ? {} : requireLevel(path); + + if (typeof level === 'function') level = level(getBem()); + if (level.Level) return level.Level; return INHERIT(level.baseLevelPath? getLevelClass(level.baseLevelPath) : Level, level); }, diff --git a/test/data/make/project/pages/.bem/level.js b/test/data/make/project/pages/.bem/level.js index 10a1e177..d553c9e7 100644 --- a/test/data/make/project/pages/.bem/level.js +++ b/test/data/make/project/pages/.bem/level.js @@ -1,36 +1,37 @@ -var BEM = require('../../..'), - PATH = require('path'), - extend = BEM.util.extend, +module.exports = function(BEM) { - BEM_TECHS = '../../bem-bl/blocks-common/i-bem/bem/techs', - PRJ_TECHS = '../../.bem/techs'; + var PATH = require('path'), + extend = BEM.util.extend, -exports.getTechs = function() { + BEM_TECHS = '../../bem-bl/blocks-common/i-bem/bem/techs'; return { - 'bemjson.js': '', - 'deps.js': 'v2/deps.js', - 'js': 'v2/js-i', - 'css': 'v2/css', - 'ie.css': 'v2/ie.css', - 'i18n': PATH.join(BEM_TECHS, 'v2/i18n.js'), - 'i18n.js': PATH.join(BEM_TECHS, 'v2/i18n.js.js'), - 'bemhtml': PATH.join(BEM_TECHS, 'v2/bemhtml.js'), - 'html': PATH.join(BEM_TECHS, 'html.js') + getTechs: function() { + + return { + 'bemjson.js': '', + 'deps.js': 'v2/deps.js', + 'js': 'v2/js-i', + 'css': 'v2/css', + 'ie.css': 'v2/ie.css', + 'i18n': PATH.join(BEM_TECHS, 'v2/i18n.js'), + 'i18n.js': PATH.join(BEM_TECHS, 'v2/i18n.js.js'), + 'bemhtml': PATH.join(BEM_TECHS, 'v2/bemhtml.js'), + 'html': PATH.join(BEM_TECHS, 'html.js') + }; + }, + + getConfig: function() { + + return extend({}, this.__base() || {}, { + + bundleBuildLevels: this.resolvePaths([ + '../../bem-bl/blocks-common', + '../../bem-bl/blocks-desktop', + '../../blocks' + ]) + + }); + } }; - -}; - -exports.getConfig = function() { - - return extend({}, this.__base() || {}, { - - bundleBuildLevels: this.resolvePaths([ - '../../bem-bl/blocks-common', - '../../bem-bl/blocks-desktop', - '../../blocks' - ]) - - }); - }; From d2be06abd8de63adad297a9bcf319e2644b1dbc8 Mon Sep 17 00:00:00 2001 From: Vladimir Alaev Date: Thu, 12 Sep 2013 16:13:33 +0300 Subject: [PATCH 2/2] Add a comment about getBem() --- lib/level.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/level.js b/lib/level.js index 33c5ec4a..9c3e1962 100644 --- a/lib/level.js +++ b/lib/level.js @@ -9,6 +9,10 @@ var PATH = require('./path'), isRequireable = bemUtil.isRequireable, BEM, + + // Use this as a function because of circular dependency which occurs + // if require is placed in the global scope of the module. BEM is also cached + // in a var to avoid require call every time (which is much slower than if statement. getBem = function() { if (!BEM) BEM = require('..');