gulp-js-base64-inject
v1.0.1
Published
Gulp plugin for injecting image files back into JS code as strings
Downloads
85
Maintainers
Readme
gulp-js-base64-inject
This is a small Gulp plugin for injecting image files right directly into your .js code as base64 strings.
What is it
The simplest example. JS code:
var coverImg = '@@import cover.jpg';
var logoImg = '@@import logo.png';
...and Gulp task:
var gulp = require('gulp');
var inject = require('gulp-js-text-inject');
gulp.task('js', function() {
return gulp.src('path/to/your/js/*.js')
.pipe(inject({
basepath: 'path/to/your/data/'
}))
.pipe(gulp.dest('path/for/output'));
});
If we use this plugin, the resulting JS will be:
var coverImg = '/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCAAQABADASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqs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BhuAlsXjz7cYfh17+fwFLmOwT/A9E/gMUlsHQBlvU//n5guPnxAcPRK/8ZPpxhZWD8wrSSkYkxC9i+gdQYFGNUABBAWPIAM6YvGf8vAxapFc9u/xO4JfWIQU9QENgcYASH9j9wLIBCHlTHfmG49+kpwwlgyf7uGDsD4zvG1cCQTwKmqF9MDKwM2LqElAKAAMLwADMWDwDBY8Y/DJu/3mePva76mUGS6zEDD5MYtJj8C/EIMNU//AIsKq8wMrw9wcXA+JpxL9DxKf//MX1jBJb0DH9p070DCCAmbDGADTMxME9iesX68+UjDoZHn18yfPv3GZqMgGX7v68Mj78+YTh7g5Hh/UkeBsbnLFcZmZkigEn9E637pwABhNmU+M+EHTMwnWH8yrzn6y1Ohvsv/zJ8AjbIvv/5ASznvzC8/P6c4dxtJoY3J/gYGJ+w3WViZgR2gP6/oUcHGyCA8NYDWBqcy/4/Y/J++Zid4ZXYewYBtr8Mn/+8Z7j+gJnh9QkBBsYHbI8Zmf+HMPxjvAmu0f/+Z6D12DdAABGViZF8sInxE+OVbzd4dZ4rfWb4I/ic4fY9XoZXJ4UZGG6yv2Zk/hcCdPIFUCXFCK2oGKlV4OMAAAGEWQ/8Y8an/gvQSZMYnnDNevWEh+HT5x8Mr08Ba+brHD8Zmf5H/Gf8d+o/tqFaGgKAACKmIkMH6xk+MzZ9viwo8RmYg5iucv8HgpT/zH/3/QdXUIwM9AQAAYS9NYofvAEmj5T/V/haQKMlQDf3MTL9WwIaNWNmoF4TgVgAEEBDfooJIICGvAcAAmjIewAggIa8BwACaMh7ACCAhrwHAAJoyHsAIMAAHPeQQ3W5OhYAAAAASUVORK5CYII=';
Changing the import pattern
var iconImg = Loader.load('myIcon.jpg');
Config to match Loader.load
:
var gulp = require('gulp');
var inject = require('gulp-js-base64-inject');
gulp.task('js', function() {
return gulp.src('path/to/your/js/*.js')
.pipe(inject({
basepath: 'path/to/your/js/',
pattern: /Loader.load\(['"]([a-zA-Z0-9\-_.\\/]+)['"]\)/g
}))
.pipe(gulp.dest('path/for/output'));
});
Options
Option | Type | Default | Details
------ | ---- | ------- | -------
basepath | string | ""
| Base path to look up referenced templates from
pattern | RegExp | /'@@import ([a-zA-Z0-9\-_.\\/]+)'/g
| Pattern to match image references. Should have one capture group that will match template path
debug | boolean | false
| Displays some information while working - processed files, found references, injection result