Skip to content

Commit

Permalink
fixed the rest part of lang bugs, added multilang test
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexej Yaroshevich committed Feb 13, 2015
1 parent 1fc982c commit 781d94f
Show file tree
Hide file tree
Showing 15 changed files with 97 additions and 23 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
5 changes: 5 additions & 0 deletions examples/blocks/image/image.i18n/ru.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
image: {
title: 'Скачать картинку'
}
};
38 changes: 38 additions & 0 deletions examples/silly-multilang/.enb/make.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
var path = require('path');
var rootPath = path.join(__dirname, '..', '..', '..');

module.exports = function (config) {
config.includeConfig(rootPath);

var tmplSpecs = config.module('enb-bem-tmpl-specs').createConfigurator('tmpl-specs');

tmplSpecs.configure({
destPath: 'tmpl-specs',
levels: ['blocks'],
langs: ['en', 'ru'],
sourceLevels: [
{ path: '../libs/bem-core/common.blocks', check: false },
{ path: '../blocks', check: true },
{ path: 'blocks', check: true }
],
coverage: true,
engines: {
'BH': {
tech: 'enb-bh/techs/bh-server-include',
options: {
sourcemap: true,
jsAttrName: 'data-bem',
jsAttrScheme: 'json'
}
},
'BEMHTML dev': {
tech: 'enb-bemxjst/techs/bemhtml-old',
options: { devMode: true }
},
'BEMHTML prod': {
tech: 'enb-bemxjst/techs/bemhtml-old',
options: { devMode: false }
}
}
});
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
({
block : 'image',
url : 'download.svg',
alt : 'Download'
})
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<img alt="Download" class="image" src="download.svg" title="Download image" />
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
({
block : 'image',
url : 'download.svg',
alt : 'Скачать'
})
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<img alt="Скачать" class="image" src="download.svg" title="Скачать картинку" />
1 change: 1 addition & 0 deletions examples/silly/.enb/make.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ module.exports = function (config) {
langs: true,
sourceLevels: [
{ path: '../libs/bem-core/common.blocks', check: false },
{ path: '../blocks', check: true },
{ path: 'blocks', check: true }
],
coverage: true,
Expand Down
6 changes: 5 additions & 1 deletion lib/assets/it.jst
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<%
// prepares
var titleSuffix = lang ? ' in `' + lang + '` lang' : '';
var subreference = lang ? '[' + lang + ']' : '';
var subreference = lang ? '[\'' + lang + '\']' : '';
var prettifyEngineName = function (ngn) {
return lang ? String(ngn).toLowerCase().replace(' ', '-') + '.' + lang : ngn;
};
Expand All @@ -13,6 +13,10 @@ it('should be equal `${ it }` by ${ engine.name }${ titleSuffix }', function (<%
var bemjson = references['${ it }']${ subreference }.bemjson,
expected = references['${ it }']${ subreference }.html;<%
if (lang) { %>
lazies['${ engine.name }${ lang }']();
<% }
if (!engine.async) { %>
// sync mode
var actual = engines['${ engine.name }']${ subreference }.apply(bemjson);
Expand Down
22 changes: 13 additions & 9 deletions lib/assets/tmpl-spec.jst
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
<%
function reRequire(where, what, lazy) {
var name = what.name,
target = what.target,
exportName = what.exportName;
lazy && print('lazies[\'' + lazy + '\'] = function () {\n');
print('dropRequireCache(require, require.resolve(\'' + target + '\'));\n');
print((lazy ? 'return ' : '') + where + '[\'' + name + '\'] = require(\'' + target + '\')' + exportName + ';\n');
lazy && print('};\n');
}
%>
var assert = require('assert'),
path = require('path'),
<% if (saveHtml) { %>
Expand All @@ -23,6 +34,7 @@ var assert = require('assert'),
htmlDiffer = new HtmlDiffer('bem'),
referencesFilename = require.resolve('${ paths.references }'),
engines,
lazies = {},
references;

describe('${ describe }', function() {
Expand All @@ -32,19 +44,11 @@ describe('${ describe }', function() {

engines = {};
<% _.forEach(engines, function(engine) {
function reRequire(where, what) {
var name = what.name,
target = what.target,
exportName = what.exportName;
print('dropRequireCache(require, require.resolve(\'' + target + '\'));\n');
print(where + '[\'' + name + '\'] = require(\'' + target + '\')' + exportName + ';\n');
}
if (langs && langs.length) {
var engineObject = 'engines[\'' + engine.name + '\']';
print(engineObject + ' = {};\n');
_.forEach(engine.langs, function(lang) {
reRequire(engineObject, lang);
reRequire(engineObject, lang, engine.name + lang.name);
});
} else {
reRequire('engines', engine);
Expand Down
32 changes: 20 additions & 12 deletions lib/techs/references.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,34 @@ module.exports = require('enb/lib/build-flow').create()
.useDirList('tmpl-specs')
.builder(function (dirs) {
var references = {},
bemjsons = {},
htmls = {};
bemjsons = [],
htmls = [];

dirs.forEach(function (dir) {
dir.files.forEach(function (file) {
var filename = file.name,
name = filename.split('.')[0];
fullname = file.fullname,
name = filename.split('.')[0],
lang = getLang(fullname),
item = {
name: name,
fullname: fullname,
lang: lang
};

if (hasExt(filename, '.bemjson.js')) {
bemjsons[name] = file.fullname;
bemjsons.push(item);
} else if (hasExt(filename, '.html')) {
htmls[name] = file.fullname;
htmls.push(item);
}
});
});

return vow.all([
vow.all(Object.keys(bemjsons).map(function (name) {
var filename = bemjsons[name],
lang = getLang(filename);
vow.all(bemjsons.map(function (file) {
var name = file.name,
lang = file.lang,
filename = file.fullname;

dropRequireCache(require, filename);

Expand All @@ -45,11 +53,11 @@ module.exports = require('enb/lib/build-flow').create()
}
});
})),
vow.all(Object.keys(htmls).map(function (name) {
var filename = htmls[name],
lang = getLang(filename);
vow.all(htmls.map(function (file) {
var name = file.name,
lang = file.lang;

return vfs.read(filename, 'utf-8')
return vfs.read(file.fullname, 'utf-8')
.then(function (source) {
var reference = references[name] || (references[name] = {});

Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,11 @@
"main": "lib/index.js",
"scripts": {
"test": "npm run lint && mocha test/techs",
"test-silly": "npm run deps && npm run clean && npm run build",
"test-travis": "npm run test && npm run test-silly",
"lint": "jshint . && jscs -c .jscs.js .",
"deps": "cd examples && bower i",
"build": "enb -d examples/silly make __magic__ tmpl-specs",
"build": "enb -d examples/silly make __magic__ tmpl-specs && enb -d examples/silly-multilang make __magic__ tmpl-specs",
"clean": "rm -rf examples/*/*tmpl-specs && rm -rf examples/*/.enb/tmp && rm -rf coverage"
}
}

0 comments on commit 781d94f

Please sign in to comment.