Skip to content

Commit

Permalink
#66 Add exports option to i18n technology
Browse files Browse the repository at this point in the history
  • Loading branch information
tormozz48 committed Aug 17, 2015
1 parent 103f50b commit 01c2ba5
Showing 1 changed file with 67 additions and 15 deletions.
82 changes: 67 additions & 15 deletions techs/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ var EOL = require('os').EOL,
* @param {Object} options Options.
* @param {String} [options.target='?.lang.{lang}.js'] Path to a target with compiled file.
* @param {String} options.lang Language identifier.
* @param {String} [options.exports={
* globals: true, commonJS: true, ym: true}] Export settings.
* @param {String} [options.keysetsFile='?.keysets.{lang}.js'] Path to a source keysets file.
*
* @example
Expand Down Expand Up @@ -52,39 +54,89 @@ module.exports = require('enb/lib/build-flow').create()
.name('i18n')
.target('target', '?.lang.{lang}.js')
.defineRequiredOption('lang')
.defineOption('exports', {
globals: true,
commonJS: true,
ym: true
})
.useSourceFilename('keysetsFile', '?.keysets.{lang}.js')
.builder(function (keysetsFilename) {
return this._readKeysetsFile(keysetsFilename)
.then(function (sources) {
var parsed = keysets.parse(sources),
opts = {
version: parsed.version,
language: this._lang
language: this._lang,
exports: this._exports
};

/**
* Builds code wrapper for provide exports into CommonJS modular system
* @param {Object} exports option
* @returns {String} generated code
*/
function buildCommonJSWrapper(exports) {
if (!exports.commonJS) {
return '';
}
return [
' if (typeof exports === "object") {',
' module.exports = __i18n__;',
exports.globals === 'force' ? '' :
' defineAsGlobal = false;',
' }'
].join(EOL);
}

/**
* Builds code wrapper for provide exports into YModules modular system
* @param {Object} exports option
* @returns {String} generated code
*/
function buildYModulesWrapper(exports) {
if (!exports.ym) {
return '';
}
return [
' if (typeof modules === "object") {',
' modules.define("i18n", function (provide) {',
' provide(__i18n__);',
' });',
exports.globals === 'force' ? '' :
' defineAsGlobal = false;',
' }'
].join(EOL);
}

/**
* Builds code wrapper for provide exports into globals namespace
* @param {Object} exports option
* @returns {String} generated code
*/
function buildGlobalsWrapper(exports) {
if (!exports.globals) {
return '';
}
return [
' if (defineAsGlobal) {',
' global.BEM || (global.BEM = {});',
' global.BEM.I18N = __i18n__;',
' }'
].join(EOL);
}

return [
'(function (global) {',
' var __i18n__ = ' + compile(parsed.core, parsed.keysets, opts) + ',',
' defineAsGlobal = true;',
'',
' // CommonJS',
' if (typeof exports === "object") {',
' module.exports = __i18n__;',
' defineAsGlobal = false;',
' }',
buildCommonJSWrapper(opts.exports),
'',
' // YModules',
' if (typeof modules === "object") {',
' modules.define("i18n", function (provide) {',
' provide(__i18n__);',
' });',
' defineAsGlobal = false;',
' }',
buildYModulesWrapper(opts.exports),
'',
' if (defineAsGlobal) {',
' global.BEM || (global.BEM = {});',
' global.BEM.I18N = __i18n__;',
' }',
buildGlobalsWrapper(opts.exports),
'})(this);'
].join(EOL);
}, this);
Expand Down

0 comments on commit 01c2ba5

Please sign in to comment.