-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(tech): add exports option #178
Changes from all commits
0939287
717baf6
8f9b9a4
d3cb1ec
7360057
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
node_modules | ||
coverage | ||
npm-debug.log |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
var fs = require('fs'), | ||
path = require('path'), | ||
mock = require('mock-fs'), | ||
safeEval = require('node-eval'), | ||
MockNode = require('mock-enb/lib/mock-node'), | ||
Tech = require('../../techs/bemhtml'), | ||
loadDirSync = require('mock-enb/utils/dir-utils').loadDirSync, | ||
|
@@ -205,6 +206,74 @@ describe('bemhtml', function () { | |
}); | ||
}); | ||
}); | ||
|
||
describe('with option `exports`', function () { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Почему тесты не где-то в Хочется как можно меньше интеграционных тестов, если нет реальной необходимости. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Кто ж знал, что тут есть разделение на интеграционные тесты ;-) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @blond Я посмотрел туда. И ничо не понял. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Пробую перенести в отдельный файл, разбиение по --node, --browser — мешает |
||
it('must export ym only', function () { | ||
var modules = {}; | ||
return build(['block("a").tag()("a")'], { | ||
exports: { ym: true }, | ||
context: { | ||
module: undefined, | ||
modules: { | ||
define: function (name, _, p) { | ||
p(function (res) { modules[name] = res; }); | ||
} | ||
} | ||
} | ||
}) | ||
.spread(function () { | ||
modules.BEMHTML.apply({ block: 'a' }) | ||
.must.eql('<a class="a"></a>'); | ||
}); | ||
}); | ||
|
||
it('must export commonjs only', function () { | ||
var m = { exports: {} }; | ||
return build(['block("a").tag()("a")'], { | ||
exports: { commonJS: true }, | ||
context: { module: m } | ||
}) | ||
.spread(function () { | ||
m.exports.BEMHTML.apply({ block: 'a' }) | ||
.must.eql('<a class="a"></a>'); | ||
}); | ||
}); | ||
|
||
it('must export global only', function () { | ||
var window = {}; | ||
return build(['block("a").tag()("a")'], { | ||
exports: { globals: true }, | ||
context: { module: undefined, window: window } | ||
}) | ||
.spread(function () { | ||
window.BEMHTML.apply({ block: 'a' }) | ||
.must.eql('<a class="a"></a>'); | ||
}); | ||
}); | ||
|
||
it('must export as ym and global', function () { | ||
var modules = {}, window = {}; | ||
return build(['block("a").tag()("a")'], { | ||
exports: { globals: 'force', ym: true }, | ||
context: { | ||
module: undefined, | ||
window: window, | ||
modules: { | ||
define: function (name, _, p) { | ||
p(function (res) { modules[name] = res; }); | ||
} | ||
} | ||
} | ||
}) | ||
.spread(function () { | ||
modules.BEMHTML.apply({ block: 'a' }) | ||
.must.eql('<a class="a"></a>', 'modules failed'); | ||
window.BEMHTML.apply({ block: 'a' }) | ||
.must.eql('<a class="a"></a>', 'global failed'); | ||
}); | ||
}); | ||
// return build(templates, { exports: {globals: true, commonJS: true, ym: true} }) | ||
}); | ||
}); | ||
|
||
function build(templates, options) { | ||
|
@@ -215,6 +284,7 @@ function build(templates, options) { | |
blocks: {}, | ||
bundle: {} | ||
}, | ||
noEval = typeof options.context === 'object', | ||
bundle, fileList; | ||
|
||
// hack for mock-fs | ||
|
@@ -230,24 +300,22 @@ function build(templates, options) { | |
scheme.blocks = templates; | ||
} | ||
|
||
if (templates.length) { | ||
templates.forEach(function (item, i) { | ||
scheme.blocks['block-' + i + '.bemhtml.js'] = item; | ||
}); | ||
} | ||
|
||
mock(scheme); | ||
|
||
bundle = new MockNode('bundle'); | ||
fileList = new FileList(); | ||
fileList.addFiles(loadDirSync('blocks')); | ||
bundle.provideTechData('?.files', fileList); | ||
|
||
return bundle.runTechAndRequire(Tech, options) | ||
return bundle[noEval ? 'runTech' : 'runTechAndRequire'](Tech, options) | ||
.spread(function (res) { | ||
var filename = bundle.resolvePath(bundle.unmaskTargetName(options.target || '?.bemhtml.js')), | ||
str = fs.readFileSync(filename, 'utf-8'); | ||
|
||
if (noEval) { | ||
res = safeEval(str, filename, options.context); | ||
} | ||
|
||
return [res, str]; | ||
}); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Давай ещё добавим описание опции
exports
вapi.ru.md
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Добавил