Skip to content

Commit

Permalink
Compiler: several require fixes (#481)
Browse files Browse the repository at this point in the history
* compiler: modules required and provided properly

* api-generate: tests updated

* Tests fixed (squash me please)
  • Loading branch information
dustyo-O authored and miripiruni committed Oct 13, 2017
1 parent 739ba5a commit 2eb33dd
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 33 deletions.
53 changes: 32 additions & 21 deletions lib/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ function getDeps(requires) {
commonJSNames: [],
ym: [],
ymVars: [],
ymVarsString: '',
ymLibs: [],
defineAsGlobal: false
};
Expand All @@ -52,7 +53,7 @@ function getDeps(requires) {
}

if (requires[lib].ym) {
deps.ym.push(lib);
deps.ym.push(requires[lib].ym);
deps.ymVars.push(lib);
deps.ymLibs.push(lib);
}
Expand All @@ -66,9 +67,14 @@ function getDeps(requires) {
}

if (deps.ymVars.length) {
deps.ymVarsString = deps.ymVars.map(function(item) {
return item + ':' + item;
}).join('');

deps.ymVars = ',' + deps.ymVars.map(function(item) {
return item.toString();
}).join();

}

return deps;
Expand All @@ -94,7 +100,7 @@ Compiler.prototype.generate = function generate(code, options) {
'var BEMContext = exports.BEMContext || context.BEMContext;',
// Provides third-party libraries from different modular systems
'BEMContext.prototype.require = function(lib) {',
'return this._libs[lib];',
'return this._libs[lib];',
'};',
'});'
].join('');
Expand Down Expand Up @@ -150,48 +156,53 @@ Compiler.prototype.generate = function generate(code, options) {
'var exp = typeof exports !== "undefined" ? exports : global;',

// Provide with CommonJS
deps.commonJSNames.length === 0 || (
deps.defineAsGlobal || deps.commonJSNames.length === 0 || (
'if (typeof module==="object" && typeof module.exports==="object") {' +
'exp["' + exportName + '"] = buildBemXjst();' +
'exp["' + exportName + '"].libs = {};' + (
'var _libs = {};' + (
deps.commonJSNames.map(function(dep) {
return 'exp["' + exportName + '"].libs["' + dep + '"] = ' +
var commonPath = deps.commonJS[dep];
return '_libs["' + dep + '"] = ' +
'glob && typeof glob["' + dep + '"] ' +
'!== "undefined" ? ' +
'glob["' + dep + '"] : require("' + dep + '");';
})
'glob["' + dep + '"] : require("' + commonPath + '");';
}) +
exportName + ' = buildBemXjst(_libs);' +
'exp["' + exportName + '"] = ' + exportName + ';' +
'exp["' + exportName + '"].libs = _libs;'
) + '}'),

// Provide to YModules
'if (typeof modules === "object") {',
'modules.define("' + exportName + '",' +
JSON.stringify(deps.ym) + ',' +
'function(provide' + deps.ymVars + ') { ' +
'var engine = buildBemXjst();' +
deps.ymLibs +
'provide(engine);' +
'}' +
'var engine = buildBemXjst({' + deps.ymVarsString + '});' +
deps.ymLibs +
'provide(engine);' +
'}' +
');',
'}',

// Provide to global scope
deps.defineAsGlobal ? deps.globalNames.length !== 0 ? (
exportName + ' = buildBemXjst(glob);' +
'var _libs = {};' +
deps.globalNames.map(function(dep) {
var globName = deps.global[dep];
return 'typeof glob["' + globName + '"] !== "undefined" && (' +
'_libs["' + dep + '"] = ' +
'glob["' + globName + '"]);';
}).join('') +
exportName + ' = buildBemXjst(_libs);' +
'exp["' + exportName + '"] = ' + exportName + ';' +
'exp["' + exportName + '"].libs = {};' +
deps.commonJSNames.map(function(dep) {
return 'typeof glob["' + dep + '"] !== "undefined" && (' +
'exp["' + exportName + '"].libs["' + dep + '"] = ' +
'glob["' + dep + '"]);';
}).join('')
'exp["' + exportName + '"].libs = _libs;'
) : (
exportName + '= buildBemXjst(this.global);' +
'exp["' + exportName + '"] = ' + exportName + ';'
'exp["' + exportName + '"] = ' + exportName + ';' +
'exp["' + exportName + '"].libs = this.global;'
) : '',

'})(typeof window !== "undefined" ? ' +
'window : typeof global !== "undefined" ? global : this);'

].join(EOL));

