Skip to content
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

Use bem-xjst in Node.js without bundling via browserify (#407 fixed) #410

Merged
merged 1 commit into from
Feb 1, 2017

Conversation

miripiruni
Copy link
Contributor

Fixes #407

Changes proposed in this pull request

  • use required bem-xjst instead of bundled
  • All public API not changed

API Demo (compile(), apply(), generate())

$ cat test.js
var bemhtml = require('./').bemhtml;
var libs = {
  jscs: require('jscs'),
  chai: require('chai')
};

var templates = function templates() {
  block('a').content()(function() {

    console.log(this.require('chai').version);
    console.log(this.chai.version);

    return applyNext();
  });
};

var htmlTemplates = bemhtml.compile(templates);

htmlTemplates.BEMContext.prototype.require = function require(libName) {
  return libs[libName];
};

htmlTemplates.BEMContext.prototype.chai = require('chai');

var bemjson = { block: 'a', content: 'First' };

console.log(htmlTemplates.apply(bemjson));

htmlTemplates.compile(function() {
  block('a').mix()('mixed');
});
console.log(htmlTemplates.apply(bemjson));

require('fs').writeFileSync('bemhtml-runtime.js', bemhtml.generate(function() {
  block('a').tag()('span');
}, { runtimeLint: true }));
$ node test
3.5.0
3.5.0
<div class="a">First</div>
3.5.0
3.5.0
<div class="a mixed">First</div>
$ node
> var b = require('./bemhtml-runtime')
undefined
> var t = b.compile()
undefined
> t.apply({block:'a'})
'<span class="a"></span>'
> t.apply({block:'a__b'})

BEM-XJST WARNING: wrong block name.
Block name can not contain modifier delimeter nor elem delimeter.
block: a__b
ctx: {"block":"a__b"}

>
(To exit, press ^C again or type .exit)
>

@qfox
Copy link
Member

qfox commented Jan 31, 2017

@miripiruni What you think about trying to bundle with rollup + rollup-plugin-node-resolve + rollup-plugin-commonjs? Guess we should get a try.

index.js Outdated
var _cache = {};

function getEngine(engineName) {
if (_cache[engineName]) return _cache[engineName];
var getEngine = function getEngine(engineName) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need var getEngine?

index.js Outdated
get bemtree() { return getEngine('bemtree'); },
get bemhtml() { return getEngine('bemhtml'); }
get bemhtml() { return getEngine('bemhtml'); },
get bemtree() { return getEngine('bemtree'); }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why you swap them? ;-)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zxqfox I just wrote it from scratch, I can swap back =)

@coveralls
Copy link

coveralls commented Jan 31, 2017

Coverage Status

Coverage increased (+0.4%) to 97.99% when pulling 2984d44 on issue-407__no-bundling into 53f0da9 on master.

@@ -519,7 +524,8 @@ BEMXJST.prototype.exportApply = function exportApply(exports) {

// Add templates at run time
exports.compile = function compile(templates) {
return self.compile(templates);
self.compile(templates);
return exports;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure it's a good behaviour at all, some times better to have immutable instances with templates.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tadatuta what reason to do so? Only inconcistency between compiles? #408

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nah, just lack a way to the same as original compile — make an instance with templates in runtime or get templates from bundled one.

@miripiruni
Copy link
Contributor Author

@zxqfox I’ll try rollup, thanks.

@qfox
Copy link
Member

qfox commented Jan 31, 2017

Looks good at all, but I'm not sure about bundled version. Don't know how to quickly check it

@miripiruni
Copy link
Contributor Author

@zxqfox see PR’s description. I provide logs from my bundle test. Template bundling, bem-xjst options and etc works.

@coveralls
Copy link

coveralls commented Jan 31, 2017

Coverage Status

Coverage increased (+0.4%) to 97.99% when pulling fc0da45 on issue-407__no-bundling into 53f0da9 on master.

Copy link
Member

@qfox qfox left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/ok

index.js Outdated
var sourceBundle = fs.readFileSync(pathToBundle, 'utf8');

runtime.source = sourceBundle;
return _cache[engineName] = new Compiler(engineName);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

||

BEMXJST.prototype.getTemplate = function(code, options) {
var templates = {};
this.compile(code, options);
this.exportApply(templates);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return this.exportApply();


// Strip the function
out = out.replace(/^function[^{]+{|}$/g, '');
var out = code ? utils.fnToString(code) : '';
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

var out = utils.fnToString(code)

lib/compiler.js Outdated
if (options.runtimeLint) {
code = fnToString(code);
code = code + ';' + require('fs')
.readFileSync('./runtime-lint/index.js', 'utf8');
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use require

lib/compiler.js Outdated
var api = new engines[this.engineName](options);

if (options.runtimeLint) {
code = fnToString(code);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

code = fnToString() + …

lib/compiler.js Outdated

var source = [
'/// -------------------------------------',
'/// --------- BEM-XJST Runtime Start ----',
'/// -------------------------------------',
'var ' + exportName + ' = function(module, exports) {',
this.runtime.source + ';',
bundleSource + ';',
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

заинлайнить

lib/compiler.js Outdated

var locals = this.runtime.prototype.locals;
var fs = require('fs');
var locals = require('./bemxjst').prototype.locals;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

заинлайнить

@coveralls
Copy link

coveralls commented Feb 1, 2017

Coverage Status

Coverage increased (+0.5%) to 98.048% when pulling e043e49 on issue-407__no-bundling into 53f0da9 on master.

@miripiruni miripiruni merged commit 687969f into master Feb 1, 2017
@miripiruni miripiruni deleted the issue-407__no-bundling branch January 29, 2018 10:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants