Skip to content

Commit 5038751

Browse files
meowtec朱勃
authored and
朱勃
committed
feat: add raster image support
1 parent 2019115 commit 5038751

File tree

4 files changed

+58
-3
lines changed

4 files changed

+58
-3
lines changed

lib/loader.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ const SVGCompiler = require('svg-baker');
55
const { NAMESPACE } = require('./config');
66
const configure = require('./configurator');
77
const Exceptions = require('./exceptions');
8+
const fileTypeDetect = require('./utils/file-type-detect');
89

910
let svgCompiler = new SVGCompiler();
1011

1112
// eslint-disable-next-line consistent-return
12-
module.exports = function loader(content) {
13+
module.exports = function loader(contentBuffer) {
1314
if (this.cacheable) {
1415
this.cacheable();
1516
}
@@ -24,7 +25,13 @@ module.exports = function loader(content) {
2425
const parentCompiler = isChildCompiler ? compiler.parentCompilation.compiler : null;
2526
const matchedRules = getOptions(loaderContext);
2627

27-
if (!content.includes('<svg')) {
28+
const content = contentBuffer.toString();
29+
const isSVG = fileTypeDetect.isSVG(content);
30+
31+
if (
32+
!isSVG &&
33+
!fileTypeDetect.isImage(contentBuffer)
34+
) {
2835
throw new Exceptions.InvalidSvg(content, matchedRules);
2936
}
3037

@@ -72,11 +79,17 @@ module.exports = function loader(content) {
7279
regExp: config.symbolRegExp
7380
});
7481
}
75-
svgCompiler.addSymbol({ id, content, path: resourcePath + resourceQuery })
82+
svgCompiler.addSymbol({
83+
id,
84+
content: isSVG ? content : contentBuffer,
85+
path: resourcePath + resourceQuery
86+
})
7687
.then((symbol) => {
7788
const runtime = runtimeGenerator({ symbol, config, context: loaderContext.context, loaderContext });
7889
done(null, runtime);
7990
}).catch(done);
8091
};
8192

8293
module.exports.NAMESPACE = NAMESPACE;
94+
95+
module.exports.raw = true;

lib/utils/file-type-detect.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const imageSize = require('image-size');
2+
3+
/**
4+
* detect whether content is an image
5+
* @param {Buffer} content
6+
* @return {boolean}
7+
*/
8+
function isImage(content) {
9+
try {
10+
imageSize(content);
11+
return true;
12+
} catch (err) {
13+
return false;
14+
}
15+
}
16+
17+
/**
18+
* detect whether content is an image
19+
* @param {string} content
20+
* @return {boolean}
21+
*/
22+
function isSVG(content) {
23+
return content.includes('<svg');
24+
}
25+
26+
exports.isImage = isImage;
27+
exports.isSVG = isSVG;

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"domready": "1.0.8",
3737
"escape-string-regexp": "1.0.5",
3838
"html-webpack-plugin": "^3.2.0",
39+
"image-size": "^0.8.3",
3940
"loader-utils": "^1.1.0",
4041
"svg-baker": "^1.5.0",
4142
"svg-baker-runtime": "^1.4.7",

yarn.lock

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3816,6 +3816,13 @@ image-size@^0.5.1:
38163816
version "0.5.5"
38173817
resolved "https://registry.yarnpkg.com/image-size/-/image-size-0.5.5.tgz#09dfd4ab9d20e29eb1c3e80b8990378df9e3cb9c"
38183818

3819+
image-size@^0.8.3:
3820+
version "0.8.3"
3821+
resolved "https://registry.yarnpkg.com/image-size/-/image-size-0.8.3.tgz#f0b568857e034f29baffd37013587f2c0cad8b46"
3822+
integrity sha512-SMtq1AJ+aqHB45c3FsB4ERK0UCiA2d3H1uq8s+8T0Pf8A3W4teyBQyaFaktH6xvZqh+npwlKU7i4fJo0r7TYTg==
3823+
dependencies:
3824+
queue "6.0.1"
3825+
38193826
import-fresh@^3.0.0:
38203827
version "3.2.1"
38213828
resolved "https://registry.yarnpkg.com/import-fresh/-/import-fresh-3.2.1.tgz#633ff618506e793af5ac91bf48b72677e15cbe66"
@@ -6507,6 +6514,13 @@ query-string@^4.1.0, query-string@^4.3.2:
65076514
object-assign "^4.1.0"
65086515
strict-uri-encode "^1.0.0"
65096516

6517+
6518+
version "6.0.1"
6519+
resolved "https://registry.yarnpkg.com/queue/-/queue-6.0.1.tgz#abd5a5b0376912f070a25729e0b6a7d565683791"
6520+
integrity sha512-AJBQabRCCNr9ANq8v77RJEv73DPbn55cdTb+Giq4X0AVnNVZvMHlYp7XlQiN+1npCZj1DuSmaA2hYVUUDgxFDg==
6521+
dependencies:
6522+
inherits "~2.0.3"
6523+
65106524
quick-lru@^1.0.0:
65116525
version "1.1.0"
65126526
resolved "https://registry.yarnpkg.com/quick-lru/-/quick-lru-1.1.0.tgz#4360b17c61136ad38078397ff11416e186dcfbb8"

0 commit comments

Comments
 (0)