This repository has been archived by the owner on Mar 25, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
196 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
var ${ exportName }; | ||
|
||
(function(global) { | ||
function buildBemXjst() { | ||
var exports = {}; | ||
|
||
${ bemxjst } | ||
|
||
return exports; | ||
}; | ||
|
||
var defineAsGlobal = true; | ||
|
||
// Provide with CommonJS | ||
if (typeof module === 'object' && typeof module.exports === 'object') { | ||
exports['${ exportName }'] = buildBemXjst(); | ||
defineAsGlobal = false; | ||
} | ||
|
||
// Provide to YModules | ||
if (typeof modules === 'object') { | ||
modules.define( | ||
'${ exportName }', | ||
[<%_.each([], function(name) {%>'${ name }',<%});%>], | ||
function( | ||
provide<%if ([].length) {%>,<%}%> | ||
${ [].join(', ') } | ||
) { | ||
provide(buildBemXjst()); | ||
} | ||
); | ||
|
||
defineAsGlobal = false; | ||
} | ||
|
||
// Provide to global scope | ||
if (defineAsGlobal) { | ||
${ exportName } = buildBemXjst(); | ||
global['${ exportName }'] = ${ exportName }; | ||
} | ||
})(typeof window !== "undefined" ? window : typeof global !== "undefined" ? global : this); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
|
||
var _ = require('lodash'); | ||
|
||
var assetDir = path.join(__dirname, '..', 'assets'); | ||
var templates = { | ||
bundle: { path: path.join(assetDir, 'bundle.jst') } | ||
}; | ||
|
||
// load templates | ||
_.mapKeys(templates, function(template, name) { | ||
templates[name] = _.template(fs.readFileSync(template.path, 'utf-8')); | ||
}); | ||
|
||
/** | ||
* Template for compile BEMHTML or BEMTREE to bundle. | ||
* | ||
* @param {String} code - Code compiled with the `bem-xjst` (BEMHTML or BEMTREE). | ||
* @param {Object} options - Options. | ||
* @param {String} [options.exportName=BEMHTML] - Name for exports. | ||
* @returns {String} | ||
*/ | ||
module.exports = function(code, options) { | ||
options || (options = {}); | ||
|
||
return templates.bundle({ | ||
exportName: options.exportName || 'BEMHTML', | ||
bemxjst: code | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,128 @@ | ||
describe('gulp-bemhtml', function () { | ||
describe('gulp-bemhtml', function() { | ||
'use strict'; | ||
|
||
var lib = require('..'); | ||
var expect = require('expect.js'); | ||
var bemxjst = require('bem-xjst'); | ||
var File = require('vinyl'); | ||
var gutil = require('gulp-util'); | ||
var _eval = require('node-eval'); | ||
var intoStream = require('into-stream'); | ||
|
||
describe('stream', function () { | ||
describe('stream', function() { | ||
var stream; | ||
var vinylFile; | ||
|
||
before(function (next) { | ||
stream = lib.bemhtml() | ||
.on('data', function (file) { | ||
vinylFile = file; | ||
}) | ||
before(function(next) { | ||
stream = lib.bemhtml(); | ||
|
||
stream.on('data', function(file) { | ||
vinylFile = file; | ||
}) | ||
.on('error', next) | ||
.on('end', next); | ||
|
||
intoStream.obj([new File({ | ||
path: 'page.bemhtml', | ||
contents: new Buffer('block(\'page\')(tag()(\'h1\'), content()(\'Hello, world!\'));') | ||
})]) | ||
.pipe(stream); | ||
stream.write(new gutil.File({ | ||
path: 'page.bemhtml', | ||
contents: new Buffer('block(\'page\')(tag()(\'h1\'), content()(\'Hello, world!\'));') | ||
})); | ||
stream.end(); | ||
}); | ||
|
||
it('changes file extension to *.bemhtml.js', function () { | ||
it('changes file extension to *.bemhtml.js', function() { | ||
expect(vinylFile.relative).to.be.equal('page.bemhtml.js'); | ||
}); | ||
|
||
it('outputs bemhtml templates compiler', function () { | ||
it('outputs bemhtml templates compiler', function() { | ||
var bemhtml = _eval(vinylFile.contents.toString()); | ||
expect(bemhtml.apply({block: 'page'})).to.be.equal('<h1 class="page">Hello, world!</h1>'); | ||
expect(bemhtml.apply({ block: 'page' })).to.be.equal('<h1 class="page">Hello, world!</h1>'); | ||
}); | ||
}); | ||
|
||
describe('stream with custom engine', function () { | ||
describe('stream with custom engine', function() { | ||
var stream; | ||
var vinylFile; | ||
|
||
before(function (next) { | ||
const stream = lib({}, bemxjst.bemhtml) | ||
.on('data', function (file) { | ||
vinylFile = file; | ||
}) | ||
before(function(next) { | ||
stream = lib({}, bemxjst.bemhtml); | ||
|
||
stream.on('data', function(file) { | ||
vinylFile = file; | ||
}) | ||
.on('error', next) | ||
.on('end', next); | ||
|
||
intoStream.obj([new File({ | ||
stream.write(new gutil.File({ | ||
path: 'page.bemhtml', | ||
contents: new Buffer('block(\'page\')(tag()(\'h1\'), content()(\'Hello, world!\'));') | ||
})]) | ||
.pipe(stream); | ||
})); | ||
stream.end(); | ||
}); | ||
|
||
it('changes file extension to *.bemhtml.js', function () { | ||
it('changes file extension to *.bemhtml.js', function() { | ||
expect(vinylFile.relative).to.be.equal('page.bemhtml.js'); | ||
}); | ||
}); | ||
|
||
describe('stream with exportName', function() { | ||
var stream; | ||
var vinylFile; | ||
|
||
before(function(next) { | ||
stream = lib.bemhtml({ exportName: 'BEMHTML' }); | ||
|
||
stream.on('data', function(file) { | ||
vinylFile = file; | ||
}) | ||
.on('error', next) | ||
.on('end', next); | ||
|
||
stream.write(new gutil.File({ | ||
path: 'page.bemhtml', | ||
contents: new Buffer('block(\'page\')(tag()(\'h1\'), content()(\'Hello, world!\'));') | ||
})); | ||
stream.end(); | ||
}); | ||
|
||
it('should export compiler to global', function() { | ||
var engine = _eval(vinylFile.contents.toString()); | ||
|
||
expect(engine).to.have.property('BEMHTML'); | ||
expect(engine.BEMHTML.apply).to.be.a('function'); | ||
}); | ||
|
||
it('should export compiler to custom name', function(next) { | ||
|
||
var testStream = lib.bemhtml({ exportName: 'customProperty' }); | ||
var testFile; | ||
|
||
testStream.on('data', function(file) { testFile = file; }) | ||
.on('error', compileDone) | ||
.on('end', compileDone); | ||
|
||
testStream.write(new gutil.File({ | ||
path: 'page.bemhtml', | ||
contents: new Buffer('block(\'page\')(tag()(\'h1\'), content()(\'Hello, world!\'));') | ||
})); | ||
testStream.end(); | ||
|
||
function compileDone() { | ||
var engine = _eval(testFile.contents.toString()); | ||
|
||
expect(engine).to.have.property('customProperty'); | ||
next(); | ||
} | ||
}); | ||
|
||
it('should export compiler to YModules', function() { | ||
var vm = require('vm'); | ||
var name = ''; | ||
|
||
vm.runInNewContext(vinylFile.contents.toString(), { | ||
require: require, | ||
console: console, | ||
modules: { define: function(exportName) { name = exportName; } } | ||
}); | ||
|
||
expect(name).to.be('BEMHTML'); | ||
}); | ||
}); | ||
}); |