-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
758b647
commit 3bab05d
Showing
5 changed files
with
71 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1 @@ | ||
/* | ||
* @license MIT http://www.opensource.org/licenses/mit-license.php | ||
* @author Hovhannes Babayan <bhovhannes at gmail dot com> | ||
*/ | ||
var loaderUtils = require('loader-utils'); | ||
|
||
var REGEX_STYLE = /<style[\s\S]*?>[\s\S]*?<\/style>/i | ||
var REGEX_DECLARATION = /^\s*<\?xml [^>]*>\s*/i | ||
|
||
var REGEX_DOUBLE_QUOTE = /"/g | ||
var REGEX_MULTIPLE_SPACES = /\s+/g | ||
var REGEX_UNSAFE_CHARS = /[{}\|\\\^~\[\]`"<>#%]/g | ||
|
||
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 = REGEX_STYLE.test(newContent) | ||
|
||
if (query.stripdeclarations) { | ||
newContent = newContent.replace(REGEX_DECLARATION, ""); | ||
} | ||
|
||
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(REGEX_DOUBLE_QUOTE, "'"); | ||
newContent = newContent.replace(REGEX_MULTIPLE_SPACES, " "); | ||
newContent = newContent.replace(REGEX_UNSAFE_CHARS, 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') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
module.exports = { | ||
coverageDirectory: './coverage', | ||
coverageReporters: ['lcov', 'html', 'text-summary'], | ||
collectCoverageFrom: ['./src/**/*.js'] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* @license MIT http://www.opensource.org/licenses/mit-license.php | ||
* @author Hovhannes Babayan <bhovhannes at gmail dot com> | ||
*/ | ||
var loaderUtils = require('loader-utils'); | ||
|
||
var REGEX_STYLE = /<style[\s\S]*?>[\s\S]*?<\/style>/i | ||
var REGEX_DECLARATION = /^\s*<\?xml [^>]*>\s*/i | ||
|
||
var REGEX_DOUBLE_QUOTE = /"/g | ||
var REGEX_MULTIPLE_SPACES = /\s+/g | ||
var REGEX_UNSAFE_CHARS = /[{}\|\\\^~\[\]`"<>#%]/g | ||
|
||
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 = REGEX_STYLE.test(newContent) | ||
|
||
if (query.stripdeclarations) { | ||
newContent = newContent.replace(REGEX_DECLARATION, ""); | ||
} | ||
|
||
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(REGEX_DOUBLE_QUOTE, "'"); | ||
newContent = newContent.replace(REGEX_MULTIPLE_SPACES, " "); | ||
newContent = newContent.replace(REGEX_UNSAFE_CHARS, 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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters