Skip to content

Commit

Permalink
Added devMode option to bh-bundle tech
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewblond committed May 25, 2015
1 parent b01227a commit 9e7b7c4
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ nodeConfig.addTech(require('enb-bh/techs/bh-bundle'));
* *String* **target** — Результирующий таргет. По умолчанию — `?.bh.js`.
* *String* **filesTarget** — files-таргет, на основе которого получается список исходных файлов (его предоставляет технология `files`). По умолчанию — `?.files`.
* *String* **sourceSuffixes** — суффиксы файлов, по которым строится `files`-таргет. По умолчанию — `['bh.js']`.
* *Boolean* **devMode** — режим сборки. По умолчанию — `true`.
* *String|Array* **mimic** — имена переменных для экспорта.
* *String* **jsAttrName** — атрибут блока с параметрами инициализации. По умолчанию — `data-bem`.
* *String* **jsAttrScheme** — Схема данных для параметров инициализации. По умолчанию — `json`. Форматы: `js` — Получаем `return { ... }`. `json` — JSON-формат. Получаем `{ ... }`.
Expand Down
9 changes: 6 additions & 3 deletions techs/bh-commonjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* (его предоставляет технология `files`). По умолчанию — `?.files`.
* * *String* **sourceSuffixes** — суффиксы файлов, по которым строится `files`-таргет. По умолчанию — ['bh.js'].
* * *String|Array* **mimic** — имена модулей для экспорта.
* * *Boolean* **devMode** — режим сборки. По умолчанию — `true`.
* * *String* **jsAttrName** — атрибут блока с параметрами инициализации. По умолчанию — `data-bem`.
* * *String* **jsAttrScheme** — Cхема данных для параметров инициализации. По умолчанию — `json`.
* * Форматы:
Expand Down Expand Up @@ -41,10 +42,12 @@ module.exports = require('enb/lib/build-flow').create()
.defineOption('jsAttrName', 'data-bem')
.defineOption('jsAttrScheme', 'json')
.defineOption('jsCls', 'i-bem')
.defineOption('devMode', true)
.defineOption('escapeContent', false)
.useFileList(['bh.js'])
.builder(function (bhFiles) {
var node = this.node;
var node = this.node,
devMode = this._devMode;

/**
* Генерирует `require`-строку для подключения исходных bh-файлов.
Expand All @@ -63,7 +66,7 @@ module.exports = require('enb/lib/build-flow').create()
}

return [
'dropRequireCache(require, require.resolve("' + relPath + '"));',
devMode ? 'dropRequireCache(require, require.resolve("' + relPath + '"));' : '',
(pre || '') + 'require("' + relPath + '")' + (post || '') + ';'
].join(EOL);
}
Expand All @@ -88,7 +91,7 @@ module.exports = require('enb/lib/build-flow').create()
].join(EOL);

return [
dropRequireCacheFunc,
devMode ? dropRequireCacheFunc : '',
buildRequire(coreFilename, 'var BH = '),
'var bh = new BH();',
'bh.setOptions({',
Expand Down
49 changes: 47 additions & 2 deletions test/techs/bh-commonjs.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
var fs = require('fs'),
var path = require('path'),
fs = require('fs'),
mock = require('mock-fs'),
TestNode = require('enb/lib/test/mocks/test-node'),
Tech = require('../../techs/bh-commonjs'),
FileList = require('enb/lib/file-list'),
bhCoreFilename = require.resolve('bh/lib/bh.js'),
writeFile = require('../lib/write-file'),
dropRequireCache = require('enb/lib/fs/drop-require-cache'),
EOL = require('os').EOL;

describe('bh-commonjs', function () {
Expand All @@ -30,6 +32,42 @@ describe('bh-commonjs', function () {
return assert(bemjson, html, templates);
});

describe('mode', function () {
it('must drop require cache in dev mode', function () {
var opts = {
devMode: true
},
bemjson = { block: 'block' },
html = '<a class="block"></a>';

return build([], opts)
.then(function () {
return build(['bh.match("block", function(ctx) {ctx.tag("a");});'], opts);
})
.then(function (BH) {
BH.apply(bemjson).must.equal(html);
});
});

it('must not drop require cache in prod mode', function () {
var opts = {
devMode: false
},
bemjson = { block: 'block' },
html = '<div class="block"></div>';

dropRequireCache(require, path.resolve('blocks', 'block-0.bh.js'));

return build(['bh.match("block", function() {});'], opts)
.then(function () {
return build(['bh.match("block", function(ctx) {ctx.tag("a");});'], opts);
})
.then(function (BH) {
BH.apply(bemjson).must.equal(html);
});
});
});

describe('jsAttr params', function () {
it('must apply default jsAttrName and jsAttrScheme params', function () {
var bemjson = { block: 'block', js: true },
Expand Down Expand Up @@ -265,7 +303,7 @@ function bhWrap(str) {
return 'module.exports = function(bh) {' + str + '};';
}

function assert(bemjson, html, templates, options) {
function build(templates, options) {
var scheme = {
blocks: {},
bundle: {}
Expand All @@ -287,6 +325,13 @@ function assert(bemjson, html, templates, options) {

return bundle.runTechAndRequire(Tech, options)
.spread(function (BH) {
return BH;
});
}

function assert(bemjson, html, templates, options) {
return build(templates, options)
.then(function (BH) {
BH.apply(bemjson).must.be(html);

options && options.mimic && [].concat(options.mimic).forEach(function (name) {
Expand Down

0 comments on commit 9e7b7c4

Please sign in to comment.