Skip to content

Commit

Permalink
feat(tech): add exports option
Browse files Browse the repository at this point in the history
  • Loading branch information
qfox committed May 18, 2016
1 parent 702b1e4 commit a7b4b96
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 8 deletions.
12 changes: 6 additions & 6 deletions lib/assets/bundle.jst
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@ var ${ exportName };
${ commonJSModules }

var defineAsGlobal = true;
var forceGlobals = ${ exports.globals === 'force' };

// Provide with CommonJS
if (typeof module === 'object' && typeof module.exports === 'object') {
exports['${ exportName }'] = buildBemXjst(${ commonJSDependencies });
defineAsGlobal = false;
if (${ exports.commonJS === true } || typeof module === 'object' && typeof module.exports === 'object') {
module.exports['${ exportName }'] = buildBemXjst(${ commonJSDependencies });
defineAsGlobal = !forceGlobals;
}

// Provide to YModules
if (typeof modules === 'object') {
if (${ exports.ym === true } || typeof modules === 'object') {
modules.define(
'${ exportName }',
[<%_.each(ymDependencyNames, function(name) {%>'${ name }',<%});%>],
Expand All @@ -31,8 +32,7 @@ var ${ exportName };
provide(buildBemXjst(${ ymDependencies }));
}
);

defineAsGlobal = false;
defineAsGlobal = !forceGlobal;
}

// Provide to global scope
Expand Down
2 changes: 2 additions & 0 deletions lib/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var compileCommonJS = require('./compile-commonjs'),
* @param {String} opts.dirname Path to a directory with compiled file.
* @param {String} [options.exportName=BEMHTML] Name for exports.
* @param {Object} [options.requires={}] Names for dependencies.
* @param {Object} [options.exports={globals: true, commonJS: true, ym: true}] Export settings.
* @returns {String}
*/
exports.compile = function (code, options) {
Expand All @@ -24,6 +25,7 @@ exports.compile = function (code, options) {
return template(code, {
exportName: options.exportName,
requires: requires,
exports: options.exports,
commonJSModules: commonJSModules
});
});
Expand Down
3 changes: 3 additions & 0 deletions lib/templates/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ _.mapKeys(templates, function (template, name) {
* @param {Object} options Options.
* @param {String} [options.exportName=BEMHTML] Name for exports.
* @param {Object} [options.requires={}] Names for dependencies.
* @param {Object} [options.exports={globals: true, commonJS: true, ym: true}] Export settings.
* @param {String} [options.commonJSModules] Code of CommonJS modules: require function
* compiled with `browserify`.
* @returns {String}
Expand All @@ -29,6 +30,7 @@ module.exports = function (code, options) {
options || (options = {});

var requires = options.requires || {},
exports = options.exports || {},
templateOpts = { requires: requires },
ymDependencyNames = [],
ymDependencyVars = [];
Expand All @@ -43,6 +45,7 @@ module.exports = function (code, options) {
return templates.bundle({
exportName: options.exportName || 'BEMHTML',
bemxjst: code,
exports: exports,
commonJSModules: options.commonJSModules,

globalDependencies: templates.globals(templateOpts),
Expand Down
11 changes: 9 additions & 2 deletions techs/bem-xjst.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,20 @@ var EOL = require('os').EOL,
*
* Compiles BEMXJST template files with BEMXJST translator and merges them into a single template bundle.<br/><br/>
*
* Important: Normally you don't need to use this tech directly.
* Important: Usually you don't need to use this tech directly.
*
* @param {Object} [options] Options
* @param {String} [options.target='?.bem-xjst.js'] Path to a target with compiled file.
* @param {Object} [options.exports={globals: true, commonJS: true, ym: true}] Export settings.
*/
module.exports = buildFlow.create()
.name('bem-xjst')
.target('target', '?.bem-xjst.js')
.defineOption('exports', {
globals: true,
commonJS: true,
ym: true
})
.methods({
/**
* Returns filenames to compile.
Expand Down Expand Up @@ -146,7 +152,8 @@ module.exports = buildFlow.create()
return bundle.compile(compiledCode, {
dirname: this.node.getDir(),
exportName: this._exportName,
requires: this._requires
requires: this._requires,
exports: this._exports
});
}, this);
},
Expand Down
1 change: 1 addition & 0 deletions techs/bemhtml.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ var path = require('path');
* Default as `__`.
* * String `mod` — separates names and values of modifiers
* from blocks and elements. Default as `_`.
* @param {Object} [options.exports={globals: true, commonJS: true, ym: true}] Export settings.
*
* @example
* var BemhtmlTech = require('enb-bemxjst/techs/bemhtml'),
Expand Down
1 change: 1 addition & 0 deletions techs/bemtree.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ var path = require('path');
* @param {Object} [options.requires] Names of dependencies which should be available from
* code of templates.
* @param {Boolean} [options.forceBaseTemplates=false] Include base templates if no user templates present
* @param {Object} [options.exports={globals: true, commonJS: true, ym: true}] Export settings.
*
* @example
* var BemtreeTech = require('enb-bemxjst/techs/bemtree'),
Expand Down

0 comments on commit a7b4b96

Please sign in to comment.