Skip to content

Instantly share code, notes, and snippets.

@qfox
Last active July 15, 2016 22:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save qfox/14eaca6283969c42a60926115b97ee15 to your computer and use it in GitHub Desktop.
Save qfox/14eaca6283969c42a60926115b97ee15 to your computer and use it in GitHub Desktop.
bundle-builder-prototype
parserOptions:
ecmaVersion: 6
sourceType: module
env:
node: true
es6: true
extends: pedant
node_modules
npm-debug.log

gulp-bem-bundle-builder

Just a prototype

const assert = require('assert');
const path = require('path');
const _eval = require('node-eval');
const merge = require('merge2');
const gulpBemSrc = require('./gulp-bem-src');
const thru = require('through2');
const BemBundle = require('bem-bundle');
const File = require('vinyl');
/**
* @param {Object} opts
* @param {?BemConfig} opts.config
* @returns {function(targets: String|String[]): Stream<Vinyl>}
*/
module.exports = function(opts) {
return function builder(targets) {
return thru.obj(function(bundle, enc, cb) {
assert(File.isVinyl(bundle) && ~['bemjson.js', 'bemdecl.js'].indexOf(pathTech(bundle.path)) ||
bundle instanceof BemBundle,
'Unacceptable object:' + bundle);
File.isVinyl(bundle) && (bundle = createBundleFromVinyl(bundle));
const ctx = Object.assign(bundle, {
src: function(techs) {
return gulpBemSrc({
config: opts.config,
sources: [].concat((opts.config || {}).levels).concat(bundle.levels),
techs: [].concat(techs)
});
},
target: function(tech) {
// later... provide stream of a target on resolve
}
});
var res = this;
merge(Object.keys(targets).map(target => {
const streamGenerator = targets[target];
const stream = streamGenerator(ctx);
// later... resolve targets for ctx.target
return stream; //.pipe(concat(ctx.name + '.' + target));
}))
.on('data', function(file) {
res.push(file);
})
.on('end', cb);
});
};
};
function createBundleFromVinyl(file) {
const key = (pathTech(file.path) === 'bemjson.js') ? 'bemjson' : 'bemdecl';
const data = {path: file.path};
data[key] = _eval(String(file.contents));
// Should we read internal levels?
return new BemBundle(data);
}
function pathTech(fullpath) {
return path.basename(fullpath).split('.').slice(1).join('.');
}
const assert = require('assert');
const path = require('path');
const Vinyl = require('vinyl');
const streamFromArray = require('stream-from-array');
/**
* src({
* config: BemConfig,
* sources: String[],
* bemdecl: *,
* techs: String[],
* techsAliases: ?Object<String,String[]>
* }): Stream<BemVinyl>
*/
/**
* @param {Object} opts
* @param {String[]} opts.techs
* @returns {Stream<Vinyl>}
*/
module.exports = function (opts) {
assert(opts.techs && Array.isArray(opts.techs), 'Requires techs array');
const disk = {
"/test/file.js": "test = 123;\n",
"/test/file2.js": "test2 = 456;\n",
"/test/file.css": ".ass { color: red }\n"
};
return streamFromArray.obj(Object.keys(disk)
.filter(fullpath => ~opts.techs.indexOf(pathTech(fullpath)))
.reduce((res, fullpath) => {
const body = disk[fullpath];
res.push(new Vinyl({
cwd: "/",
base: "/test/",
path: fullpath,
contents: Buffer.isBuffer(body) ? body : new Buffer(body)
}));
return res;
}, []));
};
function pathTech(fullpath) {
return path.basename(fullpath).split('.').slice(1).join('.');
}
const debug = require('gulp-debug');
const concat = require('gulp-concat');
const streamFromArray = require('stream-from-array');
const BemBundle = require('bem-bundle');
const Builder = require('./bundle-builder.js');
const builder = Builder({});
streamFromArray.obj([
new BemBundle({
name: 'bundle2',
path: './path-to/bundle2',
bemdecl: [],
levels: ['bundle.blocks']
})
])
// Stream<BemBundle>
.pipe(builder({
js: bundle => bundle.src('js')
.pipe(concat(bundle.name + '.js')),
css: bundle => bundle.src(['styl', 'css'])
.pipe(concat(bundle.name + '.css'))
}))
.pipe(debug());
{
"name": "gulp-bundle-test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "mocha tests.js"
},
"keywords": [],
"author": "Alexey Yaroshevich <zxqfox@gmail.com>",
"license": "MPL-2.0",
"dependencies": {
"bem-bundle": "bem-sdk/bem-bundle#519e5297f358d83e4d8130a94042f051bbb7db3f",
"gulp-concat": "^2.6.0",
"gulp-debug": "^2.1.2",
"merge2": "^1.0.2",
"node-eval": "^1.0.3",
"stream-from-array": "^1.0.0",
"stream-to-array": "^2.3.0",
"through2": "^2.0.1",
"vinyl": "^1.1.1"
},
"devDependencies": {
"chai": "^3.5.0",
"mocha": "^2.5.3"
}
}
const path = require('path');
const concat = require('gulp-concat');
const streamFromArray = require('stream-from-array');
const toArray = require('stream-to-array');
const chai = require('chai');
const BemBundle = require('bem-bundle');
const File = require('vinyl');
const Builder = require('./bundle-builder.js');
const builder = Builder({});
chai.should();
it('should generate js/css files for bemBundle', () => {
return toArray(streamFromArray.obj([
new BemBundle({
path: './path-to/bundle2/bundle2.bemdecl.js',
bemdecl: [],
levels: ['bundle.blocks']
})
])
// Stream<BemBundle>
.pipe(builder({
js: bundle => bundle.src('js').pipe(concat(bundle.name + '.js')),
css: bundle => bundle.src(['styl', 'css']).pipe(concat(bundle.name + '.css'))
}))
)
.then(array => {
array.map(f => path.basename(f.path)).should.eql(['bundle2.js', 'bundle2.css']);
});
});
it('should generate js/css files for vinyl', () => {
return toArray(streamFromArray.obj([
new File({
cwd: '.',
base: './path-to/bundle2',
path: './path-to/bundle2/bundle2.bemdecl.js',
contents: new Buffer('[]')
})
])
// Stream<BemBundle>
.pipe(builder({
js: bundle => bundle.src('js'),
css: bundle => bundle.src(['styl', 'css'])
}))
)
.then(array => {
array.map(f => path.basename(f.path)).should.eql(['file.js', 'file2.js', 'file.css']);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment