Skip to content

Commit 413246e

Browse files
committed
fix(loader): throw an exception if input is not valid SVG
Closes #170
1 parent 508af1e commit 413246e

3 files changed

Lines changed: 21 additions & 10 deletions

File tree

lib/exceptions.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const { PACKAGE_NAME } = require('./config');
22

33
class LoaderException extends Error {
4-
constructor(message) {
4+
constructor(message = '') {
55
super(`${PACKAGE_NAME} exception. ${message}`);
66

77
this.name = this.constructor.name;
@@ -14,6 +14,12 @@ class LoaderException extends Error {
1414
}
1515
}
1616

17+
class InvalidSvg extends LoaderException {
18+
constructor(content) {
19+
super(`\n\n${content}`);
20+
}
21+
}
22+
1723
class ExtractPluginMissingException extends LoaderException {
1824
constructor() {
1925
super(`${PACKAGE_NAME} in extract mode requires the corresponding plugin`);
@@ -35,6 +41,7 @@ class RemainingLoadersInExtractModeException extends LoaderException {
3541
}
3642

3743
exports.LoaderException = LoaderException;
44+
exports.InvalidSvg = InvalidSvg;
3845
exports.ExtractPluginMissingException = ExtractPluginMissingException;
3946
exports.InvalidRuntimeException = InvalidRuntimeException;
4047
exports.SeveralRulesAppliedException = SeveralRulesAppliedException;

lib/loader.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const SVGCompiler = require('svg-baker');
44

55
const { NAMESPACE } = require('./config');
66
const configure = require('./configurator');
7-
const utils = require('./utils');
7+
const { getMatchedRules, getLoadersRules } = require('./utils');
88
const Exceptions = require('./exceptions');
99

1010
let svgCompiler = new SVGCompiler();
@@ -21,6 +21,16 @@ module.exports = function loader(content) {
2121
const compiler = loaderContext._compiler;
2222
const isChildCompiler = compiler.isChild();
2323
const parentCompiler = isChildCompiler ? compiler.parentCompilation.compiler : null;
24+
const issuer = loaderContext._module && loaderContext._module.issuer;
25+
const matchedRules = getMatchedRules(resource, getLoadersRules(compiler), issuer);
26+
27+
if (matchedRules.length > 1 && !compiler.isChild()) {
28+
this.emitWarning(new Exceptions.SeveralRulesAppliedException(resource, matchedRules));
29+
}
30+
31+
if (!content.includes('<svg')) {
32+
throw new Exceptions.InvalidSvg(content, matchedRules);
33+
}
2434

2535
/**
2636
* @type {SVGSpriteLoaderConfig}
@@ -53,12 +63,6 @@ module.exports = function loader(content) {
5363
throw new Exceptions.InvalidRuntimeException(e.message);
5464
}
5565

56-
const issuer = loaderContext._module && loaderContext._module.issuer;
57-
const matchedRules = utils.getMatchedRules(resource, utils.getLoadersRules(compiler), issuer);
58-
if (matchedRules.length > 1 && !compiler.isChild()) {
59-
this.emitWarning(new Exceptions.SeveralRulesAppliedException(resource, matchedRules));
60-
}
61-
6266
const idPattern = config.symbolId + (resourceQuery ? `--${urlSlug(resourceQuery)}` : '');
6367
const id = interpolateName(loaderContext, idPattern, {
6468
content,

test/loader.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ describe('loader and plugin', () => {
3939
});
4040

4141
it('should warn if several rules applied to module without issuer applied', async () => {
42-
const { warnings } = await compile({
42+
const { warnings } = await compileAndNotReject({
4343
entry: './entry',
4444
module: rules(
4545
svgRule(),
@@ -67,7 +67,7 @@ describe('loader and plugin', () => {
6767
});
6868

6969
it('should filter rules against issuer', async () => {
70-
const { warnings } = await compile({
70+
const { warnings } = await compileAndNotReject({
7171
entry: './entry',
7272
module: rules(
7373
svgRule(),

0 commit comments

Comments
 (0)