Skip to content

Commit e8053fc

Browse files
feat: emit an error on broken HTML syntax when minimization is enabled (#229)
1 parent d515b1f commit e8053fc

File tree

6 files changed

+53
-2
lines changed

6 files changed

+53
-2
lines changed

src/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,11 @@ export default function htmlLoader(source) {
106106
}
107107
: minimize;
108108

109-
content = minify(content, minimizeOptions);
109+
try {
110+
content = minify(content, minimizeOptions);
111+
} catch (error) {
112+
this.emitError(error);
113+
}
110114
}
111115

112116
if (options.interpolate && options.interpolate !== 'require') {

test/__snapshots__/minimize-option.test.js.snap

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -790,6 +790,31 @@ Text ><img src=\\"/webpack/public/path/image.png\\">--> "
790790
791791
exports[`"minimize" option should support options for minimizer: warnings 1`] = `Array []`;
792792
793+
exports[`"minimize" option should work emit an error on broken HTML syntax: errors 1`] = `
794+
Array [
795+
"ModuleError: Module Error (from /path/to/file.js):
796+
Parse Error: < img src=\\"image.png\\" >",
797+
]
798+
`;
799+
800+
exports[`"minimize" option should work emit an error on broken HTML syntax: module 1`] = `
801+
"// Imports
802+
var ___HTML_LOADER_GET_URL_IMPORT___ = require(\\"../../src/runtime/getUrl.js\\");
803+
var ___HTML_LOADER_IDENT_0___ = require(\\"./image.png\\");
804+
var ___HTML_LOADER_IDENT_1___ = require(\\"./image.png\\");
805+
// Exports
806+
module.exports = \\"Text < img src=\\\\\\"image.png\\\\\\" >\\\\nText <<img src=\\\\\\"\\" + ___HTML_LOADER_GET_URL_IMPORT___(___HTML_LOADER_IDENT_0___) + \\"\\\\\\">\\\\nText ><img src=\\\\\\"\\" + ___HTML_LOADER_GET_URL_IMPORT___(___HTML_LOADER_IDENT_1___) + \\"\\\\\\">\\\\n\\";"
807+
`;
808+
809+
exports[`"minimize" option should work emit an error on broken HTML syntax: result 1`] = `
810+
"Text < img src=\\"image.png\\" >
811+
Text <<img src=\\"/webpack/public/path/image.png\\">
812+
Text ><img src=\\"/webpack/public/path/image.png\\">
813+
"
814+
`;
815+
816+
exports[`"minimize" option should work emit an error on broken HTML syntax: warnings 1`] = `Array []`;
817+
793818
exports[`"minimize" option should work with a value equal to "true": errors 1`] = `Array []`;
794819
795820
exports[`"minimize" option should work with a value equal to "true": module 1`] = `

test/fixtures/broken-html-syntax.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Text < img src="image.png" >
2+
Text <<img src="image.png">
3+
Text ><img src="image.png">

test/fixtures/broken-html-syntax.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import html from './broken-html-syntax.html';
2+
3+
export default html;

test/helpers/normalizeErrors.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ function removeCWD(str) {
99
cwd = cwd.replace(/\\/g, '/');
1010
}
1111

12-
return str.replace(new RegExp(cwd, 'g'), '');
12+
return str
13+
.replace(new RegExp(cwd, 'g'), '')
14+
.replace(/\(from (.*)\)/, '(from /path/to/file.js)');
1315
}
1416

1517
export default (errors) => {

test/minimize-option.test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,18 @@ describe('"minimize" option', () => {
9797
expect(getWarnings(stats)).toMatchSnapshot('warnings');
9898
expect(getErrors(stats)).toMatchSnapshot('errors');
9999
});
100+
101+
it('should work emit an error on broken HTML syntax', async () => {
102+
const compiler = getCompiler('broken-html-syntax.js', { minimize: true });
103+
const stats = await compile(compiler);
104+
105+
expect(getModuleSource('./broken-html-syntax.html', stats)).toMatchSnapshot(
106+
'module'
107+
);
108+
expect(
109+
execute(readAsset('main.bundle.js', compiler, stats))
110+
).toMatchSnapshot('result');
111+
expect(getWarnings(stats)).toMatchSnapshot('warnings');
112+
expect(getErrors(stats)).toMatchSnapshot('errors');
113+
});
100114
});

0 commit comments

Comments
 (0)