Skip to content

Commit

Permalink
engineOptions: exportName and requires tests
Browse files Browse the repository at this point in the history
  • Loading branch information
miripiruni committed Feb 12, 2018
1 parent dffa460 commit a128856
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 22 deletions.
13 changes: 11 additions & 2 deletions lib/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,22 @@ var compileCommonJS = require('./compile-commonjs'),
* @param {Object} options Options.
* @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.requires={}] Deprecated. Fallback for backward
* compatibility, use options.engineOptions.requires
* @returns {String}
*/
exports.compile = function (code, options) {
options || (options = {});

var requires = options.requires || {};
var requires;

if (options.requires) {
requires = options.requires;
} else if (options.engineOptions && options.engineOptions.requires) {
requires = options.engineOptions.requires;
} else {
requires = {};
}

return compileCommonJS(requires, options.dirname)
.then(function (commonJSModules) {
Expand Down
13 changes: 8 additions & 5 deletions techs/bem-xjst.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,14 @@ module.exports = buildFlow.create()
engineOptions.exportName || (engineOptions.exportName = this._exportName);

if (this._requires) {
this.node.getLogger().logOptionIsDeprecated(this.node.unmaskTargetName(this._target),
'enb-bemxjst', this.getName(), 'requires', 'engineOptions.requires',
' It will be removed in v9.0.0.');

engineOptions.requires || (engineOptions.requires = this._requires);
this.node.getLogger().logOptionIsDeprecated(
this.node.unmaskTargetName(this._target),
'enb-bemxjst',
this.getName(),
'requires',
'engineOptions.requires',
' It will be removed in v9.0.0.'
);
}

engineOptions.sourceMap = { from: this._target };
Expand Down
33 changes: 32 additions & 1 deletion test/techs/bemhtml.test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
var fs = require('fs'),
path = require('path'),
mock = require('mock-fs'),
assert = require('assert'),
MockNode = require('mock-enb/lib/mock-node'),
Tech = require('../../techs/bemhtml'),
loadDirSync = require('mock-enb/utils/dir-utils').loadDirSync,
FileList = require('enb/lib/file-list'),
bundlePath = path.resolve('lib/bundle.js'),
sinon = require('sinon');
sinon = require('sinon'),
utils = require('../utils'),
run = function (code) {
return utils.run(code, { runtime: 'node' });
};

describe('bemhtml', function () {
before(function () {
Expand Down Expand Up @@ -235,6 +240,32 @@ describe('bemhtml', function () {
res.BEMHTML.apply(bemjson).must.be(html);
});
});

it('must support engineOptions.exportName fallback for backward compatibility', function () {
var blocks = { 'b.bemhtml.js': 'block("b").tag()("span")' };

return build(blocks, { engineOptions: { exportName: 'htmlMaker' } })
.spread(function (res) {
assert(res.htmlMaker, 'No BEMHTML exported as htmlMaker');

res
.htmlMaker
.apply({ block: 'b' })
.must.be('<span class="b"></span>');
});
});

it('must support engineOptions.requires', function () {
var code = 'global.text = "Hello world!"',
options = {
engineOptions: { requires: { text: { globals: 'text' } } }
};

return utils.compileBundle(code, options)
.then(run)
.then(utils.getLibs)
.should.become({ text: 'Hello world!' });
});
});

describe('compat', function () {
Expand Down
34 changes: 33 additions & 1 deletion test/techs/bemtree.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ var fs = require('fs'),
Tech = require('../../techs/bemtree'),
loadDirSync = require('mock-enb/utils/dir-utils').loadDirSync,
FileList = require('enb/lib/file-list'),
bundlePath = path.resolve('lib/bundle.js');
bundlePath = path.resolve('lib/bundle.js'),
assert = require('assert'),
utils = require('../utils'),
run = function (code) {
return utils.run(code, { runtime: 'node' });
};

describe('bemtree', function () {
before(function () {
Expand Down Expand Up @@ -138,6 +143,33 @@ describe('bemtree', function () {
res.BEMTREE.apply(data).must.eql(bemjson);
});
});

it('must support engineOptions.exportName fallback for backward compatibility', function () {
var blocks = { 'b.bemtree.js': 'block("b").content()("test")' };

return build(blocks, { engineOptions: { exportName: 'bemtreeMaker' } })
.spread(function (res) {
assert(res.bemtreeMaker, 'No BEMTREE exported as bemtreeMaker');

res
.bemtreeMaker
.apply({ block: 'b' })
.content
.must.be('test');
});
});

it('must support engineOptions.requires', function () {
var code = 'global.text = "Hello world!"',
options = {
engineOptions: { requires: { text: { globals: 'text' } } }
};

return utils.compileBundle(code, options)
.then(run)
.then(utils.getLibs)
.should.become({ text: 'Hello world!' });
});
});

describe('compat', function () {
Expand Down
10 changes: 5 additions & 5 deletions test/unit/bundle--browser+ym.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe('bundle --browser+ym', function () {
it('must get dependency from global scope', function () {
var code = 'window.text = "Hello world!"',
options = {
requires: {
requires: { // fallback for backward compatibility, use engineOptions.requires
text: {
globals: 'text'
}
Expand All @@ -28,7 +28,7 @@ describe('bundle --browser+ym', function () {
it('must get dependency from global scope using dot-delimited key', function () {
var code = 'window.text = { text: "Hello world!" };',
options = {
requires: {
requires: { // fallback for backward compatibility, use engineOptions.requires
text: {
globals: 'text.text'
}
Expand All @@ -44,7 +44,7 @@ describe('bundle --browser+ym', function () {
it('must require dependency from ym', function () {
var code = 'modules.define("text", [], function (provide) { provide("Hello world!"); })',
options = {
requires: {
requires: { // fallback for backward compatibility, use engineOptions.requires
text: {
ym: 'text'
}
Expand Down Expand Up @@ -77,7 +77,7 @@ describe('bundle --browser+ym', function () {

it('must require module from CommonJS', function () {
var options = {
requires: {
requires: { // fallback for backward compatibility, use engineOptions.requires
fake: {
commonJS: 'fake'
}
Expand All @@ -100,7 +100,7 @@ describe('bundle --browser+ym', function () {
'});'
].join(EOL),
options = {
requires: {
requires: { // fallback for backward compatibility, use engineOptions.requires
fake: {
ym: 'text',
commonJS: 'fake'
Expand Down
8 changes: 4 additions & 4 deletions test/unit/bundle--browser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe('bundle --browser', function () {
it('must get dependency from global scope', function () {
var code = 'window.text = "Hello world!"',
options = {
requires: {
requires: { // fallback for backward compatibility, use engineOptions.requires
text: {
globals: 'text'
}
Expand All @@ -35,7 +35,7 @@ describe('bundle --browser', function () {
it('must get dependency from global scope using dot-delimited key', function () {
var code = 'window.text = { text: "Hello world!" };',
options = {
requires: {
requires: { // fallback for backward compatibility, use engineOptions.requires
text: {
globals: 'text.text'
}
Expand Down Expand Up @@ -68,7 +68,7 @@ describe('bundle --browser', function () {

it('must require module from CommonJS', function () {
var options = {
requires: {
requires: { // fallback for backward compatibility, use engineOptions.requires
fake: {
commonJS: 'fake'
}
Expand All @@ -87,7 +87,7 @@ describe('bundle --browser', function () {
it('must get dependency from global scope if it also is presented in CommonJS', function () {
var code = 'window.fake = { getText: function () { return "globals"; } };',
options = {
requires: {
requires: { // fallback for backward compatibility, use engineOptions.requires
fake: {
globals: 'fake',
commonJS: 'fake'
Expand Down
8 changes: 4 additions & 4 deletions test/unit/bundle--node.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe('bundle --node', function () {
it('must get dependency from global scope', function () {
var code = 'global.text = "Hello world!"',
options = {
requires: {
requires: { // fallback for backward compatibility, use engineOptions.requires
text: {
globals: 'text'
}
Expand All @@ -35,7 +35,7 @@ describe('bundle --node', function () {
it('must get dependency from global scope using dot-delimited key', function () {
var code = 'global.text = { text: "Hello world!" };',
options = {
requires: {
requires: { // fallback for backward compatibility, use engineOptions.requires
text: {
globals: 'text.text'
}
Expand Down Expand Up @@ -68,7 +68,7 @@ describe('bundle --node', function () {

it('must require module from CommonJS', function () {
var options = {
requires: {
requires: { // fallback for backward compatibility, use engineOptions.requires
fake: {
commonJS: 'fake'
}
Expand All @@ -87,7 +87,7 @@ describe('bundle --node', function () {
it('must get dependency from CommonJS if it also is presented in global scope', function () {
var code = 'global.fake = { getText: function () { return "globals"; } };',
options = {
requires: {
requires: { // fallback for backward compatibility, use engineOptions.requires
fake: {
globals: 'fake',
commonJS: 'fake'
Expand Down

0 comments on commit a128856

Please sign in to comment.