return options.sourceMap && options.sourceMap.include === false ?
Expand Down
27 changes: 15 additions & 12 deletions test/api-generate-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,15 @@ describe('API generate', function() {
var TEXT = 'Hello templates!';
var code = 'window.text = "' + TEXT + '";';
var bundle = bemhtml.generate('', {
requires: { text: { globals: 'text' } }
requires: { textModule: { globals: 'text' } }
});
var sandbox = { window: {} };

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

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

Expand All @@ -80,14 +80,15 @@ describe('API generate', function() {
it('must get dependency from global scope ' +
'if it also is presented in YModule', function() {
var TEXT = 'Hello templates!';
var fakeModule = 'modules.define("fake", [], function(provide) {' +
var fakeModule = 'modules.define(' +
'"fakeModule", [], function(provide) {' +
'provide("' + TEXT + '");});';
var bundle = bemhtml.generate(function() {
block('b').def()(function() {
return this.require('fake').getText();
return this.require('fakeReq').getText();
});
}, {
requires: { fake: { globals: 'fake', ym: 'fake' } }
requires: { fakeReq: { globals: 'fakeVar', ym: 'fakeModule' } }
});
var sandbox = {
global: {},
Expand All @@ -98,7 +99,7 @@ describe('API generate', function() {
sandbox.module = { exports: sandbox.exports };

vm.runInNewContext(
'window.fake = { getText: function() { return "globals"; } };' +
'window.fakeVar = { getText: function() { return "globals"; } };' +
EOL +
fakeModule +
EOL +
Expand All @@ -114,24 +115,26 @@ describe('API generate', function() {
'if it also is presented in CommonJS', function() {
var bundle = bemhtml.generate('', {
commonJSModules: FAKE_COMMON_MODULE,
requires: { fake: { globals: 'fake', commonJS: 'no-module' } }
requires: { fakeReq: { globals: 'fakeVar', commonJS: 'no-module' } }
});

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

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

assert.equal(sandbox.exports.bemhtml.libs.fake.getText(), 'globals');
assert.equal(
sandbox.exports.bemhtml.libs.fakeReq.getText(), 'globals'
);
});

it('as commonjs', function() {
var bundle = bemhtml.generate('', {
commonJSModules: FAKE_COMMON_MODULE,
requires: { fake: { globals: 'fake', commonJS: 'no-module' } }
requires: { fakeReq: { globals: 'fake', commonJS: 'no-module' } }
});
var module = { exports: {} };
var sandbox = {
Expand All @@ -151,14 +154,14 @@ describe('API generate', function() {
var TEXT = 'Hello templates!';
var bundle = bemhtml.generate('', {
commonJSModules: FAKE_COMMON_MODULE,
requires: { fake: { commonJS: 'fake' } }
requires: { fakeReq: { commonJS: 'fake' } }
});

var sandbox = { global: {}, exports: {} };
sandbox.module = { exports: sandbox.exports };
vm.runInNewContext(bundle, sandbox);

assert.equal(sandbox.exports.bemhtml.libs.fake.getText(), TEXT);
assert.equal(sandbox.exports.bemhtml.libs.fakeReq.getText(), TEXT);
assert.deepEqual(sandbox.global, {},
'Should not export to global in CommonJS context.');
});
Expand Down

0 comments on commit 2eb33dd

Please sign in to comment.