Skip to content

Commit

Permalink
Merge pull request #315 from bhovhannes/improvements
Browse files Browse the repository at this point in the history
Improvements
  • Loading branch information
bhovhannes authored Jun 16, 2019
2 parents 39362ca + 90e1bdc commit c6011d3
Show file tree
Hide file tree
Showing 7 changed files with 7,876 additions and 151 deletions.
55 changes: 1 addition & 54 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,54 +1 @@
/*
* @license MIT http://www.opensource.org/licenses/mit-license.php
* @author Hovhannes Babayan <bhovhannes at gmail dot com>
*/
var loaderUtils = require('loader-utils');

module.exports = function(content) {
this.cacheable && this.cacheable();

var query = loaderUtils.getOptions(this) || {};
query.encoding = query.encoding || "none";

var limit = query.limit ? parseInt(query.limit, 10) : 0;

if (limit <= 0 || content.length < limit) {
var newContent = content.toString('utf8');

var hasStyleElement = /<style[\s\S]*?>[\s\S]*?<\/style>/i.test(newContent)

if (query.stripdeclarations) {
newContent = newContent.replace(/^\s*<\?xml [^>]*>\s*/i, "");
}

var data;
if (query.encoding === "base64") {
if (typeof newContent === "string") {
newContent = new Buffer(newContent);
}
data = "data:image/svg+xml;base64," + newContent.toString("base64");
} else {
newContent = newContent.replace(/"/g, "'");
newContent = newContent.replace(/\s+/g, " ");
newContent = newContent.replace(/[{}\|\\\^~\[\]`"<>#%]/g, function(match) {
return '%'+match[0].charCodeAt(0).toString(16).toUpperCase();
});

data = 'data:image/svg+xml,' + newContent.trim();

}

if (!(query.iesafe && hasStyleElement && data.length > 4096)) {
if (query.encoding === "none" && !query.noquotes) {
data = '"'+data+'"';
}

return 'module.exports = ' + JSON.stringify(data);
}
}

var fileLoader = require('file-loader');
return fileLoader.call(this, content);
};

module.exports.raw = true;
module.exports = require('./src/loader')
8 changes: 8 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
coverageDirectory: './coverage',
coverageReporters: ['lcov', 'html', 'text-summary'],
collectCoverageFrom: ['./src/**/*.js'],
setupFilesAfterEnv: [
'<rootDir>/test/setupFiles/timeout.js'
]
}
Loading

0 comments on commit c6011d3

Please sign in to comment.