Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add raster image support #410

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions lib/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ const SVGCompiler = require('svg-baker');
const { NAMESPACE } = require('./config');
const configure = require('./configurator');
const Exceptions = require('./exceptions');
const fileTypeDetect = require('./utils/file-type-detect');

let svgCompiler = new SVGCompiler();

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

if (!content.includes('<svg')) {
const content = contentBuffer.toString();
const isSVG = fileTypeDetect.isSVG(content);

if (
!isSVG &&
!fileTypeDetect.isImage(contentBuffer)
) {
throw new Exceptions.InvalidSvg(content, matchedRules);
}

Expand Down Expand Up @@ -72,11 +79,17 @@ module.exports = function loader(content) {
regExp: config.symbolRegExp
});
}
svgCompiler.addSymbol({ id, content, path: resourcePath + resourceQuery })
svgCompiler.addSymbol({
id,
content: isSVG ? content : contentBuffer,
path: resourcePath + resourceQuery
})
.then((symbol) => {
const runtime = runtimeGenerator({ symbol, config, context: loaderContext.context, loaderContext });
done(null, runtime);
}).catch(done);
};

module.exports.NAMESPACE = NAMESPACE;

module.exports.raw = true;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is purpose of this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is purpose of this change?

this will make the first argument of loader become a Buffer from a string, so that we can detect whether it is an valid image file using image-size. (image-size is also required by svg-baker so I chose it for detecting image)

27 changes: 27 additions & 0 deletions lib/utils/file-type-detect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const imageSize = require('image-size');

/**
* detect whether content is an image
* @param {Buffer} content
* @return {boolean}
*/
function isImage(content) {
try {
imageSize(content);
return true;
} catch (err) {
return false;
}
}

/**
* detect whether content is an image
* @param {string} content
* @return {boolean}
*/
function isSVG(content) {
return content.includes('<svg');
}

exports.isImage = isImage;
exports.isSVG = isSVG;
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"deepmerge": "1.3.2",
"domready": "1.0.8",
"escape-string-regexp": "1.0.5",
"image-size": "^0.5.5",
"loader-utils": "^1.1.0",
"svg-baker": "^1.5.0",
"svg-baker-runtime": "^1.4.7",
Expand Down
Loading