Skip to content

Instantly share code, notes, and snippets.

@kompolom kompolom/make.js
Last active Apr 18, 2016

Embed
What would you like to do?
try i18n
var PRODUCTION = process.env.YENV === 'production',
langs = ['ru'],
fs = require('fs'),
path = require('path'),
techs = require('./techs'),
PLATFORMS = {
'desktop' : ['common', 'desktop'],
'mobile' : ['common', 'touch', 'mobile'],
'tablet' : ['common', 'touch', 'tablet']
},
/**
* Настройка мерджей
* <платформа> : { <мердж> : [<матчеры>] }
*/
MERGES = {
'desktop' : {
shop : [/page_shop_*/],
account : [/page_account_*/],
admin : [/page_admin_*/]
}
},
SETS = {
'desktop' : ['common', 'desktop']
// 'touch' : ['common', 'touch']
},
BH_OPTIONS = {
jsAttrName : 'data-bem',
jsAttrScheme : 'json'
};
module.exports = function (config) {
var platforms = Object.keys(PLATFORMS),
// merges = Object.keys(MERGES),
sets = Object.keys(SETS);
config.setLanguages(langs);
// config.includeConfig('enb-bem-examples');
// config.includeConfig('enb-bem-docs');
config.includeConfig('enb-bem-specs');
config.includeConfig('enb-bem-tmpl-specs');
configurePages(platforms); // Сборка бандлов
configureSets(sets, {
// tests : config.module('enb-bem-examples').createConfigurator('tests'),
// examples : config.module('enb-bem-examples').createConfigurator('examples'),
// docs : config.module('enb-bem-docs').createConfigurator('docs', 'examples'),
specs : config.module('enb-bem-specs').createConfigurator('specs'),
tmplSpecs : config.module('enb-bem-tmpl-specs').createConfigurator('tmpl-specs')
});
function configureSets(platforms, sets) {
platforms.forEach(function(platform) {
/* sets.examples.configure({
destPath : platform + '.examples',
levels : getLibLevels(platform),
techSuffixes : ['examples'],
fileSuffixes : ['bemjson.js', 'title.txt'],
inlineBemjson : true,
processInlineBemjson : wrapInPage
});
sets.tests.configure({
destPath : platform + '.tests',
levels : getLibLevels(platform),
techSuffixes : ['tests'],
fileSuffixes : ['bemjson.js', 'title.txt']
});
sets.docs.configure({
destPath : platform + '.docs',
levels : getLibLevels(platform),
exampleSets : [platform + '.examples'],
langs : config.getLanguages(),
jsdoc : { suffixes : ['vanilla.js', 'browser.js', 'js'] }
});
*/
sets.specs.configure({
destPath : platform + '.specs',
levels : getLibLevels(platform),
sourceLevels : getSpecLevels(platform),
jsSuffixes : ['vanilla.js', 'js', 'browser.js'],
templateEngine : {
templateTech : require('enb-bh/techs/bh-bundle'),
htmlTech : require('enb-bh/techs/bemjson-to-html'),
htmlTechOptionNames : { bemjsonFile : 'bemjsonFile', templateFile : 'bhFile' },
templateOptions : {
mimic : 'BEMHTML',
bhOptions : BH_OPTIONS
}
}
});
sets.tmplSpecs.configure({
destPath : platform + '.tmpl-specs',
levels : getLibLevels(platform),
sourceLevels : getSpecLevels(platform),
saveHtml : true,
engines : {
'BH.js' : {
tech : 'enb-bh/techs/bh-bundle',
options : {
bhOptions : BH_OPTIONS,
requires : { i18n : { globals : 'BEM.I18N' } }
},
},
'bemhtml' : {
tech : 'enb-bemxjst/techs/bemhtml',
options : {
requires : { i18n : { globals : 'BEM.I18N' } },
sourceSuffixes : ['bemhtml.js', 'bemhtml'],
exportName : 'BEMHTML',
devMode : true
}
}
},
langs : true,
mockI18N : function (global, bem_) {
global.BEM = bem_;
bem_.I18N = function (keyset, key) {
return keyset + '::' + key;
};
},
depsTech : techs.bem.depsOld
});
// configureNodes(platform, [platform + '.tests/*/*', platform + '.examples/*/*']);
});
}
/**
* Сборка бандлов страниц
*/
function configurePages(platforms) {
platforms.forEach(function(platform) {
// Build pages
var nodes = [platform + '.pages/*'];
config.nodes(nodes, function(nodeConfig) {
nodeConfig.addTech([techs.files.provide, { target : '?.bemdecl.js' }]);
});
configureNodes(platform, nodes);
// Build bundles
var bundleNodes = [platform + '.bundles/*'];
config.nodes(bundleNodes, function(nodeConfig) {
nodeConfig.addTechs([
[techs.files.provide, { target : '?.bemjson.js' }],
[techs.bem.bemjsonToBemdecl, { target : '?.bemdecl.js' }]
]);
nodeConfig.addTarget('?.{lang}.html');
});
configureNodes(platform, bundleNodes);
});
}
/**
* Настройка отдельного бандла
*/
function configureNodes(platform, nodes) {
configureLevels(platform, nodes);
config.nodes(nodes, function(nodeConfig) {
// Base techs
nodeConfig.addTechs([
[techs.bem.deps, { target : '.?.deps.js' }],
[techs.bem.files, { depsFile : '.?.deps.js' }]
]);
// i18n
nodeConfig.addTechs([
[techs.i18n.keysets, { lang : '{lang}' }],
[techs.i18n.lang, {
exports : { commonJS : true, ym : true },
lang : '{lang}'
}],
]);
// Client techs
nodeConfig.addTechs([
[techs.css.stylus, {
target : '.?.post.css',
sourceSuffixes : ['post.css', 'css', 'styl'],
autoprefixer : { browsers : getBrowsers(platform) },
}],
[techs.css.postcss, {
source : '.?.post.css',
target : '.?.css',
sourcemap : true,
plugins : [
require('lost')()
]
}],
[techs.js, {
target : '.?.browser.js',
includeYM : true,
// sourcemap : !PRODUCTION
}],
[techs.files.merge, {
target : '.?.{lang}.js',
sources : ['.?.browser.js', '.?.browser.{lang}.bemhtml.js'],
lang : '{lang}'
}],
]);
// js techs
nodeConfig.addTechs([
[techs.bem.depsByTechToBemdecl, {
target : '.?.js-js.bemdecl.js',
sourceTech : 'js',
destTech : 'js'
}],
[techs.bem.mergeBemdecl, {
sources : ['?.bemdecl.js', '.?.js-js.bemdecl.js'],
target : '.?.js.bemdecl.js'
}],
[techs.bem.deps, {
target : '.?.js.deps.js',
bemdeclFile : '.?.js.bemdecl.js'
}],
[techs.bem.files, {
depsFile : '.?.js.deps.js',
filesTarget : '?.js.files',
dirsTarget : '?.js.dirs'
}]
]);
// Client Template Engine
nodeConfig.addTechs([
[techs.bem.depsByTechToBemdecl, {
target : '.?.i18n.bemdecl.js',
sourceTech : 'js',
destTech : 'i18n'
}],
[techs.bem.depsByTechToBemdecl, {
target : '.?.bemhtml.bemdecl.js',
sourceTech : 'js',
destTech : 'bemhtml'
}],
[techs.bem.mergeBemdecl, {
sources : ['.?.i18n.bemdecl.js', '.?.bemhtml.bemdecl.js'],
target : '.?.template.bemdecl.js',
}],
[techs.bem.deps, {
target : '.?.template.deps.js',
bemdeclFile : '.?.template.bemdecl.js'
}],
[techs.bem.files, {
depsFile : '.?.template.deps.js',
filesTarget : '?.template.files',
dirsTarget : '?.template.dirs'
}],
[techs.i18n.keysets, {
target : '.?.browser.keysets.{lang}.js',
filesTarget : '?.template.files',
lang : '{lang}'
}],
/*[techs.i18n.lang, {
exports : { ym : true },
keysetsFile : '.?.browser.keysets.{lang}.js',
target : '?.browser.lang.{lang}.js',
lang : '{lang}'
}],*/
[techs.engines.bemhtml, {
target : '.?.browser.{lang}.bemhtml.js',
filesTarget : '?.template.files',
keysetsFile : '.?.browser.keysets.{lang}.js',
sourceSuffixes : ['bemhtml.js', 'bemhtml'],
lang : '{lang}',
}]
]);
// Server template engine
nodeConfig.addTechs([
[techs.engines.bemtree, {
sourceSuffixes : ['bemtree.js', 'bemtree'],
target : '?.{lang}.bemtree.js',
lang : '{lang}'
}],
[techs.engines.bemhtml, {
target : '?.{lang}.bemhtml.js',
sourceSuffixes : ['bemhtml.js', 'bemhtml'],
lang : '{lang}',
}],
/*
[techs.engines.bhCommonJS, {
bhOptions : BH_OPTIONS,
target : '?.{lang}.bh.js',
lang : '{lang}'
}],
[techs.html.bh, {
target : '?.{lang}.html',
bhFile : '?.{lang}.bh.js',
lang : '{lang}',
}]
*/
[techs.html.bemhtml, {
target : '?.{lang}.html',
bemhtmlFile : '?.{lang}.bemhtml.js',
lang : '{lang}'
}]
]);
nodeConfig.addTargets([
'?.{lang}.bemtree.js', '?.{lang}.bemhtml.js', '?.{lang}.js', '?.css'
]);
});
config.mode('development', function() {
config.nodes(nodes, function(nodeConfig) {
nodeConfig.addTechs([
[techs.borschik, { source : '.?.css', target : '?.css', freeze : true, minify : false }],
[techs.borschik, { source : '.?.{lang}.js', target : '?.{lang}.js', freeze : true, minify : false }]
]);
});
});
config.mode('production', function() {
config.nodes(nodes, function(nodeConfig) {
nodeConfig.addTechs([
[techs.borschik, { source : '.?.css', target : '?.css', freeze : true, tech : 'cleancss', minify : true }],
[techs.borschik, { source : '.?.js', target : '?.js', freeze : true, minify : true }]
]);
});
});
}
/**
* Получение уровней переопределения
*/
function configureLevels(platform, nodes) {
config.nodes(nodes, function(nodeConfig) {
var nodeDir = nodeConfig.getNodePath(),
blockSublevelDir = path.join(nodeDir, '..', '.blocks'),
sublevelDir = path.join(nodeDir, 'blocks'),
extendedLevels = [].concat(getSourceLevels(platform));
if(fs.existsSync(blockSublevelDir)) {
extendedLevels.push(blockSublevelDir);
}
if(fs.existsSync(sublevelDir)) {
extendedLevels.push(sublevelDir);
}
nodeConfig.addTech([techs.bem.levels, { levels : extendedLevels }]);
});
}
};
function getSourceLevels(platform) {
var platformNames = (PLATFORMS[platform] || SETS[platform]),
levels = [];
platformNames.forEach(function(name) {
// Общие блоки из библиотек, которые не поддерживают все наши платформы
if(name === 'common') {
levels.push({ path : path.join('libs', 'bem-core', name + '.blocks'), check : false });
levels.push({ path : path.join('libs', 'bem-components', name + '.blocks'), check : false });
levels.push({ path : path.join('libs', 'bem-forms', name + '.blocks'), check : false });
levels.push({ path : path.join('libs', 'bem-scrollspy', name + '.blocks'), check : false });
levels.push({ path : path.join('libs', 'bem-social', name + '.blocks'), check : false });
levels.push({ path : path.join('libs', 'bem-mvc', name + '.blocks'), check : false });
levels.push({ path : path.join('libs', 'ap-components', name + '.blocks'), check : true });
}// common
if(name === 'desktop') {
levels.push({ path : path.join('libs', 'bem-core', name + '.blocks'), check : false });
levels.push({ path : path.join('libs', 'bem-components', name + '.blocks'), check : false });
levels.push({ path : path.join('libs', 'bem-forms', name + '.blocks'), check : false });
levels.push({ path : path.join('libs', 'bem-social', name + '.blocks'), check : false });
}// common
if(name === 'touch') {
levels.push({ path : path.join('libs', 'bem-core', 'touch.blocks'), check : false });
levels.push({ path : path.join('libs', 'bem-components', 'touch.blocks'), check : false });
}// common
if(name === 'mobile') {
levels.push({ path : path.join('libs', 'bem-components', 'touch-phone.blocks'), check : false });
}// common
if(name === 'tablet') {
levels.push({ path : path.join('libs', 'bem-components', 'touch-pad.blocks'), check : false });
}// common
});
platformNames.forEach(function(name) {
levels.push({ path : name + '.blocks', check : true });
});
platformNames.forEach(function(name) {
levels.push({ path : path.join('design', name + '.blocks'), check : true });
});
return levels;
}
function getTestLevels(platform) {
return [].concat(
getSourceLevels(platform),
'test.blocks'
);
}
function getLibLevels(platform) {
return (PLATFORMS[platform] || SETS[platform]).map(function(level) {
return level + '.blocks';
});
}
function getSpecLevels(platform) {
return [].concat(
{ path : path.join('libs', 'bem-pr', 'spec.blocks'), check : false },
getSourceLevels(platform)
);
}
function getBrowsers(platform) {
switch(platform) {
case 'desktop':
return [
'last 2 versions',
'ie 10',
'ff 24',
'opera 12.1'
];
case 'touch':
return [
'android 4',
'ios >= 5',
'ie 10'
];
case 'tablet':
return [
'android 4',
'ios 5'
];
case 'mobile':
return [
'android 4',
'ios 6',
'ie 10'
];
}
}
function wrapInPage(bemjson, meta) {
var basename = '_' + path.basename(meta.filename, '.bemjson.js');
return {
block : 'page',
title : naming.stringify(meta.notation),
head : [{ elem : 'css', url : basename + '.css' }],
scripts : [{ elem : 'js', url : basename + '.js' }],
mods : { theme : getThemeFromBemjson(bemjson) },
content : bemjson
};
}
module.exports = {
files : {
provide : require('enb/techs/file-provider'),
copy : require('enb/techs/file-copy'),
merge : require('enb/techs/file-merge')
},
bem : require('enb-bem-techs'),
css : {
stylus : require('enb-stylus/techs/stylus'),
postcss : require('enb-bundle-postcss/techs/enb-bundle-postcss'),
},
js : require('enb-js/techs/browser-js'),
engines : {
bemtree : require('enb-bemxjst-i18n/techs/bemtree-i18n'),
bhCommonJS : require('enb-bh-i18n/techs/bh-commonjs-i18n'),
bhBundle : require('enb-bh/techs/bh-bundle'),
bemhtml : require('enb-bemxjst-i18n/techs/bemhtml-i18n'),
},
html : {
bh : require('enb-bh/techs/bemjson-to-html'),
bemhtml : require('enb-bemxjst/techs/bemjson-to-html')
},
borschik : require('enb-borschik/techs/borschik'),
i18n : {
keysets : require('enb-bem-i18n/techs/keysets'),
lang : require('enb-bem-i18n/techs/i18n'),
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.