Skip to content

Commit 3bab05d

Browse files
committed
test: add Jest configuration
1 parent 758b647 commit 3bab05d

File tree

5 files changed

+71
-63
lines changed

5 files changed

+71
-63
lines changed

index.js

+1-61
Original file line numberDiff line numberDiff line change
@@ -1,61 +1 @@
1-
/*
2-
* @license MIT http://www.opensource.org/licenses/mit-license.php
3-
* @author Hovhannes Babayan <bhovhannes at gmail dot com>
4-
*/
5-
var loaderUtils = require('loader-utils');
6-
7-
var REGEX_STYLE = /<style[\s\S]*?>[\s\S]*?<\/style>/i
8-
var REGEX_DECLARATION = /^\s*<\?xml [^>]*>\s*/i
9-
10-
var REGEX_DOUBLE_QUOTE = /"/g
11-
var REGEX_MULTIPLE_SPACES = /\s+/g
12-
var REGEX_UNSAFE_CHARS = /[{}\|\\\^~\[\]`"<>#%]/g
13-
14-
module.exports = function(content) {
15-
this.cacheable && this.cacheable();
16-
17-
var query = loaderUtils.getOptions(this) || {};
18-
query.encoding = query.encoding || "none";
19-
20-
var limit = query.limit ? parseInt(query.limit, 10) : 0;
21-
22-
if (limit <= 0 || content.length < limit) {
23-
var newContent = content.toString('utf8');
24-
25-
var hasStyleElement = REGEX_STYLE.test(newContent)
26-
27-
if (query.stripdeclarations) {
28-
newContent = newContent.replace(REGEX_DECLARATION, "");
29-
}
30-
31-
var data;
32-
if (query.encoding === "base64") {
33-
if (typeof newContent === "string") {
34-
newContent = new Buffer(newContent);
35-
}
36-
data = "data:image/svg+xml;base64," + newContent.toString("base64");
37-
} else {
38-
newContent = newContent.replace(REGEX_DOUBLE_QUOTE, "'");
39-
newContent = newContent.replace(REGEX_MULTIPLE_SPACES, " ");
40-
newContent = newContent.replace(REGEX_UNSAFE_CHARS, function(match) {
41-
return '%'+match[0].charCodeAt(0).toString(16).toUpperCase();
42-
});
43-
44-
data = 'data:image/svg+xml,' + newContent.trim();
45-
46-
}
47-
48-
if (!(query.iesafe && hasStyleElement && data.length > 4096)) {
49-
if (query.encoding === "none" && !query.noquotes) {
50-
data = '"'+data+'"';
51-
}
52-
53-
return 'module.exports = ' + JSON.stringify(data);
54-
}
55-
}
56-
57-
var fileLoader = require('file-loader');
58-
return fileLoader.call(this, content);
59-
};
60-
61-
module.exports.raw = true;
1+
module.exports = require('./src/loader')

jest.config.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
coverageDirectory: './coverage',
3+
coverageReporters: ['lcov', 'html', 'text-summary'],
4+
collectCoverageFrom: ['./src/**/*.js']
5+
}

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "svg-url-loader",
33
"version": "2.3.2",
44
"description": "Converts SVG file to utf-8 encoded data-uri string",
5-
"main": "src/index.js",
5+
"main": "./index.js",
66
"scripts": {
77
"test": "jest --coverage",
88
"test-watch": "jest --watch",

src/loader.js

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* @license MIT http://www.opensource.org/licenses/mit-license.php
3+
* @author Hovhannes Babayan <bhovhannes at gmail dot com>
4+
*/
5+
var loaderUtils = require('loader-utils');
6+
7+
var REGEX_STYLE = /<style[\s\S]*?>[\s\S]*?<\/style>/i
8+
var REGEX_DECLARATION = /^\s*<\?xml [^>]*>\s*/i
9+
10+
var REGEX_DOUBLE_QUOTE = /"/g
11+
var REGEX_MULTIPLE_SPACES = /\s+/g
12+
var REGEX_UNSAFE_CHARS = /[{}\|\\\^~\[\]`"<>#%]/g
13+
14+
module.exports = function(content) {
15+
this.cacheable && this.cacheable();
16+
17+
var query = loaderUtils.getOptions(this) || {};
18+
query.encoding = query.encoding || "none";
19+
20+
var limit = query.limit ? parseInt(query.limit, 10) : 0;
21+
22+
if (limit <= 0 || content.length < limit) {
23+
var newContent = content.toString('utf8');
24+
25+
var hasStyleElement = REGEX_STYLE.test(newContent)
26+
27+
if (query.stripdeclarations) {
28+
newContent = newContent.replace(REGEX_DECLARATION, "");
29+
}
30+
31+
var data;
32+
if (query.encoding === "base64") {
33+
if (typeof newContent === "string") {
34+
newContent = new Buffer(newContent);
35+
}
36+
data = "data:image/svg+xml;base64," + newContent.toString("base64");
37+
} else {
38+
newContent = newContent.replace(REGEX_DOUBLE_QUOTE, "'");
39+
newContent = newContent.replace(REGEX_MULTIPLE_SPACES, " ");
40+
newContent = newContent.replace(REGEX_UNSAFE_CHARS, function(match) {
41+
return '%'+match[0].charCodeAt(0).toString(16).toUpperCase();
42+
});
43+
44+
data = 'data:image/svg+xml,' + newContent.trim();
45+
46+
}
47+
48+
if (!(query.iesafe && hasStyleElement && data.length > 4096)) {
49+
if (query.encoding === "none" && !query.noquotes) {
50+
data = '"'+data+'"';
51+
}
52+
53+
return 'module.exports = ' + JSON.stringify(data);
54+
}
55+
}
56+
57+
var fileLoader = require('file-loader');
58+
return fileLoader.call(this, content);
59+
};
60+
61+
module.exports.raw = true;

test/loader.spec.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@ const webpack = require('webpack')
44

55
jest.setTimeout(10000)
66

7+
const svgUrlLoader = path.resolve(__dirname, '../src/loader.js')
8+
79
describe('svg-url-loader', function() {
810

911
const outputDir = path.resolve(__dirname, './output'),
1012
bundleFileName = 'bundle.js',
1113
getBundleFile = function() {
1214
return path.join(outputDir, bundleFileName)
1315
}
14-
const svgUrlLoader = path.resolve(__dirname, '../')
16+
1517
const globalConfig = {
1618
context: path.resolve(__dirname, '../'),
1719
mode: 'development',

0 commit comments

Comments
 (0)