diff --git a/README.md b/README.md index acbbfbb..083a9a7 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,7 @@ $ npm install --save-dev enb-bh * *String* **filesTarget** — files-таргет, на основе которого получается список исходных файлов (его предоставляет технология `files`). По умолчанию — `?.files`. * *String* **sourceSuffixes** — суффиксы файлов, по которым строится `files`-таргет. По умолчанию — `['bh.js']`. * *Boolean* **sourcemap** — строить карты кода. +* *String* **scope** — скоуп выполнения кода шаблонов. По умолчанию — `template`. Если значение равно `template`, то каждый шаблон будет выполнен в своём отдельном скоупе. Если значение равно `global`, то все шаблоны будут выполнены в общем скоупе. * *String|Array* **mimic** — имена переменных для экспорта. * *String* **jsAttrName** — атрибут блока с параметрами инициализации. По умолчанию — `data-bem`. * *String* **jsAttrScheme** — Схема данных для параметров инициализации. По умолчанию — `json`. Форматы: `js` — Получаем `return { ... }`. `json` — JSON-формат. Получаем `{ ... }`. diff --git a/lib/compile.js b/lib/compile.js index 6757766..b3b3546 100644 --- a/lib/compile.js +++ b/lib/compile.js @@ -20,6 +20,7 @@ core.contents = fs.readFileSync(core.path, 'utf-8'); * @param {String} [opts.jsAttrName] Use `jsAttrName` option for bh core * @param {String} [opts.jsAttrScheme] Use `jsAttrScheme` option for bh core * @param {String} [opts.jsCls] Use `jsCls` option for bh core + * @param {String} [opts.scope=template] Scope of the executing code templates * @param {String[]} [opts.mimic] Names for exports * @param {Object} [opts.dependencies] Names for requires to `BH.lib.name` * @returns {String} compiled code of bh module @@ -33,6 +34,7 @@ function compile(sources, opts) { } var file = new File(opts.filename, opts.sourcemap), + isTemplateScope = opts.hasOwnProperty('scope') ? opts.scope === 'template' : true, mimic = opts.mimic || [], dependencies = opts.dependencies || {}, depNames = Object.keys(dependencies); @@ -59,11 +61,11 @@ function compile(sources, opts) { var relPath = source.relPath || source.path; // wrap in IIFE to perform each template in its scope - file.writeLine('(function () {'); + isTemplateScope && file.writeLine('(function () {'); file.writeLine('// begin: ' + relPath); file.writeFileContent(source.path, source.contents); file.writeLine('// end: ' + relPath); - file.writeLine('}());'); + isTemplateScope && file.writeLine('}());'); }); file.writeLine('}());'); diff --git a/techs/bh-bundle.js b/techs/bh-bundle.js index 2415708..d1fb4d6 100644 --- a/techs/bh-bundle.js +++ b/techs/bh-bundle.js @@ -17,6 +17,9 @@ * (его предоставляет технология `files`). По умолчанию — `?.files`. * * *String* **sourceSuffixes** — суффиксы файлов, по которым строится `files`-таргет. По умолчанию — ['bh.js']. * * *Boolean* **sourcemap** — строить карты кода. + * * *String* **scope** — скоуп выполнения кода шаблонов. По умолчанию — `template`. Если значение равно `template`, + * то каждый шаблон будет выполнен в своём отдельном скоупе. Если значение равно `global`, + * то все шаблоны будут выполнены в общем скоупе. * * *String|Array* **mimic** — имена переменных/модулей для экспорта. * * *String* **jsAttrName** — атрибут блока с параметрами инициализации. По умолчанию — `data-bem`. * * *String* **jsAttrScheme** — Cхема данных для параметров инициализации. По умолчанию — `json`. @@ -49,6 +52,7 @@ module.exports = require('enb/lib/build-flow').create() .defineOption('jsCls', 'i-bem') .defineOption('escapeContent', false) .defineOption('sourcemap', false) + .defineOption('scope', 'template') .useFileList(['bh.js']) .builder(function (files) { return this._readTemplates(files) @@ -72,6 +76,7 @@ module.exports = require('enb/lib/build-flow').create() jsAttrScheme: this._jsAttrScheme, jsCls: this._jsCls, escapeContent: this._escapeContent, + scope: this._scope, mimic: [].concat(this._mimic), dependencies: this._dependencies }; diff --git a/test/techs/bh-bundle/bh-bundle.test.js b/test/techs/bh-bundle/bh-bundle.test.js index 3a9706e..9549f62 100644 --- a/test/techs/bh-bundle/bh-bundle.test.js +++ b/test/techs/bh-bundle/bh-bundle.test.js @@ -55,6 +55,18 @@ describe('bh-bundle', function () { return assert(bemjson, html, templates); }); + + it('must perform each template in common scope if `scope: global`', function () { + var templates = [ + 'var text = "Hello world!";', + 'bh.match("block", function(ctx) {ctx.content(typeof text === "undefined" ? "Hi!" : text);});' + ], + bemjson = { block: 'block' }, + html = '