Skip to content

Instantly share code, notes, and snippets.

@burdiuz
Last active October 24, 2016 08:12
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 burdiuz/2dfb23aa5625bed1f3fe774795dd43a1 to your computer and use it in GitHub Desktop.
Save burdiuz/2dfb23aa5625bed1f3fe774795dd43a1 to your computer and use it in GitHub Desktop.

#gulp-string-inject

Simple Gulp plugin that looks for markers {$=pathToFile} in source file and replace them with JavaScript strings created from 'linked' file content. Marker should point to existing text file. Marker may be placed into any type of quotes, they will be replaced with marker. For converting file into a JavaScript string it uses JSON.stringify, so result should always be valid JSON string. Current directory is the directory of source file.
For example:
File1.js

var Hello = "{$=File2.js}";

File2.js

World!
"Le bug" was found!

After piping File1.js through gulp-string-inject you will get

var Hello = "World!\r\n\"Le bug\" was found!";

For injecting JavaScript files you can use stringInject.STRIP_COMMENTS(uses gulp-strip-comments) or stringInject.UGLIFY(uses gulp-uglify) to minimize resulting string size.

var stringInject = require('gulp-string-inject');
gulp.src('source/master.js')
    .pipe(stringInject(stringInject.UGLIFY))
    .pipe(gulp.dest('dist'));

Installation

via npm

npm install gist:2dfb23aa5625bed1f3fe774795dd43a1 --save

via git

git clone https://gist.github.com/burdiuz/2dfb23aa5625bed1f3fe774795dd43a1 gulp-string-inject
/**
* Created by Oleg Galaburda on 23.02.16.
*/
var os = require('os');
var fs = require('fs');
var path = require('path');
var through = require('through2');
var async = require('async');
var vfs = require('vinyl-fs');
var uglify = require('gulp-uglify');
var stripComments = require('gulp-strip-comments');
const PLUGIN_NAME = 'gulp-string-inject';
var Mode = {
DEFAULT: 'default',
UGLIFY: 'uglify',
STRIP_COMMENTS: 'stripComments'
};
function jobInitContent(content) {
//console.log(' -- add jobInitContent');
return function(callback) {
//console.log(' -> exec jobInitContent');
callback(null, content);
};
}
function jobRead(filePath) {
//console.log(' -- add jobRead', filePath);
return function(content, callback) {
//console.log(' -> exec jobRead', filePath);
var data = fs.readFileSync(filePath, 'utf8');
callback(null, content, data.toString('utf8'));
}
}
function jobReadUglify(filePath) {
//console.log(' -- add jobReadUglify', filePath);
return function(content, callback) {
//console.log(' -> exec jobReadUglify', filePath);
vfs.src(filePath)
.pipe(uglify())
.pipe(vfs.dest(os.tmpdir()))
.pipe(through.obj(function(file, encoding, pipeCallback) {
var data = file.contents.toString(encoding);
fs.unlink(file.path);
callback(null, content, data);
pipeCallback(null);
}));
}
}
function jobReadStripComments(filePath) {
//console.log(' -- add jobReadStripComments', filePath);
return function(content, callback) {
//console.log(' -> exec jobReadStripComments', filePath);
vfs.src(filePath)
.pipe(stripComments())
.pipe(vfs.dest(os.tmpdir()))
.pipe(through.obj(function(file, enc, pipeCallback) {
var data = file.contents.toString('utf8');
fs.unlink(file.path);
callback(null, content, data);
pipeCallback(null);
}));
}
}
function jobWriteContent(marker) {
//console.log(' -- add jobWriteContent', marker);
return function(content, data, callback) {
//console.log(' -> exec jobWriteContent', marker);
content = content.replace(marker, JSON.stringify(data));
callback(null, content);
};
}
function jobSaveContent(file) {
//console.log(' -- add jobSaveContent');
return function(content, callback) {
//console.log(' -> exec jobSaveContent');
file.contents = new Buffer(content, 'utf8');
callback(null);
};
}
function jobQuit(file, gulpCallback) {
//console.log(' -- add jobQuit');
return function(callback) {
//console.log(' -> exec jobQuit');
callback(null);
gulpCallback(null, file);
};
}
function gulpStringInject(mode) {
return through.obj(function(file, enc, cb) {
var list = null;
if (file.isNull()) {
// return empty file
return cb(null, file);
}
var baseDir = path.dirname(file.path);
var index = 0;
var content = file.contents.toString('utf8');
var jobs = [jobInitContent(content)];
var rgx = /("|'|`)?\s*\{\$\=\s*([^\}]+)\s*\}\s*\1?/g;
while (list = rgx.exec(content)) {
var filePath = baseDir + '/' + list[2];
index = list.index;
switch (mode) {
case Mode.STRIP_COMMENTS:
jobs.push(jobReadStripComments(filePath));
break;
case Mode.UGLIFY:
jobs.push(jobReadUglify(filePath));
break;
default:
case Mode.DEFAULT:
jobs.push(jobRead(filePath));
break;
}
jobs.push(jobWriteContent(list[0]));
}
jobs.push(jobSaveContent(file));
jobs.push(jobQuit(file, cb));
async.waterfall(jobs);
});
}
gulpStringInject.DEFAULT = Mode.DEFAULT;
gulpStringInject.UGLIFY = Mode.UGLIFY;
gulpStringInject.STRIP_COMMENTS = Mode.STRIP_COMMENTS;
module.exports = gulpStringInject;
{
"name": "gulp-string-inject",
"description": "Gulp plugin to inject files into JavaScript sources as JS strings.",
"version": "0.0.2",
"main": "gulp-string-inject.js",
"keywords": [
"gulpplugin",
"source",
"inject",
"json"
],
"dependencies": {
"async": "^1.5.2",
"gulp-strip-comments": "^2.2.1",
"gulp-uglify": "^1.5.1",
"through2": "^2.0.1",
"vinyl-fs": "^2.3.1"
},
"devDependencies": {
"async": "^1.5.2",
"gulp": ">=3.0.0",
"gulp-strip-comments": "^2.2.1",
"gulp-uglify": "^1.5.1",
"through2": "^2.0.1",
"vinyl-fs": "^2.3.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment