Skip to content

Commit 0da709f

Browse files
committed
Imitate BEMHTML export behaviour
1 parent a7bd4d4 commit 0da709f

File tree

5 files changed

+28
-10
lines changed

5 files changed

+28
-10
lines changed

lib/bh-client-processor.js

+11-7
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ module.exports = {
1919
* @param {Object} dependencies example: {libName: "dependencyName"}
2020
* @param {String} jsAttrName
2121
* @param {String} jsAttrScheme
22-
* @param {String} mimic
22+
* @param {String|Array} mimic
2323
* @returns {string}
2424
*/
2525
buildModule: function (targetPath, bhEngine, inputSources, dependencies, jsAttrName, jsAttrScheme,
@@ -29,7 +29,9 @@ module.exports = {
2929
this._defineModule('bh', file, dependencies, bhEngine, inputSources, jsAttrName, jsAttrScheme, true);
3030

3131
if (mimic) {
32-
this._defineModule(mimic, file, { bh: 'bh' });
32+
[].concat(mimic).forEach(function (name) {
33+
this._defineModule(name, file, { bh: 'bh' });
34+
});
3335
}
3436

3537
return file;
@@ -43,7 +45,7 @@ module.exports = {
4345
* @param {String} jsAttrName
4446
* @param {String} jsAttrScheme
4547
* @param {Boolean} useSourceMap
46-
* @param {String} mimic
48+
* @param {String|Array} mimic
4749
* @returns {string}
4850
*/
4951
build: function (targetPath, bhEngine, inputSources, dependencies, jsAttrName, jsAttrScheme, useSourceMap, mimic) {
@@ -67,7 +69,7 @@ module.exports = {
6769
* @param {Object} dependencies example: {libName: "dependencyName"}
6870
* @param {String} jsAttrName
6971
* @param {String} jsAttrScheme
70-
* @param {String} [mimic]
72+
* @param {String|Array} [mimic]
7173
* @returns {Object} enb-source-map/lib/file instance
7274
*/
7375
_concatFile: function (file, bhEngine, inputSources, dependencies, jsAttrName, jsAttrScheme, mimic) {
@@ -81,9 +83,11 @@ module.exports = {
8183
file.writeLine('});');
8284

8385
if (mimic) {
84-
file.writeLine('if (typeof ' + mimic + ' === \'undefined\') {');
85-
file.writeLine('var ' + mimic + ' = bh;');
86-
file.writeLine('}');
86+
[].concat(mimic).forEach(function (name) {
87+
file.writeLine('if (typeof ' + name + ' === \'undefined\') {');
88+
file.writeLine('var ' + name + ' = bh;');
89+
file.writeLine('}');
90+
});
8791
}
8892

8993
libPrepares.forEach(function (libPrepare) {

techs/bh-client-module.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* (его предоставляет технология `files`). По умолчанию — `?.files`.
1313
* * *String* **sourceSuffixes** — суффиксы файлов, по которым строится `files`-таргет. По умолчанию — ['bh'].
1414
* * *Boolean* **sourcemap** — строить карты кода.
15-
* * *String* **mimic** — имя модуля для экспорта.
15+
* * *String|Array* **mimic** — имя модуля для экспорта.
1616
* * *String* **jsAttrName** — атрибут блока с параметрами инициализации. По умолчанию — `onclick`.
1717
* * *String* **jsAttrScheme** — Cхема данных для параметров инициализации. По умолчанию — `js`.
1818
* * Форматы:

techs/bh-client.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* (его предоставляет технология `files`). По умолчанию — `?.files`.
1212
* * *String* **sourceSuffixes** — суффиксы файлов, по которым строится `files`-таргет. По умолчанию — ['bh'].
1313
* * *Boolean* **sourcemap** — строить карты кода.
14-
* * *String* **mimic** — имя переменной для экспорта.
14+
* * *String|Array* **mimic** — имя переменной для экспорта.
1515
* * *String* **jsAttrName** — атрибут блока с параметрами инициализации. По умолчанию — `onclick`.
1616
* * *String* **jsAttrScheme** — Cхема данных для параметров инициализации. По умолчанию — `js`.
1717
* * Форматы:

techs/bh-server-include.js

+9
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
* (его предоставляет технология `files`). По умолчанию — `?.files`.
1313
* * *String* **sourceSuffixes** — суффиксы файлов, по которым строится `files`-таргет. По умолчанию — ['bh'].
1414
* * *Boolean* **sourcemap** — строить карты кода.
15+
* * *String|Array* **mimic** — имена модулей для экспорта.
1516
* * *String* **jsAttrName** — атрибут блока с параметрами инициализации. По умолчанию — `onclick`.
1617
* * *String* **jsAttrScheme** — Cхема данных для параметров инициализации. По умолчанию — `js`.
1718
* * Форматы:
@@ -34,6 +35,7 @@ module.exports = require('enb/lib/build-flow').create()
3435
.name('bh-server-include')
3536
.target('target', '?.bh.js')
3637
.defineOption('bhFile', '')
38+
.defineOption('mimic', [])
3739
.defineOption('jsAttrName', 'onclick')
3840
.defineOption('jsAttrScheme', 'js')
3941
.defineOption('sourcemap', false)
@@ -49,6 +51,7 @@ module.exports = require('enb/lib/build-flow').create()
4951
.builder(function (bhFiles) {
5052
var node = this.node,
5153
dependencies = {},
54+
mimic = this._mimic,
5255
jsAttrName = this._jsAttrName,
5356
jsAttrScheme = this._jsAttrScheme,
5457
sourcemap = this._sourcemap,
@@ -75,6 +78,12 @@ module.exports = require('enb/lib/build-flow').create()
7578

7679
file.writeLine('module.exports = bh;');
7780

81+
if (mimic) {
82+
[].concat(mimic).forEach(function (name) {
83+
file.writeLine('bh[\'' + name + '\'] = bh;');
84+
});
85+
}
86+
7887
return file.render();
7988
});
8089
})

techs/bh-server.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
* * *String* **filesTarget** — files-таргет, на основе которого получается список исходных файлов
1313
* (его предоставляет технология `files`). По умолчанию — `?.files`.
1414
* * *String* **sourceSuffixes** — суффиксы файлов, по которым строится `files`-таргет. По умолчанию — ['bh.js'].
15+
* * *String|Array* **mimic** — имена модулей для экспорта.
1516
* * *String* **jsAttrName** — атрибут блока с параметрами инициализации. По умолчанию — `onclick`.
1617
* * *String* **jsAttrScheme** — Cхема данных для параметров инициализации. По умолчанию — `js`.
1718
* * Форматы:
@@ -31,6 +32,7 @@ module.exports = require('enb/lib/build-flow').create()
3132
.name('bh-server')
3233
.target('target', '?.bh.js')
3334
.defineOption('bhFile', '')
35+
.defineOption('mimic', [])
3436
.defineOption('jsAttrName', 'onclick')
3537
.defineOption('jsAttrScheme', 'js')
3638
.useFileList(['bh.js'])
@@ -97,7 +99,10 @@ module.exports = require('enb/lib/build-flow').create()
9799
return buildRequire(file.fullname, '', '(bh)');
98100
}).join('\n'),
99101
'',
100-
'module.exports = bh;'
102+
'module.exports = bh;',
103+
this._mimic ? [].concat(this._mimic).map(function (name) {
104+
return 'bh[\'' + name + '\'] = bh;';
105+
}).join('\n') : ''
101106
].join('\n');
102107
})
103108
.createTech();

0 commit comments

Comments
 (0)