-
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.
refactor: make code faster and cleaner
- Loading branch information
1 parent
770c8da
commit b5ffa23
Showing
2 changed files
with
45 additions
and
34 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,60 +1,71 @@ | ||
const { getOptions, parseQuery } = require("loader-utils"); | ||
|
||
const REGEX_STYLE = /<style[\s\S]*?>[\s\S]*?<\/style>/i; | ||
const REGEX_DECLARATION = /^\s*<\?xml [^>]*>\s*/i; | ||
|
||
const REGEX_DOUBLE_QUOTE = /"/g; | ||
const REGEX_XML_DECLARATION = /^\s*<\?xml [^>]*>\s*/i; | ||
const REGEX_MULTIPLE_SPACES = /\s+/g; | ||
const REGEX_DOUBLE_QUOTE = /"/g; | ||
const REGEX_UNSAFE_CHARS = /[{}\|\\\^~\[\]`"<>#%]/g; | ||
|
||
module.exports = function (content) { | ||
this.cacheable && this.cacheable(); | ||
|
||
let query = { | ||
...{ | ||
encoding: "none", | ||
}, | ||
encoding: "none", | ||
stripdeclarations: true, | ||
...getOptions(this), | ||
...(this.resourceQuery ? parseQuery(this.resourceQuery) : undefined), | ||
}; | ||
|
||
let limit = query.limit ? parseInt(query.limit, 10) : 0; | ||
|
||
if (limit <= 0 || content.length < limit) { | ||
let newContent = content.toString("utf8"); | ||
const originalSvgSource = content.toString("utf8"); | ||
|
||
let hasStyleElement = REGEX_STYLE.test(newContent); | ||
|
||
if (!("stripdeclarations" in query) || query.stripdeclarations) { | ||
newContent = newContent.replace(REGEX_DECLARATION, ""); | ||
} | ||
newContent = newContent.replace(REGEX_MULTIPLE_SPACES, " "); | ||
const transformedSvgSource = applyGeneralTransforms( | ||
originalSvgSource, | ||
query.stripdeclarations | ||
); | ||
|
||
let data; | ||
if (query.encoding === "base64") { | ||
if (typeof newContent === "string") { | ||
newContent = Buffer.from(newContent); | ||
} | ||
data = "data:image/svg+xml;base64," + newContent.toString("base64"); | ||
data = | ||
"data:image/svg+xml;base64," + | ||
Buffer.from(transformedSvgSource).toString("base64"); | ||
} else { | ||
newContent = newContent.replace(REGEX_DOUBLE_QUOTE, "'"); | ||
newContent = newContent.replace(REGEX_UNSAFE_CHARS, function (match) { | ||
return "%" + match[0].charCodeAt(0).toString(16).toUpperCase(); | ||
}); | ||
|
||
data = "data:image/svg+xml," + newContent.trim(); | ||
data = "data:image/svg+xml," + uriEncode(transformedSvgSource); | ||
} | ||
|
||
if (!(query.iesafe && hasStyleElement && data.length > 4096)) { | ||
if ( | ||
!query.iesafe || | ||
data.length <= 4096 || | ||
!REGEX_STYLE.test(originalSvgSource) | ||
) { | ||
return "module.exports = " + JSON.stringify(data); | ||
} | ||
} | ||
|
||
const fileLoader = require("file-loader"); | ||
const childContext = Object.create(this); | ||
childContext.query = Object.assign({}, query); | ||
// childContext.query.esModule = false; | ||
return fileLoader.call(childContext, content); | ||
}; | ||
|
||
module.exports.raw = true; | ||
|
||
function uriEncodeMatch(match) { | ||
return "%" + match[0].charCodeAt(0).toString(16).toLowerCase(); | ||
} | ||
|
||
function uriEncode(svgSource) { | ||
svgSource = svgSource.replace(REGEX_DOUBLE_QUOTE, "'"); | ||
svgSource = svgSource.replace(REGEX_UNSAFE_CHARS, uriEncodeMatch); | ||
return svgSource.trim(); | ||
} | ||
|
||
function applyGeneralTransforms(svgSource, stripXmlDeclaration) { | ||
if (stripXmlDeclaration) { | ||
svgSource = svgSource.replace(REGEX_XML_DECLARATION, ""); | ||
} | ||
svgSource = svgSource.replace(REGEX_MULTIPLE_SPACES, " "); | ||
return svgSource; | ||
} |
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