-
Notifications
You must be signed in to change notification settings - Fork 48
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
Conversation
@miripiruni What you think about trying to bundle with |
index.js
Outdated
var _cache = {}; | ||
|
||
function getEngine(engineName) { | ||
if (_cache[engineName]) return _cache[engineName]; | ||
var getEngine = function getEngine(engineName) { |
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.
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'); } |
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.
why you swap them? ;-)
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.
@zxqfox I just wrote it from scratch, I can swap back =)
lib/bemxjst/index.js
Outdated
@@ -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; |
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.
Not sure it's a good behaviour at all, some times better to have immutable instances with templates.
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.
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.
Nah, just lack a way to the same as original compile — make an instance with templates in runtime or get templates from bundled one.
2984d44
to
fc0da45
Compare
@zxqfox I’ll try |
Looks good at all, but I'm not sure about bundled version. Don't know how to quickly check it |
@zxqfox see PR’s description. I provide logs from my bundle test. Template bundling, bem-xjst options and etc works. |
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.
/ok
index.js
Outdated
var sourceBundle = fs.readFileSync(pathToBundle, 'utf8'); | ||
|
||
runtime.source = sourceBundle; | ||
return _cache[engineName] = new Compiler(engineName); |
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.
||
lib/bemxjst/index.js
Outdated
BEMXJST.prototype.getTemplate = function(code, options) { | ||
var templates = {}; | ||
this.compile(code, options); | ||
this.exportApply(templates); |
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.
return this.exportApply();
lib/bemxjst/index.js
Outdated
|
||
// Strip the function | ||
out = out.replace(/^function[^{]+{|}$/g, ''); | ||
var out = code ? utils.fnToString(code) : ''; |
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.
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'); |
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.
use require
lib/compiler.js
Outdated
var api = new engines[this.engineName](options); | ||
|
||
if (options.runtimeLint) { | ||
code = fnToString(code); |
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.
code = fnToString() + …
lib/compiler.js
Outdated
|
||
var source = [ | ||
'/// -------------------------------------', | ||
'/// --------- BEM-XJST Runtime Start ----', | ||
'/// -------------------------------------', | ||
'var ' + exportName + ' = function(module, exports) {', | ||
this.runtime.source + ';', | ||
bundleSource + ';', |
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.
заинлайнить
lib/compiler.js
Outdated
|
||
var locals = this.runtime.prototype.locals; | ||
var fs = require('fs'); | ||
var locals = require('./bemxjst').prototype.locals; |
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.
заинлайнить
fc0da45
to
fdca177
Compare
fdca177
to
e043e49
Compare
Fixes #407
Changes proposed in this pull request
API Demo (compile(), apply(), generate())