Permalink
Browse files

feat: add `inlineLinkTags` support

  • Loading branch information...
sparanoid committed Jan 20, 2017
1 parent 1b0a506 commit 1092f13d5f44fc5bea3953f9c30fc6d725c0e6a7
Showing with 62 additions and 0 deletions.
  1. +21 −0 README.md
  2. +41 −0 tasks/assets_inline.js
@@ -176,6 +176,27 @@ SVG images are inlined directly into HTML by default. To have more compatibility
Please note that Base64 generated files are always slightly bigger than the original files.
#### Inline `link` Tags
Defaults to `false`.
If you want to smoosh images inside your `link` tags, for example favicons, you can set `inlineLinkTags` to true.
```js
grunt.initConfig({
assets_inline: {
all: {
options: {
inlineLinkTags: true
},
files: {
'dest-index.html': 'source-index.html',
},
},
},
});
```
#### Additional Assets URL Prefix
If you have assets in CSS or JS (for example images, fonts, SVGs), smooshing CSS or JS into HTMLs may break relative URLs, so you may have to replace the original URLs to absolute URLs.
@@ -33,6 +33,7 @@ module.exports = function(grunt) {
inlineImg: false,
inlineSvg: true,
inlineSvgBase64: false,
inlineLinkTags: false,
includeTag: "",
assetsUrlPrefix: "",
verbose: false,
@@ -78,6 +79,7 @@ module.exports = function(grunt) {
grunt.log.writeln(('Reading: ').green + path.resolve(filePair.src.toString()));
// Assets inside inline `<style>`
$('style[data-assets-inline]').each(function () {
var style = $(this).html();
if(!style) { return; }
@@ -115,6 +117,7 @@ module.exports = function(grunt) {
$(this).text(style);
});
// Assets inside external stylesheets
$('link[rel="stylesheet"]').each(function () {
var style = $(this).attr('href');
if(!style) { return; }
@@ -143,6 +146,7 @@ module.exports = function(grunt) {
}
});
// Assets inside `<script>`
$('script').each(function () {
var script = $(this).attr('src');
if(!script) { return; }
@@ -168,6 +172,43 @@ module.exports = function(grunt) {
}
});
// Assets inside `<link>`, most for favicons
if (options.inlineLinkTags) {
$('link').each(function () {
var src = $(this).attr('href');
if(!src) { return; }
if(src.match(/^\/\//)) { return; }
if(src.indexOf(options.includeTag) === -1) { return; }
if(url.parse(src).protocol) { return; }
src = src.replace(/\?.+$/, '');
var filePath = (src.substr(0,1) === '/') ? path.resolve(options.assetsDir, src.substr(1)) : path.join(path.dirname(filePair.src), src);
if (src.match(/.svg$/i)) {
if (options.inlineSvgBase64) {
$(this).attr('href', 'data:image/svg+xml;base64,' + new Buffer(grunt.file.read(filePath, { encoding: null })).toString('base64'));
} else {
$(this).attr('href', 'data:image/svg+xml;utf8,' + processSvg(grunt.file.read(filePath)));
}
grunt.log.writeln((' svg: ').cyan + filePath);
}
if (src.match(/.ico$/i)) {
$(this).attr('href', 'data:image/x-icon;base64,' + new Buffer(grunt.file.read(filePath, { encoding: null })).toString('base64'));
grunt.log.writeln((' ico: ').cyan + filePath);
}
if (src.match(/.(?:png|jpg)$/i)) {
$(this).attr('href', 'data:image/' + src.substr(src.lastIndexOf('.')+1) + ';base64,' + new Buffer(grunt.file.read(filePath, { encoding: null })).toString('base64'));
grunt.log.writeln((' image: ').cyan + filePath);
}
if (options.deleteOriginals) {
filesToDelete.push(filePath);
}
});
}
if (options.inlineSvg) {
$('img').each(function () {
var src = $(this).attr('src');

0 comments on commit 1092f13

Please sign in to comment.