Last active
August 29, 2015 13:59
-
-
Save JPGygax68/10503261 to your computer and use it in GitHub Desktop.
This is a Gulp "pipe" that wraps the content of text files into CommonJS modules exporting that content as a string.
This file contains hidden or 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
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment