Skip to content

Commit

Permalink
Merge pull request #501 from dustyo-O/global-dot-delimited-deps
Browse files Browse the repository at this point in the history
Global dot delimited deps
  • Loading branch information
miripiruni authored Mar 5, 2018
2 parents 85824a0 + 98bc964 commit fc7b19f
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ function getDeps(requires) {
for (var lib in requires) {
if (requires.hasOwnProperty(lib)) {
if (requires[lib].globals) {
deps.global[lib] = requires[lib].globals;
deps.global[lib] = requires[lib].globals.replace(/\./g, '"]["');
deps.globalNames.push(lib);
deps.defineAsGlobal = true;
}
Expand Down
115 changes: 115 additions & 0 deletions test/api-generate-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,27 @@ describe('API generate', function() {
assert.equal(result, TEXT);
});

it('should get dependencies from global scope using dot-delimited key',
function () {
var code = 'global.text = {}; global.text.value = "' + TEXT + '";';
var bundle = bemhtml.generate('', {
requires: { textModule: { globals: 'text.value' } }
});

var sandbox = { global: {}, exports: {} };
sandbox.module = { exports: sandbox.exports };

vm.runInNewContext(code + EOL + bundle, sandbox);

var result = sandbox.exports.bemhtml.compile(function () {
block('b').def()(function () {
return this.require('textModule');
});
}).apply({ block: 'b' });

assert.equal(result, TEXT);
});

it('should get dependencies from global scope to module named' +
' with special symbols',
function() {
Expand Down Expand Up @@ -123,6 +144,25 @@ describe('API generate', function() {
assert.equal(result, TEXT);
});

it('should get dependencies from window scope (any browser)',
function () {
var code = 'window.text = {}; window.text.value = "' + TEXT + '";';
var bundle = bemhtml.generate('', {
requires: { textModule: { globals: 'text.value' } }
});
var sandbox = { window: {} };

vm.runInNewContext(code + EOL + bundle, sandbox);

var result = sandbox.bemhtml.compile(function () {
block('b').def()(function () {
return this.require('textModule');
});
}).apply({ block: 'b' });

assert.equal(result, TEXT);
});

it('should get dependencies from window scope to module named' +
' with special symbols',
function() {
Expand Down Expand Up @@ -226,6 +266,45 @@ describe('API generate', function() {
return assert.equal(res.fakeReq.getText(), 'globals');
});
});

it('must get dependency from global scope with dot-delimited key' +
'if it also is presented in YModule', function () {
var fakeModule = 'modules.define(' +
'"fakeModule", [], function(provide) {' +
'provide("' + TEXT + '");});';
var bundle = bemhtml.generate(function () {
block('b').def()(function () {
return this.require('fakeReq').getText();
});
}, {
requires: { fakeReq: { globals: 'f.akeVar', ym: 'fakeModule' } }
});
var sandbox = {
global: {},
window: {},
modules: require('ym')
};

vm.runInNewContext(
'window.f = ' +
'{ akeVar: { getText: function() { return "globals"; } } };' +
EOL +
fakeModule +
EOL +
bundle, sandbox);

var getLibs = function () {
return new vow.Promise(function (resolve) {
sandbox.modules.require('bemhtml', function (bemhtml) {
resolve(bemhtml.libs);
});
});
};

return getLibs().then(function (res) {
return assert.equal(res.fakeReq.getText(), 'globals');
});
});
});

describe('global deps over CommonJS', function() {
Expand Down Expand Up @@ -593,6 +672,42 @@ describe('API generate', function() {
});
});

it('must require global using dot-delimited key + ym', function () {
var fakeModule = 'var global = {};global.text = {};' +
'global.text.value = "' + TEXT +
'";modules.define("text1", [], function(provide) {' +
'provide("' + TEXT1 + '");});';
var bundle = bemhtml.generate('', {
requires: {
textModule: { globals: 'text.value' },
textModule1: { ym: 'text1' }
}
});

var sandbox = {
modules: require('ym')
};

vm.runInNewContext(fakeModule + EOL + bundle, sandbox);

var getLibs = function () {
return new vow.Promise(function (resolve) {
sandbox.modules.require('bemhtml', function (bemhtml) {
resolve(bemhtml.compile(function () {
block('b').def()(function () {
return this.require('textModule') +
this.require('textModule1');
});
}).apply({ block: 'b' }));
});
});
};

return getLibs().then(function (res) {
return assert.equal(res, TEXT + TEXT1);
});
});

it('must require commonJS+ym', function() {
var fakeModule = 'modules.define("text1", [], function(provide) {' +
'provide("' + TEXT1 + '");});';
Expand Down

0 comments on commit fc7b19f

Please sign in to comment.