This is a Gulp "pipe" that wraps the content of text files into CommonJS modules exporting that content as a string.
var gulp = require('gulp');
var gutil = require('gulp-util');
var through = require('through2');
/* Wrap the content of text files into CommonJS modules exporting that content as JS strings.
*/
function textFileToCommonJS(filename, options) {
var lines = [];
var stream = through.obj(
// This is the "write" method
function(file, enc, callback) {
//console.log('file:', file);
console.assert(file.isBuffer());
var code = 'module.exports = "' +
file.contents.toString().split(/\r\n|\n\r|\n|\r/).map( function(line) {
return line.replace(/\\/g, '\\\\').replace(/"/, '\\"');
}).join('\\n"\n + "') + '";\n';
// We create a new output file for each input file
var out_file = new gutil.File({
base: file.base,
cwd: file.cwd,
path: file.path + '.js',
contents: new Buffer(code)
});
this.push(out_file);
callback();
},
// This is the "end" method
function(callback) {
//console.log('end');
callback();
});
stream.options = options || {}; // future extension
return stream;
}